推箱子
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 推箱子 { class Program { static void Main(string[] args) { #region 画地图 int[,,] map = new int[5,10, 10] { {{1,1,1,1,1,1,1,1,1,1}, {1,0,0,0,0,0,0,0,0,1}, {1,0,3,0,0,0,0,4,0,1}, {1,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,0,0,0,0,1}, {1,0,2,0,0,0,0,0,0,1}, {1,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,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,1,0,2,0,1}, {1,0,0,0,0,1,0,0,0,1}, {1,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,1,0,3,0,1}, {1,0,0,0,0,0,0,0,0,1}, {1,0,4,0,0,1,0,0,0,1}, {1,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,0,0,0,0,0,0,0,0,1}, {1,0,2,0,0,1,0,3,0,1}, {1,0,0,0,0,1,0,0,0,1}, {1,0,0,1,0,1,0,0,0,1}, {1,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,1,0,0,0,1}, {1,0,4,0,0,0,0,0,0,1}, {1,0,0,0,0,1,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,0,0,0,0,0,0,0,0,1}, {1,0,2,0,0,1,0,3,0,1}, {1,0,0,0,0,1,0,0,0,1}, {1,0,3,1,0,1,0,0,0,1}, {1,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,1,0,0,0,1}, {1,0,4,0,0,0,0,4,0,1}, {1,0,0,0,0,1,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,0,0,0,0,0,0,0,0,1}, {1,0,2,0,0,1,0,4,0,1}, {1,0,0,0,0,1,0,0,0,1}, {1,0,0,1,0,1,3,0,0,1}, {1,0,0,0,0,0,0,0,0,1}, {1,0,0,0,0,1,0,3,0,1}, {1,0,4,0,0,0,0,0,0,1}, {1,0,0,0,0,1,0,0,0,1}, {1,1,1,1,1,1,1,1,1,1}} }; #endregion int x = 2; int y = 7; for (int ii = 0; ii < 5; ii++) { while (true) { Console.Clear(); #region 打印地图 for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { if (map[ii, i, j] == 1) { Console.Write("■"); } else if (map[ii, i, j] == 0) { Console.Write(" "); } else if (map[ii, i, j] == 2) { Console.Write("♀"); } else if (map[ii, i, j] == 3) { Console.Write("□"); } else if (map[ii, i, j] == 4) { Console.Write("☆"); } else if (map[ii, i, j] == 5) { Console.Write("★"); } } Console.WriteLine(); } #endregion #region 通关条件 if (ii == 0) { if (map[ii, 2, 7] == 5)//通关条件 { Console.WriteLine("恭喜过关!"); Console.ReadKey(); x = 7; y = 2; break; } } else if (ii == 1) { if (map[ii, 7, 2] == 5) { Console.WriteLine("恭喜过关!"); Console.ReadKey(); x = 2; y = 2; break; } } else if (ii == 2) { if (map[ii, 7, 2] == 5) { Console.WriteLine("恭喜过关!"); Console.ReadKey(); x = 2; y = 2; break; } } else if (ii == 3) { if ((map[ii, 7, 2] == 5)&&(map[ii, 7, 7] == 5)) { Console.WriteLine("恭喜过关!"); Console.ReadKey(); x = 2; y = 2; break; } } else if (ii == 4) { if ((map[ii, 7, 2] == 5) && (map[ii, 2, 7] == 5)) { Console.WriteLine("恭喜过关!游戏通关!"); Console.ReadKey(); break; } } #endregion #region 移动 ConsoleKeyInfo info = Console.ReadKey(); if (info.Key.ToString() == "UpArrow") { if (map[ii, y - 1, x] == 1 || map[ii, y - 1, x] == 4)//小人的下一步是墙或目标点 { } else if (map[ii, y - 1, x] == 3)//小人的下一步是箱子 { if (map[ii, y - 2, x] == 1)//箱子的下一步是墙 { } else if (map[ii, y - 2, x] == 4) //箱子的下一步是目标点 { int zhong = map[ii, y, x];//记录小人的位置 map[ii, y, x] = map[ii, y - 1, x];//箱子的位置赋值给小人 map[ii, y - 1, x] = zhong;//小人的位置赋值个箱子 y--; map[ii, y + 1, x] = 0; map[ii, y - 1, x] = 5; } else if (map[ii, y - 2, x] == 0)//箱子的下一步是空地 { int zhong = map[ii, y, x];//记录小人的位置 map[ii, y, x] = map[ii, y - 1, x];//箱子的位置赋值给小人 map[ii, y - 1, x] = zhong;//小人的位置赋值个箱子 y--; zhong = map[ii, y - 1, x];//记录小人下一步位置 map[ii, y - 1, x] = map[ii, y + 1, x];//把小人前一步的箱子赋值给下一步 map[ii, y + 1, x] = zhong;//把吓人下一步位置赋值个前一步的箱子 } } else if (map[ii, y - 1, x] == 0)//小人的下一步是空地 { int zhong = map[ii, y, x]; map[ii, y, x] = map[ii, y - 1, x]; map[ii, y - 1, x] = zhong; y--; } } else if (info.Key.ToString() == "DownArrow") { if (map[ii, y + 1, x] == 1 || map[ii, y + 1, x] == 4)//小人的下一步是墙或目标点 { } else if (map[ii, y + 1, x] == 3)//小人的下一步是箱子 { if (map[ii, y + 2, x] == 1)//箱子的下一步是墙 { } else if (map[ii, y + 2, x] == 4) //箱子的下一步是目标点 { int zhong = map[ii, y, x]; map[ii, y, x] = map[ii, y + 1, x]; map[ii, y + 1, x] = zhong; y++; map[ii, y - 1, x] = 0; map[ii, y + 1, x] = 5; } else if (map[ii, y + 2, x] == 0)//箱子的下一步是空地 { int zhong = map[ii, y, x]; map[ii, y, x] = map[ii, y + 1, x]; map[ii, y + 1, x] = zhong; y++; zhong = map[ii, y + 1, x]; map[ii, y + 1, x] = map[ii, y - 1, x]; map[ii, y - 1, x] = zhong; } } else if (map[ii, y + 1, x] == 0)//小人的下一步是空地 { int zhong = map[ii, y, x]; map[ii, y, x] = map[ii, y + 1, x]; map[ii, y + 1, x] = zhong; y++; } } else if (info.Key.ToString() == "LeftArrow") { if (map[ii, y, x - 1] == 1 || map[ii, y, x - 1] == 4)//小人的下一步是墙或目标点 { } else if (map[ii, y, x - 1] == 3)//小人的下一步是箱子 { if (map[ii, y, x - 2] == 1)//箱子的下一步是墙 { } else if (map[ii, y, x - 2] == 4) //箱子的下一步是目标点 { int zhong = map[ii, y, x]; map[ii, y, x] = map[ii, y, x - 1]; map[ii, y, x - 1] = zhong; x--; map[ii, y, x + 1] = 0; map[ii, y, x - 1] = 5; } else if (map[ii, y, x - 2] == 0)//箱子的下一步是空地 { int zhong = map[ii, y, x]; map[ii, y, x] = map[ii, y, x - 1]; map[ii, y, x - 1] = zhong; x--; zhong = map[ii, y, x - 1]; map[ii, y, x - 1] = map[ii, y, x + 1]; map[ii, y, x + 1] = zhong; } } else if (map[ii, y, x - 1] == 0)//小人的下一步是空地 { int zhong = map[ii, y, x]; map[ii, y, x] = map[ii, y, x - 1]; map[ii, y, x - 1] = zhong; x--; } } else if (info.Key.ToString() == "RightArrow") { if (map[ii, y, x + 1] == 1 || map[ii, y, x + 1] == 4)//小人的下一步是墙或目标点 { } else if (map[ii, y, x + 1] == 3)//小人的下一步是箱子 { if (map[ii, y, x + 2] == 1)//箱子的下一步是墙 { } else if (map[ii, y, x + 2] == 4) //箱子的下一步是目标点 { int zhong = map[ii, y, x]; map[ii, y, x] = map[ii, y, x + 1]; map[ii, y, x + 1] = zhong; x++; map[ii, y, x - 1] = 0; map[ii, y, x + 1] = 5; } else if (map[ii, y, x + 2] == 0)//箱子的下一步是空地 { int zhong = map[ii, y, x]; map[ii, y, x] = map[ii, y, x + 1]; map[ii, y, x + 1] = zhong; x++; zhong = map[ii, y, x + 1]; map[ii, y, x + 1] = map[ii, y, x - 1]; map[ii, y, x - 1] = zhong; } } else if (map[ii, y, x + 1] == 0)//小人的下一步是空地 { int zhong = map[ii, y, x]; map[ii, y, x] = map[ii, y, x + 1]; map[ii, y, x + 1] = zhong; x++; } } #endregion } } Console.ReadLine(); } } }