If statement 测试控制台中的另一个字符是否在所选方向上显示特定字符

If statement 测试控制台中的另一个字符是否在所选方向上显示特定字符,if-statement,console,switch-statement,character,If Statement,Console,Switch Statement,Character,我制作了一小段代码,创建了一个由“.”组成的字段,并希望将“#”设置为一种屏障形式。我已经将播放器位置设置为一个变量,并用“*”表示,并编写了一段代码,允许用户输入选择它在球场上移动的方向。当按下某个方向时,我正在努力进行检查,如果出现“#”,它将显示一条消息,说明您不能朝该方向移动,否则将正常进行。虽然我理解这个问题可能已经得到了回答,但我找不到我的问题的答案,或者无法按照我给出的示例执行它 using System; class TestGame { static void Mai

我制作了一小段代码,创建了一个由“.”组成的字段,并希望将“#”设置为一种屏障形式。我已经将播放器位置设置为一个变量,并用“*”表示,并编写了一段代码,允许用户输入选择它在球场上移动的方向。当按下某个方向时,我正在努力进行检查,如果出现“#”,它将显示一条消息,说明您不能朝该方向移动,否则将正常进行。虽然我理解这个问题可能已经得到了回答,但我找不到我的问题的答案,或者无法按照我给出的示例执行它

using System;

class TestGame
{
    static void Main(string[] args)
    {
        int playerY = 2; //sets the y coordinate of the player
        int playerX = 2; //sets the x coordinate of the player
        bool gameRunning = true;

        while (gameRunning)
        {
            // Display the game board
            for (int y = 0; y < 12; y++) //sets y to 0, checks if y is less than 12, executes commands, adds 1 to y
            {
                for (int x = 0; x < 40; x++) //same as above but for x
                {
                    if (y == playerY && x == playerX) //checks if y == to playerY and x ==player X
                    {
                        Console.Write("*"); //for when, if it is then it outputs a *
                    }
                    else if (y == 2 && x == 12) //for when, if y and x are equal to a specific value
                    {
                        Console.Write("@"); //execute this
                    }
                    else if (y == 0 || x == 0 || y == 11 || x == 39)
                    {
                        Console.Write("#");
                    }
                    else
                    {
                        Console.Write("."); //otherwise just have a dot
                    }
                }
                Console.WriteLine(); //empty space
            }

            // Get user input
            Console.WriteLine("Enter move: A D W S");
            string userInput = Console.ReadLine();
            switch (userInput) //checks for user inputs
            {
                case "A":
                    playerX--;
                    break;
                case "D":
                    playerX++;
                    break;
                case "W":
                    playerY--;
                    break;
                case "S":
                    playerY++;
                    break;
                default:
                    Console.WriteLine("Invalid Key"); //defaults to this if any other key is pressed
                    break;
            }
        }

    }
}
使用系统;
类测试游戏
{
静态void Main(字符串[]参数)
{
int playerY=2;//设置播放器的y坐标
int playerX=2;//设置播放器的x坐标
bool gameRunning=true;
同时(游戏运行)
{
//显示游戏板
for(int y=0;y<12;y++)//将y设置为0,检查y是否小于12,执行命令,将1添加到y
{
对于(int x=0;x<40;x++)//与上面相同,但对于x
{
if(y==playerY&&x==playerX)//检查y==to playerY和x==playerX
{
Console.Write(“*”;//表示何时,如果是,则输出*
}
else if(y==2&&x==12)//对于何时,如果y和x等于特定值
{
Console.Write(“@”);//执行此命令
}
else如果(y==0 | | x==0 | | y==11 | | x==39)
{
控制台。写(“#”);
}
其他的
{
Console.Write(“.”;//否则只有一个点
}
}
Console.WriteLine();//空白
}
//获取用户输入
Console.WriteLine(“输入移动:A D W S”);
字符串userInput=Console.ReadLine();
开关(userInput)//检查用户输入
{
案例“A”:
playerX--;
打破
案例“D”:
playerX++;
打破
案例“W”:
游戏——;
打破
案例“S”:
playerY++;
打破
违约:
Console.WriteLine(“无效键”);//如果按下任何其他键,则默认为该键
打破
}
}
}
}
}