推箱子

推箱子

主要代码:

            #region 地图
            //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,4,1},
                {1,0,0,0,0,3,0,0,4,1},
                {1,0,0,0,0,0,0,0,0,1},
                {1,0,1,1,1,1,0,0,1,1},
                {1,0,0,0,0,0,0,0,0,1},
                {1,0,0,3,0,0,0,3,0,1},
                {1,2,0,0,0,0,0,0,0,1},
                {1,1,1,1,1,1,1,1,1,1},
            };
            #endregion
            //记录小人开始位置
            int px = 1;
            int py = 8;
            while (true)
            {
                Console.Clear();//清空控制台内容
                #region 打印
                //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();
                }
                #endregion
                #region 判断
                //11,18,82,88为完成坐标,判断是否过关。
                if (map[1, 1] == 5 && map[1, 8] == 5 && map[2, 8] == 5 && map[3, 8] == 5)
                {
                    Console.WriteLine("恭喜过关!");
                    break;
                }
                #endregion
                //3、让小人动起来
                ConsoleKeyInfo info = Console.ReadKey();
                #region 向上动
                if (info.Key.ToString() == "UpArrow")
                {
                    //向上一步是墙或者目的地再或者是完结点不动
                    if (map[py - 1, px] == 1 || map[py - 1, px] == 4 || map[py - 1, px] == 5)
                    {}
                    //向上一步是箱子
                    else if(map[py-1,px]==3)
                    {
                        //箱子向上一步是墙或者是箱子,不动
                        if (map[py - 2, px] == 1 || map[py - 2, px] == 3)
                        { }
                        else if (map[py - 2, px] == 4)
                        {
                            int a = 0;
                            a = map[py - 1, px];
                            map[py - 1, px] = map[py, px];
                            map[py, px] = a;
                            py--;
                            map[py - 1, px] = 5;
                            map[py +1, px] = 0;
                        }
                        else
                        {
                            int a = 0;
                            a = map[py - 1, px];
                            map[py - 1, px] = map[py, px];
                            map[py, px] = a;
                            py--;
                            a = map[py - 1, px];
                            map[py - 1, px] = map[py+1,px];
                            map[py +1, px] =a;
                        }
                    }
                    else
                    {
                        int a = 0;
                        a = map[py - 1, px];
                        map[py - 1, px] = map[py, px];
                        map[py, px] = a;
                        py--;
                    }
                }
                #endregion 
                #region 向下动
                if (info.Key.ToString() == "DownArrow")
                {
                    if (map[py + 1, px] == 1 || map[py +1, px] == 4 || map[py + 1, px] == 5)
                    { }
                    else if (map[py +1, px] == 3)
                    {
                        if (map[py + 2, px] == 1 || map[py + 2, px] == 3)
                        { }
                        else if (map[py + 2, px] == 4)
                        {
                            int a = 0;
                            a = map[py + 1, px];
                            map[py +1, px] = map[py, px];
                            map[py, px] = a;
                            py++;
                            map[py+ 1, px] = 5;
                            map[py -1, px] = 0;
                        }
                        else
                        {
                            int a = 0;
                            a = map[py + 1, px];
                            map[py + 1, px] = map[py, px];
                            map[py, px] = a;
                            py++;
                            a = map[py + 1, px];
                            map[py +1, px] = map[py - 1, px];
                            map[py - 1, px] = a;
                        }
                    }
                    else
                    {
                        int a = 0;
                        a = map[py + 1, px];
                        map[py + 1, px] = map[py, px];
                        map[py, px] = a;
                        py++;
                    }
                }
                #endregion
                #region 向左动
                if (info.Key.ToString() == "LeftArrow")
                {
                    if (map[py , px-1] == 1 || map[py , px-1] == 4 || map[py , px-1] == 5)
                    { }
                    else if (map[py, px-1] == 3)
                    {
                        if (map[py , px-2] == 1 || map[py , px-2] == 3)
                        { }
                        else if (map[py , px-2] == 4)
                        {
                            int a = 0;
                            a = map[py , px-1];
                            map[py , px-1] = map[py, px];
                            map[py, px] = a;
                            px--;
                            map[py , px-1] = 5;
                            map[py , px+1] = 0;
                        }
                        else
                        {
                            int a = 0;
                            a = map[py , px-1];
                            map[py, px-1] = map[py, px];
                            map[py, px] = a;
                            px--;
                            a = map[py, px-1];
                            map[py , px-1] = map[py, px+1];
                            map[py , px+1] = a;
                        }
                    }
                    else
                    {
                        int a = 0;
                        a = map[py, px - 1];
                        map[py, px - 1] = map[py, px];
                        map[py, px] = a;
                        px--;
                    }
                }
                #endregion
                #region 向右动
                if (info.Key.ToString() == "RightArrow")
                {
                    if (map[py , px+1] == 1 || map[py, px+1] == 4 || map[py, px+1] == 5)
                    { }
                    else if (map[py, px+1] == 3)
                    {
                        if (map[py, px+2] == 1 || map[py, px+2] == 3)
                        { }
                        else if (map[py , px+2] == 4)
                        {
                            int a = 0;
                            a = map[py , px+1];
                            map[py , px+1] = map[py, px];
                            map[py, px] = a;
                            px++;
                            map[py , px+1] = 5;
                            map[py , px-1] = 0;
                        }
                        else
                        {
                            int a = 0;
                            a = map[py, px + 1];
                            map[py, px + 1] = map[py, px];
                            map[py, px] = a;
                            px++;
                            a = map[py, px + 1];
                            map[py, px + 1] = map[py, px - 1];
                            map[py, px - 1] = a;
                        }
                    }
                    else
                    {
                        int a = 0;
                        a = map[py , px+1];
                        map[py , px+1] = map[py, px];
                        map[py, px] = a;
                        px++;
                    }
                }
                #endregion
            }
            Console.ReadLine();

结果:

未完待续,有些瑕疵没有修改!!

原文地址:https://www.cnblogs.com/bosamvs/p/5479026.html