用C#的控制台程序写一个飞行棋项目
using System; namespace 飞行棋项目 { class Program { ///1、画游戏头 ///2、初始化地图 ///把整数数组中数字编程控制台中显示的特殊字符显示的过程,就是初始化地图 ///3、画地图 ///4、玩游戏 //我们用静态数组用来模拟全局变量,这个数组代表地图长度以及地图坐标 public static int[] Maps = new int[100]; //声明一个静态数组用来存储玩家A跟玩家B的坐标 public static int[] PlayerPos = new int[2]; //存储两个玩家的姓名 public static string[] PlayerName = new string[2]; public static bool[] Flag = new bool[2]; static void Main(string[] args) { TitleName(); #region 输入玩家姓名 Console.WriteLine("请输入玩家A的姓名"); PlayerName[0] = Console.ReadLine(); while (PlayerName[0] == "") { Console.WriteLine("玩家A的姓名不能为空,请重新输入"); PlayerName[0] = Console.ReadLine(); } Console.WriteLine("请输入玩家B的姓名"); PlayerName[1] = Console.ReadLine(); while (PlayerName[1] == "" || PlayerName[1] == PlayerName[0]) { if (PlayerName[1] == "") { Console.WriteLine("玩家B的姓名不能为空,请重新输入"); PlayerName[1] = Console.ReadLine(); } else { Console.WriteLine("玩家B的姓名不能和玩家A的相同,请重新输入"); PlayerName[1] = Console.ReadLine(); } } #endregion //玩家姓名输入正确之后 Console.Clear();//清屏功能 TitleName(); Console.WriteLine("{0}玩家用A表示\n{1}玩家用B表示", PlayerName[0], PlayerName[1]); //在画地图之前首先要初始化地图 InitailMap(); DrawMap(); //当玩家A跟玩家B没有一个人在终点的时候,两个玩家就不停的去玩游戏 while (PlayerPos[0] < 99 && PlayerPos[1] < 99) { if (Flag[0] == false) { PlayGame(0); } else { Flag[0] = false; } if(PlayerPos [0]>=99) { Console.WriteLine("玩家{0}赢了", PlayerName[0]); break; } if (Flag[1]==false) { PlayGame(1); } else { Flag[1] = false; } if(PlayerPos[1] >= 99) { Console.WriteLine("玩家{0}赢了", PlayerName[1]); break; } }//while Console.ReadKey(); } /// <summary> /// 画游戏头 /// </summary> public static void TitleName() { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("******************"); Console.ForegroundColor = ConsoleColor.Blue; Console.WriteLine("******************"); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("******************"); Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("****飞行棋项目****"); Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("******************"); Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine("******************"); Console.ForegroundColor = ConsoleColor.DarkMagenta; Console.WriteLine("******************"); } /// <summary> /// 将不同的值赋予不同的特殊符号,方便画出地图 /// </summary> public static void InitailMap() { int[] luckyturn = { 6, 23, 38, 45, 78, 90 };//幸运轮盘 for (int i = 0; i < luckyturn.Length; i++) { Maps[luckyturn[i]] = 1; } int[] landline = { 5, 22, 31, 44, 75, 87, 92 };//地雷 for (int i = 0; i < landline.Length; i++) { Maps[landline[i]] = 2; } int[] pause = { 7, 15, 25, 46, 56, 84 };//暂停 for (int i = 0; i < pause.Length; i++) { Maps[pause[i]] = 3; } int[] timeTunnel = { 9, 18, 29, 37, 42, 63, 72, 88, 96 };//时空隧道 for (int i = 0; i < timeTunnel.Length; i++) { Maps[timeTunnel[i]] = 4; } } /// <summary> /// 画地图 /// </summary> public static void DrawMap() { Console.WriteLine("图例:幸运轮盘:▲ 地雷:☆ 暂停:○ 时空隧道:¥"); #region 第一横行 //第一横行 for (int i = 0; i < 30; i++) { Console.Write(DrawStringMap(i));//直接调用画地图的那个方法 }//for #endregion //画完第一横行需要换行 Console.WriteLine(); #region 第一竖行 //画第一竖行 for (int i = 30; i < 35; i++) { for (int j = 0; j <= 28; j++) { Console.Write(" ");//这里画两个空格 } Console.Write(DrawStringMap(i));//直接调用画地图的那个方法 Console.WriteLine(); } #endregion #region 第二横行 for (int i = 64; i >= 35; i--) { Console.Write(DrawStringMap(i)); } #endregion //画完第二横行需要换行 Console.WriteLine(); #region 第二竖行 for (int i = 65; i <= 69; i++) { Console.WriteLine(DrawStringMap(i)); } #endregion #region 第三横行 for (int i = 70; i <= 99; i++) { Console.Write(DrawStringMap(i)); } #endregion //画完最后一行地图需要换行 Console.WriteLine(); } #region 从画地图的方法中抽象出来的一种方法 /// <summary> /// 从画地图的方法中抽象出来的一种方法 /// </summary> /// <param name="i">传参进来的一个变量</param> /// <returns></returns> public static string DrawStringMap(int i) { //画图 string str = ""; if (PlayerPos[0] == PlayerPos[1] && PlayerPos[0] == i) { str = "<>"; } else if (PlayerPos[0] == i) { str = "A"; } else if (PlayerPos[1] == i) { str = "B"; } else { switch (Maps[i]) { case 0: Console.ForegroundColor = ConsoleColor.Red; str = "□"; break; case 1: Console.ForegroundColor = ConsoleColor.Green; str = "▲"; break; case 2: Console.ForegroundColor = ConsoleColor.Gray; str = "☆"; break; case 3: Console.ForegroundColor = ConsoleColor.Yellow; str = "○"; break; case 4: Console.ForegroundColor = ConsoleColor.Blue; str = "¥"; break; }//switch }//if return str; } #endregion /// <summary> /// 玩游戏 /// </summary> /// <param name="playerNumber">传递进来的一个参数来判定是谁在玩游戏</param> public static void PlayGame(int playerNumber) { Random r = new Random();//设定随机数 int rNumber = r.Next(1, 7);//限定随机数范围在1-7之间 Console.WriteLine("{0}按下任意键开始掷骰子", PlayerName[playerNumber]); Console.ReadKey(true); Console.WriteLine("{0}掷出了{1}", PlayerName[playerNumber], rNumber);//掷出了随机数1-7之间 PlayerPos[playerNumber] += rNumber;//玩家坐标加上掷出的随机数 ChangePos();//在每一个玩家坐标变化的地方都要进行一次限制,限制玩家坐标在地图0-99之内 Console.ReadKey(true); if (PlayerPos[playerNumber] == PlayerPos[1 - playerNumber]) { Console.WriteLine("玩家{0}踩到了玩家{1},玩家{2}退6格", PlayerName[playerNumber], PlayerName[1 - playerNumber], PlayerName[1 - playerNumber]); PlayerPos[1 - playerNumber] -= 6; ChangePos(); Console.ReadKey(true); } else { switch (Maps[PlayerPos[playerNumber]]) { case 0: Console.WriteLine("玩家{0}踩到了方块,啥都没有", PlayerName[playerNumber]); Console.ReadKey(true); break; case 1: Console.WriteLine("玩家{0}踩到了幸运轮盘,请选择1--交换位置,2--轰炸对方", PlayerName[playerNumber]); Console.ReadKey(true); string input = Console.ReadLine(); while (true) { if (input == "1") { Console.WriteLine("玩家{0}跟玩家{1}交换位置", PlayerName[playerNumber], PlayerName[1 - playerNumber]); Console.ReadKey(true); int temp = PlayerPos[playerNumber]; PlayerPos[playerNumber] = PlayerPos[1 - playerNumber]; PlayerPos[1 - playerNumber] = temp; ChangePos(); Console.WriteLine("交换完成,按任意键继续游戏"); Console.ReadKey(true); break; } else if (input == "2") { Console.WriteLine("玩家{0}选择轰炸玩家{1},玩家{2}退6格", PlayerName[playerNumber], PlayerName[1 - playerNumber], PlayerName[1 - playerNumber]); Console.ReadKey(true); PlayerPos[1 - playerNumber] -= 6; ChangePos(); Console.WriteLine("玩家{0}退了6格,按任意键继续游戏", PlayerName[1 - playerNumber]); Console.ReadKey(true); break; } else { Console.WriteLine("你输入的文本有误,请重新输入!"); input=Console.ReadLine(); Console.ReadKey(true); } } break; case 2: Console.WriteLine("玩家{0}踩到了地雷,退6格", PlayerName[playerNumber]); Console.ReadKey(true); PlayerPos[playerNumber] -= 6; ChangePos(); Console.ReadKey(true); break; case 3: Console.WriteLine("玩家{0}踩到了暂停,暂停一回合", PlayerName[playerNumber]); Flag[playerNumber] = true; Console.ReadKey(true); break; case 4: Console.WriteLine("玩家{0}踩到了时空隧道,前进10格", PlayerName[playerNumber]); Console.ReadKey(true); PlayerPos[playerNumber] += 10; ChangePos(); Console.ReadKey(true); break; }//switch }//else ChangePos();//perfect Console.Clear(); DrawMap(); } /// <summary> /// 当玩家坐标发生改变的时候调用这个方法,用于限制玩家坐标在0-99之间 /// </summary> public static void ChangePos() { if (PlayerPos[0] < 0) { PlayerPos[0] = 0; } if (PlayerPos[0] >= 99) { PlayerPos[0] = 99; } if (PlayerPos[1] < 0) { PlayerPos[1] = 0; } if (PlayerPos[1] >= 99) { PlayerPos[1] = 99; } } } }