zoukankan      html  css  js  c++  java
  • 5月9日 推箱子

    推箱子练习

    //1、画地图

                int[,] map = new int[10, 10]{

                {1,1,1,1,1,1,1,1,1,1},

                {1,4,0,0,0,0,0,0,4,1},

                {1,0,3,0,0,0,0,0,0,1},

                {1,0,0,0,0,0,0,3,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,0,0,0,1},

                {1,0,0,3,0,0,0,3,0,1},

                {1,2,4,0,0,0,0,0,4,1},

                {1,1,1,1,1,1,1,1,1,1}

                };

                //记录小人开始的位置,在大循环外面规定,只记录一次,其他的根据改变进行加减

                int renx = 1;

                int reny = 8;

                while (true)

                {

                    Console.Clear();//清空控制台内容

                    //2、打印

                    //0:空位,1:墙,2:小人,3:箱子,4:目标点,5:完成点

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

                            }

                            else if (map[i, j] == 5)

                            {

                                Console.Write("★");

                            }

                        }

                        Console.WriteLine();                

                    }

                    //1,1;1,8;8,2;8,8  完成坐标,判断是否过关,过关就break跳出循环

                    if (map[1, 1] == 5 && map[1, 8] == 5 && map[8, 2] == 5 && map[8, 8] == 5)

                    {

                        Console.WriteLine("恭喜过关!");

                        break;

                    }

                    //3、让小人动起来

                    ConsoleKeyInfo info = Console.ReadKey();//获取用户的操作

                    

                    //判断用户的操作是哪个方向

                    if (info.Key.ToString() == "UpArrow")//向上

                    {

                        //向上一步如果是墙,向上一步是目标点,向上一步是完成的目标点,都不允许动

                        if (map[reny - 1, renx] == 1 || map[reny - 1, renx] == 4 || map[reny - 1, renx] == 5 )

                        {

                        }

                        else if (map[reny - 1, renx] == 3)//如果向上走一步是箱子

                        {

                            //箱子的下一步是墙,箱子的下一步是箱子,都不允许动

                            if (map[reny - 2, renx] == 1 || map[reny - 2, renx] == 3)

                            {

                            }

                            else if (map[reny - 2, renx] == 4)//箱子的右边是目标点

                            {

                                int zhong = 0;

                                zhong = map[reny - 1, renx];

                                map[reny - 1, renx] = map[reny, renx];

                                map[reny, renx] = zhong;

                                reny--;//及时更新小人当前位置的记录

                                map[reny - 1, renx] = 5;

                                map[reny + 1, renx] = 0;

                            }

                            else

                            {

                                int zhong = 0;

                                zhong = map[reny - 1, renx];

                                map[reny - 1, renx] = map[reny, renx];

                                map[reny, renx] = zhong;

                                reny--;//及时跟新小人当前的位置记录

                                zhong = map[reny - 1, renx];

                                map[reny - 1, renx] = map[reny + 1, renx];

                                map[reny + 1, renx] = zhong;

                            }

                        }

                        else

                        {

                            int zhong = 0;

                            zhong = map[reny - 1, renx];

                            map[reny - 1, renx] = map[reny, renx];

                            map[reny,renx] = zhong;

                            reny--;//及时更新小人当前的位置记录

                        }

                    }

                    else if (info.Key.ToString() == "DownArrow")//向下

                    {

                        if (map[reny + 1, renx] == 1 || map[reny + 1, renx] == 4 || map[reny + 1, renx] == 5)

                        {

                        }

                        else if (map[reny + 1, renx] == 3)//如果向下走一步是箱子

                        {

                            //箱子的下一步是墙,箱子的下一步是箱子,都不允许动

                            if (map[reny + 2, renx] == 1 || map[reny + 2, renx] == 3)

                            {

                            }

                            else if (map[reny + 2, renx] == 4)//箱子的下边是目标点

                            {

                                int zhong = 0;

                                zhong = map[reny + 1, renx];

                                map[reny + 1, renx] = map[reny, renx];

                                map[reny, renx] = zhong;

                                reny++;//及时更新小人当前位置的记录

                                map[reny + 1, renx] = 5;

                                map[reny - 1, renx] = 0;

                            }

                            else

                            {

                                int zhong = 0;

                                zhong = map[reny + 1, renx];

                                map[reny + 1, renx] = map[reny, renx];

                                map[reny, renx] = zhong;

                                reny++;//及时跟新小人当前的位置记录

                                zhong = map[reny + 1, renx];

                                map[reny + 1, renx] = map[reny - 1, renx];

                                map[reny - 1, renx] = zhong;

                            }

                        }

                        else

                        {

                            int zhong = 0;

                            zhong = map[reny + 1, renx];

                            map[reny + 1, renx] = map[reny, renx];

                            map[reny, renx] = zhong;

                            reny++;//及时更新小人当前的位置记录

                        }

                        

                    }

                    else if (info.Key.ToString() == "LeftArrow")//向左

                    {

                        if (map[reny, renx - 1] == 1 || map[reny, renx - 1] == 4 || map[reny, renx - 1] == 5)

                        {

                        }

                        else if (map[reny, renx - 1] == 3)//如果向左走一步是箱子

                        {

                            //箱子的下一步是墙,箱子的下一步是箱子,都不允许动

                            if (map[reny, renx - 2] == 1 || map[reny, renx - 2] == 3)

                            {

                            }

                            else if (map[reny, renx - 2] == 4)//箱子的左边是目标点

                            {

                                int zhong = 0;

                                zhong = map[reny, renx - 1];

                                map[reny, renx - 1] = map[reny, renx];

                                map[reny, renx] = zhong;

                                renx--;//及时更新小人当前位置的记录

                                map[reny, renx - 1] = 5;

                                map[reny, renx + 1] = 0;

                            }

                            else

                            {

                                int zhong = 0;

                                zhong = map[reny, renx - 1];

                                map[reny, renx - 1] = map[reny, renx];

                                map[reny, renx] = zhong;

                                renx--;//及时跟新小人当前的位置记录

                                zhong = map[reny, renx - 1];

                                map[reny, renx - 1] = map[reny, renx + 1];

                                map[reny, renx + 1] = zhong;

                            }

                        }

                        else

                        {

                            int zhong = 0;

                            zhong = map[reny, renx - 1];

                            map[reny, renx - 1] = map[reny, renx];

                            map[reny, renx] = zhong;

                            renx--;//及时更新小人当前的位置记录

                        }  

                    }

                    else if (info.Key.ToString() == "RightArrow")//向右

                    {

                        if (map[reny, renx + 1] == 1 || map[reny, renx + 1] == 4 || map[reny, renx + 1] == 5)

                        {

                        }

                        else if (map[reny, renx + 1] == 3)//如果向右走一步是箱子

                        {

                            //箱子的下一步是墙,箱子的下一步是箱子,都不允许动

                            if (map[reny, renx + 2] == 1 || map[reny, renx + 2] == 3)

                            {

                            }

                            else if (map[reny, renx + 2] == 4)//箱子的右边是目标点

                            {

                                int zhong = 0;

                                zhong = map[reny, renx + 1];

                                map[reny, renx + 1] = map[reny, renx];

                                map[reny, renx] = zhong;

                                renx++;//及时更新小人当前位置的记录

                                map[reny, renx + 1] = 5;

                                map[reny, renx - 1] = 0;

                            }

                            else

                            {

                                int zhong = 0;

                                zhong = map[reny, renx + 1];

                                map[reny, renx + 1] = map[reny, renx];

                                map[reny, renx] = zhong;

                                renx++;//及时跟新小人当前的位置记录

                                zhong = map[reny, renx + 1];

                                map[reny, renx + 1] = map[reny, renx - 1];

                                map[reny, renx - 1] = zhong;

                            }

                        }

                        else

                        {

                            int zhong = 0;

                            zhong = map[reny, renx + 1];

                            map[reny, renx + 1] = map[reny, renx];

                            map[reny, renx] = zhong;

                            renx++;//及时更新小人当前的位置记录

                        }  

                    }

                }

                Console.ReadLine();

  • 相关阅读:
    eclipse 设置默认编码为Utf-8
    重新打开Eclipse出现“An internal error has occurred. java.lang.NullPointerException”
    Eclipse启动时出现错误 An internal error occurred during: "Updating indexes"
    eclipse卸载自带maven
    JAVA实现SFTP实例
    springboot-24-restTemplate的使用
    Jmeter在restful风格接口测试中的应用
    Swagger介绍-一套流行的API框架
    SQL 里ESCAPE的用法
    CALL 'SYSTEM' ID 'COMMAND'
  • 原文地址:https://www.cnblogs.com/juyangchao12/p/5478540.html
Copyright © 2011-2022 走看看