using System;
 using System.Collections.Generic;
 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];//定义地图,0至100格
 static int[] playerPos = { 0, 1 };//定义玩家位子,下标为1的是1号玩家,为2的是2号玩家
 static void Main(string[] args)
         {
             Random r = new Random();//产生随机数
 int step = 0;           //step存储产生的随机数
 string msg = "";//用于存储用户踩到某位置输出的话
 bool[] isStop = { false, false };//isStop[0]表示A是否上次走到了暂停 isStop[1]表示B 
             //如果走到暂停,则设置为true
             showUI();
             string[] name = new string[2];
             Console.WriteLine("请输入玩家A的姓名");
 name[0] = Console.ReadLine();
             while (name[0] == "")//判断玩家A的姓名是否合法
 {
 Console.WriteLine("用户名不能为空,请重新输入");
 Console.WriteLine("请输入玩家B的姓名");
 name[0] = Console.ReadLine();
             }
             Console.WriteLine("请输入玩家B的姓名");
 name[1] = Console.ReadLine();
             #region 判断玩家B的姓名是否和玩家A一样
 while (name[1] == "" || name [1] == name[0])//判断玩家B的姓名是否和玩家A一样
 {
 if (name[0] == name[1])
                 {
                     Console.WriteLine("姓名已被玩家A占用,请从新输入");
 Console.WriteLine("请输入玩家B的姓名");
                 }
 else
                 {
                     Console.WriteLine("姓名不能为空,请重新输入");
 Console.WriteLine("请输入玩家B的姓名");
                 } 

 name[1] = Console.ReadLine();
             }
             #endregion
             Console.WriteLine("玩家A的姓名为:{0},玩家B的姓名为{1}", name[0], name[1]);
             Console.Clear();
             showUI();
             Console.WriteLine("对战开始");
 Console.WriteLine("{0}用A来表示",name[0]);
             Console.WriteLine("{0}用B来表示",name[1]);
             Console.WriteLine("如果{0}和{1}在一起则用<>来表示",name[0],name[1]);
             InitialMap();
             drawMap();
             Console.WriteLine("对战开始");
 while (playerPos[0] < 99 && playerPos[1] < 99)
             {   
                 //A开始掷骰子
 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();//检查玩家是否越界.
                     #region 判断A是否踩到了B和A是否碰到了关卡
 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;
                         }                     }
                     #endregion
                     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胜利了,就退出。
 {
 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])
                     {
                         #region //判断B是否踩到A和B是否碰到了关卡
 playerPos[0] = 0;
                         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;
                         }                     }
                     #endregion
                     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 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;
             }
         }
         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++)
                 {
                     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();//重置前景色
 }
 static public void showUI()//显示用户界面
 {
 Console.WriteLine("*****************************************************");
             Console.WriteLine("*                                                   *");
             Console.WriteLine("*                骑 士 飞 行 棋                     *");
 Console.WriteLine("*                                                   *");
             Console.WriteLine("*****************************************************");
         }
         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.Red; 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;
         }
         static void CheckPos()//检查玩家位子是否大于99或者小于0
         {
             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("输入的只能是数字,请重新输入!");
                 }
             }        }
    }
 }