zoukankan      html  css  js  c++  java
  • 推箱子

                //有一张地图
                //地图里有箱子、小人、和目标点
                //用户可以操纵这小人移动
                //小人推着箱子走,遇到墙就不能走了
                //小人把箱子推到目标点就胜利了,然后进入下一关
    
                //画地图(墙:1,箱子:4,小人:5,目标点:3)
                //1.1画地图
                #region
                int[,] dt = new int[10, 10]              
                       {
                       //0,1,2,3,4,5,6,7,8,9,0,                   
                        {0,0,0,0,0,0,0,0,0,0},//0
                        {0,0,0,1,1,1,0,0,0,0},//1
                        {0,0,0,1,3,1,0,0,0,0},//2
                        {0,0,0,1,2,1,1,1,1,0},//3
                        {0,1,1,1,4,2,4,3,1,0},//4
                        {0,1,3,2,4,5,1,1,1,0},//5
                        {0,1,1,1,1,4,1,0,0,0},//6
                        {0,0,0,0,1,3,1,0,0,0},//7
                        {0,0,0,0,1,1,1,0,0,0},//8
                        {0,0,0,0,0,0,0,0,0,0},//9
                        };
    
                #endregion
                int x = 5, y = 5;
                while (true)
                {
                    Console.Clear();
                    //1.2打印地图
                    #region
                    for (int i = 0; i < 10; i++)
                    {
                        for (int j = 0; j < 10; j++)
                        {
                            if (dt[i, j] == 0)
                            {
                                Console.Write(" ");//空余地图
                            }
                            else if (dt[i, j] == 1)
                            {
                                Console.Write("");//
                            }
                            else if (dt[i, j] == 2)
                            {
                                Console.Write("  ");//墙中空地
                            }
                            else if (dt[i, j] == 3)
                            {
                                Console.Write("");//目标点
                            }
                            else if (dt[i, j] == 4)
                            {
                                Console.Write("");//箱子
                            }
                            else if (dt[i, j] == 5)
                            {
                                Console.Write("");//小人
                            }
                            else if (dt[i, j] == 6)
                            {
                                Console.Write("");//胜利
                            }
                        }
                        Console.WriteLine();
                    }
                    #endregion
                    if (dt[2, 4] == 6 && dt[4, 7] == 6 && dt[5, 2] == 6 && dt[7, 5] == 6)
                    {
                        Console.WriteLine("恭喜过关!");
                        break;
                    }
                    ConsoleKeyInfo info = Console.ReadKey();
                    //2.1记录小人的初始位置
                    if (info.Key.ToString() == "UpArrow")
                    {
                        if (dt[y - 1, x] == 1 || dt[y - 1, x] == 3)//小人的下一步是墙或者是目标点,不能动
                        { }
                        else if (dt[y - 1, x] == 4)//小人的下一步是箱子
                        {
                            if (dt[y - 2, x] == 1)//小人的下两步是墙,就不能动了
                            { }
                            else if (dt[y - 2, x] == 3)
                            {
                                //小人箱子换位置,箱子到小人后面
                                int z = dt[y, x];
                                dt[y, x] = dt[y - 1, x];
                                dt[y - 1, x] = z;
                                y--;
                                dt[y + 1, x] = 2;//小人当前的上一个位置变成空地
                                dt[y - 1, x] = 6;//小人当前的下一个位置(即目标点)变成胜利形状
                            }
                            else
                            {
                                //小人箱子换位置,箱子到小人后面
                                int z = dt[y, x];
                                dt[y, x] = dt[y - 1, x];
                                dt[y - 1, x] = z;
                                y--;
                                //小人当前位置与上一步的箱子位置互换
                                z = dt[y - 1, x];
                                dt[y - 1, x] = dt[y + 1, x];
                                dt[y + 1, x] = z;
                            }
                        }
                        else if (dt[y - 1, x] == 2)
                        {
                            int z = dt[y, x];
                            dt[y, x] = dt[y - 1, x];
                            dt[y - 1, x] = z;
                            y--;
                        }
                    }
    
                    else if (info.Key.ToString() == "DownArrow")
                    {
                        if (dt[y + 1, x] == 1 || dt[y + 1, x] == 3)
                        { }
                        else if (dt[y + 1, x] == 4)
                        {
                            if (dt[y + 2, x] == 1)//小人的下两步是墙,就不能动了
                            { }
                            else if (dt[y + 2, x] == 3)
                            {
                                //小人箱子换位置,箱子到小人上面
                                int z = dt[y, x];
                                dt[y, x] = dt[y + 1, x];
                                dt[y + 1, x] = z;
                                y++;
                                dt[y - 1, x] = 2;
                                dt[y + 1, x] = 6;
                            }
                            else
                            {
                                //小人箱子换位置,箱子到小人后面
                                int z = dt[y, x];
                                dt[y, x] = dt[y + 1, x];
                                dt[y + 1, x] = z;
                                y++;
                                //小人当前位置与上一步的箱子位置互换
                                z = dt[y + 1, x];
                                dt[y + 1, x] = dt[y - 1, x];
                                dt[y - 1, x] = z;
                            }
                        }
                        else if (dt[y + 1, x] == 2)
                        {
                            int z = dt[y, x];
                            dt[y, x] = dt[y + 1, x];
                            dt[y + 1, x] = z;
                            y++;
                        }
                    }
    
                    else if (info.Key.ToString() == "LeftArrow")
                    {
                        if (dt[y, x - 1] == 1 || dt[y, x - 1] == 3)
                        { }
                        else if (dt[y, x - 1] == 4)
                        {
                            if (dt[y, x - 2] == 1)//小人的下两步是墙,就不能动了
                            { }
                            else if (dt[y, x - 2] == 3)
                            {
                                //小人箱子换位置,箱子到小人左面
                                int z = dt[y, x];
                                dt[y, x] = dt[y, x - 1];
                                dt[y, x - 1] = z;
                                x--;
                                dt[y, x + 1] = 2;
                                dt[y, x - 1] = 6;
                            }
                            else
                            {
                                //小人箱子换位置,箱子到小人左面
                                int z = dt[y, x];
                                dt[y, x] = dt[y, x - 1];
                                dt[y, x - 1] = z;
                                x--;
                                //小人当前位置与上一步的箱子位置互换
                                z = dt[y, x - 1];
                                dt[y, x - 1] = dt[y, x + 1];
                                dt[y, x + 1] = z;
                            }
                        }
                        else if (dt[y, x - 1] == 2)
                        {
                            int z = dt[y, x];
                            dt[y, x] = dt[y, x - 1];
                            dt[y, x - 1] = z;
                            x--;
                        }
                    }
    
                    else if (info.Key.ToString() == "RightArrow")
                    {
                        if (dt[y, x + 1] == 1 || dt[y, x + 1] == 3)
                        { }
                        else if (dt[y, x + 1] == 4)
                        {
                            if (dt[y, x + 2] == 1)//小人的下两步是墙,就不能动了
                            { }
                            else if (dt[y, x + 2] == 3)
                            {
                                //小人箱子换位置,箱子到小人左面
                                int z = dt[y, x];
                                dt[y, x] = dt[y, x + 1];
                                dt[y, x + 1] = z;
                                x++;
                                dt[y, x - 1] = 2;
                                dt[y, x + 1] = 6;
                            }
                            else
                            {
                                //小人箱子换位置,箱子到小人右面
                                int z = dt[y, x];
                                dt[y, x] = dt[y, x + 1];
                                dt[y, x + 1] = z;
                                x++;
                                //小人当前位置与上一步的箱子位置互换
                                z = dt[y, x + 1];
                                dt[y, x + 1] = dt[y, x - 1];
                                dt[y, x - 1] = z;
                            }
                        }
                        else if (dt[y, x + 1] == 2)
                        {
                            int z = dt[y, x];
                            dt[y, x] = dt[y, x + 1];
                            dt[y, x + 1] = z;
                            x++;
                        }
                    }
                }
                Console.ReadKey();

  • 相关阅读:
    预备作业02-162315马军
    预备作业01-162315马军
    # 2017-2018-1 20155224 《信息安全系统设计基础》第四周学习总结
    # 2017-2018-1 20155224 《信息安全系统设计基础》第3周学习总结
    # 第二周课堂实践以及MyOD
    # 2017-2018-1 20155224 《信息安全系统设计基础》第1周学习总结
    # 20155224 2016-2017-2《Java程序设计》课程总结
    # 20155224 实验五 Java网络编程及安全
    # 20155224 课堂实践 MyOD
    # 20155224 实验四 Android程序设计
  • 原文地址:https://www.cnblogs.com/123lucy/p/5538853.html
Copyright © 2011-2022 走看看