1 int a = 2, b = 1;//人的初始坐标 2 #region 造地图 3 int[,] map = new int[10,10]{ 4 {8,8,8,8,8,8,8,8,8,8}, 5 {8,0,0,0,0,0,8,8,0,8}, 6 {8,1,2,0,0,8,8,8,0,8}, 7 {8,0,0,0,0,8,8,8,0,8}, 8 {8,0,0,0,0,0,8,8,0,8}, 9 {8,0,8,0,0,0,8,8,0,8}, 10 {8,0,8,8,0,0,8,8,0,8}, 11 {8,0,8,8,0,0,0,0,0,8}, 12 {8,0,8,8,0,0,0,0,3,8}, 13 {8,8,8,8,8,8,8,8,8,8}, 14 }; 15 #endregion 16 #region 显示地图 17 for (int x = 0; x < 10; x++) 18 { 19 Console.ForegroundColor = ConsoleColor.White; 20 for (int y = 0; y < 10; y++) 21 { 22 if (map[x, y] == 8) 23 { 24 Console.Write("■"); 25 } 26 if (map[x, y] == 0) 27 { 28 Console.Write(" "); 29 } 30 if (map[x, y] == 1) 31 { 32 Console.Write("♀"); 33 } 34 if (map[x, y] == 2) 35 { 36 Console.Write("□"); 37 } 38 if (map[x, y] == 3) 39 { 40 Console.ForegroundColor = ConsoleColor.Red; 41 Console.Write("※"); 42 Console.ForegroundColor = ConsoleColor.White; 43 } 44 } 45 Console.WriteLine(); 46 } 47 #endregion 48 49 while (map[8, 8] != 2) 50 { 51 ConsoleKeyInfo key= Console.ReadKey(); //读键 52 #region 按下上键时执行的程序 53 if (key.Key.ToString().ToLower() == "uparrow") 54 { 55 if (a > 1)//如果人不在靠墙的位置 56 { 57 if (map[a - 1, b] == 0)//如果人前面是路 58 { 59 map[a - 1, b] = 1; 60 map[a, b] = 0; 61 map[8, 8] = 3; 62 a--; 63 } 64 else if (map[a - 1, b] == 3) 65 { 66 map[a - 1, b] = 1; 67 map[a, b] = 0; 68 a--; 69 } 70 else if (map[a - 1, b] == 2 && map[a - 2, b] == 0) //如果人上面是箱子,箱子上面是路 71 { 72 map[a - 2, b] = 2; 73 map[a - 1, b] = 1; 74 map[a, b] = 0; 75 a--; 76 } 77 else if (map[a - 1, b] == 2 && map[a - 2, b] == 3)//如果人上面是箱子,箱子上面是终点 78 { 79 map[a - 2, b] = 2; 80 map[a - 1, b] = 1; 81 map[a, b] = 0; 82 a--; 83 } 84 else 85 { 86 Console.WriteLine("a"); 87 } 88 } 89 } 90 #endregion 91 #region 下键 92 else if (key.Key.ToString().ToLower() == "downarrow") 93 { 94 if(a<8) 95 { 96 if (map[a + 1, b] == 0) 97 { 98 map[a + 1, b] = 1; 99 map[a, b] = 0; 100 map[8, 8] = 3; 101 a++; 102 } 103 else if (map[a + 1, b] == 3) 104 { 105 map[a + 1, b] = 1; 106 map[a, b] = 0; 107 a++; 108 } 109 else if (map[a + 1, b] == 2 && map[a + 2, b] == 0) 110 { 111 map[a + 2, b] = 2; 112 map[a + 1, b] = 1; 113 map[a, b] = 0; 114 a++; 115 } 116 else if (map[a + 1, b] == 2 && map[a + 2, b] == 3) 117 { 118 map[a + 2, b] = 2; 119 map[a + 1, b] = 1; 120 map[a, b] = 0; 121 a++; 122 } 123 else 124 { 125 Console.WriteLine("a"); 126 } 127 } 128 } 129 #endregion 130 #region 左键 131 else if (key.Key.ToString().ToLower() == "leftarrow") 132 { 133 if (b > 1) 134 { 135 136 if (map[a, b - 1] == 0) 137 { 138 map[a, b - 1] = 1; 139 map[a, b] = 0; 140 map[8, 8] = 3; 141 b--; 142 } 143 else if (map[a, b - 1] == 3) 144 { 145 map[a, b - 1] = 1; 146 map[a, b] = 0; 147 b--; 148 } 149 else if (map[a, b - 1] == 2 && map[a, b - 2] == 0) 150 { 151 map[a, b - 2] = 2; 152 map[a, b - 1] = 1; 153 map[a, b] = 0; 154 b--; 155 } 156 else if (map[a, b - 1] == 2 && map[a, b - 2] == 3) 157 { 158 map[a, b - 2] = 2; 159 map[a, b - 1] = 1; 160 map[a, b] = 0; 161 b--; 162 } 163 else 164 { 165 Console.WriteLine("a"); 166 } 167 } 168 } 169 #endregion 170 #region 右键 171 else if (key.Key.ToString().ToLower() == "rightarrow") 172 { 173 if (b < 8) 174 { 175 176 if (map[a, b + 1] == 0) 177 { 178 map[a, b + 1] = 1; 179 map[a, b] = 0; 180 map[8, 8] = 3; 181 b++; 182 } 183 else if (map[a, b + 1] == 3) 184 { 185 map[a, b + 1] = 1; 186 map[a, b] = 0; 187 b++; 188 } 189 else if (map[a, b + 1] == 2 && map[a, b + 2] == 0) 190 { 191 map[a, b + 2] = 2; 192 map[a, b + 1] = 1; 193 map[a, b] = 0; 194 b++; 195 } 196 else if (map[a, b + 1] == 2 && map[a, b + 2] == 3) 197 { 198 map[a, b + 2] = 2; 199 map[a, b + 1] = 1; 200 map[a, b] = 0; 201 b++; 202 } 203 else 204 { 205 Console.WriteLine("a"); 206 } 207 } 208 } 209 #endregion 210 211 #region 刷新地图 212 Console.Clear(); 213 for (int x = 0; x < 10; x++) 214 { 215 Console.ForegroundColor = ConsoleColor.White; 216 for (int y = 0; y < 10; y++) 217 { 218 if (map[x, y] == 8) 219 { 220 Console.Write("■"); 221 } 222 if (map[x, y] == 0) 223 { 224 Console.Write(" "); 225 } 226 if (map[x, y] == 1) 227 { 228 Console.Write("♀"); 229 } 230 if (map[x, y] == 2) 231 { 232 Console.Write("□"); 233 } 234 if (map[x, y] == 3) 235 { 236 Console.ForegroundColor = ConsoleColor.Red; 237 Console.Write("※"); 238 Console.ForegroundColor = ConsoleColor.White; 239 } 240 } 241 Console.WriteLine(); 242 } 243 #endregion 244 } 245 Console.WriteLine("恭喜你!通关了!"); 246 Console.ReadLine();