推箱子

Posted 斐雪

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了推箱子相关的知识,希望对你有一定的参考价值。

            //画制地图
            int[, ,] a = new int[3, 10, 10]
            {{
              {0,0,0,0,0,0,0,0,0,0},
              {0,0,0,1,1,1,0,0,0,0},
              {0,0,0,1,4,1,0,0,0,0},
              {0,0,0,1,0,1,1,1,1,0},
              {0,1,1,1,3,0,3,4,1,0},
              {0,1,4,0,3,2,1,1,1,0},
              {0,1,1,1,1,3,1,0,0,0},
              {0,0,0,0,1,4,1,0,0,0},
              {0,0,0,0,1,1,1,0,0,0},
              {0,0,0,0,0,0,0,0,0,0}},
             {
              {1,1,1,1,1,0,0,0,0,0},
              {1,2,0,0,1,0,0,0,0,0},
              {1,0,0,3,1,0,1,1,1,0},
              {1,0,0,0,1,0,1,4,1,0},
              {1,1,1,0,1,1,1,0,1,0},
              {0,1,1,0,0,0,0,0,1,0},
              {0,1,0,0,0,1,0,0,1,0},
              {0,1,0,0,0,1,1,1,1,0},
              {0,1,1,1,1,1,0,0,0,0},
              {0,0,0,0,0,0,0,0,0,0}},
             {
              {0,0,0,0,0,0,0,0,0,0},
              {0,1,1,1,1,1,1,1,0,0},
              {0,1,0,0,0,0,0,1,1,1},
              {1,1,3,1,1,1,0,0,0,1},
              {1,0,2,0,3,0,0,3,0,1},
              {1,0,0,0,1,0,0,0,1,1},
              {1,1,4,4,1,4,0,0,1,0},
              {0,1,1,1,1,1,1,1,1,0},
              {0,0,0,0,0,0,0,0,0,0},
              {0,0,0,0,0,0,0,0,0,0}}
            };
            int y = 5;
            int x = 5;

            for (int ii = 0; ii < 3; ii++)
            {
                while (true)
                {
                    Console.Clear();
                    #region 打印地图
                    //打印地图
                    for (int i = 0; i < 10; i++)
                    {
                        for (int j = 0; j < 10; j++)
                        {
                            //0是空地,1是墙,2是小人,3是箱子,4是目标点,5是完成点
                            if (a[ii, i, j] == 0)
                            {
                                Console.Write("  ");
                            }
                            if (a[ii, i, j] == 1)
                            {
                                Console.Write("");
                            }
                            if (a[ii, i, j] == 2)
                            {
                                Console.Write("");
                            }
                            if (a[ii, i, j] == 3)
                            {
                                Console.Write("");
                            }
                            if (a[ii, i, j] == 4)
                            {
                                Console.Write("");
                            }
                            if (a[ii, i, j] == 5)
                            {
                                Console.Write("");
                            }
                        }
                        Console.WriteLine();
                    }
                    #endregion

                    if (ii==0)
                    {
                        if (a[ii, 2, 4] == 5 && a[ii, 4, 7] == 5 && a[ii, 5, 2] == 5 && a[ii, 7, 5] == 5)
                        {
                            Console.ReadKey();
                            x = 1;
                            y = 1;
                            break;
                        }
                    }
                    if (ii == 1)
                    {
                        if (a[ii, 3, 7] == 5)
                        {
                            Console.ReadKey();
                            x = 2;
                            y = 4;
                            break;
                        }
                    }
                    else if (ii==2)
                    {
                        if (a[ii, 6, 2] == 5 && a[ii, 6, 3] == 5 && a[ii, 6, 5] == 5)
                        {
                            Console.Write("恭喜过关");
                            Console.ReadKey();
                            break;
                        }
                    }
                    ConsoleKeyInfo info = Console.ReadKey();
                    if (info.Key.ToString() == "UpArrow")
                    {
                        if (a[ii, y - 1, x] == 1 || a[ii, y - 1, x] == 4)//小人的下一步是墙和目标点,不能动
                        { }
                        else if (a[ii, y - 1, x] == 3)//小人的下一步是箱子
                        {
                            if (a[ii, y - 2, x] == 1)//箱子的下一步是墙,就不能动了
                            { }
                            else if (a[ii, y - 2, x] == 4)//箱子的下一步是目标点
                            {
                                //小人与箱子的位置互换,箱子跑到小人的下面
                                int zhong = a[ii, y, x];
                                a[ii, y, x] = a[ii, y - 1, x];
                                a[ii, y - 1, x] = zhong;
                                y--;
                                a[ii, y - 1, x] = 5;
                                a[ii, y + 1, x] = 0;
                            }
                            else
                            {
                                //小人与箱子互换位置,箱子跑到小人的前面
                                int zhong = a[ii, y, x];
                                a[ii, y, x] = a[ii, y - 1, x];
                                a[ii, y - 1, x] = zhong;
                                y--;
                                //小人的下一步与上一步的箱子互换位置
                                zhong = a[ii, y - 1, x];
                                a[ii, y - 1, x] = a[ii, y + 1, x];
                                a[ii, y + 1, x] = zhong;
                            }
                        }
                        else if (a[ii, y - 1, x] == 0)
                        {
                            int zhong = a[ii, y, x];
                            a[ii, y, x] = a[ii, y - 1, x];
                            a[ii, y - 1, x] = zhong;
                            y--;
                        }
                    }
                    if (info.Key.ToString() == "DownArrow")
                    {
                        if (a[ii, y + 1, x] == 1 || a[ii, y + 1, x] == 4)//小人的下一步是墙和目标点,不能动
                        { }
                        else if (a[ii, y + 1, x] == 3)
                        {
                            if (a[ii, y + 2, x] == 1)
                            { }
                            else if (a[ii, y + 2, x] == 4)
                            {
                                int zhong = a[ii, y, x];
                                a[ii, y, x] = a[ii, y + 1, x];
                                a[ii, y + 1, x] = zhong;
                                y++;
                                a[ii, y + 1, x] = 5;
                                a[ii, y - 1, x] = 0;
                            }
                            else
                            {
                                int zhong = a[ii, y, x];
                                a[ii, y, x] = a[ii, y + 1, x];
                                a[ii, y + 1, x] = zhong;
                                y++;
                                zhong = a[ii, y + 1, x];
                                a[ii, y + 1, x] = a[ii, y - 1, x];
                                a[ii, y - 1, x] = zhong;
                            }
                        }
                        else if (a[ii, y + 1, x] == 0)
                        {
                            int zhong = a[ii, y, x];
                            a[ii, y, x] = a[ii, y + 1, x];
                            a[ii, y + 1, x] = zhong;
                            y++;
                        }
                    }
                    if (info.Key.ToString() == "LeftArrow")
                    {
                        if (a[ii, y, x - 1] == 1 || a[ii, y, x - 1] == 4)//小人的下一步是墙和目标点,不能动
                        { }
                        else if (a[ii, y, x - 1] == 3)
                        {
                            if (a[ii, y, x - 2] == 1)
                            { }
                            else if (a[ii, y, x - 2] == 4)
                            {
                                int zhong = a[ii, y, x];
                                a[ii, y, x] = a[ii, y, x - 1];
                                a[ii, y, x - 1] = zhong;
                                x--;
                                a[ii, y, x - 1] = 5;
                                a[ii, y, x + 1] = 0;
                            }
                            else
                            {
                                int zhong = a[ii, y, x];
                                a[ii, y, x] = a[ii, y, x - 1];
                                a[ii, y, x - 1] = zhong;
                                x--;
                                zhong = a[ii, y, x - 1];
                                a[ii, y, x - 1] = a[ii, y, x + 1];
                                a[ii, y, x + 1] = zhong;
                            }
                        }
                        else if (a[ii, y, x - 1] == 0)
                        {
                            int zhong = a[ii, y, x];
                            a[ii, y, x] = a[ii, y, x - 1];
                            a[ii, y, x - 1] = zhong;
                            x--;
                        }
                    }
                    if (info.Key.ToString() == "RightArrow")
                    {
                        if (a[ii, y, x + 1] == 1 || a[ii, y, x + 1] == 4)//小人的下一步是墙和目标点,不能动
                        { }
                        else if (a[ii, y, x + 1] == 3)
                        {
                            if (a[ii, y, x + 2] == 1)
                            { }
                            else if (a[ii, y, x + 2] == 4)
                            {
                                int zhong = a[ii, y, x];
                                a[ii, y, x] = a[ii, y, x + 1];
                                a[ii, y, x + 1] = zhong;
                                x++;
                                a[ii, y, x + 1] = 5;
                                a[ii, y, x - 1] = 0;
                            }
                            else
                            {
                                int zhong = a[ii, y, x];
                                a[ii, y, x] = a[ii, y, x + 1];
                                a[ii, y, x + 1] = zhong;
                                x++;
                                zhong = a[ii, y, x + 1];
                                a[ii, y, x + 1] = a[ii, y, x -1];
                                a[ii, y, x - 1] = zhong;
                            }
                        }
                        else if (a[ii, y, x + 1] == 0)
                        {
                            int zhong = a[ii, y, x];
                            a[ii, y, x] = a[ii, y, x + 1];
                            a[ii, y, x + 1] = zhong;
                            x++;
                        }
                    }
                }
            }

            Console.ReadLine();

以上是关于推箱子的主要内容,如果未能解决你的问题,请参考以下文章

c语言推箱子小游戏

python实现推箱子

项目: 推箱子游戏c/c++

推箱子小游戏——c/c++实现

C语言沈梦溪推箱子小游戏

C语言沈梦溪推箱子小游戏