黑马程序员--飞行棋(二)

---------------------- ASP.Net+Android+IO开发S.Net培训、期待与您交流! -----------------------

上一篇主要讲了画飞行棋的地图,今天就把剩下的功能实现。

写一个掷骰子的方法,玩家A玩家B都可以调用。

/// <summary>
        /// 开始掷骰子行动
        /// </summary>
        /// <param name="play"></param>
        static void Run(int play)
        {
            string msg = "";
            Console.WriteLine();
            Console.WriteLine("{0}按任意健开始掷骰子......", name[play]);
            Console.ReadKey(true);
            Random rdm = new Random();
            int pos = rdm.Next(1, 7);
            Console.WriteLine("{0}掷出来了:{1}", name[play], pos);
            Console.WriteLine("{0}按任意健开始行动......", name[play]);
            Console.ReadKey(true);
            gamerPos[play] = gamerPos[play] + pos;
            CheckPos();
            if (gamerPos[0] == gamerPos[1])
            {
                gamerPos[1 - play] = 0;
            }
            else
            {
                switch (Map[gamerPos[play]])
                {
                    case 0:
                        break;
                    case 1:
                        //幸运轮盘
                        Console.Clear();
                        drawMap();
                        Console.WriteLine("{0}走到了幸运轮盘,请选择运气?", name[play]);
                        Console.WriteLine("请选择:1.交换位置 2.轰炸对方?");
                        int setp = ConsoleInt(1, 2);
                        if (setp == 1)
                        {
                            int temp = gamerPos[play];
                            gamerPos[play] = gamerPos[1 - play];
                            gamerPos[1 - play] = temp;
                            msg = string.Format("{0}与对方交换了位置", name[play]);
                        }
                        else
                        {
                            gamerPos[1 - play] = gamerPos[1 - play] - 6;
                            msg = string.Format("{0}轰炸了对方{1},{1}后退6格", name[play], name[1 - play]);
                        }
                        CheckPos();
                        break;
                    case 2:
                        //地雷,退六格
                        gamerPos[play] = gamerPos[play] - 6;
                        msg = string.Format("{0}踩到了地雷,后退6格", name[play]);
                        CheckPos();
                        break;
                    case 3:
                        //暂停
                        isStop[play] = true;
                        msg = string.Format("{0}暂停一次", name[play]);
                        break;
                    case 4:
                        //时空隧道,前进10格
                        gamerPos[play] = gamerPos[play] + 10;
                        msg = string.Format("{0}踩到了时空隧道,前进10格", name[play]);
                        CheckPos();
                        break;
                }
            }
            Console.Clear();
            drawMap();
            if (msg != "")
            {
                Console.WriteLine(msg);
            }
            Console.WriteLine("{0}掷出了{1},行动开始!",name[play],pos);
            Console.WriteLine("******************************************");
            Console.WriteLine("{0}的位置为{1}",name[play],gamerPos[play]+1);
            Console.WriteLine("{0}的位置为{1}", name[1 - play], gamerPos[1 - play] + 1);
        }

/// <summary>
        /// 检测坐标是否越界
        /// </summary>
        static void CheckPos()
        {
            for (int i = 0; i < 2; i++)
            {
                if (gamerPos[i] > 99)
                {
                    gamerPos[i] = 99;
                }
                if(gamerPos[i]<0)
                {
                    gamerPos[i] = 0;
                }
            }
        }
        /// <summary>
        /// 输入一个数字
        /// </summary>
        /// <param name="min">最小值</param>
        /// <param name="max">最大值</param>
        /// <returns></returns>
        static int ConsoleInt(int min, int max)
        {
            int i = 0;
            while (true)
            {
                if (int.TryParse(Console.ReadLine(), out i))
                {
                    if (i >= min && i <= max)
                    {
                        return i;
                    }
                    else
                    {
                        Console.WriteLine("只能输入{0}{1}之间的数!", min, max);
                    }
                }
                else
                {
                    Console.WriteLine("请输入一个数字!");
                }
            }
        }

上面就是完整的掷骰子的方法,接下来就是调用通过方法实现功能 了

接着在我们上篇的main方法中添加调用方法:

	    Console.Clear();
            ShowUI();
            Console.WriteLine("游戏开始。。。");
            Console.WriteLine("{0}代表A玩家", name[0]);
            Console.WriteLine("{0}代表B玩家", name[1]);
            Console.WriteLine("当玩家同时在一个位置的时候,用<>表示");

            InitialMap();//初始化地图
            drawMap();//绘制地图
            while (gamerPos[0] < 99 && gamerPos[1] < 99)
            {
                if (!isStop[0])//判断是否暂停,初始值为false
                {
                    Run(0);
                }
                else
                {
                    isStop[0] = false;
                }
                if (!isStop[1])
                {
                    Run(1);
                }
                else
                {
                    isStop[1] = false;
                }
            }
            Console.Clear();
            ShowUI();
            if (gamerPos[0] >= 99)
            {
                Console.WriteLine("{0}胜利了。。。", name[0]);
            }
            else
            {
                Console.WriteLine("{0}胜利了。。。", name[1]);
            }
            Console.ReadKey();


ok,我们整个功能就实现了  ,新手多多学习

---------------------ASP.Net+Android+IOS开发.Net培训、期待与您交流! ----------------------

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值