using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace XTU0{ class Program { #region===将数值型的二维数组的值,赋值给字符串型的二维数组,并打印地图=== ////// 将数值型的二维数组的值,赋值给字符串型的二维数组tu,并打印地图 /// /// public void ditus(int[,,] tu,int m) { string[,] ditu = new string[10, 11]; for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { if (tu[m, i, j] == 0) { ditu[i, j] = " "; } if (tu[m, i, j] == 1) { ditu[i, j] = "■"; } if (tu[m, i, j] == 2) { ditu[i, j] = "●"; } if (tu[m, i, j] == 3) { ditu[i, j] = "◎"; } if (tu[m, i, j] == 4) { ditu[i, j] = "人"; } if (tu[m, i, j] == 5) { ditu[i, j] = "※"; } } ditu[i, 10] = "\n"; } foreach (string s in ditu)//打印出地图 { Console.Write(s); } } #endregion static void Main(string[] args) { const int guan=3;//const 定义常量,作为有多少关 Program pd = new Program();//初始化函数 int[,,] tu = new int[guan,10, 10] { //0表示空地,1表示墙,2表示箱子xzx,xzy,3表示箱子空位kwx,kwy,4表示光标gbx,gby,5表示放好的箱子fhx,fhy { //7表示空位上的光标,8表示钥匙ysx,ysy,9表示带锁的出口ckx,cky { 1,1,1,1,1,1,1,1,1,1},//j,y横坐标0-9 { 1,0,0,1,0,0,0,0,0,1},//纵坐标i,x { 1,4,0,0,0,0,2,0,3,1},//0 { 1,0,1,1,0,1,0,0,0,1},// | { 1,0,0,0,0,0,0,0,0,1},//9 { 1,3,1,1,1,1,1,2,1,1}, { 1,2,1,0,0,0,0,0,0,1}, { 1,0,0,0,0,1,1,0,0,1}, { 1,1,0,1,0,0,0,3,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,4,0,1,0,3,1}, { 1,1,1,3,0,0,2,0,0,1}, { 1,1,0,0,2,0,1,0,0,1}, { 1,0,0,0,1,1,1,0,0,1}, { 1,0,1,1,0,0,0,0,0,1}, { 1,0,0,0,0,2,0,0,1,1}, { 1,0,3,0,0,0,1,1,1,1}, { 1,0,0,0,0,1,1,1,1,1}, { 1,1,1,1,1,1,1,1,1,1}, }, { //第3关 { 1,1,1,1,1,1,1,1,1,1}, { 1,1,0,0,4,0,0,3,1,1}, { 1,1,0,2,0,0,0,2,3,1}, { 1,1,0,0,2,1,1,0,0,1}, { 1,1,0,1,0,0,0,3,0,1}, { 1,0,3,0,0,0,1,0,1,1}, { 1,0,0,1,1,2,0,0,1,1}, { 1,3,2,0,0,0,2,0,1,1}, { 1,1,3,0,0,0,0,0,1,1}, { 1,1,1,1,1,1,1,1,1,1}, }, }; for (int m = 0; m < guan; ) { #region===从地图上获得互动元素的坐标,并打印地图=== int x = 0, y = 0;/*xy表示光标坐标 4*/ //int ysx = 0, ysy = 0;/*表示钥匙的坐标 8*/ //int ckx = 0, cky = 0;/*表示出口坐标 9*/ int xzx = 0, xzy = 0;/*表示箱子坐标 2*/ int kwx = 0, kwy = 0;/*表示箱子空 3*/ int fhx = 0, fhy = 0;/*表示放好的箱子 5*/ int yao = 0, shi = 0; for (int i = 0; i < 10; i++)//从地图上获得互动元素的坐标 { for (int j = 0; j < 10; j++) { if (tu[m, i, j] == 2) { xzx = i; xzy = j; } if (tu[m, i, j] == 3) { kwx = i; kwy = j; shi++;} if (tu[m, i, j] == 4) { x = i; y = j; } if (tu[m, i, j] == 5) { fhx = i; fhy = j; } //if (tu[m, i, j] == 8) { ysx = i; ysy = j; } //if (tu[m, i, j] == 9) { ckx = i; cky = j; } } } pd.ditus(tu, m); #endregion string jilu=" "; int tui = 0; for (; ; ) { if (yao == shi) { m = m + 1; Console.WriteLine("过关!!\n按回车进入下一关。"); Console.ReadLine(); Console.Clear(); break; } Console.WriteLine("按w,a,s,d移动\n推错时按backspace后退一步"); ConsoleKeyInfo fangxiang = Console.ReadKey(); string shuru = fangxiang.Key.ToString().ToLower(); if(shuru=="enter") { Console.WriteLine("输入next进入下一关"); string ne= Console.ReadLine(); if (ne == "next") { m++; Console.Clear(); break; } } if (shuru == "backspace"&&tui==1) { if (tu[m, x, y - 1] == 0) { if (jilu == "d0") { tu[m, x, y] = 2; tu[m, x, y + 1] = 0; tu[m, x, y - 1] = 4; y = y - 1; tui = 0; } if (jilu == "d1") { tu[m, x, y] = 2; tu[m, x, y + 1] = 3; tu[m, x, y - 1] = 4; y = y - 1; yao--; tui = 0; } } if (tu[m, x, y + 1] == 0) { if (jilu == "a0") { tu[m, x, y] = 2; tu[m, x, y - 1] = 0; tu[m, x, y + 1] = 4; y = y + 1; tui = 0; } if (jilu == "a1") { tu[m, x, y] = 2; tu[m, x, y - 1] = 3; tu[m, x, y + 1] = 4; y = y + 1; yao--; tui = 0; } } if (tu[m, x + 1, y] == 0) { if (jilu == "w0") { tu[m, x, y] = 2; tu[m, x - 1, y] = 0; tu[m, x + 1, y] = 4; x = x + 1; tui = 0; } if (jilu == "w1") { tu[m, x, y] = 2; tu[m, x - 1, y] = 3; tu[m, x + 1, y] = 4; x = x + 1; yao--; tui = 0; } } if (tu[m, x - 1, y] == 0) { if (jilu == "s0") { tu[m, x, y] = 2; tu[m, x + 1, y] = 0; tu[m, x - 1, y] = 4; x = x - 1; tui = 0; } if (jilu == "s1") { tu[m, x, y] = 2; tu[m, x + 1, y] = 3; tu[m, x - 1, y] = 4; x = x - 1; yao--; tui = 0; } } Console.Clear(); pd.ditus(tu, m); Console.WriteLine("每次只能退一步"); } #region===d 向右=== if (shuru == "d" && tu[m, x, y + 1] != 1 && tu[m, x, y + 1] != 3 && tu[m, x, y + 1] != 5)//如果光标下一步不是墙,进入if判断下一步是什么 { //if (tu[x, y] == 7) { } if (tu[m, x, y + 1] == 2 && tu[m, x, y + 2] != 1 && tu[m, x, y + 2] != 2 && tu[m, x, y + 2] != 5)//光标的下一步是箱子,并且箱子的下一步不是墙,不是箱子,不是放好的箱子 { if (tu[m, x, y + 2] == 0)//箱子的下一步是空地 { tu[m, x, y + 2] = 2;//箱子的下一步变成箱子 jilu = "d0"; tui = 1; } if (tu[m, x, y + 2] == 3)//如果箱子下一步是空位 { tu[m, x, y + 2] = 5;//箱子下一步变成放好的箱子 yao++; jilu = "d1"; tui = 1; } tu[m, x, y + 1] = 4; tu[m, x, y] = 0; y = y + 1; } if (tu[m, x, y + 1] == 0) { tu[m, x, y + 1] = 4; tu[m, x, y] = 0; y = y + 1; jilu = " "; } Console.Clear(); pd.ditus(tu, m); } #endregion #region===a 向左=== if (shuru == "a" && tu[m, x, y - 1] != 1 && tu[m, x, y - 1] != 3 && tu[m, x, y - 1] != 5)//如果光标下一步不是墙,进入if判断下一步是什么 { if (tu[m, x, y - 1] == 2 && tu[m, x, y - 2] != 1 && tu[m, x, y - 2] != 2 && tu[m, x, y - 2] != 5)//光标的下一步是箱子,并且箱子的下一步不是墙,不是箱子,不是放好的箱子 { if (tu[m, x, y - 2] == 0)//箱子的下一步是空地 { tu[m, x, y - 2] = 2;//箱子的下一步变成箱子 jilu = "a0"; tui = 1; } if (tu[m, x, y - 2] == 3)//如果箱子下一步是空位 { tu[m, x, y - 2] = 5;//箱子下一步变成放好的箱子 yao++; jilu = "a1"; tui = 1; } tu[m, x, y] = 0;//光标变成空地 tu[m, x, y - 1] = 4;//光标的下一步变成光标 y = y - 1;//光标坐标改变 } if (tu[m, x, y - 1] == 0) { tu[m, x, y] = 0;//光标变成空地 tu[m, x, y - 1] = 4;//光标的下一步变成光标 y = y - 1;//光标坐标改变 jilu = " "; } Console.Clear(); pd.ditus(tu, m); } #endregion #region===w 向上=== if (shuru == "w" && tu[m, x - 1, y] != 1 && tu[m, x - 1, y] != 3 && tu[m, x - 1, y] != 5)//如果光标下一步不是墙,进入if判断下一步是什么 { if (tu[m, x - 1, y] == 2 && tu[m, x - 2, y] != 1 && tu[m, x - 2, y] != 2 && tu[m, x - 2, y] != 5)//光标的下一步是箱子,并且箱子的下一步不是墙,不是箱子,不是放好的箱子 { if (tu[m, x - 2, y] == 0)//箱子的下一步是空地 { tu[m, x - 2, y] = 2;//箱子的下一步变成箱子 jilu = "w0"; tui = 1; } if (tu[m, x - 2, y] == 3)//如果箱子下一步是空位 { tu[m, x - 2, y] = 5;//箱子下一步变成放好的箱子 yao++; jilu = "w1"; tui = 1; } tu[m, x, y] = 0; tu[m, x - 1, y] = 4; x = x - 1; } if (tu[m, x - 1, y] == 0) { tu[m, x, y] = 0; tu[m, x - 1, y] = 4; x = x - 1; jilu = " "; } Console.Clear(); pd.ditus(tu, m); } #endregion #region===s 向下=== if (shuru == "s" && tu[m, x + 1, y] != 1 && tu[m, x + 1, y] != 3&& tu[m, x + 1, y] != 5)//如果光标下一步不是墙,进入if判断下一步是什么 { if (tu[m, x + 1, y] == 2 && tu[m, x + 2, y] != 1 && tu[m, x + 2, y] != 2 && tu[m, x + 2, y] != 5)//光标的下一步是箱子,并且箱子的下一步不是墙,不是箱子,不是放好的箱子 { if (tu[m, x + 2, y] == 0)//箱子的下一步是空地 { tu[m, x + 2, y] = 2;//箱子的下一步变成箱子 jilu = "s0"; tui = 1; } if (tu[m, x + 2, y] == 3)//如果箱子下一步是空位 { tu[m, x + 2, y] = 5;//箱子下一步变成放好的箱子 yao++; jilu = "s1"; tui = 1; } tu[m, x, y] = 0;//光标变成空地 tu[m, x + 1, y] = 4;//光标的下一步变成光标 x = x + 1;//光标坐标改变 } if (tu[m, x + 1, y] == 0) { tu[m, x, y] = 0;//光标变成空地 tu[m, x + 1, y] = 4;//光标的下一步变成光标 x = x + 1;//光标坐标改变 jilu = " "; } Console.Clear(); pd.ditus(tu, m); } #endregion } } Console.ReadLine(); } }}