namespace 骑士飞行棋 { class Program { //在下面的数组存储我们游戏地图各各关卡 //数组的下标为0的元素对应地图上的第1格 下标为1的元素对应元素第2格...下标为n的元素对应n+1格 //在数组中 1:表示幸运轮盘 ◎ // 2: 表示地雷 ☆ // 3: 表示暂停 ▲ // 4: 表示时空隧道 卍 // 0: 表示普通 □ static int[] map = new int[100]; static string[] names = new string[2]; //names[0]存储玩家A的姓名 name[1]存玩家B的姓名 static int[] playerPos = { 0, 0 };//playPos[0]存玩家A的位置,playPos[1]存玩家B的位置 static int step = 0; //用于存放产生的随机数 static string input = ""; //用户存储用户的输入 static string msg = ""; //用于存储用户踩到某个关卡,输出的话 static bool[] isStop = { false, false };//isStop[0]表示玩家A是否上次一走到暂停,似的话为true,不是为false static Random r = new Random();//r是产生的随机数 static void Main( string[] args) { ShowUI(); //显示游戏 InitialName(); Console.Clear(); ShowUI(); Console.WriteLine("对战开始......"); Console.WriteLine("{0}用A来表示", names[0]); Console.WriteLine("{0}用B来表示", names[1]); Console.WriteLine("如果AB在同一位置,用<>表示"); InitialMap();//初始化地图 drawMap();//绘制地图 Console.WriteLine("开始游戏......"); //这个循环中让玩家A和玩家B轮流掷骰子 当玩家A或者玩家B的坐标>=99时,则循环结束 while (playerPos[0] < 99 && playerPos[1] < 99) { Action(0);//A掷筛子 Action(1);//B掷筛子 } Console.ReadKey(); } /// <summary> /// 用于绘制飞行棋的名称 /// </summary> static void ShowUI() { Console.WriteLine("*******************************************************"); Console.WriteLine("* *"); Console.WriteLine("* 骑 士 飞 行 棋 *"); Console.WriteLine("* *"); Console.WriteLine("*******************************************************"); } static void InitialName() { 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的姓名不能为空,请重新输入!"); names[1] = Console.ReadLine(); } else { Console.WriteLine("你输入的姓名与玩家A的姓名{0}相同,请重新输入", names[0]); names[1] = Console.ReadLine(); } } } 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[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 };//时空隧道 4 for (int i = 0; i < 100; i++) // 初始化map数组的数据 map[i] = 0; //把幸运轮盘位置填入map中 for (int i = 0; i < luckyTurn.Length; i++) map[luckyTurn[i]] = 1; //把地雷填入map中 for (int i = 0; i < landMine.Length; i++) map[landMine[i]] = 2; //把暂停填入map中 for (int i = 0; i < pause.Length; i++) map[pause[i]] = 3; //把时空隧道填入map中 for (int i = 0; i < timeTunnel.Length; i++) map[timeTunnel[i]] = 4; } /// <summary> /// 获得第pos坐标上应该绘制的图案 /// </summary> /// <param name="pos">要绘制的坐标</param> /// <returns></returns> 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; result = "B"; } else { switch (map[pos]) { case 0: Console.ForegroundColor = ConsoleColor.White; result = "□"; break; case 1: Console.ForegroundColor = ConsoleColor.Red; result = "◎"; break; case 2: Console.ForegroundColor = ConsoleColor.Green; result = "☆"; break; case 3: Console.ForegroundColor = ConsoleColor.Blue; result = "▲"; break; case 4: Console.ForegroundColor = ConsoleColor.DarkBlue; result = "卍"; break; } } return result; } static void drawMap() { Console.WriteLine("图例:幸运轮盘:◎ 地雷:☆ 暂停:▲ 时空隧道:卍 "); //画第一行 for (int i = 0; i < 30; i++) Console.Write(getMapString(i)); Console.WriteLine(); //左边的第一列 for (int i = 30; i < 35; i++) { for (int j = 0; j < 29; j++) Console.Write(" "); Console.Write(getMapString(i)); Console.WriteLine(); } //第二行 for (int i = 64; i >= 35; i--) Console.Write(getMapString(i)); Console.WriteLine(); //右边的列 for (int i = 65; i < 70; i++) { Console.Write(getMapString(i)); Console.WriteLine(); } //第三行 for (int i = 70; i < 100; i++) Console.Write(getMapString(i)); Console.ResetColor(); Console.WriteLine(); } 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 int ReadInt()//产生一个整数 { int i = ReadInt(int.MaxValue, int.MinValue); return i; } static int ReadInt(int min, int max)//产生min--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("只能输入数字,请重新输入!"); } } } /// <summary> /// A或者B掷筛子的方法 /// </summary> /// <param name="playerNumber">A掷筛子传0过来 B掷筛子传1过来</param> static void Action(int playerNumber) { if (isStop[playerNumber] == false) { Console.WriteLine("{0}按任意键开始掷筛子......", names[playerNumber]); ConsoleKeyInfo sec = Console.ReadKey(true); step = r.Next(1, 7);//产生一个1到6之间的随机数 if (sec.Key == ConsoleKey.Tab) { ConsoleKeyInfo sec1 = Console.ReadKey(true); if (sec1.Key == ConsoleKey.F1) { step = ReadInt(1, 100); } } Console.WriteLine("{0}掷出了{1}", names[playerNumber], step); Console.WriteLine("{0}按任意键开始行动......", names[playerNumber]); Console.ReadKey(true); playerPos[playerNumber] += step; //注意,一旦坐标发生改变,就要判断坐标值是否>99||<0 checkPos();//检查坐标是否越界 if (playerPos[playerNumber] == playerPos[1 - playerNumber]) //玩家A采到玩家B { playerPos[1 - playerNumber] = 0; msg = string.Format("{0}踩到了{1},{1}退回了原点", names[playerNumber], names[1 - playerNumber]); } else {//没踩到,要判断玩家A现在所在的位置是否有其他关卡 switch (map[playerPos[playerNumber]]) { case 0: //普通,没有效果 msg = ""; break; case 1: //走到了 幸运轮盘关卡 Console.Clear(); Console.WriteLine("你走到了幸运轮盘,请选择运气?"); Console.WriteLine("1 ---交换位置 2---轰炸对方"); int userSelect = ReadInt(1, 2); if (userSelect == 1) {//要与对方交换位置 int temp = playerPos[playerNumber]; playerPos[playerNumber] = playerPos[1 - playerNumber]; playerPos[1 - playerNumber] = temp; msg = string.Format("{0}选了与对方交换位置", names[playerNumber]); } else {//轰炸对方 playerPos[1 - playerNumber] -= 6; msg = string.Format("{0}轰炸了{1},{1}退回了6格", names[playerNumber], names[1 - playerNumber]); checkPos(); } break; case 2: //踩到了地雷 playerPos[playerNumber] -= 6; checkPos(); msg = string.Format("{0}踩到了地雷,{0}退了6格", names[playerNumber]); break; case 3: //暂停一次 isStop[playerNumber] = true; msg = string.Format("{0}走到了暂停,下次暂停一次啊", names[playerNumber]); break; case 4: //踩到时空隧道 playerPos[playerNumber] += 10; msg = string.Format("{0}进入了时空隧道,爽死了,进了10格", names[playerNumber]); break; } } } else { isStop[playerNumber] = false; } if (playerPos[playerNumber] >= 99) { //判断谁胜利,谁失败 Console.Clear(); if (playerPos[0] >= 99) { Console.WriteLine("{0}胜利了!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", names[0]); } else { Console.WriteLine("{0}胜利了!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", names[1]); } } Console.Clear(); drawMap(); if (msg != "") { Console.WriteLine(msg); } Console.WriteLine("{0}掷出了{1},行动完成!", names[playerNumber], step); Console.WriteLine("*************玩家A和玩家B的位置*********"); Console.WriteLine("{0}的位置为:{1}", names[0], playerPos[0] + 1); Console.WriteLine("{0}的位置为:{1}", names[1], playerPos[1] + 1); } } }
改进版
原版:当遇到地雷后退6格,如果在遇到幸运轮盘,地雷,暂停,时空隧道时不做处理
改进版:改进版加进了对此功能的处理。
由于本人没有充分的测试,有可能有不完善的地方,请指正。
//在下面的数组存储我们游戏地图哥哥关卡 //数组的下标0的元素对应地图上的第1格 下标为1的元素对应第2格...下标为n的元素对应n+1格 //在数组中用 1:表示幸运轮盘 ◎ 两种选择 1.交换位置 2.轰炸对方(对方退后6格) // 2: 地雷 ☆ 后退6格 // 3: 暂停 ▲ 暂停一次 // 4: 时空隧道 卐 前进10格 // 0: 表示普通 □ namespace 骑士飞行棋 { class Program { static int[] Map = new int[100]; static string[] names = new string[2]; //names[0]玩家A names[1]玩家B static int[] playerPos = {0, 0}; //playerPos[0]玩家A坐标 playerPos[1]玩家B的坐标 static Random r = new Random(); static bool[] isStop = { false, false };//isStop[0]表示玩家A是否上次一走到暂停,似的话为true,不是为false static int step; static void Main(string[] args) { ShowUI(); InitialMap(); inputName(); Console.Clear(); ShowUI(); Console.WriteLine("对战开始。。。。。"); Console.WriteLine("{0}用A来表示",names[0]); Console.WriteLine("{0}用B来表示", names[1]); Console.WriteLine("如果AB在同一位置,用<>符号表示"); DrawMap(); //A和B轮流掷筛子 while(playerPos[0] < 99 && playerPos[1] < 99) { #region A掷筛子 action(0); #endregion #region B掷筛子 action(1); #endregion } Console.ReadKey(); } static void ShowUI() { Console.WriteLine("*******************************************************"); Console.WriteLine("* *"); Console.WriteLine("* 骑 士 飞 行 棋 *"); Console.WriteLine("* *"); Console.WriteLine("*******************************************************"); } static void inputName() { Console.Write("请输入玩家A的姓名?"); names[0] = Console.ReadLine(); while (names[0] == "") { Console.WriteLine("万家A的姓名不能为空,请重新输入!"); names[0] = Console.ReadLine(); } Console.Write("请输入玩家B的姓名?"); names[1] = Console.ReadLine(); while (names[1] == "" || names[1] == names[0]) { if (names[1] == "") Console.WriteLine("玩家B的姓名不能为空,请重新输入!"); else Console.WriteLine("玩家B的姓名不能和玩家A{0}的姓名一样,请重新输入!", names[0]); names[1] = Console.ReadLine(); } } static void InitialMap() { int[] luckTurn = { 6, 23, 55, 69, 83 };//幸运轮盘 1 int[] landMine = { 5, 11, 17, 33, 38, 50, 64, 80, 94 };//地雷 2 int[] pause = { 9, 27, 60, 93 };//时空隧道 3 int[] timeTunnel = { 20, 30, 40, 63, 72, 88, 90 };//时空隧道 4 for (int i = 0; i < luckTurn.Length; ++i) { int pos = luckTurn[i]; Map[pos] = 1; } for (int i = 0; i < landMine.Length; ++i) { int pos = landMine[i]; Map[pos] = 2; } for (int i = 0; i < pause.Length; ++i) { int pos = pause[i]; Map[pos] = 3; } for (int i = 0; i < timeTunnel.Length; ++i) { int pos = timeTunnel[i]; Map[pos] = 4; } } static void DrawMap() { //第一行 for (int i = 0; i < 30; ++i) { lookSymbol(i); } Console.WriteLine(); //第一列 for(int i = 30; i < 35; ++i) { for(int j = 0;j < 29; ++j) Console.Write(" "); lookSymbol(i); Console.WriteLine(); } //第二行 for (int i = 64; i >= 35; --i) { lookSymbol(i); } Console.WriteLine(); //第二列 for (int i = 65; i < 70; ++i) { lookSymbol(i); Console.WriteLine(); } //第三行 for (int i = 70; i < 100; ++i) { lookSymbol(i); } Console.ResetColor(); Console.WriteLine(); } static void lookSymbol(int i) { if (playerPos[0] == i && playerPos[1] == i) { Console.ForegroundColor = ConsoleColor.Yellow; Console.Write("<>"); } else if (playerPos[0] == i) { Console.ForegroundColor = ConsoleColor.Green; Console.Write("A"); } else if (playerPos[1] == i) { Console.ForegroundColor = ConsoleColor.Green; Console.Write("B"); } else { switch (Map[i]) { case 0: Console.ForegroundColor = ConsoleColor.White; Console.Write("□"); break; case 1: Console.ForegroundColor = ConsoleColor.Blue; Console.Write("◎"); break; case 2: Console.ForegroundColor = ConsoleColor.Magenta; Console.Write("☆"); break; case 3: Console.ForegroundColor = ConsoleColor.Cyan; Console.Write("▲"); break; case 4: Console.ForegroundColor = ConsoleColor.Red; Console.Write("卐"); break; } } } 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 action(int playerNumber) { if(isStop[playerNumber] == false) { Console.WriteLine("玩家{0}按任意键开始掷筛子...", names[playerNumber]); step = r.Next(1, 7); debug();//测试程序用 Console.WriteLine("玩家{0}掷出了{1}", names[playerNumber], step); Console.WriteLine("按任意键开始行动...."); Console.ReadKey(true); Console.Clear(); DrawMap(); playerPos[playerNumber] += step; checkPos(); test(playerNumber); Console.WriteLine("按任意键更新地图...."); Console.ReadKey(true); Console.Clear(); DrawMap(); Console.WriteLine("*************玩家A和玩家B的位置*********"); Console.Write("{0}的位置为:{1}\t", names[0], playerPos[0] + 1); Console.WriteLine("{0}的位置为:{1}", names[1], playerPos[1] + 1); } else { isStop[playerNumber] = false; } } static int ReadInt(int min, int max) { int tmp; while (true) { try { tmp = Convert.ToInt32(Console.ReadLine()); if (tmp < min || tmp > max) { Console.WriteLine("只能输入{0}到{1}之间的数,请重新输入",min,max); continue; } return tmp; } catch { Console.WriteLine("只能输入数字,请重新输入"); } } }//获取min到max之间的整数 static void debug() //测试程序,按F1 { ConsoleKeyInfo sec = Console.ReadKey(true); if (sec.Key == ConsoleKey.Tab) { ConsoleKeyInfo sec1 = Console.ReadKey(true); if (sec1.Key == ConsoleKey.F1) { Console.Write("请输入测试数字:"); step = ReadInt(1, 100); } } } static void test(int playerNumber)//用于A或B经到地雷后退又遇到特殊位置(比如该位置有地雷或者幸运轮盘)的情况 { string msg = ""; if (playerPos[playerNumber] == playerPos[1 - playerNumber] && playerPos[playerNumber] != 0) { playerPos[1 - playerNumber] = 0; msg = string.Format("{0}踩到了{1},{1}退回了原点", names[playerNumber], names[1 - playerNumber]); Console.WriteLine(msg); test(playerNumber); } else { switch (Map[playerPos[playerNumber]]) { case 0: msg = ""; break; case 1: //幸运轮盘 Console.Clear(); Console.WriteLine("恭喜你走到了幸运轮盘,请选择运气"); Console.WriteLine("1-----交换位置, 2-----轰炸对方"); int userSelect = ReadInt(1, 2); if (userSelect == 1) { int t = playerPos[0]; playerPos[0] = playerPos[1]; playerPos[1] = t; msg = string.Format("{0}选择与对方交换位置", names[playerNumber]); } else { playerPos[1 - playerNumber] -= 6; checkPos(); msg = string.Format("{0}轰炸了{1},{1}退后了6格,玩家{1}的位置{2}", names[playerNumber], names[1 - playerNumber],playerPos[1 - playerNumber]+1); } Console.WriteLine(msg); break; case 2: //踩到了地雷 playerPos[playerNumber] -= 6; checkPos(); msg = string.Format("{0}踩到了地雷,{0}退了6格,玩家{0}的位置{1}", names[playerNumber],playerPos[playerNumber]+1); Console.WriteLine(msg); test(playerNumber); break; case 3: //暂停一次 isStop[playerNumber] = true; msg = string.Format("{0}走到了暂停,下次暂停一次啊", names[playerNumber]); Console.WriteLine(msg); break; case 4: //踩到时空隧道 playerPos[playerNumber] += 10; checkPos(); msg = string.Format("{0}进入了时空隧道,爽死了,进了10格,玩家{0}的位置{1}", names[playerNumber],playerPos[playerNumber]+1); Console.WriteLine(msg); test(playerNumber); break; } } if (playerPos[playerNumber] >= 99) { //判断谁胜利,谁失败 Console.Clear(); if (playerPos[0] >= 99) { Console.WriteLine("{0}胜利了!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", names[0]); } else { Console.WriteLine("{0}胜利了!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", names[1]); } return; } } } }