黑马程序员_骑士飞行棋

---------------------- Windows Phone 7手机开发.Net培训、期待与您交流! ----------------------


骑士飞行棋代码: 来自 传智播客 视频!

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace 骑士飞行棋01

{

    class Program

    {

        //用下面的整数数组存储我们的游戏地图关卡

        //数组的下标为0的元素对应地图上的第1,下标为n的元素对应第n+1

        //在数组中用 1:表示幸运轮盘

        //           2:地雷

        //           3:暂停

        //           4:时空隧道卐

        //           0:表示普通

        static int[] Map = new int[100];

        static int[] playerPos = { 0, 0 };//playerPos[0]存储玩家A的坐标 playerPos[1]存储玩家B的坐标

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

        //stringinput = ""; //存储用户的输入,输入1或者2的时候

        static bool[] isStop = { false,false };//isStop[0]表示A上次是否走到了暂停 isStop[1]表示B

        //如果走到暂停,设为True下次要设为false

 

        static void Main(string[]args)

        {

            ShowUI(); //显示游戏名称

            Console.WriteLine("请输入玩家A的姓名:");

            names[0] = Console.ReadLine();

 

            //判断用户输入是否为空,如果为空,重新输入

            while(names[0] == "")

            {

                Console.WriteLine("玩家A的姓名不能为空,请重新输入:");

                names[0] = Console.ReadLine();

            }

 

            Console.WriteLine("请输入玩家B的姓名:");

            names[1] = Console.ReadLine();

            //玩家B的姓名为空或者与A重名要重新输入

            while(names[1] == "" || names[1] ==names[0])

           {

                if(names[1] == "")         //玩家B姓名为空

                {

                    Console.WriteLine("玩家B的姓名不能为空,请重新输入:");

                }

                if(names[1] == names[0]) //玩家B的姓名与A重名

                {

                    Console.WriteLine("该用户名已存在,请重新输入玩家B的姓名:");

                }

                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("激情游戏开始...");

 

            //玩家AB轮流掷骰子,当玩家A或者B的坐标>=99,循环结束

            while(playerPos[0] <99 && playerPos[1] <99)

            {

                if(isStop[0] == false)

                {

                    Action(0);   //A调用掷骰子的方法

                }

                else

                { //说明isStop==true

                    isStop[0] = false;

                   

                }

 

                if(playerPos[0] >= 99)

                {

                    break;

                }

 

                if(isStop[1] == false)

                {

                    Action(1);  //B调用掷骰子的方法

                }

                else

                {

                    isStop[1] = false;

                }

            }

 

            //判断谁胜利谁失败

            Console.Clear();

            ShowUI();

            if(playerPos[0] >= 99)

            {

                Console.WriteLine("{0}胜利了!!!", names[0]);

            }

            else

            {

                Console.WriteLine("{0}胜利了!!!", names[1]);

            }

 

            Console.ReadKey();

        }

 

        ///<summary>

        /// AB掷骰子的方法

        ///</summary>

        ///<paramname="playerNumber">A掷骰子传0过来 B掷骰子传1过来</param>

        public static void Action(int playerNumber)

        {

            //playerNumber中存储的就是当前玩家姓名,坐标,是否暂停这三个数组的下标

            //当前0对方1

            //当前1对方0

            //1-playerNumber就是对方的

 

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

            stringmsg = ""; //用于存储用户踩到某关卡输出的话

            intstep = 0; //用于存放产生的随机数

            Console.WriteLine("{0}按任意键开始掷骰子......", names[playerNumber]);

            ConsoleKeyInforec =Console.ReadKey(true);//不显示按键

            step = r.Next(1, 7); //产生一个1-6的随机整数

            if(rec.Key == ConsoleKey.Tab &&rec.Modifiers == (ConsoleModifiers.Control |ConsoleModifiers.Shift))

            {

                ConsoleKeyInfocc =Console.ReadKey();

                if(cc.Key == ConsoleKey.F1)

                {

                    step = ReadInt(1, 100);

                }

            }

            //if(rec.Key == ConsoleKey.F11)

            //{

            //    step = 20;

            //}

            //else

            //{

            //    step = r.Next(1, 7); //产生一个1-6的随机整数

            //}

            Console.WriteLine("玩家{0}掷出了{1}", names[playerNumber], step);

            Console.WriteLine("按任意键开始行动......");

            Console.ReadKey(true);

            playerPos[playerNumber] += step;//一旦坐标发生改变,就要判断坐标值是否>99<0

            CheckPos();     //检测坐标是否越界

            if(playerPos[0] == playerPos[1]) //玩家A踩到玩家B

            {

                playerPos[1-playerNumber] =0;  //对方设置为0

                msg = string.Format("{0}踩到了{1},{1}退回原点!", names[playerNumber],names[1-playerNumber]); //{1}都是取的第二个变量

            }

            else

            { //没踩到,要判断玩家A所在的位置是否有其他机关

                switch(Map[playerPos[playerNumber]])

                {

                    case0://普通,没有效果

                        msg = "";

                        break;

                    case1://走到了幸运轮盘

                        Console.Clear();

                        DrawMap();

                        Console.WriteLine("{0}很幸运到了幸运轮盘,请选择运气:", names[playerNumber]);

                        Console.WriteLine("1.交换位置   2.向对方丢一个高爆手雷");

                        int userSelect = ReadInt(1, 2);

 

                        if (userSelect == 1)

                        { //要与对方交换位置

                            int temp = playerPos[0];

                            playerPos[0] =playerPos[1];

                            playerPos[1] =temp;

                            msg = string.Format("{0}选择了与对方交换位置!", names[playerNumber]);

                        }

                        else

                        { //轰炸对方

                            playerPos[1] -= 6;

                            CheckPos();

                            msg = string.Format("{0}无情的轰炸了{1},{1}退6!", names[playerNumber],names[1-playerNumber]);

                        }

                        break;

                    case2://踩到了地雷

                        playerPos[playerNumber] -= 6;

                        CheckPos();

                        msg = string.Format("{0}踩到了地雷,退6!", names[playerNumber]);

                        break;

                    case3://暂停一次

                        isStop[playerNumber] = true;

                        msg = string.Format("{0}走到红灯,下次暂停一次!", names[playerNumber]);

                        break;

                    case4://时空隧道

                        playerPos[playerNumber]+= 10;

                        msg = string.Format("{0}进入了时空隧道,爽死啦,10!", names[playerNumber]);

                        break;

                }

 

            }

 

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

 

 

          

       

        }

 

        ///<summary>

        ///输出游戏界面

        ///</summary>

        public static voidShowUI()  

        {

            Console.WriteLine("********************************************");

            Console.WriteLine("*                                          *");

            Console.WriteLine("*                            *");

            Console.WriteLine("*                                          *");

            Console.WriteLine("********************************************");

        }

 

        ///<summary>

        ///进行玩家AB坐标进行越界的判断

        ///</summary>

        public static voidCheckPos()

        {

            for(int count = 0; count <= 1; count++)

            {

                if(playerPos[count] > 99)

                {

                    playerPos[count] = 99;

                }

                if(playerPos[count] < 0)

                {

                    playerPos[count] = 0;

                }

            }

 

        }

 

        ///<summary>

        ///初始化地图

        ///</summary>

        public static voidInitialMap()

        {

           

            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 count = 0; count < luckyTurn.Length;count++)

            {

                intpos = luckyTurn[count];      //取下标

                Map[pos] = 1;                     //设置相应的元素

            }

 

            for(int count = 0; count < landMine.Length;count++)

            {

                Map[landMine[count]] = 2;

            }

 

            for(int count = 0; count < pause.Length;count++)

            {

                Map[pause[count]] = 3;

            }

 

            for(int count = 0; count < timeTunnel.Length;count++)

            {

                Map[timeTunnel[count]] = 4;

            }

        }

 

        ///<summary>

        ///获得第pos坐标上应该画的图

        ///</summary>

        ///<paramname="pos">要画的坐标</param>

        ///<returns></returns>

        public static stringGetMapString(int pos)

        {

            stringresult = "";

            //判断AB是否在当前要画的第count格上

            if(playerPos[0] == pos && playerPos[1] == pos)

            {

                Console.ForegroundColor=ConsoleColor.Yellow;

                result = "<>";

            }

            elseif (playerPos[0] == pos)//A在当前格上

            {

                Console.ForegroundColor=ConsoleColor.Yellow;

                result = "";//全角A

            }

            elseif (playerPos[1] == pos)//B在当前格上

            {

                Console.ForegroundColor=ConsoleColor.Yellow;

                result = "";

            }

            else

            {

                switch(Map[pos])        //正常

                {

                    case0:

                        Console.ForegroundColor =ConsoleColor.White;

                        result = "□";

                        break;

                    case1:

                        Console.ForegroundColor =ConsoleColor.Blue;

                        result = "◎";

                        break;

                    case2:

                        Console.ForegroundColor =ConsoleColor.DarkRed;

                        result = "☆";

                        break;

                    case3:

                        Console.ForegroundColor =ConsoleColor.Red;

                        result = "▲";

                        break;

                    case4:

                        Console.ForegroundColor =ConsoleColor.Green;

                        result = "";

                        break;

                }

            }

            returnresult;

        }

 

        ///<summary>

        ///画地图

        ///</summary>

        public static void DrawMap()

        {

            Console.WriteLine("图例:幸运轮盘:◎ 地雷:☆ 暂停:▲ 时空隧道:");

 

            //画第一行

            for(int count = 0; count <= 29; count++)

            {

                Console.Write(GetMapString(count));

            }

            //第一行画完换行

            Console.WriteLine();

            //画第一列

            for(int count = 30; count <= 34; count++)

            {

                for(int var1 = 0; var1 < 29; var1++)

                {

                    Console.Write("  ");//二个空格*29

                }

                Console.WriteLine(GetMapString(count));//换行

               

            }

            //画第二行

            for(int count = 64; count >= 35; count--)

            {

                Console.Write(GetMapString(count));

            }

            Console.WriteLine();   //换行

            //画第二列

            for(int count = 65; count <= 69; count++)

            {

                Console.WriteLine(GetMapString(count));//画完每一个都要换行

            }

            //画第三行

            for(int count = 70; count <= 99; count++)

            {

                Console.Write(GetMapString(count));//不用换行

            }

            Console.WriteLine();

            Console.ResetColor();//如果99用了颜色,后面的也会使用这个颜色所以重置

        }

 

 

        //publicstatic int ReadInt()

        //{

        //    int i = ReadInt(int.MinValue,int.MaxValue);

        //    return i;

        //}

        ///<summary>

        ///测试输入的数字

        ///</summary>

        ///<paramname="min"></param>

        ///<paramname="max"></param>

        ///<returns></returns>

        public static int ReadInt(int min,int max)

        {

            while(true)

            {

                try

                {

                    intnumber = int.Parse(Console.ReadLine());

                    if(number < min || number > max)

                    {

                        Console.WriteLine("只能输入{0}-{1}之间的数字,请重新输入!", min, max);

                        continue;

                    }

                    returnnumber;

                }

                catch

                {

                    Console.WriteLine("只能输入数字,请重新输入");

 

                }

            }

        }

    }

}

 

 

---------------------- Windows Phone 7手机开发.Net培训、期待与您交流! ----------------------
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值