using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FlyChess
{
     class Program
    {
         static  int[] map  =  new  int[ 100]; //初始化地图数组
         static  int[] playerPos  = {  0,  0 }; //初始化玩家位置 0:A 1:B
         static  void Main( string[] args)
        {
            Random r  =  new Random(); //产生随机数
             int step  =  0; //用于临时存放产生的随机数
             string message  =  " "; //特殊格提示
             string[] names  =  new  string[ 2]; //数组存储玩家姓名
             bool[] isStop  = {  false,  false }; //判断玩家上一次是否走到暂停格
            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();
             while (names[ 1]  ==  " "  || names[ 1]  == names[ 0])
            {
                 if (names[ 1]  ==  " ")
                {
                    Console.WriteLine( " 玩家B的姓名不能为空,请重新输入: ");                 
                }
                 if (names[ 1]  == names[ 0])
                {
                    Console.WriteLine( " 玩家B的姓名不能与玩家A的姓名【{0}】相同,请重新输入: ", names[ 0]);                  
                }
                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轮流掷骰子,任意玩家坐标值>=99时结束循环
             while (playerPos[ 0]  <  99  && playerPos[ 1]  <  99)
            {
                 if (isStop[ 0]  ==  false)
                {
                    #region //玩家A掷骰子
                    Console.WriteLine( " 玩家A【{0}】按任意键开始掷骰子...... ", names[ 0]);
                    ConsoleKeyInfo rec  = Console.ReadKey( true);
                     if (rec.Key  == ConsoleKey.Backspace)
                    {
                        step  =  7;
                    }
                     else  if (rec.Key  == ConsoleKey.F12)
                    {
                        step  =  999;
                    }
                     else
                    {
                        step  = r.Next( 1,  7);
                    }
                    Console.WriteLine( " {0}掷出了{1}点。 ", names[ 0], step);
                    Console.WriteLine( " 请按任意键开始行动...... ");
                    Console.ReadKey( true);
                    playerPos[ 0]  = playerPos[ 0]  + step;
                    checkPos();
                     if (playerPos[ 0]  == playerPos[ 1]) //如果玩家A踩到玩家B
                    {
                        playerPos[ 1]  =  0;
                        message  =  string.Format( " {0}踩到了{1},{1}退回起点! ", names[ 0], names[ 1], names[ 1]);
                    }
                     else  //判断玩家A是否踩到特殊效果
                    {
                         switch (map[playerPos[ 0]])
                        {
                             case  0:
                                 //普通格子,无效果。
                                message  =  " ";
                                 break;
                             case  1:
                                 //幸运轮盘
                                Console.Clear();
                                drawMap();
                                Console.WriteLine( " {0}走到了幸运轮盘,请选择运气: ", names[ 0]);
                                Console.WriteLine( " 1、---交换位置  2、---轰炸 ");
                                 int input  = readInt( 1,  2);
                                 if (input  ==  1)
                                {
                                     //交换位置
                                     int temp  = playerPos[ 0];
                                    playerPos[ 0]  = playerPos[ 1];
                                    playerPos[ 1]  = temp;
                                    message  =  string.Format( " {0}选择了与{1}交换位置! ", names[ 0], names[ 1]);
                                }
                                 else  if(input == 2)
                                {
                                    playerPos[ 1]  = playerPos[ 1]  -  6;
                                    checkPos();
                                    message  =  string.Format( " {0}轰炸了{1},{1}退6格! ", names[ 0], names[ 1], names[ 1]);
                                }
                                 break;
                             case  2:
                                 //地雷,倒退6格
                                playerPos[ 0]  = playerPos[ 0]  -  6;
                                checkPos();
                                message  =  string.Format( " {0}踩到了地雷,倒退6格! ", names[ 0]);
                                 break;
                             case  3:
                                 //暂停一次
                                isStop[ 0]  =  true;
                                message  =  string.Format( " {0}真倒霉,暂停一回合! ", names[ 0]);
                                 break;
                             case  4:
                                 //时空隧道,前进10格。
                                playerPos[ 0]  = playerPos[ 0]  +  10;
                                checkPos();
                                message  =  string.Format( " {0}进入了时空隧道,太牛X了!前进10格! ", names[ 0]);
                                 break;
                        }
                    }
                    Console.Clear();
                    drawMap();
                     if (message  !=  " ")
                    {
                        Console.WriteLine(message);
                    }
                    Console.WriteLine( " {0}掷出了【{1}】点,行动完成。 ", names[ 0], step);
                    Console.WriteLine( " ********************玩家A和玩家B的当前位置如下******************** ");
                    Console.WriteLine( " {0}的位置为:第【{1}】格。 ", names[ 0], playerPos[ 0]  +  1);
                    Console.WriteLine( " {0}的位置为:第【{1}】格。 ", names[ 1], playerPos[ 1]  +  1);
                     if (playerPos[ 0]  >=  99)
                    {
                         break;
                    }
                    #endregion
                }
                 else
                {
                    isStop[ 0]  =  false;
                }
                 if (isStop[ 1]  ==  false)
                {
                    #region //玩家B掷骰子
                    Console.WriteLine( " 玩家B【{0}】按任意键开始掷骰子...... ", names[ 1]);
                    Console.ReadKey( true);
                    step  = r.Next( 1,  7);
                    Console.WriteLine( " {0}掷出了{1}点。 ", names[ 1], step);
                    Console.WriteLine( " 请按任意键开始行动...... ");
                    Console.ReadKey( true);
                    playerPos[ 1]  = playerPos[ 1]  + step;
                    checkPos();
                     if (playerPos[ 1]  == playerPos[ 0]) //如果玩家B踩到玩家A
                    {
                        playerPos[ 0]  =  0;
                        message  =  string.Format( " {0}踩到了{1},{1}退回起点! ", names[ 1], names[ 0], names[ 0]);
                    }
                     else  //判断玩家B是否踩到特殊效果
                    {
                         switch (map[playerPos[ 1]])
                        {
                             case  0:
                                 //普通格子,无效果。
                                message  =  " ";
                                 break;
                             case  1:
                                 //幸运轮盘
                                Console.Clear();
                                drawMap();
                                Console.WriteLine( " {0}走到了幸运轮盘,请选择运气: ", names[ 1]);
                                Console.WriteLine( " 1、---交换位置  2、---轰炸 ");
                                 int input  = readInt( 1,  2);
                                 if (input  ==  1)
                                {
                                     //交换位置
                                     int temp  = playerPos[ 1];
                                    playerPos[ 1]  = playerPos[ 0];
                                    playerPos[ 0]  = temp;
                                    message  =  string.Format( " {0}选择了与{1}交换位置! ", names[ 1], names[ 0]);
                                }
                                 else
                                {
                                    playerPos[ 0]  = playerPos[ 0]  -  6;
                                    checkPos();
                                    message  =  string.Format( " {0}轰炸了{1},{1}退6格! ", names[ 1], names[ 0], names[ 0]);
                                }
                                 break;
                             case  2:
                                 //地雷,倒退6格
                                playerPos[ 1]  = playerPos[ 1]  -  6;
                                checkPos();
                                message  =  string.Format( " {0}踩到了地雷,倒退6格! ", names[ 1]);
                                 break;
                             case  3:
                                 //暂停一次
                                 isStop[ 1]  =  true;
                                message  =  string.Format( " {0}真倒霉,暂停一回合! ", names[ 1]);
                                 break;
                             case  4:
                                 //时空隧道,前进10格。
                                playerPos[ 1]  = playerPos[ 1]  +  10;
                                checkPos();
                                message  =  string.Format( " {0}进入了时空隧道,太牛X了!前进10格! ", names[ 1]);
                                 break;
                        }
                    }
                    Console.Clear();
                    drawMap();
                     if (message  !=  " ")
                    {
                        Console.WriteLine(message);
                    }
                    Console.WriteLine( " {0}掷出了【{1}】点,行动完成。 ", names[ 1], step);
                    Console.WriteLine( " ********************玩家A和玩家B的当前位置如下******************** ");
                    Console.WriteLine( " {0}的位置为:第【{1}】格。 ", names[ 0], playerPos[ 0]  +  1);
                    Console.WriteLine( " {0}的位置为:第【{1}】格。 ", names[ 1], playerPos[ 1]  +  1);
                     if (playerPos[ 1]  >=  99)
                    {
                         break;
                    }
                    #endregion
                }
                 else
                {
                    isStop[ 1]  =  false;
                }
            }
            Console.Clear();
            ShowUI();
             if (playerPos[ 0]  >=  99) //判断胜利者
            {
                Console.WriteLine( " 哎呦我去!!!简直屌爆了!!!{0}如有神助,比{1}提前到达了终点!!! ", names[ 0], names[ 1]);
            }
             else  if (playerPos[ 1]  >=  99)
            {
                Console.WriteLine( " 哎呦我去!!!简直屌爆了!!!{1}如有神助,比{0}提前到达了终点!!! ", names[ 1], names[ 0]);
            }
            Console.ReadKey();
        }
         /// <summary>
         /// 绘制飞行棋LOGO
         /// </summary>
         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 }; //幸运轮盘 1 ◎
             int[] landMine  = {  5,  13,  17,  33,  38,  50,  64,  80,  94 }; //地雷 2 ★
             int[] pause  = {  9,  27,  60,  93 }; //暂停 3 ▲
             int[] timeTravel  = {  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  < timeTravel.Length; i ++)
            {
                map[timeTravel[i]]  =  4;
            }
        }
         /// <summary>
         /// 获取第pos个坐标上的图案
         /// </summary>       
         static  string getMapString( int pos)
        {
             string result  =  " ";
             if (playerPos[ 0]  == pos  && playerPos[ 1]  == pos)
            {
                Console.ForegroundColor  = ConsoleColor.Yellow;
                result  =  " <> ";
            }
             else  if (playerPos[ 0]  == pos)
            {
                Console.ForegroundColor  = ConsoleColor.Yellow;
                result  =  " A ";
            }
             else  if (playerPos[ 1]  == pos)
            {
                Console.ForegroundColor  = ConsoleColor.Yellow;
                Console.Write( " B ");
            }
             else
            {
                 switch (map[pos])
                {
                     case  0:
                        Console.ForegroundColor  = ConsoleColor.White;
                        result  =  " □ ";
                         break;
                     case  1:
                        Console.ForegroundColor  = ConsoleColor.Green;
                        result  =  " ◎ ";
                         break;
                     case  2:
                        Console.ForegroundColor  = ConsoleColor.Red;
                        result  =  " ★ ";
                         break;
                     case  3:
                        Console.ForegroundColor  = ConsoleColor.Blue;
                        result  =  " ▲ ";
                         break;
                     case  4:
                        Console.ForegroundColor  = ConsoleColor.Cyan;
                        result  =  " 卐 ";
                         break;
                }
            }
             return result;
        }
         /// <summary>
         /// 判断玩家是否完成游戏
         /// </summary>
         static  void checkPos()
        {
             for ( int i  =  0; i  <=  1; i ++)
            {
                 if (playerPos[i]  >  99)
                {
                    playerPos[i]  =  99;
                }
                 if (playerPos[i]  <  0)
                {
                    playerPos[i]  =  0;
                }
            }
        }
         /// <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 ++)
                {
                    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>
         /// 输入限制
         /// </summary>
         /// <returns></returns>
         static  int reaadInt()
        {
             int i  = readInt( int.MinValue,  int.MaxValue);
             return i;
        }
         /// <summary>
         /// 输入限制
         /// </summary>
         /// <param name="min"></param>
         /// <param name="max"></param>
         /// <returns></returns>
         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( " 输入错误,只能输入数字! ");
                }                
            }
        }
    }
}