C#推箱子

前言

推箱子的逻辑部分都已经做好了注释。
并没有增加游戏如何胜利。
游戏的规则由自己决定。

演示

在这里插入图片描述

代码

using System;
namespace Game {
    //推箱子
    public class Sokoban {
        //步数
        public int steps;

        //地图大小
        public const int N = 10;

        //角色的坐标
        public int roleX;
        public int roleY;

        //箱子的坐标
        public int boxX;
        public int boxY;

        //地图,二维数组
        public int[,] map =  new int[N,N];


        //移动方向,上下左右
        public int[] dx = new int[4] { -1,1,0,0};
        public int[] dy = new int[4] { 0,0,-1,1};


        //键盘的值,上下左右
        public Dictionary<char, int> keyboradValue;


        //初始化
        public void initGame(int boxRow,int boxCol,int roleRow,int roleCol) {
            steps = 0;
            roleX = roleRow;
            roleY = roleCol;
            boxX = boxRow;
            boxY = boxCol;
            keyboradValue = new Dictionary<char, int>() { 
                 {'W',0 },{'S',1 }, {'A',2 },{'D',3 }
                //{ 'w', 0 },{ 's',1},{ 'a',2},{ 'd',3}
            };
            for(int i = 0; i < N; i++) {
                for(int j = 0; j < N; j++) {
                    map[i,j] = 0;
                    if (i == 0 || i == N - 1 || j == 0 | j == N - 1) {
                        map[i, j] = -1;
                    }
                }
            }
        }



        //打印地图
        public void showMap() {
            for(int i = 0; i < N; i++) {
                for(int j = 0; j < N; j++) {
                    /*
                     * box是箱子:用0表示
                     * -1是墙:用-1表示
                     * 空格是可移动区域
                     * role是角色:用@表示
                    */
                    if (i == boxX && j == boxY) Console.Write("0");
                    else if (map[i, j] == -1) Console.Write("#");
                    else if (i == roleX && j == roleY) Console.Write("@");
                    else if (map[i, j] == 0) Console.Write(" ");
                }
                Console.WriteLine();
            }
        }


        //判断坐标是否在N×N的地图中
        public bool coordinateIsValid(int x,int y) {
            return (x > 0 && x < N && y > 0 && y < N);
        }


        //角色移动:通过键盘
        public void moveRole(char keyValue) {
            if (keyboradValue.ContainsKey(keyValue)) {
                int direction = keyboradValue[keyValue];
                int newX = roleX+dx[direction];
                int newY = roleY+dy[direction]; 
                if(coordinateIsValid(newX,newY)) {
                    bool isBox = false;
                    bool isPushed = false;
                    //如果前进的点碰到了箱子
                    if(newX==boxX && newY==boxY) {
                        isBox = true;
                        int newBoxX = boxX+dx[direction];
                        int newBoxY = boxY+dy[direction];
                        if (coordinateIsValid(newBoxX, newBoxY) &&map[newBoxX, newBoxY] !=-1) {
                            boxX = newBoxX;
                            boxY = newBoxY;
                            isPushed= true;
                        }
                    }
                    //如果前面是箱子,但是没推动,说明角色不能动
                    if (isBox && isPushed == false) return;
                    roleX = newX;
                    roleY = newY;
                }
            }
        }

        //主函数
        public static void Main(string[] args) {
            Sokoban sokoban = new Sokoban();
            sokoban.initGame(4, 6, 2, 3);
            Console.WriteLine("推箱子:你可以按下键盘的[WASD]来控制角色的移动!\n当你按下[e]可以推出游戏。");
            char keyValue;
            while (true) {
                //从键盘获取输入,并且不显示在屏幕上
                keyValue = (char)(Console.ReadKey(true).Key);
                if (keyValue == 'e' || keyValue == 'E') break;
                sokoban.steps++;
                sokoban.moveRole(keyValue);
                Console.Clear();
                sokoban.showMap();
                Console.WriteLine($"你已经走了: {sokoban.steps} 步");
            }
        }
    }
}
以下是一个简单的 C# 推箱子游戏的代码示例,仅供参考: ```csharp using System; using System.Collections.Generic; namespace PushBoxGame { class Program { static void Main(string[] args) { Console.WriteLine("Welcome to Push Box Game!"); // 初始化地图和玩家位置 char[,] map = new char[,] { { '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' }, { '#', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', '#' }, { '#', ' ', ' ', ' ', ' ', '#', ' ', 'X', ' ', '#' }, { '#', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', '#' }, { '#', '#', '#', '#', ' ', '#', ' ', ' ', ' ', '#' }, { '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' }, { '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' }, { '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' }, { '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' }, { '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' } }; int playerX = 4; int playerY = 5; while (true) { // 显示地图 DisplayMap(map, playerX, playerY); // 获取玩家输入 Console.Write("Enter your move (WASD): "); ConsoleKeyInfo key = Console.ReadKey(); Console.WriteLine(); // 根据玩家输入移动玩家 int dx = 0; int dy = 0; switch (key.Key) { case ConsoleKey.W: dy = -1; break; case ConsoleKey.A: dx = -1; break; case ConsoleKey.S: dy = 1; break; case ConsoleKey.D: dx = 1; break; } int newPlayerX = playerX + dx; int newPlayerY = playerY + dy; // 检查新的玩家位置是否合法 if (IsPositionValid(map, newPlayerX, newPlayerY)) { // 如果新的玩家位置是空地,则直接移动玩家 if (map[newPlayerY, newPlayerX] == ' ') { playerX = newPlayerX; playerY = newPlayerY; } // 如果新的玩家位置是箱子,则需要检查箱子是否可以移动 else if (map[newPlayerY, newPlayerX] == 'X') { int newBoxX = newPlayerX + dx; int newBoxY = newPlayerY + dy; if (IsPositionValid(map, newBoxX, newBoxY) && map[newBoxY, newBoxX] == ' ') { map[newPlayerY, newPlayerX] = ' '; map[newBoxY, newBoxX] = 'X'; playerX = newPlayerX; playerY = newPlayerY; } } } // 检查是否胜利 if (IsGameWon(map)) { Console.WriteLine("Congratulations, you won!"); break; } } } // 显示地图和玩家位置 static void DisplayMap(char[,] map, int playerX, int playerY) { for (int y = 0; y < map.GetLength(0); y++) { for (int x = 0; x < map.GetLength(1); x++) { if (x == playerX && y == playerY) { Console.Write('@'); } else { Console.Write(map[y, x]); } } Console.WriteLine(); } } // 检查位置是否合法 static bool IsPositionValid(char[,] map, int x, int y) { if (x < 0 || x >= map.GetLength(1) || y < 0 || y >= map.GetLength(0)) { return false; } return map[y, x] != '#'; } // 检查是否胜利 static bool IsGameWon(char[,] map) { for (int y = 0; y < map.GetLength(0); y++) { for (int x = 0; x < map.GetLength(1); x++) { if (map[y, x] == 'X') { return false; } } } return true; } } } ``` 在这个示例中,我们使用一个二维字符数组来表示地图,其中 `' '` 表示空地,`'#'` 表示墙,`'X'` 表示箱子。玩家的位置用 `playerX` 和 `playerY` 来表示。每次循环中,我们首先显示地图和玩家位置,然后获取玩家输入,并根据输入移动玩家。如果新的玩家位置是空地,则直接移动玩家。如果新的玩家位置是箱子,则需要检查箱子是否可以移动。移动箱子后,我们需要更新地图和玩家位置,然后检查是否胜利。如果所有箱子都被推到目标位置,则游戏胜利。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值