标签:
namespace 自己琢磨推箱子
{
class Program
{
static void Main(string[] args)
{
//定义一个地图
int x=2,y=1;//小人的位置
int temp = 0;
int[,] map = new int[10, 10]
{
{1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,1,0,0,0,9,1},
{1,8,3,0,1,0,0,0,0,1},
{1,0,0,0,1,0,0,0,0,1},
{1,0,0,0,1,0,0,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,0,0,0,0,0,0,1},
{1,0,0,0,0,0,1,0,0,1},
{1,1,1,1,1,1,1,1,1,1},
};
//下面开始将小人移动,编辑移动指令算法,简单地说就是位置交换
while (true)
{ //读取输入的键
ConsoleKeyInfo s=Console.ReadKey();
if (s.Key.ToString() =="RightArrow")
{ if(map[x,y+1]==0) //如果小人的下一步是空格,则两个元素交换,只有满足这一个条件两个元素才会交换,否则就不会变
{
temp=map[x,y];
map[x, y] = map[x,y+1];
map[x,y+1]=temp;
y++;
}
if(map[x,y+1]==3&&map[x,y+2]==0) //如果小人的下一步是箱子,并且箱子的下一步是空格,则三个元素互相交换
{
temp = map[x, y + 2];
map[x, y + 2] = map[x, y + 1];
map[x, y + 1] = temp;
temp = map[x, y + 1];
map[x, y + 1] = map[x, y];
map[x, y] = temp;
y++;
if (map[x, y + 2] == 9) //在满足如果小人的下一步是箱子,并且箱子的下一步是空格,则三个元素互相交换的条件下,
//如果小人的下一步是箱子,并且箱子的下一步是终点。把五角星变成箱子,把箱子变成小人。
{
map[x, y + 2] = 3;
map[x, y + 1] = 8;
map[x, y] = 0;
temp = 6;
}
}
}
if (s.Key.ToString() == "LeftArrow")
{ if(map[x,y-1]==0)
{
temp = map[x, y];
map[x, y] = map[x, y-1 ];
map[x, y -1] = temp;
y--;
}
if(map[x,y-1]==3&&map[x,y-2]==0)
{
temp = map[x, y - 2];
map[x, y - 2] = map[x, y - 1];
map[x, y - 1] = temp;
temp = map[x, y - 1];
map[x, y - 1] = map[x, y];
map[x, y] = temp;
y--;
if (map[x, y - 2] == 9)
{
map[x, y - 2] = 3;
map[x, y - 1] = 8;
map[x, y] = 0;
temp = 6;
}
}
}
if (s.Key.ToString() == "DownArrow")
{
if (map[x+1, y] == 0)
{temp = map[x, y];
map[x, y] = map[x+1, y ];
map[x+1, y] = temp;
x++;
}
if(map[x+1,y]==3&&map[x+2,y]==0)
{
temp = map[x + 2, y];
map[x + 2, y] = map[x + 1, y];
map[x + 1, y] = temp;
temp = map[x + 1, y];
map[x + 1, y] = map[x, y];
map[x, y] = temp;
x++;
if(map[x+2,y]==9)
{
map[x+2, y] = 3;
map[x+1, y ] = 8;
map[x, y] = 0;
temp = 6;
}
}
}
if (s.Key.ToString() == "UpArrow")
{
if(map[x-1,y]==0)
{temp = map[x, y];
map[x, y] = map[x - 1, y];
map[x - 1, y] = temp;
x--;
}
if (map[x-1, y] == 3 && map[x-2, y] == 0)
{
temp = map[x-2, y];
map[x-2, y] = map[x-1, y ];
map[x-1, y] = temp;
temp = map[x-1, y ];
map[x-1, y] = map[x, y];
map[x, y] = temp;
x--;
if (map[x-2, y] == 9)
{
map[x-2, y] = 3;
map[x-1, y] = 8;
map[x, y] = 0;
temp = 6;
}
}
}
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] == 8)
{
Console.Write("♀");
}
else if (map[i, j] == 9)
{
Console.Write("★");
}
else if (map[i,j] == 3)
{
Console.Write("□");
}
}
Console.WriteLine();
}
if (temp == 6)
{
Console.WriteLine("恭喜您过关了!");
Console.WriteLine("按任意键进入下一关!");
Console.ReadKey();
}
}
}
}
}
标签:
原文地址:http://www.cnblogs.com/lk-kk/p/4395125.html