多个箱子:
namespace ConsoleApplication1 { class Program { private static void ditu(int[, ,] dt, int i) { for (int j = 0; j < 10; j++) { for (int k = 0; k < 10; k++) { if (dt[i, j, k] == 0) { Console.Write(" "); } else if (dt[i, j, k] == 1) { Console.Write("■"); } else if (dt[i, j, k] == 2) { Console.Write("□"); } else if (dt[i, j, k] == 3) { Console.Write("★"); } else if (dt[i, j, k] == 4) { Console.Write("♀"); } } Console.Write("\n"); } } static void Main(string[] args) { int x = 1, y = 1, i = 0; int[, ,] dt = new int[3, 10, 10] //定义一个二维数组用于接收地图 { { {1,1,1,1,1,1,1,1,1,1}, {1,4,0,1,0,0,0,0,0,1}, {1,0,0,0,0,0,2,0,3,1}, {1,0,1,1,0,1,0,0,0,1}, {1,0,0,0,0,0,0,0,0,1}, {1,3,1,1,1,1,1,2,1,1}, {1,2,1,0,0,0,0,0,0,1}, {1,0,0,0,0,1,1,0,0,1}, {1,1,0,1,0,0,0,3,0,1}, {1,1,1,1,1,1,1,1,1,1}, }, { {1,1,1,1,1,1,1,1,1,1}, {1,1,1,1,4,0,1,0,3,1}, {1,1,1,3,0,0,2,0,0,1}, {1,1,0,0,2,0,1,0,0,1}, {1,0,0,0,1,1,1,0,0,1}, {1,0,1,1,0,0,0,0,0,1}, {1,0,0,0,0,2,0,0,1,1}, {1,0,3,0,0,0,1,1,1,1}, {1,0,0,0,0,1,1,1,1,1}, {1,1,1,1,1,1,1,1,1,1}, }, { {1,1,1,1,1,1,1,1,1,1}, {1,1,0,0,4,0,0,3,1,1}, {1,1,0,2,0,0,0,2,3,1}, {1,1,0,0,2,1,1,0,0,1}, {1,1,0,1,0,0,0,3,0,1}, {1,0,3,0,0,0,1,0,1,1}, {1,0,0,1,1,2,0,0,1,1}, {1,3,2,0,0,0,2,0,1,1}, {1,1,3,0,0,0,0,0,1,1}, {1,1,1,1,1,1,1,1,1,1}, } }; try { int count = 0;//临时变量 int xzs = 0;//箱子数 for (int j = 0; j < 10; j++) { for (int k = 0; k < 10; k++) { if (dt[i, j, k] == 3) { xzs++; } } } while (true) { int temp = 0; ConsoleKeyInfo s = Console.ReadKey(); //s用于接收用户输入的键 if (s.Key.ToString() == "DownArrow") { if (dt[i, x + 1, y]!=1) { if (dt[i, x + 1, y] == 0)//如果小人的下一步是0(空格),则两个元素交换 (下同) { temp = dt[i, x, y]; dt[i, x, y] = dt[i, x + 1, y]; dt[i, x + 1, y] = temp; x++; } else if (dt[i, x + 1, y] == 2 && dt[i, x + 2, y] == 0)//如果小人的下一步是箱子,箱子的下一步是空格,则三个元素进行交换 (下同) { temp = dt[i, x + 2, y]; dt[i, x + 2, y] = dt[i, x + 1, y]; dt[i, x + 1, y] = temp; temp = dt[i, x + 1, y]; dt[i, x + 1, y] = dt[i, x, y]; dt[i, x, y] = temp; x++; } else if (dt[i, x + 2, y] == 3 && dt[i, x + 1, y] == 2) //如果小人的下面第二个元素是“★” { dt[i, x + 2, y] = 2;//让五角星变成箱子 dt[i, x + 1, y] = 4;// 让箱子变成小人 dt[i, x, y] = 0; //让小人变成空格 x++; count++; //给临时变量赋值作为跳出循环的条件,当count等于xzs时跳出循环 } } } else if (s.Key.ToString() == "UpArrow") { if (dt[i, x - 1, y] != 1) { if (dt[i, x - 1, y] == 0) { temp = dt[i, x - 1, y]; dt[i, x - 1, y] = dt[i, x, y]; dt[i, x, y] = temp; x--; } else if (dt[i, x - 1, y] == 2 && dt[i, x - 2, y] == 0) { temp = dt[i, x - 2, y]; dt[i, x - 2, y] = dt[i, x - 1, y]; dt[i, x - 1, y] = temp; temp = dt[i, x, y]; dt[i, x, y] = dt[i, x - 1, y]; dt[i, x - 1, y] = temp; x--; } else if (dt[i, x - 2, y] == 3 && dt[i, x - 1, y] == 2) //如果小人的下面第二个元素是“★” { dt[i, x - 2, y] = 2; //让五角星变成箱子 dt[i, x - 1, y] = 4;// 让箱子变成小人 dt[i, x, y] = 0; //让小人变成空格 x--; count++; //给临时变量赋值作为跳出循环的条件,当count等于xzs时跳出循环 } } } else if (s.Key.ToString() == "LeftArrow") { if (dt[i, x, y - 1] != 1) { if (dt[i, x, y - 1] == 0) { temp = dt[i, x, y - 1]; dt[i, x, y - 1] = dt[i, x, y]; dt[i, x, y] = temp; y--; } else if (dt[i, x, y - 1] == 2 && dt[i, x, y - 2] == 0) { temp = dt[i, x, y - 2]; dt[i, x, y - 2] = dt[i, x, y - 1]; dt[i, x, y - 1] = temp; temp = dt[i, x, y]; dt[i, x, y] = dt[i, x, y - 1]; dt[i, x, y - 1] = temp; y--; } else if (dt[i, x, y - 2] == 3 && dt[i, x, y - 1] == 2) //如果小人的下面第二个元素是“★” { dt[i, x, y - 2] = 2; //让五角星变成箱子 dt[i, x, y - 1] = 4;// 让箱子变成小人 dt[i, x, y] = 0; //让小人变成空格 y--; count++; //给临时变量赋值作为跳出循环的条件,当count等于xzs时跳出循环 } } } else if (s.Key.ToString() == "RightArrow") { if (dt[i, x, y + 1] != 1) { if (dt[i, x, y + 1] == 0) { temp = dt[i, x, y + 1]; dt[i, x, y + 1] = dt[i, x, y]; dt[i, x, y] = temp; y++; } else if (dt[i, x, y + 1] == 2 && dt[i, x, y + 2] == 0) { temp = dt[i, x, y + 2]; dt[i, x, y + 2] = dt[i, x, y + 1]; dt[i, x, y + 1] = temp; temp = dt[i, x, y + 1]; dt[i, x, y + 1] = dt[i, x, y]; dt[i, x, y] = temp; y++; } else if (dt[i, x, y + 2] == 3 && dt[i, x, y + 1] == 2) //如果小人的下面第二个元素是“★” { dt[i, x, y + 2] = 2; //让五角星变成箱子 dt[i, x, y + 1] = 4;// 让箱子变成小人 dt[i, x, y] = 0; //让小人变成空格 y++; count++; //给临时变量赋值作为跳出循环的条件,当count等于xzs时跳出循环 } } } else { } Console.Clear(); //清屏 ditu(dt, i);// 调用ditu函数打印地图 if (count==xzs) { Console.WriteLine("恭喜你过关了!"); Console.WriteLine("按任意键进入下一关!"); Console.ReadKey(); x = 1; y = 4;//初始化小人的位置 i++; count = 0; xzs = 0; for (int j = 0; j < 10; j++) { for (int k = 0; k < 10; k++) { if (dt[i, j, k] == 3) { xzs++; } } } } } } catch (Exception) { // Console.WriteLine("恭喜你全部过关了!"); Console.ReadKey(); } } } }