推箱子
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了推箱子相关的知识,希望对你有一定的参考价值。
namespace ConsoleApplication2
{
class Program
{
#region===将数值型的二维数组的值,赋值给字符串型的二维数组,并打印地图===
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] = "※"; }
if (tu[m, i, j] == 7) { ditu[i, j] = "人"; }
}
ditu[i, 10] = "\n";
}
foreach (string s in ditu)
{
Console.Write(s);
}
}
#endregion
static void Main(string[] args)
{
#region=======地图信息==========
const int guan=5;
Program pd = new Program();
int[,,] tu = new int[guan,10, 10]
{
{
{1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,3,0,0,3,0,0,1},
{1,0,3,2,2,2,0,3,3,1},
{1,0,0,2,0,0,2,0,0,1},
{1,0,0,2,4,0,2,0,0,1},
{1,3,3,0,2,2,2,3,0,1},
{1,0,0,3,0,0,3,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,3,0,1,0,0,0,0,0,1},
{1,0,2,0,0,0,0,0,3,1},
{1,0,1,1,3,1,0,0,0,1},
{1,4,2,0,0,0,0,0,0,1},
{1,0,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,0,0,1,0,3,1},
{1,1,1,3,0,0,0,0,0,1},
{1,1,0,0,2,0,1,2,0,1},
{1,0,0,0,1,1,1,0,0,1},
{1,0,1,1,1,0,0,0,0,1},
{1,0,0,0,0,2,0,0,1,1},
{1,0,1,0,0,4,1,1,1,1},
{1,1,3,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,1,1},
{1,1,1,1,1,1,0,0,3,1},
{1,1,1,3,3,0,2,0,0,1},
{1,1,1,0,0,0,0,0,1,1},
{1,1,1,0,1,1,0,1,1,1},
{1,1,1,0,1,1,0,1,1,1},
{1,1,0,0,0,0,0,0,1,1},
{1,0,0,2,0,2,2,2,0,1},
{1,3,0,0,1,1,0,4,3,1},
{1,1,1,1,1,1,1,1,1,1},
},
{
{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},
},
};
#endregion======
for (int m = 0; m < guan; )
{
#region===从地图上获得互动元素的坐标,并打印地图===
int x = 0, y = 0;
int xzx = 0, xzy = 0;
int kwx = 0, kwy = 0;
int fhx = 0, fhy = 0;
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; }
}
}
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; }
}
#region======back=======
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("每次只能退一步");
}
#endregion
#region===d 向右===
if (shuru == "d" && tu[m, x, y + 1] != 1 && tu[m, x, y + 1] != 5)
{
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] == 4)
{
tu[m, x, y + 2] = 5;
yao++;
jilu = "d1"; tui = 1;
}
tu[m, x, y + 1] = 4;
}
if (tu[m, x, y + 1] == 0) { tu[m, x, y + 1] = 4; jilu = ""; }
if (tu[m, x, y + 1] == 3) { tu[m, x, y + 1] = 7; jilu = ""; }
if (tu[m, x, y] == 4) { tu[m, x, y] = 0; }
if (tu[m, x, y] == 7) { tu[m, x, y] = 3; }
y = y + 1;
Console.Clear();
pd.ditus(tu, m);
}
#endregion
#region===a 向左===
if (shuru == "a" && tu[m, x, y - 1] != 1 && tu[m, x, y - 1] != 5)
{
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 - 1] = 4;
}
if (tu[m, x, y - 1] == 0) { tu[m, x, y - 1] = 4; jilu = ""; }
if (tu[m, x, y - 1] == 3) { tu[m, x, y - 1] = 7; jilu = ""; }
if (tu[m, x, y] == 4) { tu[m, x, y] = 0; }
if (tu[m, x, y] == 7) { tu[m, x, y] = 3; }
y = y - 1;
Console.Clear();
pd.ditus(tu, m);
}
#endregion
#region===w 向上===
if (shuru == "w" && tu[m, x - 1, y] != 1 && tu[m, x - 1, y] != 5)
{
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 - 1, y] = 4;
}
if (tu[m, x - 1, y] == 0) { tu[m, x - 1, y] = 4; jilu = ""; }
if (tu[m, x - 1, y] == 3) { tu[m, x - 1, y] = 7; jilu = ""; }
if (tu[m, x, y] == 4) { tu[m, x, y] = 0; }
if (tu[m, x, y] == 7) { tu[m, x, y] = 3; }
x = x - 1;
Console.Clear();
pd.ditus(tu, m);
}
#endregion
#region===s 向下===
if (shuru == "s" && tu[m, x + 1, y] != 1 && tu[m, x + 1, y] != 5)
{
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 + 1, y] = 4;
}
if (tu[m, x + 1, y] == 0) { tu[m, x + 1, y] = 4; jilu = ""; }
if (tu[m, x + 1, y] == 3) { tu[m, x + 1, y] = 7; jilu = ""; }
if (tu[m, x, y] == 4) { tu[m, x, y] = 0; }
if (tu[m, x, y] == 7) { tu[m, x, y] = 3; }
x = x + 1;
Console.Clear();
pd.ditus(tu, m);
}
#endregion
}
}
Console.ReadLine();
}
}
}
以上是关于推箱子的主要内容,如果未能解决你的问题,请参考以下文章