本文共 5406 字,大约阅读时间需要 18 分钟。
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Collections;namespaceConsoleApplication1
{classProgram
{static void Main(string[] args)
{//推箱子//一、设计地图 空地为0,墙为1,箱子=2,人=3,目标=4,完成=5
int[,] map = new int[12, 13]
{
{0,0,0,0,0,1,1,1,1,1,1,1,0},
{0,0,0,0,0,1,0,0,1,0,0,1,0},
{0,0,0,0,0,1,0,0,2,2,0,1,0},
{1,1,1,1,1,1,0,2,1,0,0,1,0},
{1,4,4,4,1,1,1,0,1,0,0,1,1},
{1,4,0,0,1,0,0,2,0,1,0,0,1},
{1,4,0,0,0,0,2,0,2,0,2,0,1},
{1,4,0,0,1,0,0,2,0,1,0,0,1},
{1,4,4,4,1,1,1,0,1,0,0,1,1},
{1,1,1,1,1,1,0,2,0,0,0,1,0},
{0,0,0,0,0,1,0,3,1,0,0,1,0},
{0,0,0,0,0,1,1,1,1,1,1,1,0}
};int[,] map0 = new int[12, 13]
{
{0,0,0,0,0,1,1,1,1,1,1,1,0},
{0,0,0,0,0,1,0,0,1,0,0,1,0},
{0,0,0,0,0,1,0,0,0,0,0,1,0},
{1,1,1,1,1,1,0,0,1,0,0,1,0},
{1,4,4,4,1,1,1,0,1,0,0,1,1},
{1,4,0,0,1,0,0,0,0,1,0,0,1},
{1,4,0,0,0,0,0,0,0,0,0,0,1},
{1,4,0,0,1,0,0,0,0,1,0,0,1},
{1,4,4,4,1,1,1,0,1,0,0,1,1},
{1,1,1,1,1,1,0,0,0,0,0,1,0},
{0,0,0,0,0,1,0,0,1,0,0,1,0},
{0,0,0,0,0,1,1,1,1,1,1,1,0}
};//小人的初始坐标
int y = 10, x = 7;//让小人动起来(用方向键↑↓←→控制),即改变小人的坐标位置,与下一个坐标内容互换,如果指定方向的下一个坐标是1(即面对墙),小人坐标不变(if进行判断)//for循环中控制,清屏后打印新位置
for(; ; )
{//打印新地图
for (int i = 0; i < 12; i++)
{for (int j = 0; j < 13; j++)
{if (map[i, j] == 0)
{
Console.Write(" ");
}if (map[i, j] == 1)
{
Console.Write("■");
}if (map[i, j] == 2)
{
Console.Write("□");
}if (map[i, j] == 3)
{
Console.Write("♀");
}if (map[i, j] == 4)
{
Console.Write("☆");
}if (map[i, j] == 5)
{
Console.Write("★");
}
}
Console.WriteLine();
}//接收输入的按键
ConsoleKeyInfo aj =Console.ReadKey();//清屏
Console.Clear();//向上移动
if (aj.Key.ToString() == "UpArrow")
{if (map[y - 1, x] == 0 || map[y - 1, x] == 4)//小人的下一个坐标为空地,进行移动
{if (map0[y,x]==4)//如果小人现在的坐标是目标点的坐标
{
map[y- 1, x] = 3;
map[y, x]= 4;
y--;
}else//如果小人现在的坐标不是目标点的坐标
{
map[y- 1, x] = 3;
map[y, x]= 0;
y--;
}
}else if (map[y - 1, x] == 1)//小人的下一个坐标为墙,跳过此次循环
{ continue; }else if (map[y - 1, x] == 2 || map[y - 1, x] == 5)//小人的下一个坐标为未到达目标的箱子或到达目标的箱子
{if (map[y - 2, x] == 1 || map[y - 2, x] == 2 || map[y - 2, x] == 5)//箱子的下一个目标为墙或空箱子或到达目标的箱子
{ continue; }else if (map[y - 2, x] == 0)//箱子的下一个目标为空地
{if (map0[y, x] == 4)//如果小人现在的坐标是目标点的坐标
{
map[y- 2, x] = 2;
map[y- 1, x] = 3;
map[y, x]= 4;
y--;
}else//如果小人现在的坐标不是目标点的坐标
{
map[y- 2, x] = 2;
map[y- 1, x] = 3;
map[y, x]= 0;
y--;
}
}else if (map[y - 2, x] == 4)//箱子的下一个目标为目标点
{if (map0[y, x] == 4)//如果小人现在的坐标是目标点的坐标
{
map[y- 2, x] = 5;
map[y- 1, x] = 3;
map[y, x]= 4;
y--;
}else//如果小人现在的坐标不是目标点的坐标
{
map[y- 2, x] = 5;
map[y- 1, x] = 3;
map[y, x]= 0;
y--;
}
}
}
}//向下移动
if (aj.Key.ToString() == "DownArrow")
{if (map[y + 1, x] == 0 || map[y + 1, x] == 4)//小人的下一个坐标为空地,进行移动
{if (map0[y, x] == 4)//如果小人现在的坐标是目标点的坐标
{
map[y+ 1, x] = 3;
map[y, x]= 4;
y++;
}else//如果小人现在的坐标不是目标点的坐标
{
map[y+ 1, x] = 3;
map[y, x]= 0;
y++;
}
}else if (map[y + 1, x] == 1)//小人的下一个坐标为墙,跳过此次循环
{ continue; }else if (map[y + 1, x] == 2 || map[y + 1, x] == 5)//小人的下一个坐标为未到达目标的箱子或到达目标的箱子
{if (map[y + 2, x] == 1 || map[y + 2, x] == 2 || map[y + 2, x] == 5)//箱子的下一个目标为墙或空箱子或到达目标的箱子
{ continue; }else if (map[y + 2, x] == 0)//箱子的下一个目标为空地
{if (map0[y, x] == 4)//如果小人现在的坐标是目标点的坐标
{
map[y+ 2, x] = 2;
map[y+ 1, x] = 3;
map[y, x]= 4;
y++;
}else//如果小人现在的坐标不是目标点的坐标
{
map[y+ 2, x] = 2;
map[y+ 1, x] = 3;
map[y, x]= 0;
y++;
}
}else if (map[y + 2, x] == 4)//箱子的下一个目标为目标点
{if (map0[y, x] == 4)//如果小人现在的坐标是目标点的坐标
{
map[y+ 2, x] = 5;
map[y+ 1, x] = 3;
map[y, x]= 4;
y++;
}else//如果小人现在的坐标不是目标点的坐标
{
map[y+ 2, x] = 5;
map[y+ 1, x] = 3;
map[y, x]= 0;
y++;
}
}
}
}//向左移动
if (aj.Key.ToString() == "LeftArrow")
{if (map[y, x - 1] == 0 || map[y, x - 1] == 4)//小人的下一个坐标为空地,进行移动
{if (map0[y, x] == 4)//如果小人现在的坐标是目标点的坐标
{
map[y, x- 1] = 3;
map[y, x]= 4;
x--;
}else//如果小人现在的坐标不是目标点的坐标
{
map[y, x- 1] = 3;
map[y, x]= 0;
x--;
}
}else if (map[y, x - 1] == 1)//小人的下一个坐标为墙,跳过此次循环
{ continue; }else if (map[y, x - 1] == 2 || map[y, x - 1] == 5)//小人的下一个坐标为未到达目标的箱子或到达目标的箱子
{if (map[y, x - 2] == 1 || map[y, x - 2] == 2 || map[y, x - 2] == 5)//箱子的下一个目标为墙或空箱子或到达目标的箱子
{ continue; }else if (map[y, x - 2] == 0)//箱子的下一个目标为空地
{if (map0[y, x] == 4)//如果小人现在的坐标是目标点的坐标
{
map[y, x- 2] = 2;
map[y, x- 1] = 3;
map[y, x]= 4;
x--;
}else//如果小人现在的坐标不是目标点的坐标
{
map[y, x- 2] = 2;
map[y, x- 1] = 3;
map[y, x]= 0;
x--;
}
}else if (map[y, x - 2] == 4)//箱子的下一个目标为目标点
{if (map0[y, x] == 4)//如果小人现在的坐标是目标点的坐标
{
map[y, x- 2] = 5;
map[y, x- 1] = 3;
map[y, x]= 4;
x--;
}else//如果小人现在的坐标不是目标点的坐标
{
map[y, x- 2] = 5;
map[y, x- 1] = 3;
map[y, x]= 0;
x--;
}
}
}
}//向右移动
if (aj.Key.ToString() == "RightArrow")
{if (map[y, x + 1] == 0 || map[y, x + 1] == 4)//小人的下一个坐标为空地,进行移动
{if (map0[y, x] == 4)//如果小人现在的坐标是目标点的坐标
{
map[y, x+ 1] = 3;
map[y, x]= 4;
x++;
}else//如果小人现在的坐标不是目标点的坐标
{
map[y, x+ 1] = 3;
map[y, x]= 0;
x++;
}
}else if (map[y, x + 1] == 1)//小人的下一个坐标为墙,跳过此次循环
{ continue; }else if (map[y, x + 1] == 2 || map[y, x + 1] == 5)//小人的下一个坐标为未到达目标的箱子或到达目标的箱子
{if (map[y, x + 2] == 1 || map[y, x + 2] == 2 || map[y, x + 2] == 5)//箱子的下一个目标为墙或空箱子或到达目标的箱子
{ continue; }else if (map[y, x + 2] == 0)//箱子的下一个目标为空地
{if (map0[y, x] == 4)//如果小人现在的坐标是目标点的坐标
{
map[y, x+ 2] = 2;
map[y, x+ 1] = 3;
map[y, x]= 4;
x++;
}else//如果小人现在的坐标不是目标点的坐标
{
map[y, x+ 2] = 2;
map[y, x+ 1] = 3;
map[y, x]= 0;
x++;
}
}else if (map[y, x + 2] == 4)//箱子的下一个目标为目标点
{if (map0[y, x] == 4)//如果小人现在的坐标是目标点的坐标
{
map[y, x+ 2] = 5;
map[y, x+ 1] = 3;
map[y, x]= 4;
x++;
}else//如果小人现在的坐标不是目标点的坐标
{
map[y, x+ 2] = 5;
map[y, x+ 1] = 3;
map[y, x]= 0;
x++;
}
}
}
}
}
Console.ReadLine();
}
}
}
转载地址:http://dbima.baihongyu.com/