static void Main41推箱子(string[] args) { //static void Main(string[] args) { //定义地图 #region 定义地图 int x = 6; int y = 1; //人的初始坐标 int[,] map = new int[10, 10] { {8,8,8,8,8,8,8,8,8,8}, {8,0,0,0,0,8,8,0,0,8}, {8,0,8,0,0,8,8,0,0,8}, {8,0,8,8,0,8,8,0,0,8}, {8,0,8,0,0,0,0,0,0,8}, {8,0,0,0,0,8,0,0,0,8}, {8,1,0,2,0,8,8,0,0,8}, {8,0,0,0,0,8,0,0,8,8}, {8,0,0,0,0,8,0,0,3,8}, {8,8,8,8,8,8,8,8,8,8}, }; #endregion #region 显示地图 for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { Console.ForegroundColor = ConsoleColor.White; if (map[i, j] == 0)//0代表空地 { Console.Write(" "); } else if (map[i, j] == 1)//1代表人 { Console.Write("♀"); } else if (map[i, j] == 2)//2代表箱子 { Console.Write("□"); } else if (map[i, j] == 3)//3代表终点 { Console.ForegroundColor = ConsoleColor.Red; Console.Write("※"); Console.ForegroundColor = ConsoleColor.White; } else if (map[i, j] == 8)//8代表墙壁 { Console.Write("■"); } } Console.WriteLine(); } #endregion //推箱子开始 while (map[8, 8] != 2)//当箱子没到终点的时候 { ConsoleKeyInfo key = Console.ReadKey(); //按下上键时执行的程序 #region 上键 if (key.Key.ToString().ToLower() == "uparrow") { if (x > 1) //如果人不在靠墙的位置 { if (map[x - 1, y] == 0) //如果人上面是路 { map[x - 1, y] = 1; map[x, y] = 0; map[8, 8] = 3; x--; } else if (map[x - 1, y] == 3)//如果人上面是终点 { map[x - 1, y] = 1; map[x, y] = 0; x--; } else if (map[x - 1, y] == 2 && map[x - 2, y] == 0)//如果人上面是箱子,箱子上面是路 { map[x - 2, y] = 2; map[x - 1, y] = 1; map[x, y] = 0; x--; //a--; } else if (map[x - 1, y] == 2 && map[x - 2, y] == 3)//如果人上面是箱子,箱子上面是终点 { map[x - 2, y] = 2; map[x - 1, y] = 1; map[x, y] = 0; x--; //a--; } else { Console.Write("a"); } } else { Console.Write("a"); } } #endregion #region 下键 //按下的键为下键时执行的程序 else if (key.Key.ToString().ToLower() == "downarrow") { if (x < 8) //如果人不在靠墙的位置 { if (map[x + 1, y] == 0) //如果人下面是路 { map[x + 1, y] = 1; map[x, y] = 0; map[8, 8] = 3; x++; } else if (map[x + 1, y] == 3)//如果人下面是终点 { map[x + 1, y] = 1; map[x, y] = 0; x++; } else if (map[x + 1, y] == 2 && map[x + 2, y] == 0)//如果人下面是箱子,箱子下面是路 { map[x + 2, y] = 2; map[x + 1, y] = 1; map[x, y] = 0; x++; } else if (map[x + 1, y] == 2 && map[x + 2, y] == 3)//如果人下面是箱子,箱子下面是终点 { map[x + 2, y] = 2; map[x + 1, y] = 1; map[x, y] = 0; x++; } else { Console.Write("a"); } } else { Console.Write("a"); } } #endregion #region 左键 //按下的键为左键时执行的程序 else if (key.Key.ToString().ToLower() == "leftarrow") { if (y > 1) //如果人不在靠墙的位置 { if (map[x, y - 1] == 0) //如果人左面是路 { map[x, y - 1] = 1; map[x, y] = 0; map[8, 8] = 3; y--; } else if (map[x, y - 1] == 3)//如果人左边是终点 { map[x, y - 1] = 1; map[x, y] = 0; y--; } else if (map[x, y - 1] == 2 && map[x, y - 2] == 0)//如果人左面是箱子,箱子左面是路 { map[x, y - 2] = 2; map[x, y - 1] = 1; map[x, y] = 0; y--; } else if (map[x, y - 1] == 2 && map[x, y - 2] == 3)//如果人左面是箱子,箱子左面是终点 { map[x, y - 2] = 2; map[x, y - 1] = 1; map[x, y] = 0; y--; } else { Console.Write("a"); } } else { Console.Write("a"); } } #endregion #region 右键 //按下的键为右键时执行的程序s else if (key.Key.ToString().ToLower() == "rightarrow") { if (y < 8) { if (map[x, y + 1] == 0)//如果人右边是空地 { map[x, y + 1] = 1; map[x, y] = 0; map[8, 8] = 3; y++; } else if (map[x, y + 1] == 3)//如果人右边是终点 { map[x, y + 1] = 1; map[x, y] = 0; y++; } else if (map[x, y + 1] == 2 && map[x, y + 2] == 0)//如果人右边是箱子,箱子右边是空地 { map[x, y + 2] = 2; map[x, y + 1] = 1; map[x, y] = 0; y++; } else if (map[x, y + 1] == 2 && map[x, y + 2] == 3)//如果人右边是箱子,箱子右边是终点 { map[x, y + 2] = 2; map[x, y + 1] = 1; map[x, y] = 0; y++; } else { Console.Write("a"); } } else { Console.Write("a"); } } #endregion #region 重新显示 Console.Clear(); for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { Console.ForegroundColor = ConsoleColor.White; if (map[i, j] == 0) { Console.Write(" "); } else if (map[i, j] == 1) { Console.Write("♀"); } else if (map[i, j] == 2) { Console.Write("□"); } else if (map[i, j] == 3) { Console.ForegroundColor = ConsoleColor.Red; Console.Write("※"); Console.ForegroundColor = ConsoleColor.White; } else if (map[i, j] == 8) { Console.Write("■"); } } Console.WriteLine(); } #endregion } Console.WriteLine("恭喜你,通关成功!"); Console.ReadLine(); } }