namespace ConsoleApplication15//推箱子本质就是变量的相互替换,3个变量的循环赋值 { class Program { static void Main(string[] args) { int x = 1, y = 1, i = 0; int[, ,] dt = new int[2, 30, 30] //定义一个二维数组用于接收地图 { { {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,1,1,1}, {1,4,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,2,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0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,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,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,1,1,1,1} } }; try { while (true) { int temp = 0; ConsoleKeyInfo s = Console.ReadKey(); //s用于接收用户输入的键 if (s.Key.ToString() == "DownArrow") { 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; //让小人变成空格 temp = 6; //给临时变量赋一个值作为跳出循环的条件 } } else if (s.Key.ToString() == "UpArrow") { 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; //让小人变成空格 temp = 6; //给临时变量赋一个值作为跳出循环的条件 } } else if (s.Key.ToString() == "LeftArrow") { 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; //让小人变成空格 temp = 6; //给临时变量赋一个值作为跳出循环的条件 } } else if (s.Key.ToString() == "RightArrow") { 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; //让小人变成空格 temp = 6; //给临时变量赋一个值作为跳出循环的条件 } } Console.Clear(); //清屏 ditu(dt, i);// 调用ditu函数打印地图;一开始就从这里开始,前面没有打印地图。 if (temp == 6)// 跳出循环 { Console.WriteLine("恭喜您过关了!"); Console.WriteLine("按任意键进入下一关!"); Console.ReadKey(); x = 1; y = 1;//初始化小人的位置 i++; } } } catch (Exception) { Console.WriteLine("暂时只有两张图!"); Console.ReadKey(); } } private static void ditu(int[, ,] dt, int i) { for (int j = 0; j < 30; j++) { for (int k = 0; k < 30; 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(" "); } } } }