编程语言
首页 > 编程语言> > 学习的老赵的C#飞行棋例子代码 CSharp

学习的老赵的C#飞行棋例子代码 CSharp

作者:互联网

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

/*
 * 游戏规则:
 * 1、如果玩家A踩到了玩家B,玩家B退6格;
 * 2、踩到了地雷,退6格;
 * 3、踩到了时空隧道,进10格;
 * 4、踩到了幸运轮盘,1-交换位置,2-使对方退6格;
 * 5、踩到了暂停,暂停一回合;
 * 6、踩到了方块,什么都不干。
 */


namespace飞行棋
{
    class Program
    {
        // 用静态字段来模拟全局变量
        static int[] Maps = new int[100];
        // 声明一个静态数组来存储玩家A和玩家B的坐标
        static int[] PlayerPositions = new int[2];
        // 声明一个静态数组来存储玩家A和玩家B是否暂停
        static bool[] FlagOfPause = new bool[2];  // bool数组的默认值全是false
        // 声明一个静态数组来存储玩家A和玩家的名字
        static string[] PlayerNames = new string[2];
        // 声明是否游戏结束的一个Flag变量,
        // 值为-1时,游戏尚未结束;
        // 值为0时,游戏结束,胜利者为PlayerNames[0]
        // 值为1时,游戏结束,胜利者为PlayerNames[1]
        static int WinnerNumber = -1;



        static void Main(string[] args)
        {
            GameShow();
            #region 输入玩家的姓名
            Console.ForegroundColor = ConsoleColor.White;
            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的姓名不能为空,请重新输入玩家B的姓名:");
                }
                else
                {
                    Console.WriteLine("玩家B的姓名不能与玩家A的姓名一样,请重新输入玩家B的姓名:");
                }
                PlayerNames[1] = Console.ReadLine();
            }
            #endregion
            // 玩家姓名输入Ok之后,我们首先应该清屏
            Console.Clear();  // 清屏
            GameShow();
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("{0}的士兵用A表示\r\n{1}的士兵用B表示", PlayerNames[0], PlayerNames[1]);
            InitialMap();
            DrawMap();
            #region 游戏开始
            // 当玩家A跟玩家B没有一个人到达终点的时候,两个玩家不停地去玩游戏
            while (PlayerPositions[0] < 99 && PlayerPositions[1] < 99)
            {
                if (FlagOfPause[0])
                {
                    FlagOfPause[0] = false;
                }
                else
                {
                    PlayGameOnce(0);
                }
                if (PlayerPositions[0] >= 99)
                {
                    //Console.WriteLine("玩家{0}已经到达了终点", PlayerNames[0]);
                    continue;
                }

                if (FlagOfPause[1])
                {
                    FlagOfPause[1] = false;
                }
                else
                {
                    PlayGameOnce(1);
                }
                //if (PlayerPositions[0] >= 99)
                //{
                //    Console.WriteLine("玩家{0}已经到达了终点", PlayerNames[0]);
                //    continue;
                //}
            }  // while()
            if (Program.WinnerNumber > -1)
            {
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine("{0}无耻地战胜了{1}", PlayerNames[WinnerNumber], PlayerNames[1 - WinnerNumber]);
                Console.WriteLine();
                Console.WriteLine("C#牛逼,十项全能,天下第一!");
                Console.WriteLine("C#牛逼,十项全能,天下第一!");
                Console.WriteLine("C#牛逼,十项全能,天下第一!");
            }
            #endregion
            Console.WriteLine("按回车键结束");
            Console.ReadLine();
        }  // Main方法结尾

        /// <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.Cyan;
            Console.WriteLine("*******玉田新村の飞行棋游戏*******");
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.WriteLine("**********************************");
            Console.ForegroundColor = ConsoleColor.Magenta;
            Console.WriteLine("**********************************");
            Console.ForegroundColor = ConsoleColor.DarkRed;
            Console.WriteLine("**********************************");
            Console.WriteLine();
        }  // GameShow()方法结尾


        /// <summary>
        /// 初始化地图
        /// </summary>
        public static void InitialMap()
        {
            int[] luckyTurn = new int[] { 6, 23, 40, 55, 69, 83 };  // 幸运轮盘1 ◎
            foreach (int index in luckyTurn)
            {
                Maps[index] = 1;
            }
            int[] landMind = new int[] { 5, 13, 17, 33, 38, 50, 64, 80, 94 };  // 地雷2 ☆

            for (int i = 0; i < landMind.Length; i++)
            {
                Maps[landMind[i]] = 2;
            }
            int[] pause = new int[] { 9, 27, 60, 93 };  // 暂停3 ▲
            for (int i = 0; i < pause.Length; i++)
            {
                Maps[pause[i]] = 3;
            }
            int[] timeTunnel = new int[] { 20, 25, 45, 63, 72, 88, 90 };  // 时空隧道4 卍
            for (int i = 0; i < timeTunnel.Length; i++)
            {
                Maps[timeTunnel[i]] = 4;
            }
        }

        /// <summary>
        /// 绘制地图
        /// </summary>
        public static void DrawMap()
        {
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("图例:幸运轮盘 ◎,地雷 ☆,暂停 ▲,超时空隧道 卍");
            Console.WriteLine();
            #region 第一横行
            for (int i = 0; i < 30; i++)
            {
                Console.Write(StringOfPosition(i));
            }
            Console.WriteLine();
            #endregion
            #region 第一竖列
            for (int i = 30; i < 35; i++)
            {
                for (int j = 0; j < 29; j++)
                {
                    Console.Write(" ");
                }
                Console.WriteLine(StringOfPosition(i));
            }
            #endregion
            #region 第二横行
            for (int i = 64; i > 34; i--)
            {
                Console.Write(StringOfPosition(i));
            }
            Console.WriteLine();
            #endregion
            #region 第二竖列
            for (int i = 65; i < 70; i++)
            {
                Console.WriteLine(StringOfPosition(i));
            }
            #endregion
            #region 第三横行
            for (int i = 70; i < 100; i++)
            {
                Console.Write(StringOfPosition(i));
            }
            Console.WriteLine();
            #endregion
            Console.WriteLine();
        }

        /// <summary>
        /// 返回地图上某一位置上的关卡
        /// </summary>
        /// <param name="i">地图的位置</param>
        /// <returns>从0开始,第i个位置上代表关卡的字符</returns>
        public static string StringOfPosition(int i)
        {
            string str = null;
            // 如果玩家A跟玩家B的坐标相同,并且都在地图上。画一个尖括号
            if (PlayerPositions[0] == PlayerPositions[1] && PlayerPositions[0] == i)
            {
                Console.ForegroundColor = ConsoleColor.Cyan;
                str = "<>";
            }
            else if (PlayerPositions[0] == i)
            {
                // shift+空格
                Console.ForegroundColor = ConsoleColor.Cyan;
                str = "A";
            }
            else if (PlayerPositions[1] == i)
            {
                Console.ForegroundColor = ConsoleColor.Cyan;
                str = "B";
            }
            else
            {
                switch (Maps[i])
                {
                    case 0:
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        str = "□";
                        break;
                    case 1:
                        Console.ForegroundColor = ConsoleColor.Green;
                        str = "◎";
                        break;
                    case 2:
                        Console.ForegroundColor = ConsoleColor.Red;
                        str = "☆";
                        break;
                    case 3:
                        Console.ForegroundColor = ConsoleColor.Blue;
                        str = "▲";
                        break;
                    case 4:
                        Console.ForegroundColor = ConsoleColor.DarkMagenta;
                        str = "卍";
                        break;

                } // switch
            } // else
            return str;
        }  // DrawOnePositin()

        public static void JudgeInNewPosition(int playerNumber)
        {
            if (PlayerPositions[playerNumber] < 0)
            {
                Console.WriteLine("玩家{0}不能退出到地图之外,返回到出发的位置", PlayerNames[playerNumber]);
                PlayerPositions[playerNumber] = 0;
            }
            else if (PlayerPositions[playerNumber] >= 99)
            {
                WinnerNumber = playerNumber;
                Console.WriteLine("{0}已经到达了终点", PlayerNames[playerNumber]);
                PlayerPositions[playerNumber] = 99;
                //return;
            }
            else if (PlayerPositions[playerNumber] == PlayerPositions[1 - playerNumber])
            {
                Console.WriteLine("玩家{0}踩到了玩家{1},玩家{1}退6格", PlayerNames[playerNumber], PlayerNames[1 - playerNumber]);
                PlayerPositions[1 - playerNumber] -= 6;
                JudgeInNewPosition(1 - playerNumber);


            }
            else //踩到了关卡
            {
                switch (Maps[PlayerPositions[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]);
                                Console.ReadKey(true);
                                int temp;
                                temp = PlayerPositions[playerNumber];
                                PlayerPositions[playerNumber] = PlayerPositions[1 - playerNumber];
                                PlayerPositions[1 - playerNumber] = temp;
                                Console.WriteLine("交换完成,按任意键继续游戏");
                                Console.ReadKey(true);
                                break;
                            }
                            else if (input == "2")
                            {
                                Console.WriteLine("玩家{0}选择轰炸玩家{1},玩家{1}退6格", PlayerNames[playerNumber], PlayerNames[1 - playerNumber]);
                                Console.ReadKey(true);
                                PlayerPositions[1 - playerNumber] -= 6;
                                //Console.WriteLine("玩家{0}退了6格", PlayerNames[1 - playerNumber]);
                                JudgeInNewPosition(1 - playerNumber);
                                Console.ReadKey(true);
                                break;
                            }
                            else
                            {
                                Console.WriteLine("只能输入1或者2,请重新选择  1--交位置,2--轰炸对方");
                                input = Console.ReadLine();
                            }
                        }//while
                        break;
                    case 2:
                        Console.WriteLine("玩家{0}踩到了地雷,退6格", PlayerNames[playerNumber]);
                        PlayerPositions[playerNumber] -= 6;
                        Console.ReadKey(true);
                        JudgeInNewPosition(playerNumber);
                        break;
                    case 3:
                        Console.WriteLine("玩家{0}踩到了暂停,暂停一回合", PlayerNames[playerNumber]);
                        FlagOfPause[playerNumber] = true;
                        Console.ReadKey(true);
                        break;
                    case 4:
                        Console.WriteLine("玩家{0}踩到了时空隧道,前进10格", PlayerNames[playerNumber]);
                        PlayerPositions[playerNumber] += 10;
                        Console.ReadKey(true);
                        JudgeInNewPosition(playerNumber);
                        break;
                }//switch
            }//else
        }

        /// <summary>
        /// 玩游戏
        /// </summary>
        public static void PlayGameOnce(int playerNumber)
        {
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("玩家{0}按任意键开始掷骰子", PlayerNames[playerNumber]);
            Console.ReadKey(true);
            Random r = new Random();
            int steps = r.Next(1, 7);
            Console.WriteLine("玩家{0}掷骰子掷出了{1}", PlayerNames[playerNumber], steps);
            Console.WriteLine("玩家{0}按任意键开始行动", PlayerNames[playerNumber]);
            Console.ReadKey(true);
            PlayerPositions[playerNumber] += steps;
            JudgeInNewPosition(playerNumber);
            Console.WriteLine("玩家{0}行动完了", PlayerNames[playerNumber]);
            Console.ReadKey(true);
            Console.Clear();
            DrawMap();
        }
    }
}

标签:Console,C#,飞行棋,玩家,int,WriteLine,CSharp,playerNumber,PlayerNames
来源: https://blog.csdn.net/Mahy158/article/details/116781221