黑马程序员-骑士飞行棋

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

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

namespace 骑士飞行棋
{
    class Program
    {
        //存储游戏地图各关卡
        //0:表示普通
        //1:表示幸运轮盘◎
        //2:地雷☆
        //3:暂停▲
        //4:时空隧道卐
        static int[] Map = new int[100];
        static int[] playerPos = { 0, 0 };//playerPos[0]存玩家A下标,playerPos[1]存玩家B下标
        //保存用户姓名name[0]保存玩家A姓名
        static string[] name = new string[2];
        //是否走到暂停位置,如果是则设为true
        static bool[] isStop = { false, false };
        static string msg ;
        static void Main(string[] args)
        {



            //显示界面
            ShowUI();
            Console.WriteLine("请输入玩家A的姓名?");
            name[0] = Console.ReadLine();
            //判断用户输入是否为空,为空重新输入
            while (string.IsNullOrEmpty(name[0]))
            {
                Console.WriteLine("玩家A的姓名不能为空,请重新输入!");
                name[0] = Console.ReadLine().Trim();

            }

            Console.WriteLine("请输入玩家B的姓名?");
            name[1] = Console.ReadLine();
            while (string.IsNullOrEmpty(name[1]) || name[1] == name[0])
            {
                if (string.IsNullOrEmpty(name[1]))
                {
                    Console.WriteLine("玩家B的姓名不能为空,请重新输入!");
                }
                if (name[1] == name[0])
                {
                    Console.WriteLine("该姓名已被A占用!请重新输入");
                }
                name[1] = Console.ReadLine().Trim();
            }

            Console.Clear();
            ShowUI();
            Console.WriteLine("对战开始.....");
            Console.WriteLine("{0}用A表示", name[0]);
            Console.WriteLine("{0}用B表示", name[1]);
            Console.WriteLine("如果AB在同一位置,用<>来表示");

            InitialMap();
            DropMap();
            Console.WriteLine("开始游戏...");

            //A和B轮流掷骰子当玩家A或B坐标>=99时,结束循环
            while (playerPos[0] < 99 && playerPos[1] < 99)
            {
                if (isStop[0] == false)
                {
                    Action(0);
                }
                else
                {
                    isStop[0] = false;
                }

                if (playerPos[0] >= 99)
                {
                    break;
                }

                if (isStop[1] == false)
                {
                    Action(1);
                }
                else
                {
                    isStop[1] = false;
                }
            }

            Console.Clear();
            ShowUI();
            if (playerPos[0] >= 99)
            {
                Console.WriteLine("{0}获得了胜利!!!!!!!!!!!!!!", name[0]);
            }
            else
            {
                Console.WriteLine("{0}获得了胜利!!!!!!!!!!!!!!", name[1]);
            }
            Console.ReadKey();

        }

        /// <summary>
        /// A或B掷骰子的方法,A传0,B传1
        /// </summary>
        static void Action(int playNumber)
        {
            Random r = new Random();
            int k;
            msg = "";//用户踩到某关卡,输出的话
            Console.WriteLine("{0}按任意键开始掷骰子......", name[playNumber]);
            ConsoleKeyInfo rec = Console.ReadKey(true);
            k = r.Next(1, 7);//产生一个随机数

            if (rec.Key == ConsoleKey.P)
            {
                k = 20;
            }


            Console.WriteLine("{0}掷出了:{1}点", name[playNumber], k);
            Console.WriteLine("按任意键开始行动......");
            Console.ReadKey(true);

            playerPos[playNumber]=playerPos[playNumber]+ k;
            CheckPos(); //检测坐标是否越界
            if (playerPos[0] == playerPos[1])//A踩到B
            {
                playerPos[1 - playNumber] = 0;
                msg = string.Format("{0}踩到了{1},{1}退回原点", name[playNumber], name[1 - playNumber]);
            }
            else
            {
                //没踩到
                switch (Map[playerPos[playNumber]])
                {
                    case 0:
                        //普通,没效果
                        msg = "";
                        CheckYD(playNumber);
                        break;
                    case 1:
                        //幸运轮盘
                        Console.Clear();
                        DropMap();
                        Console.WriteLine("{0}走到幸运轮盘,请选择运气?", name[playNumber]);
                        Console.WriteLine("1--交互位置 2--轰炸对方");
                        int userSlect = ReadInt(1, 2);
                        if (userSlect == 1)
                        {//换位
                            int temp = playerPos[playNumber];
                            playerPos[playNumber] = playerPos[1 - playNumber];
                            playerPos[1 - playNumber] = temp;
                            msg += string.Format("{0}选择了与对方交换位置!", name[playNumber]);
                        }
                        else
                        {
                            //轰炸对方
                            playerPos[1 - playNumber] = playerPos[1 - playNumber] - 6;
                            CheckPos();
                            msg += string.Format("{0}轰炸了{1},{1}退6格", name[playNumber], name[1 - playNumber]);
                        }
                        CheckYD(playNumber);
                        break;
                    case 2:
                        //地雷
                        playerPos[playNumber] = playerPos[playNumber] - 6;
                        CheckPos();
                        msg += string.Format("{0}踩到了地雷,退6格!", name[playNumber]);
                        CheckYD(playNumber);
                        break;
                    case 3:
                        //暂停
                        isStop[0] = true;
                        msg += string.Format("{0}走到红灯,下次暂停一次", name[playNumber]);
                        CheckYD(playNumber);
                        break;
                    case 4:
                        //时空隧道
                        playerPos[playNumber] = playerPos[playNumber] + 10;
                        msg += string.Format("{0},进入时空隧道,-_-,进10步!", name[playNumber]);
                        CheckYD(playNumber);
                        break;
                }

            }
            Console.Clear();
            DropMap();
            if (msg != "")
            {
                Console.WriteLine(msg);
            }
            Console.WriteLine("{0}掷出了{1},完成行动!", name[playNumber], k);
            Console.WriteLine("*********************玩家{0}和{1}的位置如下************************", name[playNumber], name[1 - playNumber]);
            Console.WriteLine("{0}的位置为:{1}", name[0], playerPos[0] + 1);
            Console.WriteLine("{0}的位置为:{1}", name[1], playerPos[1] + 1);

        }

        static void CheckYD(int playNumber)
        {
            if (playerPos[0] == playerPos[1])//A踩到B
            {
                playerPos[1 - playNumber] = 0;
                msg += string.Format("\n{0}踩到了{1},{1}退回原点", name[playNumber], name[1 - playNumber]);
            }
        }
      


        //检查坐标位置
        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 void ShowUI()
        {
            Console.WriteLine("*************************************************************");
            Console.WriteLine("*                                                           *");
            Console.WriteLine("*                   骑 士 飞 行 棋                          *");
            Console.WriteLine("*                                                           *");
            Console.WriteLine("*************************************************************");
        }

        //初始化地图
        static void InitialMap()
        {
            int[] luckTurn = { 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 < luckTurn.Length; i++)
            {
                Map[luckTurn[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;
            }
        }

        //画地图
        static void DropMap()
        {
            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++)
                {
                    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();
        }

        /// <summary>
        /// 获得第pos坐标上要绘制的图案
        /// </summary>
        /// <param name="pos"></param>
        /// <returns></returns>
        static string GetMapString(int pos)
        {
            string t = "";
            //判断A和B是否在当前要画的第i格上
            if (playerPos[0] == pos && playerPos[1] == pos)
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                t = "<>";
            }
            else if (playerPos[0] == pos)
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                t = "A";
            }
            else if (playerPos[1] == pos)
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                t = "B";
            }
            else
            {
                switch (Map[pos])
                {
                    case 0:
                        Console.ForegroundColor = ConsoleColor.White;
                        t = "□";
                        break;
                    case 1:
                        Console.ForegroundColor = ConsoleColor.Red;
                        t = "◎";
                        break;
                    case 2:
                        Console.ForegroundColor = ConsoleColor.Green;
                        t = "☆";
                        break;
                    case 3:
                        Console.ForegroundColor = ConsoleColor.Blue;
                        t = "▲";
                        break;
                    default:
                        Console.ForegroundColor = ConsoleColor.DarkMagenta;
                        t = "卐";
                        break;
                }
            }

            return t;
        }

        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://net.itheima.com

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值