控制台飞行棋游戏



namespace 飞行棋游戏2
{
    class Program
    {
        #region 静态变量、数组
        //我们用静态字段来模拟全局变量
        static int[] Maps = new int[100];
        //声明一个静态数组用来存储玩家A跟玩家B的坐标
        static int[] PlayerPos = new int[2];

        //存储两位玩家的姓名
        static string[] PlayerName = new string[2];

        //判断玩家暂停状态,false为继续
        static bool[] zhangting = new bool[2];
        #endregion
        static void Main(string[] args)
        {
            #region 初始化游戏头
            Console.Title = "飞行棋游戏";
            GameShow();
            #endregion 

            #region 输入玩家姓名
            Console.WriteLine("请输入玩家A的姓名:");
            PlayerName[0] = Console.ReadLine();
            while (PlayerName[0] == "" || PlayerName[0] == null)
            {
                Console.WriteLine("玩家姓名不能为空。请重新输入:");
                PlayerName[0] = Console.ReadLine();
            }


            Console.WriteLine("请输入玩家B的姓名:");
            PlayerName[1] = Console.ReadLine();
            bool b;
            do
            {
                b = false;
                if (PlayerName[1] == "" || PlayerName[1] == null)
                {
                    Console.WriteLine("玩家姓名不能为空。请重新输入:");
                    b = true;
                }
                else if (PlayerName[0] == PlayerName[1])
                {
                    Console.WriteLine("玩家姓名相同。请重新输入:");
                    b = true;

                }
                if (b)
                {
                    PlayerName[1] = Console.ReadLine();
                }

            } while (b);
            #endregion

            #region 初始化游戏地图
            //玩家姓名输入后,清屏
            Console.Clear();
            Console.WriteLine($"说明:
"{PlayerName[0]}"的士兵用"A"表示");
            Console.WriteLine($""{PlayerName[1]}"的士兵用"B"表示");
            InitailMap();
            DrawMap();
            Console.WriteLine($"请按任意键开始游戏!");
            Console.ReadKey(true);
            Console.Clear();
            DrawMap();
            #endregion

            #region 开始游戏
            //当两个玩家没有一个到终点的时候,两个玩家一直不停的玩
            while (PlayerPos[0] < 99 && PlayerPos[1] < 99)
            {
                //暂停判断
                if (zhangting[0] == false)
                {
                    PlayerGame(0);
                }
                else
                {
                    zhangting[0] = false;
                }

                if (zhangting[1] == false)
                {
                    PlayerGame(1);
                }
                else
                {
                    zhangting[1] = false;
                }
            }
            Console.ReadKey();
            #endregion
        }
        /// <summary>
        /// 画游戏头
        /// </summary>
        public static void GameShow()
        {
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("**************************");
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("**************************");
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("**************************");
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.WriteLine("***0505.Net基础班飞行棋***");
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine("**************************");
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("**************************");
            Console.ResetColor();
        }

        /// <summary>
        /// 初始化地图
        /// </summary>
        public static void InitailMap()
        {
            int[] luckyturn = { 6, 23, 40, 55, 69, 83 };//幸运轮盘◎
            for (int i = 0; i < luckyturn.Length; i++)
            {
                //int index = luckyturn[i];
                Maps[luckyturn[i]] = 1;
            }
            int[] landMine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 };//地雷☆
            for (int i = 0; i < landMine.Length; i++)
            {
                Maps[landMine[i]] = 2;
            }
            int[] pause = { 9, 27, 60, 93 };//暂停▲
            for (int i = 0; i < pause.Length; i++)
            {
                Maps[pause[i]] = 3;
            }
            int[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 };//时空隧道卐
            for (int i = 0; i < timeTunnel.Length; i++)
            {
                Maps[timeTunnel[i]] = 4;
            }
        }

        /// <summary>
        /// 画地图
        /// </summary>
        public static void DrawMap()
        {
            Console.WriteLine("图例:幸运轮盘:◎   地雷:☆   暂停:▲   时空隧道:卐");
            Console.WriteLine($"A:{PlayerName[0]} B:{PlayerName[1]}");
            #region 第一横行
            for (int i = 0; i < 30; i++)
            {
                Console.Write(DrawStringMap(i));

            }//for
            #endregion

            #region 第一竖行
            for (int i = 30; i < 35; i++)
            {
                Console.WriteLine();
                for (int j = 0; j < 29; j++)
                {
                    Console.Write("  ");
                }
                Console.Write(DrawStringMap(i));
            }//for
            #endregion 第一竖行尾

            #region 第二横行
            Console.WriteLine();
            for (int i = 64; i >= 35; i--)
            {
                Console.Write(DrawStringMap(i));
            }
            Console.WriteLine();
            #endregion 第二横行结尾

            #region 第二竖行
            for (int i = 65; i < 70; i++)
            {
                Console.WriteLine(DrawStringMap(i));
            }
            #endregion 第二竖行结尾

            #region 第三(最后)横行
            for (int i = 70; i < 100; i++)
            {
                Console.Write(DrawStringMap(i));
            }
            Console.WriteLine();
            #endregion 第三(最后)横行结尾

        }

        /// <summary>
        /// 从画地图的方法中抽象出一个方法
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        public static string DrawStringMap(int i)
        {
            string str = "";
            if (PlayerPos[0] == PlayerPos[1] && PlayerPos[0] == i)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                str = "<>";
            }
            else if (PlayerPos[0] == i)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                str = "A";
            }
            else if (PlayerPos[1] == i)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                str = "B";
            }
            else
            {
                switch (Maps[i])
                {
                    case 0:
                        Console.ForegroundColor = ConsoleColor.White;
                        str = "□"; break;
                    case 1:
                        Console.ForegroundColor = ConsoleColor.DarkBlue;
                        str = "◎"; break;
                    case 2:
                        Console.ForegroundColor = ConsoleColor.Blue;
                        str = "☆"; break;

                    case 3:
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        str = "▲"; break;

                    case 4:
                        Console.ForegroundColor = ConsoleColor.Green;
                        str = "卐"; break;
                    default:
                        Console.ResetColor();
                        break;
                }//switch
            }//else
            return str;
        }

        /// <summary>
        /// 玩游戏
        /// </summary>
        /// <param name="id"></param>
        public static void PlayerGame(int id)
        {
            Random random = new Random();
            Console.WriteLine($"由玩家"{PlayerName[id]}"按任意键郑骰子");
            Console.ReadKey(true);
            int ran = random.Next(1, 7);
            PlayerPos[id] += ran;
            Console.WriteLine($"玩家{PlayerName[id]}掷出数字:{ran}。");
            Console.ReadKey(true);
            InitailMap();
            Console.Clear();
            DrawMap();
            if (PlayerPos[id] == PlayerPos[1 - id])
            {
                PlayerPos[1 - id] -= 6;
                Console.WriteLine($"玩家{PlayerName[id]}踩到了{PlayerName[1 - id]},玩家{PlayerName[1 - id]}倒跟6格");

            }
            else if (PlayerPos[id] >= 0 && PlayerPos[id] <= 99)

            {
                switch (Maps[PlayerPos[id]])
                {
                    case 0:
                        Console.WriteLine($"玩家{PlayerName[id]}踩到了方块□,什么也不做");
                        break;
                    case 1:
                        Console.WriteLine($"玩家{PlayerName[id]}踩到了幸运轮盘◎,请选择1--交换位置  2--轰炸对方 使对方退6格:");
                        string s = Console.ReadLine();

                        while (s != "1" && s != "2")
                        {

                            Console.WriteLine("只能输入1或2,请重新输入 :");
                            s = Console.ReadLine();

                        }
                        if (s == "1")
                        {
                            int temp;
                            temp = PlayerPos[id];
                            PlayerPos[id] = PlayerPos[1 - id];
                            PlayerPos[1 - id] = temp;
                            Console.WriteLine("玩家交换位置");
                        }
                        else if (s == "2")
                        {
                            PlayerPos[1 - id] -= 6;
                            Console.WriteLine($"玩家{PlayerName[1 - id]}倒退6格");
                        }
                        break;
                    case 2:
                        PlayerPos[id] -= 6;
                        Console.WriteLine($"玩家{PlayerName[id]}踩到了地雷 ☆,退6格");
                        break;
                    case 3:
                        zhangting[id] = true;
                        Console.WriteLine($"玩家{PlayerName[id]}踩到了暂停▲,暂停一回");
                        break;
                    case 4:
                        PlayerPos[id] += 10;
                        Console.WriteLine($"玩家{PlayerName[id]}踩到了时空隧道卐,前进10格");
                        break;
                }
            }
            if (PlayerPos[id] < 0)
            {
                PlayerPos[id] = 0;
            }
            else if (PlayerPos[id] >= 99)
            {
                PlayerPos[id] = 99;
                Console.WriteLine($"玩家{PlayerName[id]}:");
                Win();
            }
            if (PlayerPos[1 - id] < 0)
            {
                PlayerPos[1 - id] = 0;
            }
            else if (PlayerPos[1 - id] >= 99)
            {
                PlayerPos[1 - id] = 99;
                Console.WriteLine($"玩家{PlayerName[1 - id]}:");
                Win();
            }
            Console.ReadKey(true);
            InitailMap();
            Console.Clear();
            DrawMap();
        }

        /// <summary>
        /// 输出胜利大字
        /// </summary>
        public static void Win()
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("                                          ◆                      ");
            Console.WriteLine("                    ■                  ◆               ■        ■");
            Console.WriteLine("      ■■■■  ■  ■                ◆■         ■    ■        ■");
            Console.WriteLine("      ■    ■  ■  ■              ◆  ■         ■    ■        ■");
            Console.WriteLine("      ■    ■ ■■■■■■       ■■■■■■■   ■    ■        ■");
            Console.WriteLine("      ■■■■ ■   ■                ●■●       ■    ■        ■");
            Console.WriteLine("      ■    ■      ■               ● ■ ●      ■    ■        ■");
            Console.WriteLine("      ■    ■ ■■■■■■         ●  ■  ●     ■    ■        ■");
            Console.WriteLine("      ■■■■      ■             ●   ■   ■    ■    ■        ■");
            Console.WriteLine("      ■    ■      ■            ■    ■         ■    ■        ■");
            Console.WriteLine("      ■    ■      ■                  ■               ■        ■ ");
            Console.WriteLine("     ■     ■      ■                  ■           ●  ■          ");
            Console.WriteLine("    ■    ■■ ■■■■■■             ■              ●         ●");
            Console.ResetColor();
        }



    }
}

  

原文地址:https://www.cnblogs.com/bcd589/p/6233587.html