黑马程序员_c#基础综合练习:骑士飞行棋

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

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


namespace 骑士飞行棋
{
    class Program
    {
        //数组的下标为0的元素对应地图上的第1格,下标为1的元素对应第二格...下标为n的元素对应n+1格。
        //在数组中用:1.表示幸运轮盘◎1
        //          2.地雷☆2
        //          3.暂停▲3
        //          4.时空隧道卐4
        //          0.表示普通
        static int[] Map = new int[100];//放在外面以便Initial方法访问到.并需要加static修饰.
        static int[] playerPos = { 0, 0 };//用于存储玩家位置,playerPos[0]表示玩家A的位置,playerPos[1]表示玩家B的位置.
        static void Main(string[] args)
        {


            Random r = new Random();//r是产生随机数用的
            int step = 0;           //step用于存储产生的随机数
            string msg = "";//用于存储用户踩到某位置输出的话
            bool[] isStop = { false, false };//isStop[0]表示A是否上次走到了暂停 isStop[1]表示B
            //如果走到暂停,则设置为true
            string[] name = new string[2];
            ShowUI();
            Console.WriteLine("请输入玩家A的姓名:");
            name[0] = Console.ReadLine();
            while (name[0] == "")
            {
                Console.WriteLine("玩家A的姓名不能为空,请重新输入!");
                name[0] = Console.ReadLine();
            }
            Console.WriteLine("请输入玩家B的姓名:");
            name[1] = Console.ReadLine();
            while (name[1] == "" || name[1] == name[0])
            {
                if (name[1] == "")
                {
                    Console.WriteLine("玩家B的姓名不能为空,请重新输入!");
                }
                else
                {
                    Console.WriteLine("该姓名已被玩家A占用,请重新输入!");
                }
                name[1] = Console.ReadLine();
            }
            Console.Clear();
            ShowUI();
            Console.WriteLine("对战开始......");
            Console.WriteLine("{0}用A来表示", name[0]);
            Console.WriteLine("{0}用B来表示", name[1]);
            Console.WriteLine("如果A与B在同一位置,用<>表示");
            InitialMap();
            drawMap();
            Console.WriteLine("开始游戏...");
            while (playerPos[0] < 99 && playerPos[1] < 99)
            {
                if (isStop[0] == false)
                {
                    #region 玩家A掷骰子
                    Console.WriteLine("{0}按任意键掷骰子...", name[0]);
                    ConsoleKeyInfo rec = Console.ReadKey(true);//加true后按任意键不会被显示出来.
                    if (rec.Key == ConsoleKey.Tab)
                    {
                        step = 20;//设置的外挂!
                    }
                    else
                    {
                        step = r.Next(1, 7);
                    }


                    Console.WriteLine("{0}掷出了{1}", name[0], step);
                    Console.WriteLine("按任意键行动...");
                    Console.ReadKey(true);
                    playerPos[0] += step;//一旦坐标发生改变,就要判断 playerPos[0]是否>99.
                    CheckPos();//检查玩家是否越界.
                    if (playerPos[0] == playerPos[1])
                    {
                        playerPos[1] = 0;
                        msg = string.Format("{0}踩到了{1},{1}退回原点", name[0], name[1]);


                    }
                    else
                    {
                        switch (Map[playerPos[0]])
                        {


                            case 0: msg = ""; //必须清空字符串
                                break;//普通,没有效果.
                            case 1: //幸运轮盘
                                Console.Clear();
                                drawMap();
                                Console.WriteLine("{0}走到了幸运,请选择运气:", name[0]);
                                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}选择了与对方交换位置! ", name[0]);
                                }
                                else
                                {
                                    //轰炸对方
                                    playerPos[1] -= 6;
                                    CheckPos();
                                    msg = string.Format("{0}轰炸了{1},{1}退6格! ", name[0], name[1]);
                                }
                                break;
                            case 2://踩到地雷
                                playerPos[0] -= 6;
                                CheckPos();
                                msg = string.Format("{0}踩到了地雷,退6格! ", name[0]);
                                break;
                            case 3: //暂停一次
                                isStop[0] = true;
                                msg = string.Format("{0}走到红灯,暂停一次掷骰子", name[0]);
                                break;
                            case 4://时空隧道
                                playerPos[0] += 10;
                                msg = string.Format("{0}进入时空隧道,爽死了,进10格! ", name[0]);
                                break;
                        }


                    }
                    Console.Clear();//不清屏的话,上面还会有
                    drawMap();
                    if (msg != "")
                    {
                        Console.WriteLine(msg);
                    }
                    Console.WriteLine("{0}掷出了{1},行动完成!", name[0], step);
                    Console.WriteLine("************玩家A和玩家B的位置如下************");
                    Console.WriteLine("{0}的位置为{1}", name[0], playerPos[0] + 1);
                    Console.WriteLine("{0}的位置为{1}", name[1], playerPos[1] + 1);


                    #endregion
                }
                else
                {
                    //说明isStop[0] == true;
                    isStop[0] = false;
                }


                if (playerPos[0] >= 99)//要进行判断,以防A已经胜利了,B还可以掷一次骰子
                {
                    break;
                }


                //开始轮到B掷骰子
                if (isStop[1] == false)
                {
                    #region 玩家B掷骰子
                    Console.WriteLine("{0}按任意键掷骰子...", name[1]);
                    Console.ReadKey(true);//加true后按任意键不会被显示出来.
                    step = r.Next(1, 7);
                    Console.WriteLine("{0}掷出了{1}", name[1], step);
                    Console.WriteLine("按任意键行动...");
                    Console.ReadKey(true);
                    playerPos[1] += step;//一旦坐标发生改变,就要判断 playerPos[0]是否>99.
                    CheckPos();//检查玩家是否越界.
                    if (playerPos[0] == playerPos[1])
                    {
                        playerPos[0] = 0;//玩家B踩到A
                        msg = string.Format("{0}踩到了{1},{1}退回原点", name[1], name[0]);


                    }
                    else
                    {
                        switch (Map[playerPos[1]])
                        {


                            case 0: msg = ""; //必须清空字符串
                                break;//普通,没有效果.
                            case 1: //幸运轮盘
                                Console.Clear();
                                drawMap();
                                Console.WriteLine("{0}走到了幸运,请选择运气:", name[1]);
                                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}选择了与对方交换位置! ", name[1]);
                                }
                                else
                                {
                                    //轰炸对方
                                    playerPos[0] -= 6;
                                    CheckPos();
                                    msg = string.Format("{0}轰炸了{1},{1}退6格! ", name[1], name[0]);
                                }
                                break;
                            case 2://踩到地雷
                                playerPos[1] -= 6;
                                CheckPos();
                                msg = string.Format("{0}踩到了地雷,退6格! ", name[1]);
                                break;
                            case 3: //暂停一次
                                isStop[1] = true;
                                msg = string.Format("{0}走到红灯,暂停一次掷骰子", name[1]);
                                break;
                            case 4://时空隧道
                                playerPos[1] += 10;
                                msg = string.Format("{0}进入时空隧道,爽死了,进10格! ", name[1]);
                                break;
                        }


                    }
                    Console.Clear();//不清屏的话,上面还会有
                    drawMap();
                    if (msg != "")
                    {
                        Console.WriteLine(msg);
                    }
                    Console.WriteLine("{0}掷出了{1},行动完成!", name[1], step);
                    Console.WriteLine("************玩家A和玩家B的位置如下************");
                    Console.WriteLine("{0}的位置为{1}", name[0], playerPos[0] + 1);
                    Console.WriteLine("{0}的位置为{1}", name[1], playerPos[1] + 1);

 


                    #endregion
                }
                else
                {
                    //说明isStop[1] == true;
                    isStop[1] = false;
                }
            }
            Console.Clear();
            ShowUI();
            if (playerPos[0] >= 99)
            {
                Console.WriteLine("{0}胜利了!!!!!!!!!!", name[0]);
            }
            else
            {
                Console.WriteLine("{0}胜利了!!!!!!!!!!", name[1]);
            }

 


            Console.ReadKey();


        }
        static void ShowUI()
        {
            Console.WriteLine("***********************************************");
            Console.WriteLine("*                                                        *");
            Console.WriteLine("*       骑      士     飞    行    棋              *");
            Console.WriteLine("*                                                        *");
            Console.WriteLine("***********************************************");
        }
        /// <summary>
        /// 用于地图的初始化.
        /// </summary>
        static void InitialMap()
        {
            // 用于存储在地图中为地雷的下标
            int[] luckyTurn = { 6, 23, 40, 55, 69, 83, 98 };//幸运轮盘◎ 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 < luckyTurn.Length; i++)
            {
                Map[luckyTurn[i]] = 1;
            }
            for (int i = 0; i < landMine.Length; i++)
            {
                Map[landMine[i]] = 2;
            }
            for (int i = 0; i < pause.Length; i++)
            {
                Map[pause[i]] = 3;
            }
            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)//判断A的位置
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                result = "A";
            }
            else if (playerPos[1] == pos)//判断B的位置
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                result = "B";
            }
            else
            {
                switch (Map[pos])
                {   //在数组中用:1.表示幸运轮盘◎1
                    //                     2.地雷☆2
                    //                     3.暂停▲3
                    //                     4.时空隧道卐4
                    //                     0.表示普通
                    case 0: Console.ForegroundColor = ConsoleColor.White; result = "□"; break;
                    case 1: Console.ForegroundColor = ConsoleColor.Green; result = "◎"; break;
                    case 2: Console.ForegroundColor = ConsoleColor.Blue; result = "☆"; break;
                    case 3: Console.ForegroundColor = ConsoleColor.Magenta; result = "▲"; break;
                    case 4: Console.ForegroundColor = ConsoleColor.Cyan; result = "卐"; break;
                }
            }
            return result;
        }
        /// <summary>
        /// 分行分列分别画出地图.
        /// </summary>
        static void drawMap()
        {
            Console.WriteLine("图例:幸运轮盘◎   地雷☆   暂停▲   时空隧道卐    普通□");
            //画第一行
            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++)
                {
                    //输入2个半角空格,一共28*2个
                    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();//地图绘制完成以后,重置颜色,因为如果最后一个图标如果有颜色会有BUG.
        }
        /// <summary>
        /// 判断玩家位置是否越界
        /// </summary>
        static void CheckPos()
        {
            for (int i = 0; i < 2; i++)
            {
                if (playerPos[i] > 99)
                {
                    playerPos[i] = 99;
                }
                if (playerPos[i] < 0)
                {
                    playerPos[i] = 0;
                }
            }
        }
        //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("输入的只能是数字,请重新输入!");
                }
            }

        }

    }
}

 运行效果如下:

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

详细请查看:http://edu.csdn.net/heima/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值