string[,] mapArray = new string[5, 5];
//? 數(shù)組初始化
for (int i = 0; i < mapArray.GetLength (0); i++) {
for (int j = 0; j < mapArray.GetLength (1); j++) {
mapArray [i, j] = "*";
}
}
// 初始化玩家位置
mapArray [mapArray.GetLength (0) - 1, 0] = "#";
//記錄當(dāng)前位置
int x = 0;? ?// 列
int y = mapArray.GetLength (0) - 1;? ?// 行
while (true) {
// 打印
for (int i = 0; i < mapArray.GetLength (0); i++) {
for (int j = 0; j < mapArray.GetLength (1); j++) {
Console.Write (mapArray [i, j] + " ");
}
Console.WriteLine ();
}
Console.WriteLine ("請輸入移動(dòng)方向(wasd):");
string dir = Console.ReadLine ();
switch (dir) {
case "w":
if (y - 1 >= 0) {
mapArray [y, x] = "*";
mapArray [y - 1, x] = "#";
// 重新記錄當(dāng)前位置
y--;
} else {
Console.WriteLine ("你想上天凹套琛废酷!小伙子!");
}
break;
case "s":
if (y + 1 < mapArray.GetLength (0)) {
mapArray [y, x] = "*";
mapArray [y + 1, x] = "#";
y++;
} else {
Console.WriteLine ("你想下地獄澳痢趴俘!");
}
break;
case "d":
if (x + 1 < mapArray.GetLength (1)) {
mapArray [y, x] = "*";
mapArray [y, ++x] = "#";
} else {
Console.WriteLine ("這是d的盡頭了!");
}
break;
case "a":
if (x - 1 >= 0) {
mapArray [y, x] = "*";
mapArray [y, --x] = "#";
} else {
Console.WriteLine ("小伙子太惠,到頭了疲憋!傻逼");
}
break;
default:
break;
}
}