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();

  • 相关阅读:
    Oracle sql的基本优化写法和思路。
    Linux的简单介绍和开发基本运维时候用到的命令
    Nginx的使用(反向代理,负载均衡)
    Mybatis传值为空需要配置JdbcType来解决吗?(XML文件不需要配置JdbcType)
    Mybatis Blob和String互转,实现文件上传等。
    Ckeditor上传图片返回的JS直接显示出来,未执行!!!
    学习中的错误——ubuntu 14.04 LTS 启动eclipse报错
    2016计算机大会后记——机器学习:发展与未来
    2016计算机大会后记——大数据时代的模式识别
    近期编程问题——epoll failed:bad file descriptor
  • 原文地址:https://www.cnblogs.com/123lucy/p/5538853.html
Copyright © 2011-2022 走看看