C#基础知识---飞行棋小游戏

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

namespace 飞行棋
{
    class Program
    {
        //静态数组模拟全局变量,地图,100
        static int[] Maps = new int[100];   //字段
        //玩家的位置
        static int[] PlayerPos = new int[2];       //字段
        //玩家的名字
        static string[] PlayerName = new string[2];   //字段
        //Flags为true,跳过一回合
        static bool[] Flags = new bool[2];         //字段

        static void Main(string[] args)
        {
            GameShow();
            #region 输入玩家姓名
            Console.WriteLine("请输入玩家A的姓名");
            PlayerName[0] = Console.ReadLine();
            while (PlayerName[0] == "")
            {
                Console.WriteLine("玩家A姓名不能为空");
                Console.ReadLine();
            }

            Console.WriteLine("请输入玩家B的姓名");
            PlayerName[1] = Console.ReadLine();
            while (PlayerName[1] == "" || PlayerName[0] == PlayerName[1])
            {
                if (PlayerName[1] == "")
                {
                    Console.WriteLine("玩家B姓名不能为空");
                }
                else
                {
                    Console.WriteLine("玩家A和玩家B姓名不能相同");
                }
                Console.ReadLine();
            }
            #endregion
            //输完姓名清屏
            Console.Clear();

            InitailMap();
            DrawMap();
            #region         //玩游戏
            while ((PlayerPos[0] < 99) && (PlayerPos[1] < 99))
            {
                if (Flags[0] == false)
                {
                    PlayGame(0);
                }
                else
                {
                    //如果踩到了暂停,停一回合,把Flags设为假,下回合接着玩
                    Flags[0] = false;
                }
                if (PlayerPos[0] >= 99)
                {
                    Console.WriteLine("{0}赢了", PlayerName[0]);
                }
                if (Flags[1] == false)
                {
                    PlayGame(1);
                }
                else
                {
                    Flags[1] = false;
                }

                if (PlayerPos[1] >= 99)
                {
                    Console.WriteLine("{0}赢了", PlayerName[1]);
                }
            }
            #endregion
            Console.WriteLine();
            Console.ReadKey();
        }
        //游戏头
        public static void GameShow()
        {
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("******************飞行棋******************");

        }
        //初始化地图,即初始化数组
        public static void InitailMap()
        {
            int[] luckyTurn = { 5, 13, 17, 33, 6, 23, 40, 55, 69, 83 };
            for (int i = 0; i < luckyTurn.Length; i++)
            {
                Maps[luckyTurn[i]] = 1;
            }

            int[] landMine = { 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;
            }
        }
        //画地图,画出方块和关卡
        public static void DrawMap()
        {
            Console.WriteLine("{0}的士兵用A表示", PlayerName[0]);
            Console.WriteLine("{0}的士兵用B表示", PlayerName[1]);
            Console.WriteLine("图例:幸运轮盘○,地雷☆,暂停△,时空隧道卍");

            #region 第一横行
            for (int i = 0; i < 30; i++)
            {
                Console.Write(DrawStringMap(i));
            }
            #endregion

            //画完第一横行换行
            Console.WriteLine();
            #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));
            }
            #endregion

            //画完第二横行换行
            Console.WriteLine();

            #region 第二竖行
            for (int i = 65; i <= 69; i++)
            {
                Console.WriteLine(DrawStringMap(i));
            }
            #endregion
            #region 第三横行
            for (int i = 70; i <= 99; i++)
            {
                Console.Write(DrawStringMap(i));
            }
            #endregion

            //画完第三横行换行
            Console.WriteLine();
        }
        //画关卡 返回一个关卡
        public static string DrawStringMap(int i)
        {
            string str = "";
            //玩家A踩到了玩家B,玩家A和玩家B位置相同,刚开始在同一位置
            if (PlayerPos[0] == PlayerPos[1] && PlayerPos[0] == 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.Yellow;
                        str = "□";
                        break;
                    case 1:
                        Console.ForegroundColor = ConsoleColor.Red;
                        str = "○";
                        break;
                    case 2:
                        Console.ForegroundColor = ConsoleColor.White;
                        str = "★";
                        break;
                    case 3:
                        Console.ForegroundColor = ConsoleColor.Blue;
                        str = "△";
                        break;
                    case 4:
                        Console.ForegroundColor = ConsoleColor.Green;
                        str = "卍";
                        break;
                }
            return str;
        }
        //玩游戏
        public static void PlayGame(int playNumber)
        {
            Random r = new Random();
            int rNumber = r.Next(1, 7);
            Console.WriteLine("{0}按任意键开始掷骰子", PlayerName[playNumber]);
            Console.ReadKey(true);
            Console.WriteLine("{0}掷出了{1}点", PlayerName[playNumber], rNumber);
            PlayerPos[playNumber] += rNumber;
            Console.ReadKey(true);
            Console.WriteLine("{0}按任意键开始行动", PlayerName[playNumber]);
            Console.ReadKey(true);
            Console.WriteLine("{0}行动完了", PlayerName[playNumber]);
            Console.ReadKey(true);

            if (PlayerPos[playNumber] == PlayerPos[1 - playNumber])
            {
                Console.WriteLine("玩家{0}踩到了玩家{1},玩家{2}退六格", PlayerName[playNumber], PlayerName[1 - playNumber], PlayerName[1 - playNumber]);
                PlayerPos[1 - playNumber] -= 6;
            }
            else
            {
                switch (Maps[PlayerPos[playNumber]])
                {
                    case 0: Console.WriteLine("{0}踩到了方块,安全", PlayerName[playNumber]);
                        Console.ReadKey(true);
                        break;
                    case 1: Console.WriteLine("{0}踩到了幸运轮盘,选择1交换位置,2轰炸对方", PlayerName[playNumber]);
                        string input = Console.ReadLine();
                        while (true)
                        {
                            if (input == "1")
                            {
                                Console.WriteLine("{0}选择跟{1}交换位置", PlayerName[playNumber], PlayerName[1 - playNumber]);
                                Console.ReadKey(true);
                                int temp = PlayerPos[playNumber];
                                PlayerPos[playNumber] = PlayerPos[1 - playNumber];
                                PlayerPos[1 - playNumber] = temp;
                                Console.WriteLine("交换成功");
                                break;
                            }
                            else if (input == "2")
                            {
                                Console.WriteLine("{0}选择轰炸{1}", PlayerName[playNumber], PlayerName[1 - playNumber]);
                                Console.ReadKey(true);
                                PlayerPos[1 - playNumber] -= 6;
                                Console.WriteLine("{0}退了六格", PlayerName[1 - playNumber]);
                                Console.ReadKey(true);
                                break;
                            }
                            else
                            {
                                Console.WriteLine("只能输入1或2");
                                input = Console.ReadLine();
                            }
                        }
                        break;

                    case 2: Console.WriteLine("{0}踩到了地雷,退六格", PlayerName[playNumber]);
                        Console.ReadKey(true);
                        PlayerPos[playNumber] -= 6;
                        break;
                    case 3: Console.WriteLine("{0}踩到了暂停,跳过一回合", PlayerName[playNumber]);
                        Flags[playNumber] = true;
                        Console.ReadKey(true);
                        break;

                    case 4: Console.WriteLine("{0}踩到了时空隧道,前进十格", PlayerName[playNumber]);
                        Console.ReadKey(true);
                        PlayerPos[playNumber] += 10;
                        break;
                    default:
                        break;
                }
            }
            ChangePos();
            Console.Clear();
            DrawMap();
        }
        //限定位置,防止出界
        public 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;
            }
        }
    }
}

你可能感兴趣的:(C#,C#)