zoukankan      html  css  js  c++  java
  • 二维数组及做推箱子

     //定义地图
                #region 定义地图
    
                int x=6; int y=1; //人的初始坐标
                int a=6; int b=3; //箱子的初始坐标
    
                int[,] map = new int[10, 10] { 
                    {8,8,8,8,8,8,8,8,8,8},
                    {8,0,0,0,0,8,8,0,0,8},
                    {8,0,8,0,0,8,8,0,0,8},
                    {8,0,8,8,0,8,8,0,0,8},
                    {8,0,8,0,0,0,0,0,0,8},
                    {8,0,0,0,0,8,0,0,0,8},
                    {8,1,0,2,0,8,8,0,0,8},
                    {8,0,0,0,0,8,0,0,8,8},
                    {8,0,0,0,0,8,0,0,3,8},
                    {8,8,8,8,8,8,8,8,8,8},
                };
                #endregion
    
                #region 显示地图
    
                for (int i = 0; i < 10; i++)
                {
                    for (int j = 0; j < 10; j++)
                    {
                        Console.ForegroundColor = ConsoleColor.White;
                        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.ForegroundColor = ConsoleColor.Red;
                            Console.Write("");
                            Console.ForegroundColor = ConsoleColor.White;
                        }
                        else if (map[i, j] == 8)
                        {
                            Console.Write("");
                        }
                    }
                    Console.WriteLine();
                }
    
              #endregion 
    
                //推箱子开始
                while(map[8,8]!=2)
                {
    
                   ConsoleKeyInfo key = Console.ReadKey();
                   //按下上键时执行的程序
                   #region 上键
                   if (key.Key.ToString().ToLower() == "uparrow")
                   {
                       if (x > 1) //如果人不在靠墙的位置
                       {
                           if (map[x - 1, y] == 0) //如果人上面是路
                           {
                               map[x - 1, y] = 1;
                               map[x, y] = 0;
                               map[8, 8] = 3;
                               x--;
                           }
                           else if (map[x - 1, y] == 3)
                           {
                               map[x - 1, y] = 1;
                               map[x, y] = 0;
                               x--;
                           }
                           else if (map[x - 1, y] == 2 && map[x - 2, y] == 0)//如果人上面是箱子,箱子上面是路
                           {
                               map[x - 2, y] = 2;
                               map[x - 1, y] = 1;
                               map[x, y] = 0;
                               x--;
                               //a--;
                           }
                           else if (map[x - 1, y] == 2 && map[x - 2, y] == 3)//如果人上面是箱子,箱子上面是终点
                           {
                               map[x - 2, y] = 2;
                               map[x - 1, y] = 1;
                               map[x, y] = 0;
                               x--;
                               //a--;
                           }
                           else
                           {
                               Console.Write("a");
                           }
                       }
                       else
                       {
                           Console.Write("a");
                       }
                   }
                   #endregion
                   #region 下键
                   //按下的键为下键时执行的程序
                   else if (key.Key.ToString().ToLower() == "downarrow")
                   {
                       if (x < 8) //如果人不在靠墙的位置
                       {
                           if (map[x + 1, y] == 0) //如果人下面是路
                           {
                               map[x + 1, y] = 1;
                               map[x, y] = 0;
                               map[8, 8] = 3;
                               x++;
                           }
                           else if (map[x + 1, y] == 3)
                           {
                               map[x + 1, y] = 1;
                               map[x, y] = 0;
                               x++;
                           }
                           else if (map[x + 1, y] == 2 && map[x + 2, y] == 0)//如果人下面是箱子,箱子下面是路
                           {
                               map[x + 2, y] = 2;
                               map[x + 1, y] = 1;
                               map[x, y] = 0;
                               x++;
                           }
                           else if (map[x + 1, y] == 2 && map[x + 2, y] == 3)//如果人下面是箱子,箱子下面是终点
                           {
    
                               map[x + 2, y] = 2;
                               map[x + 1, y] = 1;
                               map[x, y] = 0;
                               x++;
                           }
                           else
                           {
                               Console.Write("a");
                           }
                       }
                       else
                       {
                           Console.Write("a");
                       }
                   }
                   #endregion
                   #region 左键
                   //按下的键为左键时执行的程序
                   else if (key.Key.ToString().ToLower() == "leftarrow")
                   {
                       if (y >1) //如果人不在靠墙的位置
                       {
                           if (map[x, y-1] == 0) //如果人左面是路
                           {
                               map[x, y-1] = 1;
                               map[x, y] = 0;
                               map[8, 8] = 3;
                               y--;
                           }
                           else if (map[x, y - 1] == 3)
                           {
                               map[x, y - 1] = 1;
                               map[x, y] = 0;
                               y--;
                           }
                           else if (map[x, y - 1] == 2 && map[x, y - 2] == 0)//如果人左面是箱子,箱子左面是路
                           {
                               map[x, y - 2] = 2;
                               map[x, y - 1] = 1;
                               map[x, y] = 0;
                               y--;
                           }
                           else if (map[x, y - 1] == 2 && map[x, y - 2] == 3)//如果人左面是箱子,箱子左面是终点
                           {
    
                               map[x, y - 2] = 2;
                               map[x, y - 1] = 1;
                               map[x, y] = 0;
                               y--;
                           }
                           else
                           {
                               Console.Write("a");
                           }
                       }
                       else
                       {
                           Console.Write("a");
                       }
                   }
                   #endregion
                   #region 右键
                   //按下的键为右键时执行的程序s
                   else if (key.Key.ToString().ToLower() == "rightarrow")
                   {
                       if (y < 8)
                       {
                           if (map[x, y + 1] == 0)
                           {
                               map[x, y + 1] = 1;
                               map[x, y] = 0;
                               map[8, 8] = 3;
                               y++;
                           }
                           else if (map[x, y + 1] == 3)
                           {
                               map[x, y + 1] = 1;
                               map[x, y] = 0;
                               y++;
                               
                           }
                           else if (map[x, y + 1] == 2 && map[x, y + 2] == 0)
                           {
                               map[x, y + 2] = 2;
                               map[x, y + 1] = 1;
                               map[x, y] = 0;
                               y++;
                           }
                           else if (map[x, y + 1] == 2 && map[x, y + 2] == 3)
                           {
                               map[x, y + 2] = 2;
                               map[x, y + 1] = 1;
                               map[x, y] = 0;
                               y++;
                           }
                           else
                           {
                               Console.Write("a");
                           }
                       }
                       else
                       {
                           Console.Write("a");
                       }
                   }
                   #endregion
                   #region 重新显示
                   Console.Clear();
                   for (int i = 0; i < 10; i++)
                   {
                       for (int j = 0; j < 10; j++)
                       {
                           Console.ForegroundColor = ConsoleColor.White;
                           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.ForegroundColor = ConsoleColor.Red;
                               Console.Write("");
                               Console.ForegroundColor = ConsoleColor.White;
                           }
                           else if (map[i, j] == 8)
                           {
                               Console.Write("");
                           }
                       }
                       Console.WriteLine();
                   }
                   #endregion
    
                }
                Console.WriteLine("恭喜你,通关成功!");
                Console.ReadLine();
  • 相关阅读:
    使用awk根据多维度统计系统tps
    Java实现身份证号码校验
    Java分布式数据导出实践
    Java服务器端消息队列实战
    Java获取当前服务器IP实现
    Jvm dump介绍与使用(内存与线程)
    Java进行身份证格式强校验(准)
    Java性能监控之Instrumentation
    基于linux操作系统安装、使用memcached详解
    基于linux操作系统安装、使用redis详解
  • 原文地址:https://www.cnblogs.com/franky2015/p/4645677.html
Copyright © 2011-2022 走看看