//0是空位,1是墙,2是人,3是箱子,4是终点 int[][,] a = new int[2][,]; int[,] b0 = new int[10, 10]{ {1,1,1,1,1,1,1,1,1,1}, {1,0,0,0,1,0,1,0,0,1}, {1,0,0,0,1,0,1,0,0,1}, {1,0,3,0,1,0,1,0,0,1}, {1,0,0,0,1,1,1,0,0,1}, {1,0,0,0,0,0,0,0,0,1}, {1,0,0,1,0,0,0,0,0,1}, {1,2,0,1,1,1,1,0,0,1}, {1,0,0,1,0,4,0,0,0,1}, {1,1,1,1,1,1,1,1,1,1}}; int[,] b1 = new int[10, 10]{ {1,1,1,1,1,1,1,1,1,1}, {1,0,0,0,1,0,1,0,0,1}, {1,0,3,0,1,0,1,3,0,1}, {1,0,0,0,1,0,1,0,0,1}, {1,0,0,0,1,1,1,0,0,1}, {1,0,0,0,0,0,0,0,0,1}, {1,0,0,1,4,0,0,0,0,1}, {1,2,0,1,1,1,1,0,0,1}, {1,0,0,1,0,4,0,0,0,1}, {1,1,1,1,1,1,1,1,1,1}}; //int[,]b2=new int[15,15]{ //{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, //{1,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,1}, //{1,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,1}, //{1,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,1}, //{1,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,1}, //{1,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,1}, //{1,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,1}, //{1,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}} a[0] = b0; a[1] = b1; for (int z = 0; z < 2; z++) //如果增加地图,需修改判断条件 { Console.Clear(); Console.WriteLine("第{0}关!", z + 1); ConsoleKeyInfo start = Console.ReadKey(); string st = start.Key.ToString(); st = st.ToLower(); //游戏开始 if (st == "spacebar") { int[,] map = a[z]; //取出地图 //判断人的位置,i为行,j为列 int i = 0, j = 0; for (int m = 0; m < 10; m++) { for (int n = 0; n < 10; n++) { if (map[m, n] == 2) { i = m; j = n; break; } } } //判断终点个数 int over = 0; for (int m = 0; m < 10; m++) { for (int n = 0; n < 10; n++) { if (map[m, n] == 4) { over++; } } } //显示及操作 for (; true; ) { //输出显示 Console.Clear(); for (int x = 0; x < 10; x++) { for (int y = 0; y < 10; y++) { if (map[x, y] == 0) { Console.Write(" "); } else if (map[x, y] == 1) { Console.Write("■"); } else if (map[x, y] == 2 || map[x, y] == 6) { Console.Write("♀"); } else if (map[x, y] == 3 || map[x, y] == 7) { Console.Write("□"); } else if (map[x, y] == 4) { Console.Write("※"); } } Console.Write(" "); //换行 } //判断有箱子的终点个数 int over1 = 0; for (int m = 0; m < 10; m++) { for (int n = 0; n < 10; n++) { if (map[m, n] == 7) { over1++; } } } //判断是否所有终点有箱子 if (over1 == over) { Console.WriteLine("过关!"); break; } //操作部分 ConsoleKeyInfo K = Console.ReadKey(); string k = K.Key.ToString(); k = k.ToLower(); if (k == "uparrow") //判断人是否是向上 { if (map[i - 1, j] == 0 || map[i - 1, j] == 4) //判断人上方是不是空位 { map[i - 1, j] = map[i - 1, j] + 2; map[i, j] = map[i, j] - 2; i--; } else if ((map[i - 1, j] == 3 || map[i - 1, j] == 7) && map[i - 2, j] != 1) //人上方是箱子,判断箱子上方是否是空位 { map[i - 2, j] = map[i - 2, j] + 3; map[i - 1, j] = map[i - 1, j] - 3 + 2; map[i, j] = map[i, j] - 2; i--; } else //如果人无法移动,输出提示音 Console.Write("a"); } else if (k == "downarrow") { if (map[i + 1, j] == 0 || map[i + 1, j] == 4) { map[i + 1, j] = map[i + 1, j] + 2; map[i, j] = map[i, j] - 2; i++; } else if ((map[i + 1, j] == 3 || map[i + 1, j] == 7) && map[i + 2, j] != 1) { map[i + 2, j] = map[i + 2, j] + 3; map[i + 1, j] = map[i + 1, j] - 3 + 2; map[i, j] = map[i, j] - 2; i++; } else Console.Write("a"); } else if (k == "leftarrow") { if (map[i, j - 1] == 0 || map[i, j - 1] == 4) { map[i, j - 1] = map[i, j - 1] + 2; map[i, j] = map[i, j] - 2; j--; } else if ((map[i, j - 1] == 3 || map[i, j - 1] == 7) && map[i, j - 2] != 1) { map[i, j - 2] = map[i, j - 2] + 3; map[i, j - 1] = map[i, j - 1] - 3 + 2; map[i, j] = map[i, j] - 2; j--; } else Console.Write("a"); } else if (k == "rightarrow") { if (map[i, j + 1] == 0 || map[i, j + 1] == 4) { map[i, j + 1] = map[i, j + 1] + 2; map[i, j] = map[i, j] - 2; j++; } else if ((map[i, j + 1] == 3 || map[i, j + 1] == 7) && map[i, j + 2] != 1) { map[i, j + 2] = map[i, j + 2] + 3; map[i, j + 1] = map[i, j + 1] - 3 + 2; map[i, j] = map[i, j] - 2; j++; } else Console.Write("a"); } else if (k == "b") //按“B”键,重新开始 { z--; break; } else if (k == "n") //按“N”键,进入下一关 { continue; } }//显示及操作 }//游戏开始 else z--; }//for,逐次取地图 }