C# 飞行棋Demo

using System;

namespace My_First_demo
{
    class Program
    {
        public static int[] maps = new int[100];           // 地图集
        public static int[] indexs = new int[2] { -1,-1};  // 保存两位玩家的索引
        public static string[] names = new string[2];      // 保留两位玩家的名字
        // 0表示普通方块,1表示幸运轮盘,2表示地雷,3表示暂停,4表示时空隧道     
        static int[] luckytrun = { 6, 23, 40, 55, 69, 83 };           // 幸运轮盘    
        static int[] landMine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 };// 地雷     
        static int[] pause = { 9, 27, 60, 93 };                       // 暂停     
        static int[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 };     // 时空隧道  

        static void Main(string[] args)
        {
            GameShow();
            InitMap();
            ReadName();
            Console.Clear();
            GameShow();
            ShowMap();
            Start();
            Console.ReadKey();
        }

        /// <summary>
        /// 流程控制,两位玩家轮流行动
        /// </summary>
        public static void Start()
        {
            int temp = 0;
            while (true)
            {
                int flag = 0;
                if (indexs[0] >= 99) {
                    Console.WriteLine("\n{0}无耻的赢了{1}", names[0], names[1]); 
                }
                if (indexs[1] >= 99) {
                    Console.WriteLine("\n{0}无耻的赢了{1}", names[1], names[0]); 
                }
                if (indexs[0] >= 99 || indexs[1] >= 99) break;
                if (temp == 0)
                {
                    Crap(temp);
                    Condition(temp);
                    temp = 1;
                    Console.ReadKey();
                    Console.Clear();
                    GameShow();
                    ShowMap();
                }
                else if (temp == 1)
                {
                    Crap(temp);
                    Condition(temp);
                    temp = 0;
                    Console.ReadKey();
                    Console.Clear();
                    GameShow();
                    ShowMap();
                }
            }
        }

        /// <summary>
        /// 踩到特殊关卡的处理方法
        /// </summary>
        /// <param name="temp"></param>
        public static void Condition(int temp)
        {
            int flag = Array.IndexOf(luckytrun, indexs[temp]);
            if (flag != -1)
            {
                Console.WriteLine("进入幸运轮盘,[1:交换位置,2:轰炸对方]");
                while (true)
                {
                    Console.Write("请选择:");
                    string a = Console.ReadLine();
                    if (a == "1")
                    {
                        int _t = indexs[1];
                        indexs[1] = indexs[0];
                        indexs[0] = _t;
                        break;
                    }
                    else if (a == "2")
                    {
                        indexs[temp == 0 ? 1 : 0] -= 3;
                        Console.WriteLine("玩家{0}被轰炸,后退了三步", names[temp == 0 ? 1 : 0]);
                        break;
                    }
                    else
                    {
                        Console.WriteLine("请输入正确的选项");
                    }
                }
            }
            flag = Array.IndexOf(landMine, indexs[temp]);
            if (flag != -1)
            {
                Console.WriteLine("{0}踩中了地雷,后退三步", names[temp]);
                indexs[temp] -= 3;
            }
            flag = Array.IndexOf(timeTunnel, indexs[temp]);
            if (flag != -1)
            {
                Console.WriteLine("{0}进入了时空隧道,biu~的一下跑远了", names[temp]);
                if (flag != timeTunnel.Length - 1)
                    indexs[temp] = timeTunnel[flag + 1];
                else indexs[temp] = indexs[temp];
            }
            flag = Array.IndexOf(pause, indexs[temp]);
            if (flag != -1)
            {
                Console.WriteLine("{0}踩到了暂停陷阱,被限制行动一回合", names[temp]);
                Console.ReadKey();
                Console.Clear();
                GameShow();
                ShowMap();
                Crap(temp == 0 ? 1 : 0);
                Condition(temp == 0 ? 1 : 0);
                Console.ReadKey();
                Console.Clear();
                GameShow();
                ShowMap();
            }
        }

        /// <summary>
        /// 掷骰子
        /// </summary>
        /// <param name="key"></param>
        public static void Crap(int key)
        {
            Console.WriteLine("\n玩家{0}按任意键开始掷骰子", names[key]);
            Console.ReadKey();
            Random random = new Random();
            int num = random.Next(1, 7);
            Console.WriteLine("玩家{0}掷出了{1}点", names[key],num);
            Console.WriteLine("玩家{0}按任意键开始行动", names[key]);
            Console.ReadKey();
            Move(key, num);
            Console.WriteLine("玩家{0}行动完了", names[key]);
            Console.WriteLine("按任意键继续");
        }

        /// <summary>
        /// 具体的移动
        /// </summary>
        /// <param name="key"></param>
        /// <param name="step"></param>
        public static void Move(int key,int step)
        {
            indexs[key] += step;
            if (indexs[key] >= 99) indexs[key] = 99;
        }

        /// <summary>
        /// 显示游戏欢迎页
        /// </summary>
        public static void GameShow()
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("**********************");
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("**********************");
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.WriteLine("**********************");
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("***飞行棋游戏 v 1.0***");
            Console.ForegroundColor = ConsoleColor.DarkGray;
            Console.WriteLine("**********************");
            Console.ForegroundColor = ConsoleColor.DarkBlue;
            Console.WriteLine("**********************");
            Console.ForegroundColor = ConsoleColor.DarkYellow;
            Console.WriteLine("**********************");
        }

        /// <summary>
        /// 初始化地图
        /// </summary>
        public static void InitMap()
        {
            int len = luckytrun.Length;
            for(int i = 0;i < len; i++) maps[luckytrun[i]] = 1;
            len = landMine.Length;
            for (int i = 0; i < len; i++) maps[landMine[i]] = 2;
            len = pause.Length;
            for (int i = 0; i < len; i++) maps[pause[i]] = 3;
            len = timeTunnel.Length;
            for (int i = 0; i < len; i++) maps[timeTunnel[i]] = 4;
        }

        /// <summary>
        /// 显示地图
        /// </summary>
        public static void ShowMap()
        {
            int[] arr = new int[30];
            int j = 29;
            Console.WriteLine(names[0] + "的士兵用A表示");
            Console.WriteLine(names[1] + "的士兵用B表示");
            Console.WriteLine("○幸运轮盘,●地雷,▲暂停,※时空隧道");
            for (int i = 0;i < maps.Length; i++)
            {
                if (i < 30) Console.Write(Draw(maps[i],i));
                if (i >= 30 && i < 35) Console.Write("\n\t\t\t\t\t\t\t--" + Draw(maps[i], i));
                if (i >= 35 && i < 65)
                {
                    arr[j--] = maps[i];
                    if(i == 64)
                    {
                        Console.WriteLine();
                        int _t = 64;
                        for (int k = 0; k < arr.Length; k++) Console.Write(Draw(arr[k], _t--));
                    }
                }
                if (i >= 65 && i < 70) Console.Write("\n" + Draw(maps[i], i));
                if (i >= 70 && i < 100)
                {
                    if (i == 70) Console.WriteLine();
                    Console.Write(Draw(maps[i], i));
                }
            }
        }

        /// <summary>
        /// 画单个格子
        /// </summary>
        /// <param name="i"></param>
        /// <param name="j"></param>
        /// <returns></returns>
        public static string Draw(int i,int j)
        {
            if (indexs[0] == indexs[1] && indexs[0] == j) return "<>";
            else if (indexs[0] == j) return "A";
            else if (indexs[1] == j) return "B";
            else
            {
                if (i == 0)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    return "□";
                }
                else if (i == 1)
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    return "○";
                }
                else if (i == 2)
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    return "●";
                }
                else if (i == 3)
                {
                    Console.ForegroundColor = ConsoleColor.DarkCyan;
                    return "▲";
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.DarkMagenta;
                    return "※";
                }
            }
        }

        /// <summary>
        /// 接收控制台输入,保存玩家的名字
        /// </summary>
        public static void ReadName()
        {
            Console.Write("请输入玩家A的姓名:");
            while (true)
            {
                names[0] = Console.ReadLine();
                if (names[0].Length < 1) Console.WriteLine("玩家输入名字长度不能为空");
                else break;
            }

            Console.Write("\n请输入玩家B的姓名:");
            while (true)
            {
                names[1] = Console.ReadLine();
                if (names[1].Length < 1) Console.WriteLine("玩家输入名字长度不能为空");
                else if (names[1] == names[0]) Console.WriteLine("玩家B名字不能和玩家A相同");
                else break;
            }
            indexs[0] = 0;
            indexs[1] = 0;
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值