迷宫

string[,] ditu = new string[8, 8]
{
{"■","人","■","■","■","■","■","■"},
{"■"," ","■"," "," "," "," ","■"},
{"■"," "," "," ","■","■"," ","■"},
{"■","■","■","■"," "," "," ","■"},
{"■"," "," "," "," ","■","■","■"},
{"■"," ","■","■","■","■","■","■"},
{"■"," "," "," "," "," "," "," "},
{"■","■","■","■","■","■","■","■"},
};


int renx = 1;    
int reny = 0;   //人的位置
int mubiaox = 7;
int mubiaoy = 6;  //目标点的位置
while (true)
{
Console.Clear();
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
Console.Write(ditu[i, j]);
}
Console.WriteLine("");
}
Console.WriteLine("请输入w上a左s下d右");
string s = Console.ReadLine();
int ydhx = 0;
int ydhy = 0;
if (s == "w" || s == "W")   //往上走一格
{
ydhx = renx;
ydhy = reny - 1;
}
else if (s == "S" || s == "s")//往下走一格
{
ydhx = renx;
ydhy = reny + 1;
}
else if (s == "A" || s == "a")//往左走一格
{
ydhx = renx - 1;
ydhy = reny;
}
else if (s == "d" || s == "D")//往右走一格
{
ydhx = renx + 1;
ydhy = reny;
}
else
{
Console.WriteLine("您输入有误");
continue;
}
if (ditu[ydhy, ydhx] == "■")
{ }
else if(ydhx==mubiaox&&ydhy==mubiaoy)
{
Console.WriteLine("恭喜走出迷宫");//到达目标点
break;
}

else
{
ditu[reny, renx] = " ";
renx = ydhx;
reny = ydhy;
ditu[reny, renx] = "人";//每走一格“人”变化
}
}
Console.ReadLine();

原文地址:https://www.cnblogs.com/jskbk/p/5354684.html