飞行棋游戏代码(C#)

220224飞行器v1.0

using System;

namespace AeroplaneChess
{
    class Program
    {
        //地图
        static int[] Maps = new int[100];
        //玩家A B坐标
        static int[] playerPos = new int[2];
        //玩家姓名
        static string[] playerNames = new string[2];
        //玩家回合标记
        static bool[] Flags = new bool[2];
        /// <summary>
        /// 主函数
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            //游戏封面
            GameShow();
            #region 输入玩家姓名
            Console.WriteLine("请输入玩家A姓名");
            playerNames[0] = Console.ReadLine();
            while (playerNames[0]=="")
            {
                Console.WriteLine("玩家A姓名不能为空,请重新输入");
                playerNames[0] = Console.ReadLine();
            }
            Console.WriteLine("请输入玩家B姓名");
            playerNames[1] = Console.ReadLine();
            while (playerNames[1] == "" || playerNames[1] == playerNames[0])
            {
                if(playerNames[1] == "")
                {
                    Console.WriteLine("玩家B姓名不能为空,请重新输入");
                    playerNames[1] = Console.ReadLine();
                }
                if (playerNames[1] == playerNames[0])
                {
                    Console.WriteLine("玩家B姓名不能与玩家A相同,请重新输入");
                    playerNames[1] = Console.ReadLine();
                }
                    
            }
            #endregion
            Console.Clear();//清屏
            GameShow();
            Console.WriteLine("{0}的飞机用A表示,{1}的士兵用B表示", playerNames[0], playerNames[1]);
            //初始化地图
            InitMap();
            //显示棋盘
            DrawMap();
            //胜利界面

            //游戏逻辑
            while (playerPos[0] < 99 && playerPos[1] < 99)
            {
                if (Flags[0] == false)
                {
                    playGame(0);
                }
                else
                {
                    Flags[0] = false;
                }
                if(playerPos[0]>=99)
                {
                    Console.WriteLine("玩家{0}无耻的赢了玩家{1}",playerNames[0],playerNames[1]);
                    break;
                }
                if(Flags[1] == false)
                {
                    playGame(1);
                }
                else
                {
                    Flags[1] = false;
                }
                if (playerPos[1] >= 99)
                {
                    Console.WriteLine("玩家{0}无耻的赢了玩家{1}", playerNames[1], playerNames[0]);
                    break;
                }
                

            }//while
            Win();
        }//mian

        //
        static void Win()
        {
            Console.WriteLine(@"                                
          ■■■         ■■         
 ■■■■■ ■■ ■■     ■■■■■■■    ■■  
 ■■■■■ ■■ ■■     ■■■■■      ■■  
 ■■ ■■ ■■■■■■■■     ■■   ■■ ■■  
 ■■ ■■ ■■■■■■■■     ■■   ■■ ■■  
 ■■■■■■■  ■■    ■■■■■■■■ ■■ ■■  
 ■■ ■■    ■■    ■■■■■■■■ ■■ ■■  
 ■■ ■■  ■■■■■■     ■■■   ■■ ■■  
 ■■■■■  ■■■■■■    ■■■■■  ■■ ■■  
 ■■ ■■    ■■     ■■ ■■■■ ■■ ■■  
 ■■ ■■    ■■    ■■■ ■■ ■ ■■ ■■  
 ■■ ■■    ■■     ■  ■■      ■■  
 ■■ ■■ ■■■■■■■■     ■■      ■■  
■■ ■■■ ■■■■■■■■     ■■     ■■■  
■■ ■■               ■■     ■■   ");
        }
        /// <summary>
        /// 坐标限制
        /// </summary>
        static void ChangePos()
        {
            if(playerPos[0]<0)
            {
                playerPos[0] = 0;
            }
            if (playerPos[0] >= 99)
            {
                playerPos[0] = 99;
            }
            if (playerPos[1] < 0)
            {
                playerPos[1] = 0;
            }
            if (playerPos[1] >= 99)
            {
                playerPos[1] = 99;
            }
        }
        /// <summary>
        /// 玩游戏
        /// </summary>
        static void playGame(int playerNumber )
        {
            //随机数
            Random r = new Random();
            int rNumber = r.Next(1,7);

            Console.WriteLine("{0}按任意键开始投骰子", playerNames[playerNumber]);
            Console.ReadKey(true);
            Console.WriteLine("{0}掷出了{1}", playerNames[playerNumber], rNumber);
            playerPos[playerNumber] += rNumber;
            ChangePos();//如果不限制 siwtch出界限会崩溃
            Console.ReadKey(true);
            Console.WriteLine("{0}按任意键开始行动", playerNames[playerNumber]);
            Console.ReadKey(true);
            Console.WriteLine("{0}行动完了", playerNames[playerNumber]);
            Console.ReadKey(true);
            //玩家A可能踩到玩家B 方块 幸运轮盘 地雷 暂停 时空隧道
            if (playerPos[playerNumber] == playerPos[1-playerNumber])
            {
                Console.WriteLine("玩家{0}踩到了玩家{1},玩家{2}退6格", playerNames[playerNumber], playerNames[1- playerNumber], playerNames[1- playerNumber]);
                playerPos[1- playerNumber] -= 6;
                Console.ReadKey(true);
            }
            else
            {
                switch (Maps[playerPos[playerNumber]])
                {
                    case 0:
                        Console.WriteLine("玩家{0}踩到了方块,安全", playerNames[playerNumber]);
                        Console.ReadKey(true);
                        break;
                    case 1:
                        Console.WriteLine("玩家{0}踩到了幸运轮盘,请选择1--交换位置 2--轰炸对手", playerNames[playerNumber]);
                        string input = Console.ReadLine();
                        while (true)
                        {
                            if (input == "1")
                            {
                                Console.WriteLine("玩家{0}选择与玩家{1}交换位置", playerNames[playerNumber], playerNames[1- playerNumber]);
                                int temp = playerPos[playerNumber];
                                playerPos[playerNumber] = playerPos[1- playerNumber];
                                playerPos[playerNumber] = temp;
                                Console.WriteLine("交换完成!按下任意键继续游戏");
                                Console.ReadKey(true);
                                break;
                            }
                            else if (input == "2")
                            {
                                Console.WriteLine("玩家{0}选择轰炸玩家{1},玩家{2}退6格", playerNames[playerNumber], playerNames[1- playerNumber], playerNames[1- playerNumber]);
                                Console.ReadKey(true);
                                playerPos[1- playerNumber] -= 6;

                                Console.WriteLine("玩家{0}退了6格!按下任意键继续游戏", playerNames[1- playerNumber]);
                                Console.ReadKey(true);
                                break;
                            }
                            else
                            {
                                Console.WriteLine("只能输入1或者2 1--交换位置 2--轰炸对手");
                                input = Console.ReadLine();
                            }
                        }

                        break;
                    case 2:
                        Console.WriteLine("玩家{0}踩到了地雷,退6格", playerNames[playerNumber]);
                        Console.ReadKey(true);
                        playerPos[playerNumber] -= 6;
                        break;
                    case 3:
                        Console.WriteLine("玩家{0}踩到了暂停,暂停一回合", playerNames[playerNumber]);
                        Flags[playerNumber] = true;
                        Console.ReadKey(true);
                        break;
                    case 4:
                        Console.WriteLine("玩家{0}踩到了时空隧道,前进十格", playerNames[playerNumber]);
                        Console.ReadKey(true);
                        playerPos[playerNumber] += 10;
                        break;

                }//switch
            }//else
            ChangePos();
            Console.Clear();
            DrawMap();
        }


        /// <summary>
        /// 画地图
        /// </summary>
        static void DrawMap()
        {
            Console.WriteLine("图例:幸运轮盘◎   地雷☆   暂停▲   时空隧道※");
            #region 第一横行
            for (int i = 0; i < 30; i++)
            {
                Console.Write(DrawStringMap(i));
            }
            Console.WriteLine();
            #endregion
            #region 第一竖列
            for (int i = 30; i < 35; i++)
            {
                for (int j = 0; j < 29; j++)
                {
                    Console.Write("  ");                   
                }
                Console.Write(DrawStringMap(i));
                Console.WriteLine();
            }
            #endregion
            #region 第二横行
            for (int i = 64; i >=35; i--)
            {
                Console.Write(DrawStringMap(i));
            }
            Console.WriteLine();
            #endregion
            #region 第二竖列
            for (int i = 65; i < 70; i++)
            {
                Console.Write(DrawStringMap(i));
                Console.WriteLine();
            }
            #endregion
            #region 第三横行
            for (int i = 70; i < 100; i++)
            {
                Console.Write(DrawStringMap(i));
            }
            Console.WriteLine();
            #endregion
        }
        /// <summary>
        /// 从画地图方法中抽象出来的一个方法
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        static string DrawStringMap(int i)
        {
 
            string str = "";
            //如果玩家坐标相同,画一个尖括号
            if (playerPos[0] == playerPos[1] && playerPos[1] == i)
            {

                str = "<>";
            }
            else if (playerPos[0] == i)
            {

                str = "A";
            }
            else if (playerPos[1] == i)
            {

                str = "B";
            }
            else
            {
                switch (Maps[i])
                {
                    case 0:
                        Console.ForegroundColor = ConsoleColor.Red;
                        str = "□";
                        break;
                    case 1:
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        str = "◎";
                        break;
                    case 2:
                        Console.ForegroundColor = ConsoleColor.Green;
                        str = "☆";
                        break;
                    case 3:
                        Console.ForegroundColor = ConsoleColor.Blue;
                        str = "▲";
                        break;
                    case 4:
                        Console.ForegroundColor = ConsoleColor.Cyan;
                        str = "※";
                        break;
                }
            }


            return str;
   
        }

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

        /// <summary>
        /// 画游戏头
        /// </summary>
        static void GameShow()
        {
            
            //背景色
            //Console.BackgroundColor = ConsoleColor.Red;
            //前景色 字体颜色
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("**************************");
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("**************************");
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("**********飞行棋**********");
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.WriteLine("*********20220203*********");
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("************肖******V1.0**");
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine("**************************");
        }
    }
}

上一篇:基于C#开发的图书管理系统 书籍借还登记管理统计


下一篇:C# 文本框定位到文本末尾