首页 > 编程 > C# > 正文

C# 骑士飞行棋的源码(分享)

2020-01-24 03:22:22
字体:
来源:转载
供稿:网友

代码如下所示:

复制代码 代码如下:

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


namespace 骑士飞行棋
{
    class Program
    {
        //在下面的数组存储我们游戏地图各各关卡
        //数组的下标为0的元素对应地图上的第1格    下标为1的元素对应元素第2格...下标为n的元素对应n+1格
        //在数组中   1:表示幸运轮盘  ◎
        //           2: 表示地雷 ☆
        //           3: 表示暂停 ▲
        //           4: 表示时空隧道
        //           0: 表示普通  □
        static int[] map = new int[100];

        static string[] names = new string[2]; //names[0]存储玩家A的姓名    name[1]存玩家B的姓名

        static int[] playerPos = { 0, 0 };//playPos[0]存玩家A的位置,playPos[1]存玩家B的位置

        static int step = 0; //用于存放产生的随机数

        static string input = ""; //用户存储用户的输入

        static string msg = ""; //用于存储用户踩到某个关卡,输出的话

        static bool[] isStop = { false, false };//isStop[0]表示玩家A是否上次一走到暂停,似的话为true,不是为false

        static Random r = new Random();//r是产生的随机数

        static void Main( string[] args)
        {
           


            ShowUI(); //显示游戏
            InitialName();
            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时,则循环结束
            while (playerPos[0] < 99 && playerPos[1] < 99)
            {
                Action(0);//A掷筛子
                Action(1);//B掷筛子 
            }
            Console.ReadKey();
        }

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

        static void InitialName()
        {
            Console.WriteLine("请输入玩家A的姓名");
            names[0] = Console.ReadLine();
            //判断用书输入的内容是否为空,如果为空,则让用户重新输入
            while (names[0] == "")
            {
                Console.WriteLine("玩家A的姓名不能为空,请重新输入!");
                names[0] = Console.ReadLine();
            }
            Console.WriteLine("请输入玩家B的姓名");
            names[1] = Console.ReadLine();
            //判断用户输入的内容是否为空,如果为空,则让用户重新输入
            while (names[1] == "" || names[1] == names[0])
            {
                if (names[1] == "")
                {
                    Console.WriteLine("玩家B的姓名不能为空,请重新输入!");
                    names[1] = Console.ReadLine();
                }
                else
                {
                    Console.WriteLine("你输入的姓名与玩家A的姓名{0}相同,请重新输入", names[0]);
                    names[1] = Console.ReadLine();
                }

            }
        }

        static void InitialMap()
        {
            //用于存储在地图中为地雷的下标
            int[] luckyTurn = { 6, 23, 40, 55, 69, 83 };//幸运轮盘 1
            int[] landMine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 };//地雷 2
            int[] pause = { 9, 27, 60, 93 };//暂停的坐标 3
            int[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 };//时空隧道  4

            for (int i = 0; i < 100; i++) // 初始化map数组的数据
                map[i] = 0;

            //把幸运轮盘位置填入map中
            for (int i = 0; i < luckyTurn.Length; i++)
                map[luckyTurn[i]] = 1;
            //把地雷填入map中
            for (int i = 0; i < landMine.Length; i++)
                map[landMine[i]] = 2;
            //把暂停填入map中
            for (int i = 0; i < pause.Length; i++)
                map[pause[i]] = 3;
            //把时空隧道填入map中
            for (int i = 0; i < timeTunnel.Length; i++)
                map[timeTunnel[i]] = 4;
        }

        /// <summary>
        ///  获得第pos坐标上应该绘制的图案
        /// </summary>
        /// <param name="pos">要绘制的坐标</param>
        /// <returns></returns>
        static string getMapString(int pos)
        {
            string result = "";
            if (playerPos[0] == pos && playerPos[1] == pos)
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                result = "<>";
            }
            else if (playerPos[0] == pos)
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                result = "A";
            }
            else if (playerPos[1] == pos)
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                result = "B";
            }
            else
            {
                switch (map[pos])
                {
                    case 0:
                        Console.ForegroundColor = ConsoleColor.White;
                        result = "□";
                        break;
                    case 1:
                        Console.ForegroundColor = ConsoleColor.Red;
                        result = "◎";
                        break;
                    case 2:
                        Console.ForegroundColor = ConsoleColor.Green;
                        result = "☆";
                        break;
                    case 3:
                        Console.ForegroundColor = ConsoleColor.Blue;
                        result = "▲";
                        break;
                    case 4:
                        Console.ForegroundColor = ConsoleColor.DarkBlue;
                        result = "";
                        break;
                }
            }
            return result;
        }

        static void drawMap()
        {
            Console.WriteLine("图例:幸运轮盘:◎   地雷:☆   暂停:▲     时空隧道:  ");
            //画第一行
            for (int i = 0; i < 30; i++)
                Console.Write(getMapString(i));
            Console.WriteLine();

            //左边的第一列
            for (int i = 30; i < 35; i++)
            {
                for (int j = 0; j < 29; j++)
                    Console.Write(" ");
                Console.Write(getMapString(i));
                Console.WriteLine();
            }

            //第二行
            for (int i = 64; i >= 35; i--)
                Console.Write(getMapString(i));
            Console.WriteLine();

            //右边的列
            for (int i = 65; i < 70; i++)
            {
                Console.Write(getMapString(i));
                Console.WriteLine();
            }

            //第三行
            for (int i = 70; i < 100; i++)
                Console.Write(getMapString(i));
            Console.ResetColor();
            Console.WriteLine();
        }

        static void checkPos()
        {
            for (int i = 0; i <= 1; i++)
            {
                if (playerPos[i] > 99)
                {
                    playerPos[i] = 99;
                }
                if (playerPos[i] < 0)
                {
                    playerPos[i] = 0;
                }
            }
        }

        static int ReadInt()//产生一个整数
        {
            int i = ReadInt(int.MaxValue, int.MinValue);
            return i;
        }

        static int ReadInt(int min, int max)//产生min--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("只能输入数字,请重新输入!");
                }

            }

        }

        /// <summary>
        /// A或者B掷筛子的方法
        /// </summary>
        /// <param name="playerNumber">A掷筛子传0过来   B掷筛子传1过来</param>
        static void Action(int playerNumber)
        {
            if (isStop[playerNumber] == false)
            {
                Console.WriteLine("{0}按任意键开始掷筛子......", names[playerNumber]);
                ConsoleKeyInfo sec = Console.ReadKey(true);
                step = r.Next(1, 7);//产生一个1到6之间的随机数
                if (sec.Key == ConsoleKey.Tab)
                {
                    ConsoleKeyInfo sec1 = Console.ReadKey(true);
                    if (sec1.Key == ConsoleKey.F1)
                    {
                        step = ReadInt(1, 100);
                    }
                }

                Console.WriteLine("{0}掷出了{1}", names[playerNumber], step);
                Console.WriteLine("{0}按任意键开始行动......", names[playerNumber]);
                Console.ReadKey(true);
                playerPos[playerNumber] += step; //注意,一旦坐标发生改变,就要判断坐标值是否>99||<0
                checkPos();//检查坐标是否越界

                if (playerPos[playerNumber] == playerPos[1 - playerNumber]) //玩家A采到玩家B
                {
                    playerPos[1 - playerNumber] = 0;
                    msg = string.Format("{0}踩到了{1},{1}退回了原点", names[playerNumber], names[1 - playerNumber]);
                }
                else
                {//没踩到,要判断玩家A现在所在的位置是否有其他关卡
                    switch (map[playerPos[playerNumber]])
                    {
                        case 0:
                            //普通,没有效果
                            msg = "";
                            break;
                        case 1:
                            //走到了 幸运轮盘关卡
                            Console.Clear();
                            Console.WriteLine("你走到了幸运轮盘,请选择运气?");
                            Console.WriteLine("1 ---交换位置  2---轰炸对方");
                            int userSelect = ReadInt(1, 2);
                            if (userSelect == 1)
                            {//要与对方交换位置
                                int temp = playerPos[playerNumber];
                                playerPos[playerNumber] = playerPos[1 - playerNumber];
                                playerPos[1 - playerNumber] = temp;
                                msg = string.Format("{0}选了与对方交换位置", names[playerNumber]);
                            }
                            else
                            {//轰炸对方
                                playerPos[1 - playerNumber] -= 6;
                                msg = string.Format("{0}轰炸了{1},{1}退回了6格", names[playerNumber], names[1 - playerNumber]);
                                checkPos();
                            }
                            break;
                        case 2:
                            //踩到了地雷
                            playerPos[playerNumber] -= 6;
                            checkPos();
                            msg = string.Format("{0}踩到了地雷,{0}退了6格", names[playerNumber]);
                            break;
                        case 3:
                            //暂停一次
                            isStop[playerNumber] = true;
                            msg = string.Format("{0}走到了红灯,下次暂停一次啊", names[playerNumber]);
                            break;
                        case 4:
                            //踩到时空隧道
                            playerPos[playerNumber] += 10;
                            msg = string.Format("{0}进入了时空隧道,爽死了,进了10格", names[playerNumber]);
                            break;
                    }

                }
            }
            else
            {
                isStop[playerNumber] = false;
            }

            if (playerPos[playerNumber] >= 99)
            {
                //判断谁胜利,谁失败
                Console.Clear();
                if (playerPos[0] >= 99)
                {
                    Console.WriteLine("{0}胜利了!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", names[0]);
                }
                else
                {
                    Console.WriteLine("{0}胜利了!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", names[1]);
                }
            }

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

        }
    }
}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表