using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
int[,] map = new int[,]{
{1,1,1,1,1,1,1,1,1,1},
{1,4,0,0,0,0,0,0,0,1},
{1,0,1,0,0,0,1,0,0,1},
{1,0,1,0,3,0,1,0,0,1},
{1,0,0,0,0,0,1,3,0,1},
{1,0,0,0,3,2,0,0,0,1},
{1,0,1,0,0,0,0,0,0,1},
{1,1,4,0,0,4,0,0,0,1},
{1,0,1,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1}
};
int x = 5;
int y = 5;
int x1 = 1;
int y1 = 1;
//空地-0 墙-1 人-2 箱子-3 目标点-4 完成-5
for (; ; )
{
Console.Clear();
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
if (map[i, j] == 1)
{
Console.Write("■");
}
else if (map[i, j] == 0)
{
Console.Write(" ");
}
else if (map[i, j] == 2)
{
Console.Write("人");
}
else if (map[i, j] == 3)
{
Console.Write("□");
}
else if (map[i, j] == 4)
{
Console.Write("☆");
}
else if (map[i, j] == 5)
{
Console.Write("★");
}
}
Console.WriteLine();
}
ConsoleKeyInfo key = Console.ReadKey();
if (key.Key.ToString() == "W")//按W 向上
{
if (map[x - 1, y] == 1)//如果下一步是墙
{
continue;
}
else if (map[x - 1, y] == 0)//如果下一步是空地
{
if (map[x, y] == map[x1, y1])//如果人所在位置原本是目标点
{
map[x - 1, y] = 2;
map[x, y] = 4;
}
else//如果人的原本位置是空地
{
map[x - 1, y] = 2;
map[x, y] = 0;
}
}
else if (map[x - 1, y] == 3)//如果下一步是箱子
{
if (map[x - 2, y] == 0)//如果箱子的下一步是空地
{
if (map[x, y] == map[x1, y1])//如果人的原本位置是目标点
{
map[x - 2, y] = 3;
map[x - 1, y] = 2;
map[x, y] = 4;
}
else//如果人的原本位置是空地
{
map[x - 2, y] = 3;
map[x - 1, y] = 2;
map[x, y] = 0;
}
}
else if (map[x - 2, y] == 4)//如果箱子的下一步是目标点
{
if (map[x, y] == map[x1, y1])//如果人的原本位置是目标点
{
map[x - 2, y] = 3;
map[x - 1, y] = 2;
map[x, y] = 4;
}
else//如果人的原本位置是空地
{
map[x - 2, y] = 5;
map[x - 1, y] = 2;
map[x, y] = 0;
}
}
else//其他情况:如果箱子的下一步是已经放置好的目标点或墙或箱子
{
continue;
}
}
else if (map[x - 1, y] == 4)//如果下一步是目标点
{
map[x - 1, y] = 2;
map[x, y] = 0;
x1 = x - 1;
y1 = y; //记录一下原本应该为目标点的坐标
}
else
{
continue;
}
x--;
}
else if (key.Key.ToString() == "S")
{
if (map[x+1 , y] == 1)
{
continue;
}
else if (map[x + 1, y] == 0)
{
if (map[x, y] == map[x1, y1])
{
map[x + 1, y] = 2;
map[x, y] = 4;
}
else
{
map[x + 1, y] = 2;
map[x, y] = 0;
}
}
else if (map[x + 1, y] == 3)
{
if (map[x+2, y] == 0)
{
if (map[x, y] == map[x1, y1])
{
map[x + 2, y] = 3;
map[x + 1, y] = 2;
map[x, y] = 4;
}
else
{
map[x + 2, y] = 3;
map[x + 1, y] = 2;
map[x, y] = 0;
}
}
else if (map[x+2, y] == 4)
{
if (map[x, y] == map[x1, y1])
{
map[x + 2, y] = 3;
map[x + 1, y] = 2;
map[x, y] = 4;
}
else
{
map[x + 2, y] = 5;
map[x + 1, y] = 2;
map[x, y] = 0;
}
}
else
{
continue;
}
}
else if (map[x + 1, y] == 4)
{
map[x + 1, y] = 2;
map[x, y] = 0;
x1 = x + 1;
y1 = y;
}
else
{
continue;
}
x++;
}
else if (key.Key.ToString() == "A")
{
if (map[x, y - 1] == 1)
{
continue;
}
else if (map[x, y - 1] == 0)
{
if (map[x, y] == map[x1, y1])
{
map[x, y - 1] = 2;
map[x, y] = 4;
}
else
{
map[x, y - 1] = 2;
map[x, y] = 0;
}
}
else if (map[x, y - 1] == 3)
{
if (map[x, y - 2] == 0)
{
if (map[x, y] == map[x1, y1])
{
map[x, y - 2] = 3;
map[x, y - 1] = 2;
map[x, y] = 4;
}
else
{
map[x, y - 2] = 3;
map[x, y - 1] = 2;
map[x, y] = 0;
}
}
else if (map[x, y - 2] == 4)
{
if (map[x, y] == map[x1, y1])
{
map[x, y - 2] = 3;
map[x, y - 1] = 2;
map[x, y] = 4;
}
else
{
map[x, y - 2] = 5;
map[x, y - 1] = 2;
map[x, y] = 0;
}
}
else
{
continue;
}
}
else if (map[x, y - 1] == 4)
{
map[x, y - 1] = 2;
map[x, y] = 0;
x1 = x;
y1 = y - 1;
}
else
{
continue;
}
y--;
}
else if (key.Key.ToString() == "D")
{
if (map[x, y + 1] == 1)
{
continue;
}
else if (map[x, y + 1] == 0)
{
if (map[x, y] == map[x1, y1])
{
map[x, y + 1] = 2;
map[x, y] = 4;
}
else
{
map[x, y + 1] = 2;
map[x, y] = 0;
}
}
else if (map[x, y + 1] == 3)
{
if (map[x, y + 2] == 0)
{
if (map[x, y] == map[x1, y1])
{
map[x, y + 2] = 3;
map[x, y + 1] = 2;
map[x, y] = 4;
}
else
{
map[x, y + 2] = 3;
map[x, y + 1] = 2;
map[x, y] = 0;
}
}
else if (map[x, y + 2] == 4)
{
if (map[x, y] == map[x1, y1])
{
map[x, y + 2] = 3;
map[x, y + 1] = 2;
map[x, y] = 4;
}
else
{
map[x, y + 2] = 5;
map[x, y + 1] = 2;
map[x, y] = 0;
}
}
else
{
continue;
}
}
else if (map[x, y + 1] == 4)
{
map[x, y + 1] = 2;
map[x, y] = 0;
x1 = x;
y1 = y + 1;
}
else
{
continue;
}
y++;
}
}
}
}
}