飞行棋控制台源码(仅作学习讨论)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace ConsoleApplication3
{

    class Program
    {
        /*
             * 再输组中用     1:表示幸运轮盘◎
             *                2:地雷☆
             *                3:暂停▲
             *                4:时空隧道卐
             *                0:普通
             */
        static int[] Map = new int[100];//地图
        static int[] playerPos = { 0, 0 };//playerPos[0]存玩家一的位置,playerPos[1]存玩家二的位置
        static void Main(string[] args)
        {
            
            //r是产生随机数用的
            Random r = new Random();

            //用于存储产生的临时随机数
            int step = 0;
            
            ShowUI();
            string[] names = new string[2];

            bool []isStop = { false, false };//isStop[0]表示A上一次是否走到了暂停,isStop[1]表示B上一次是否走到了暂停,如果走到暂停则设置其值为True。

            string msg = "";//用于存储用户到某关卡输出的话
            Console.WriteLine("请输入玩家A的姓名");
            names[0] = Console.ReadLine();
            while (names[0] == "")
            {
                Console.WriteLine("输入的字符不能为空,请重新输入!");
                names[0] = Console.ReadLine();
            }
            Console.WriteLine("请输入玩家B的姓名");
            names[1] = Console.ReadLine();
            while ((names[1] == "") || (names[1] == names[0]))
            {
                if (names[1] == "")
                {
                    Console.WriteLine("输入的字符不能为空,请重新输入!");
                    names[1] = Console.ReadLine();
                }
                if (names[1] == names[0])
                {
                    Console.WriteLine("不能与玩家A的名字相同,请重新输入!");
                    names[1] = Console.ReadLine();
                }
            }
            Console.Clear();
            ShowUI();
            Console.WriteLine("战斗开始。。。。。。。。");
            Console.WriteLine("{0}用A来表示", names[0]);
            Console.WriteLine("{0}用B来表示", names[1]);
            Console.WriteLine("如果AB在同一未知那么用<>表示");
            InitialMap();//设置关卡
            DrawMap();//绘制地图
            Console.WriteLine("开始游戏。。。。。。");

            //这个循环让玩家A和玩家B轮流掷骰子,当玩家A或者玩家B的坐标>=99时,则循环结束。
            //循环条件就是两个玩家坐标都小于99
            while((playerPos[0]<99)&&(playerPos[1]<99))
            {
                if (isStop[0] == false)
                {

                    #region  玩家A掷骰子
                    Console.WriteLine("{0}按任意键掷骰子。。。。。", names[0]);
                    ConsoleKeyInfo rec = Console.ReadKey(true);
                    if (rec.Key == ConsoleKey.Tab)
                    {
                        step = 20;
                    }
                    else
                    {
                        step = r.Next(1, 7);
                    }
                    Console.WriteLine("{0}掷出了{1}", names[0], step);
                    Console.WriteLine("按任意键开始行动........");
                    Console.ReadKey(true);


                    playerPos[0] += step;//注意一旦坐标发生改变,就要判断坐标值是否>=99或<0。
                    CheckPos();//检测坐标是否越界
                    if (playerPos[0] == playerPos[1])  //玩家A踩到玩家B
                    {
                        playerPos[1] = 0;             //玩家B退到原点
                        msg = string.Format("{0}踩到了{1},{1}退回远点", names[0], names[1]);
                    }
                    else
                    {//判断玩家A是否碰到有功能的关卡
                        switch (Map[playerPos[0]])
                        {
                            case 1:
                                //走到了幸运轮盘关卡
                                Console.Clear();
                                DrawMap();
                                Console.WriteLine("{0}走到了幸运轮盘,请选择运气", names[0]);
                                Console.WriteLine("1----交换位置    2----轰炸");
                                int userSelect = ReadInt(1, 2);
                                if (userSelect == 1)
                                {//交换AB的位置
                                    int temp = playerPos[1];
                                    playerPos[1] = playerPos[0];
                                    playerPos[0] = temp;
                                    msg = string.Format("{0}选择了和对方交换了位置", names[0]);
                                }
                                else
                                {
                                    playerPos[1] = playerPos[1] - 6;
                                    CheckPos();
                                    msg = string.Format("{0}轰炸了{1},{1}退6步", names[0], names[1]);
                                }
                                break;
                            case 2:
                                //走到了地雷
                                playerPos[0] -= 6;
                                CheckPos();
                                msg = string.Format("{0}踩到了地雷退6步", names[0]);
                                break;
                            case 3:
                                //暂停一次
                                isStop[0] = true;
                                msg = string.Format("{0}走到红灯下次暂停一次", names[0]);
                                break;
                            case 4:
                                //时空隧道
                                playerPos[0] += 10;
                                CheckPos();
                                msg = string.Format("{0}进入了时空隧道前进10步", names[0]);
                                break;
                            default:
                                msg = "";
                                break;
                        }
                    }


                    Console.Clear();
                    DrawMap();
                    if (msg != "")
                    {
                        Console.WriteLine(msg);
                    }
                    Console.WriteLine("{0}掷骰子掷出了{1},行动完成!", names[0], step);
                    Console.WriteLine("****************玩家A和玩家B的位置如下****************");
                    Console.WriteLine("{0}的位置为:{1}", names[0], playerPos[0] + 1);
                    Console.WriteLine("{1}的位置为:{1}", names[1], playerPos[1] + 1);
                    Console.ReadKey();

                    #endregion
                }
                else
                {
                    isStop[0] = false;
                }

                if (playerPos[0] >= 99)
                {
                    break;
                }

                if (isStop[1] == false)
                {
                    #region  玩家B掷骰子
                    Console.WriteLine("{0}按任意键掷骰子。。。。。", names[1]);
                    Console.ReadKey(true);
                    step = r.Next(1, 7);
                    Console.WriteLine("{0}掷出了{1}", names[1], step);
                    Console.WriteLine("按任意键开始行动........");
                    Console.ReadKey(true);
                    playerPos[1] += step;//注意一旦坐标发生改变,就要判断坐标值是否>=99或<0。
                    CheckPos();//检测坐标是否越界
                    if (playerPos[1] == playerPos[0])  //玩家B踩到玩家A
                    {
                        playerPos[0] = 0;             //玩家A退到原点
                        msg = string.Format("{0}踩到了{1},{1}退回远点", names[1], names[0]);
                    }
                    else
                    {//判断玩家B是否碰到有功能的关卡
                        switch (Map[playerPos[1]])
                        {
                            case 1:
                                //走到了幸运轮盘关卡
                                Console.Clear();
                                DrawMap();
                                Console.WriteLine("{0}走到了幸运轮盘,请选择运气", names[1]);
                                Console.WriteLine("1----交换位置    2----轰炸");
                                int userSelect = ReadInt(1, 2);
                                if (userSelect == 1)
                                {//交换AB的位置
                                    int temp = playerPos[1];
                                    playerPos[1] = playerPos[0];
                                    playerPos[0] = temp;
                                    msg = string.Format("{0}选择了和对方交换了位置", names[1]);
                                }
                                else
                                {
                                    playerPos[0] = playerPos[0] - 6;
                                    CheckPos();
                                    msg = string.Format("{0}轰炸了{1},{1}退6步", names[1], names[0]);
                                }
                                break;
                            case 2:
                                //走到了地雷
                                playerPos[1] -= 6;
                                CheckPos();
                                msg = string.Format("{0}踩到了地雷退6步", names[1]);
                                break;
                            case 3:
                                //暂停一次
                                isStop[1] = true;
                                msg = string.Format("{0}走到红灯下次暂停一次", names[1]);
                                break;
                            case 4:
                                //时空隧道
                                playerPos[1] += 10;
                                CheckPos();
                                msg = string.Format("{0}进入了时空隧道前进10步", names[0]);
                                break;
                            default:
                                msg = "";
                                break;
                        }
                    }


                    Console.Clear();
                    DrawMap();
                    if (msg != "")
                    {
                        Console.WriteLine(msg);
                    }
                    Console.WriteLine("{0}掷骰子掷出了{1},行动完成!", names[1], step);
                    Console.WriteLine("****************玩家A和玩家B的位置如下****************");
                    Console.WriteLine("{0}的位置为:{1}", names[0], playerPos[0] + 1);
                    Console.WriteLine("{1}的位置为:{1}", names[1], playerPos[1] + 1);
                    Console.ReadKey();
                    #endregion
                }
                else
                {
                    isStop[1] = false;
                }
            }

            //判断谁赢了
            Console.Clear();
            ShowUI();
            if (playerPos[0] >= 99)
            {
                Console.WriteLine("{0}赢了!!!!!!!", names[0]);
            }
            else
            {
                Console.WriteLine("{0}赢了!!!!!!!", names[0]);
            }


            Console.ReadKey();
            
        }

        /// <summary>
        /// 进行玩家A和玩家B的坐标越界的判断
        /// </summary>
        static void CheckPos()
        {
            for (int i = 0; i <= 1; i++)
            {
                if (playerPos[i] > 99)
                {
                    playerPos[i] = 99;
                }

                if (playerPos[i] < 0)
                {
                    playerPos[i] = 0;
                }
            }
        }


        /// <summary>
        /// 用于显示飞行器名称
        /// </summary>
        static void ShowUI()
        {
            Console.WriteLine("**************************************************");
            Console.WriteLine("*                                                *");
            Console.WriteLine("*           骑   士   飞   行   棋               *");
            Console.WriteLine("*                                                *");
            Console.WriteLine("**************************************************");
        }

        /// <summary>
        /// 对地图关卡初始化
        /// </summary>
        static void InitialMap()
        {
            for (int i = 0; i < Map.Length; i++)
            {
                Map[i] = 0;
            }
            int[] luckyTurn = { 6, 23, 40, 55, 69, 93 };//幸运轮盘
            int[] lendMine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 };//地雷
            int[] pause = { 9, 27, 60, 93 };//暂停的坐标
            int[] timeTunnel = { 20, 25, 45, 63, 72, 83, 90 };//时空隧道

            for (int i = 0; i < luckyTurn.Length; i++)
            {
                int pos = luckyTurn[i];
                Map[pos] = 1;
            }
            for (int i = 0; i < lendMine.Length; i++)
            {
                int pos = lendMine[i];
                Map[pos] = 2;
            }
            for (int i = 0; i < pause.Length; i++)
            {
                int pos = pause[i];
                Map[pos] = 3;
            }
            for (int i = 0; i < timeTunnel.Length; i++)
            {
                int pos = timeTunnel[i];
                Map[pos] = 4;
            }
        }

        /// <summary>
        /// 获取pos坐标该绘制的图案
        /// </summary>
        /// <param name="pos">要绘制的坐标</param>
        /// <returns></returns>
        static string GetMapString(int pos)
        {
            //判断A和B是否在第一个要画的格上
            string result = "";
            if (playerPos[0] == pos && playerPos[1] == pos)
            {
                Console.ForegroundColor = ConsoleColor.Magenta;
                result = "<>";
            }
            else if (playerPos[0] == pos) //A在当前格上
            {
                Console.ForegroundColor = ConsoleColor.Magenta;
                result = "A";
            }
            else if (playerPos[1] == pos)  //B在当前格上
            {
                Console.ForegroundColor = ConsoleColor.Magenta;
                result = "B";
            }
            else
            {
                switch (Map[pos])
                {
                    case 0:
                        Console.ForegroundColor = ConsoleColor.Red;
                        result = "□";
                        break;
                    case 1:
                        Console.ForegroundColor = ConsoleColor.Gray;
                        result = "◎";
                        break;
                    case 2:
                        Console.ForegroundColor = ConsoleColor.DarkGreen;
                        result = "☆";
                        break;
                    case 3:
                        Console.ForegroundColor = ConsoleColor.Cyan;
                        result = "▲";
                        break;
                    case 4:
                        Console.ForegroundColor = ConsoleColor.DarkMagenta;
                        result = "卐";
                        break;
                }
            }
            return result;
        }

        /// <summary>
        /// 绘画地图
        /// </summary>
        static void DrawMap()
        {
            //输出图例
            Console.WriteLine("1:幸运轮盘◎   2:地雷☆   3:暂停▲   4:时空隧道卐 ");
            //画第一行
            for (int i = 0; i <= 29; i++)
            {
                Console.Write(GetMapString(i));
            }
            //第一行绘制完毕

            Console.WriteLine();

            //绘制第一列
            for (int i = 30; i <= 34; i++)
            {
                for (int j = 0; j < 29; j++)
                {
                    Console.Write("  ");
                }
                Console.WriteLine(GetMapString(i));
            }
            //第一列绘制完毕

            //画第二行
            for (int i = 64; i >= 35; i--)
            {
                Console.Write(GetMapString(i));
            }
            //第二行绘制完毕
            Console.WriteLine();

            //绘制第二列
            for (int i = 65; i <= 69; i++)
            {
                Console.WriteLine(GetMapString(i));
            }
            //第二列绘制完毕

            //绘制第三行
            for (int i = 70; i <= 99; i++)
            {
                Console.Write(GetMapString(i));
            }
            Console.WriteLine();
            Console.ResetColor();
        }
        
        static int ReadInt()
        {
            int i=ReadInt(int.MinValue,int.MaxValue);
            return i;
        }
        
        static int ReadInt(int min, int max)
        {
            while (true)
            {
                try
                {
                    int number = Convert.ToInt32(Console.ReadLine());
                    if (number < min || number > max)
                    {
                        Console.WriteLine("只能输入{0}到{1}之间的数字,请重新输入:", min, max);
                        continue;
                    }
                    return number;
                }
                catch
                {
                    Console.WriteLine("只能输入数字,请重新输入");
                }
            }
        }
    }
}
 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值