当前位置:  编程技术>.net/c#/asp.net

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

    来源: 互联网  发布时间:2014-10-20

    本文导语:  代码如下所示: 代码如下:using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks; namespace 骑士飞行棋{    class Program    {        //在下面的数组存储我们游戏地图各各关卡        //数组...

代码如下所示:

代码如下:

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();
        }

        ///
        /// 用于绘制飞行棋的名称
        ///
        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;
        }

        ///
        ///  获得第pos坐标上应该绘制的图案
        ///
        /// 要绘制的坐标
        ///
        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 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("只能输入数字,请重新输入!");
                }

            }

        }

        ///
        /// A或者B掷筛子的方法
        ///
        /// A掷筛子传0过来   B掷筛子传1过来
        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||= 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);

        }
    }
}


    
 
 

您可能感兴趣的文章:

 
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • _Shakespeare(网络骑士)请来拿分
  • _Shakespeare(网络骑士)请来拿分


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3