c#菜鸟:(迷宫)控制台小程序(参考大师作品)

来源:互联网 发布:matlab 优化工具箱 编辑:程序博客网 时间:2024/04/29 18:36

class Program
    {

//游戏迷宫设置数组
        private static int[,] maze = {
                          {2,0,2,2,2,2,2,2,2,2,2,2,2,2},
                          {2,0,0,0,0,0,2,2,0,0,0,2,2,2},
                          {2,0,2,0,2,0,0,0,0,2,0,0,0,2},
                          {2,0,0,2,0,2,2,2,2,2,2,2,0,2},
                          {2,2,0,2,0,2,2,0,0,0,0,0,0,2},
                          {2,0,0,0,0,0,2,0,2,2,2,2,2,2},
                          {2,2,2,2,0,2,2,0,2,0,0,0,0,2},
                          {2,0,0,0,0,0,0,0,2,0,2,2,0,2},
                          {2,0,2,2,2,2,2,2,2,0,2,0,0,2},
                          {2,0,2,0,0,0,0,0,2,0,2,0,2,2},
                          {2,0,2,0,2,0,2,0,2,0,2,0,2,2},
                          {2,0,0,0,0,0,0,0,0,0,2,0,0,0},
                          {2,0,2,2,0,2,0,2,2,2,2,2,2,2},
                          {2,2,2,2,2,2,2,2,2,2,2,2,2,2}};

 

        static void Main(string[] args)
        {
            
int x = 0;
            int y = 1;
            games(x, y);
          

//键盘按键读取

            while (true)
            {
                char input=Console.ReadKey().KeyChar;
                switch (input.ToString().ToUpper())
                {
                    case "W"://向上移动
                        x--;
                        if (justfy(x, y)==false)
                        {
                            x++;
                        }
                        break;
                    case "S"://向下移动
                        x++;
                        if (justfy(x, y) == false)
                        {
                            x--;
                        }
                        break;
                    case "A"://向左移动
                        y--;
                        if (justfy(x, y) == false)
                        {
                            y++;
                        }
                        break;
                    case "D"://向右移动
                        y++;
                        if (justfy(x, y) == false)
                        {
                            y--;
                        }
                        break;
                    case "E"://退出程序
                        return;
                }
            }

           
        }

//判断移动方向是否有阻挡物

        public static bool justfy(int x,int y)
        {
            if (y>13)
            {
                Console.WriteLine("successful");
                return false;
            }
            if (x < 0 || (int)maze.GetValue(x, y) == 2)
            {
                Console.WriteLine("there is no way");
                return false;
            }
           
            Console.Clear();
            games(x, y);
            return true;
        }

 

//画迷宫


        public static void games(int x,int y)
        {
          
            for (int i = 0; i < 14; i++)
            {
                for (int j = 0; j < 14; j++)
                {
                    if ((int)maze.GetValue(i, j) == 2)
                    {

                        Console.Write("■");

                    }
                    else if(i==x && j==y)
                 {
                        Console.Write("◇");
                 }
                    else
                    {
                        Console.Write("  ");
                    }
                }
                Console.Write("/n");
            }

        }
    }

原创粉丝点击