zoukankan      html  css  js  c++  java
  • 简单推箱子:两张图每张图1个箱子→思路

    namespace ConsoleApplication15//推箱子本质就是变量的相互替换,3个变量的循环赋值
    {
        class Program
        {
           
               
            static void Main(string[] args)
            {
                int x = 1, y = 1, i = 0;
                int[, ,] dt = new int[2, 30, 30]  //定义一个二维数组用于接收地图
                    {
                       { 
                        {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                        {1,4,0,1,0,1,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,0,2,0,0,1,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,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,0,0,0,1},
                        {1,0,0,0,3,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,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,0,0,0,1},
                        {1,0,0,0,0,1,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,0,0,0,0,1,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,0,0,1,0,1,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,0,0,1,0,1,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,0,0,1,0,1,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,0,0,1,0,1,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,0,0,1,0,1,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,0,0,1,0,1,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,0,0,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,0,0,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,1},
                        {1,0,0,1,0,1,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,0,0,1,0,1,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,0,0,1,0,1,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,0,0,1,0,1,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,0,0,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,0,0,1,0,1,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,0,0,1,0,1,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,0,0,1,0,1,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,0,0,1,0,1,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,0,0,1,0,1,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,0,0,1,0,1,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,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,0,0,0,1},
                        {1,0,0,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,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
                       },
                       { 
                        {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                        {1,4,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,0,0,1},
                        {1,0,2,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,0,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,0,0,0,1},
                        {1,0,0,0,3,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,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,0,0,0,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,0,0,0,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,0,0,0,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,0,0,0,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,0,0,0,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,0,0,0,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,0,0,0,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,0,0,0,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,0,0,0,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,0,0,0,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,0,0,0,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,0,0,0,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,0,0,0,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,0,0,0,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,0,0,0,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,0,0,0,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,0,0,0,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,0,0,0,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,0,0,0,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,0,0,0,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,0,0,0,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,0,0,0,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,0,0,0,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,0,0,0,1},
                        {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
                       }
    
                       
    
                    
                    };
                try
                {
    
    
    
                    while (true)
                    {
                        int temp = 0;
    
                        ConsoleKeyInfo s = Console.ReadKey();  //s用于接收用户输入的键
                        if (s.Key.ToString() == "DownArrow")
                        {
                            if (dt[i, x + 1, y] == 0)//如果小人的下一步是0(空格),则两个元素交换  (下同)
                            {
    
                                temp = dt[i, x, y];
                                dt[i, x, y] = dt[i, x + 1, y];
                                dt[i, x + 1, y] = temp;
                                x++;
                            }
                            else if (dt[i, x + 1, y] == 2 && dt[i, x + 2, y] == 0)//如果小人的下一步是箱子,箱子的下一步是空格,则三个元素进行交换 (下同)
                            {
    
                                temp = dt[i, x + 2, y];
                                dt[i, x + 2, y] = dt[i, x + 1, y];
                                dt[i, x + 1, y] = temp;
                                temp = dt[i, x + 1, y];
                                dt[i, x + 1, y] = dt[i, x, y];
                                dt[i, x, y] = temp;
                                x++;
                                
                            }
                            else if (dt[i, x + 2, y] == 3 && dt[i, x + 1, y]==2) //如果小人的下面第二个元素是“★”
                                {
    
                                    dt[i, x+2, y ] = 2; //让五角星变成箱子
                                    dt[i, x+1, y ] = 4;// 让箱子变成小人
                                    dt[i, x, y] = 0;    //让小人变成空格
                                    temp = 6; //给临时变量赋一个值作为跳出循环的条件
                                }
                        }
                        else if (s.Key.ToString() == "UpArrow")
                        {
                            if (dt[i, x - 1, y] == 0)
                            {
    
                                temp = dt[i, x - 1, y];
                                dt[i, x - 1, y] = dt[i, x, y];
                                dt[i, x, y] = temp;
                                x--;
                            }
                            else if (dt[i, x - 1, y] == 2 && dt[i, x - 2, y] == 0)
                            {
    
                                temp = dt[i, x - 2, y];
                                dt[i, x - 2, y] = dt[i, x - 1, y];
                                dt[i, x - 1, y] = temp;
                                temp = dt[i, x, y];
                                dt[i, x, y] = dt[i, x - 1, y];
                                dt[i, x - 1, y] = temp;
                                x--;
                                
                            }
                            else if (dt[i, x - 2, y] == 3 && dt[i, x - 1, y]==2) //如果小人的下面第二个元素是“★”
                            {
    
                                dt[i, x - 2, y] = 2; //让五角星变成箱子
                                dt[i, x - 1, y] = 4;// 让箱子变成小人
                                dt[i, x, y] = 0;    //让小人变成空格
                                temp = 6; //给临时变量赋一个值作为跳出循环的条件
                            }
                        }
                        else if (s.Key.ToString() == "LeftArrow")
                        {
                            if (dt[i, x, y - 1] == 0)
                            {
    
                                temp = dt[i, x, y - 1];
                                dt[i, x, y - 1] = dt[i, x, y];
                                dt[i, x, y] = temp;
                                y--;
                            }
                            else if (dt[i, x, y - 1] == 2 && dt[i, x, y - 2] == 0)
                            {
    
                                temp = dt[i, x, y - 2];
                                dt[i, x, y - 2] = dt[i, x, y - 1];
                                dt[i, x, y - 1] = temp;
                                temp = dt[i, x, y];
                                dt[i, x, y] = dt[i, x, y - 1];
                                dt[i, x, y - 1] = temp;
                                y--;
                                
    
                            }
                            else if (dt[i, x, y - 2] == 3&&dt[i, x, y - 1] == 2) //如果小人的下面第二个元素是“★”
                            {
    
                                dt[i, x, y - 2] = 2; //让五角星变成箱子
                                dt[i, x, y - 1] = 4;// 让箱子变成小人
                                dt[i, x, y] = 0;    //让小人变成空格
                                temp = 6; //给临时变量赋一个值作为跳出循环的条件
                            }
                        }
                        else if (s.Key.ToString() == "RightArrow")
                        {
                            if (dt[i, x, y + 1] == 0)
                            {
    
                                temp = dt[i, x, y + 1];
                                dt[i, x, y + 1] = dt[i, x, y];
                                dt[i, x, y] = temp;
                                y++;
                            }
                            else if (dt[i, x, y + 1] == 2 && dt[i, x, y + 2] == 0)
                            {
    
                                temp = dt[i, x, y + 2];
                                dt[i, x, y + 2] = dt[i, x, y + 1];
                                dt[i, x, y + 1] = temp;
                                temp = dt[i, x, y + 1];
                                dt[i, x, y + 1] = dt[i, x, y];
                                dt[i, x, y] = temp;
                                y++;
                               
                            }
                            else if (dt[i, x, y + 2] == 3&&dt[i, x, y + 1] == 2) //如果小人的下面第二个元素是“★”而且小人下面第一个元素是
                            {
    
                                dt[i, x, y + 2] = 2; //让五角星变成箱子
                                dt[i, x, y + 1] = 4;// 让箱子变成小人
                                dt[i, x, y] = 0;    //让小人变成空格
                                temp = 6; //给临时变量赋一个值作为跳出循环的条件
                            }
                        }
    
    
                        Console.Clear();  //清屏
    
                       ditu(dt, i);// 调用ditu函数打印地图;一开始就从这里开始,前面没有打印地图。
                        if (temp == 6)// 跳出循环
                        {
                            Console.WriteLine("恭喜您过关了!");
                            Console.WriteLine("按任意键进入下一关!");
                            Console.ReadKey();
                            x = 1; y = 1;//初始化小人的位置
                            i++;
    
                        } 
    
                    }
                }
                catch (Exception)
                {
    
                    Console.WriteLine("暂时只有两张图!");
                    Console.ReadKey();
                }
    
    
    
            }
    
            private static void ditu(int[, ,] dt, int i)
            {
    
    
                for (int j = 0; j < 30; j++)
                {
                    for (int k = 0; k < 30; k++)
                    {
    
    
                        if (dt[i, j, k] == 0)
                        {
                            Console.Write("  ");
                        }
                        else if (dt[i, j, k] == 1)
                        {
                            Console.Write("");
                        }
                        else if (dt[i, j, k] == 2)
                        {
                            Console.Write("");
                        }
                        else if (dt[i, j, k] == 3)
                        {
                            Console.Write("");
                        }
                        else if (dt[i, j, k] == 4)
                        {
                            Console.Write("");
                        }
    
    
                    }
                    Console.Write("
    ");
                }
    
    
            }
        }
    }
    二百个不间断的重复,只是让我看到了人的命运无法改变这一事实而已。
  • 相关阅读:
    Hadoop之MapReduce
    Hadoop之序列化
    Hadoop之mapreduce 实例五
    Hadoop之hive和hadoop的交互流程
    protel总结1
    将正数十进制转换为2~16进制数
    基于verilog分频器总结
    MATLAB设计FIR滤波器
    centos7+hadoop完全分布式集群搭建
    Linux下常用配置文件
  • 原文地址:https://www.cnblogs.com/dlexia/p/4398460.html
Copyright © 2011-2022 走看看