C#---飞行棋

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

namespace ConsoleApp3
{
    class Program
    {
        //用静态字段来模拟全局变量
       static int[] maps = new int[100];
        //声明一个静态数组用来存储玩家A跟玩家B的坐标
        static int[] playerPos = new int[2];
        //存储两个玩家的姓名
        static string[] playerNames = new string[2];
        //两个玩家的标记
        static bool[] flag = new bool[2];//flag[0]默认是false flag[1]默认也是false
        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();
                }
                else
                {
                 Console.WriteLine("玩家A和玩家B不能相同,请重新输入");
                 playerNames[1] = Console.ReadLine();
                }
               
            }
            #endregion
            //玩家姓名输入ok之后,我们首先应该清屏
            Console.Clear();
            GameShow();
            Console.WriteLine("{0}的士兵用A表示",playerNames[0]);
            Console.WriteLine("{0}的士兵用B表示",playerNames[1]);
            InitailMap();
            DrawMap();

            //当玩家跟玩家B没有一个人在终点的适合,两个玩家可以继续玩
            while (playerPos[0] < 99 && playerPos[1] < 99)
            {
                if (flag[0] == false)
                {
                  PlayGame(0);//flag[0]=true
                }
                else
                {
                    flag[0] = false;
                }
                if (playerPos[0]>=99)
                {
                    Console.WriteLine("玩家{0}欢喜的赢了玩家{1}",playerNames[0],playerNames[1]);
                    break;
                }
                if (flag[1] == false)
                { PlayGame(1);

                }
                else
                {
                    flag[1] = false;
                }
                if (playerPos[1] >= 99)
                {
                    Console.WriteLine("玩家{0}欢喜的赢了玩家{1}", playerNames[1], playerNames[0]);
                    break;
                }

            }
            Win();
            Console.ReadKey();
        }
        /// <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();
        }
        /// <summary>
        ///游戏头
        /// </summary>
        public static void GameShow()
        {
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("******************************");
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("******************************");
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine("**********HAHA飞行棋**********");
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("******************************");
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.WriteLine("******************************");
        }
        /// <summary>
        /// 初始化地图
        /// </summary>
        public static void InitailMap()
        {   //幸运轮盘
            int[] luckyturn = {6,23,40,55,69,83 };//maps数组下标
            for (int i = 0; i <luckyturn.Length; 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("图例:幸运轮盘:◎;  地雷:★; 暂停:▲;时空隧道:卍;");
            #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 <=28; 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();
        }
        /// <summary>
        /// 从画地图中抽象出来的一个方法
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        public static String DrawStringMap(int i)
        {
            #region  画图
            String str = " ";
            //如果玩家A和玩家B的坐标相同,并且都在一个地图上,画一个尖括号
            if (playerPos[0] == playerPos[1] && playerPos[0] == i)
            {
                Console.ForegroundColor = ConsoleColor.Magenta;
                str="<>";
            }
            else if (playerPos[0] == i)
            {
                str="A";
            }
            else if (playerPos[1] == i)
            {
                str="B";
            }
            else
            {
                switch (maps[i])
                {
                    case 0: Console.ForegroundColor = ConsoleColor.Gray; str="□"; break;
                    case 1: Console.ForegroundColor = ConsoleColor.Green; str="◎"; break;
                    case 2: Console.ForegroundColor = ConsoleColor.Red; str="★"; break;
                    case 3: Console.ForegroundColor = ConsoleColor.Yellow; str="▲"; break;
                    case 4: Console.ForegroundColor = ConsoleColor.Cyan; str="卍"; break;
                }
            }
            return str;
            #endregion
        }
        /// <summary>
        /// 玩游戏
        /// </summary>
        public 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();
            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;
                ChangePos();
                Console.ReadKey(true);
            }
            else//踩到关卡
            {
                //玩家的坐标
                switch (maps[playerPos[playerNumber]])// 0 1 2 3 4
                {
                    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 = playerPos[playerNumber];
                                playerPos[playerNumber] = playerPos[1 - playerNumber];
                                playerPos[1 - 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;
                                ChangePos();
                                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}踩到了地雷,退六格", playerNames[playerNumber]);
                        Console.ReadKey(true);
                        playerPos[playerNumber] -= 6;
                        ChangePos();
                        break;
                    case 3:
                        Console.WriteLine("玩家{0}踩到了暂停,暂停一回合", playerNames[playerNumber]);
                        flag[playerNumber] = true;
                        Console.ReadKey(true);
                        break;
                    case 4:
                        Console.WriteLine("玩家{0}踩到了时空隧道,前进10格", playerNames[playerNumber]);
                        playerPos[playerNumber] += 10;
                        ChangePos();
                        Console.ReadKey(true);
                        break;
                }
            }
            ChangePos();
            Console.Clear();
            DrawMap();
        }
        /// <summary>
        /// 当玩家坐标发生改变的时候调用
        /// </summary>
        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;
            }
        }
    }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值