C# 推箱子(只有一关)

class Program
{
static void ditu(int[,] map) //建立一张地图的函数,10x10的
{
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
if (map[i, j] == 0)
{
Console.Write(" ");
}
else if (map[i, j] == 1)
{
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("♀");
}
}
Console.WriteLine();
}

}
static void Main(string[] args)
{
int[,] Map = new int[10, 10]
{
{1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,1,0,0,1},
{1,4,2,0,0,0,1,0,0,1},
{1,0,0,0,0,0,1,1,1,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,0,0,1},
{1,0,0,1,0,0,1,0,0,1},
{1,3,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1},
};
int x = 2, y = 1, t = 0, t1 = 0, tx = 0;
while (true)
{
ditu(Map);
if (tx == 100) //最后一步提到前面。
{
Console.WriteLine("恭喜通第一关。");

break;
}
ConsoleKeyInfo s = Console.ReadKey(); //赋值一个按键的变量
#region====向上
if (s.Key.ToString() == "UpArrow")
{
if (Map[x - 1, y] == 2 && Map[x - 2, y] == 0)//两次冒泡
{
t = Map[x - 2, y];
Map[x - 2, y] = Map[x - 1, y];
Map[x - 1, y] = t;

t1 = Map[x - 1, y];
Map[x - 1, y] = Map[x, y];
Map[x, y] = t1;
x--;
}
else if (Map[x - 1, y] == 0)
{
t = Map[x - 1, y];
Map[x - 1, y] = Map[x, y];
Map[x, y] = t;
x--;
}
else if (Map[x - 1, y] == 1 || (Map[x - 1, y] == 2 && Map[x - 2, y] == 1))
{
Console.WriteLine("您未学会穿墙术,请去商城购买!");
Console.ReadLine(); //撞墙回车
}
}
#endregion
#region====向下
if (s.Key.ToString() == "DownArrow")
{
if (Map[x + 1, y] == 2 && Map[x + 2, y] == 0)
{
t = Map[x + 2, y];
Map[x + 2, y] = Map[x + 1, y];
Map[x + 1, y] = t;

t1 = Map[x + 1, y];
Map[x + 1, y] = Map[x, y];
Map[x, y] = t1;
x++;
}
else if (Map[x + 1, y] == 0)
{
t = Map[x + 1, y];
Map[x + 1, y] = Map[x, y];
Map[x, y] = t;
x++;
}
else if (Map[x + 1, y] == 1 || (Map[x + 1, y] == 2 && Map[x + 2, y] == 1))
{
Console.WriteLine("您未学会穿墙术,请去商城购买!");
Console.ReadLine(); //撞墙回车
}
}
#endregion
#region====向右
if (s.Key.ToString() == "RightArrow")
{
if (Map[x, y + 1] == 2 && Map[x, y + 2] == 0)
{
t = Map[x, y + 2];
Map[x, y + 2] = Map[x, y + 1];
Map[x, y + 1] = t;

t1 = Map[x, y + 1];
Map[x, y + 1] = Map[x, y];
Map[x, y] = t1;
y++;
}
else if (Map[x, y + 1] == 0)
{
t = Map[x, y + 1];
Map[x, y + 1] = Map[x, y];
Map[x, y] = t;
y++;
}
else if (Map[x, y + 1] == 1 || (Map[x, y + 1] == 2 && Map[x, y + 2] == 1))
{
Console.WriteLine("您未学会穿墙术,请去商城购买!");
Console.ReadLine(); //撞墙回车
}
}
#endregion
#region====向左
if (s.Key.ToString() == "LeftArrow")
{
if (Map[x, y - 1] == 2 && Map[x, y - 2] == 0)
{
t = Map[x, y - 2];
Map[x, y - 2] = Map[x, y - 1];
Map[x, y - 1] = t;

t1 = Map[x, y - 1];
Map[x, y - 1] = Map[x, y];
Map[x, y] = t1;
y--;
}
else if (Map[x, y - 1] == 0)
{
t = Map[x, y - 1];
Map[x, y - 1] = Map[x, y];
Map[x, y] = t;
y--;
}
else if (Map[x, y - 1] == 1 || (Map[x, y - 1] == 2 && Map[x, y - 2] == 1))
{
Console.WriteLine("您未学会穿墙术,请去商城购买!");
Console.ReadLine(); //撞墙回车
}
}
#endregion
#region====结束
if (Map[x, y - 1] == 2 && Map[x, y - 2] == 3)
{
Map[x, y - 2] = Map[x, y - 1];

Map[x, y - 1] = Map[x, y];
Map[x, y] = t;
tx = 100; //随意的赋值一个变量,作为后面IF的运行条件。
}

#endregion
Console.Clear(); //每运行一遍,刷新一次地图。

}

Console.ReadLine();
}
}

posted @ 2015-03-14 06:47  指尖的人生  阅读(327)  评论(0编辑  收藏  举报