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 }; static int step = 0; //存放骰子数 static bool[] isStop = {false,false}; //0是A,1是B,true暂停 static Random r = new Random(); //r产生随机数 static string[] names = new string[2];//string[0]为A的姓名,string[1]为B的姓名 static string msg = ""; static void Main(string[] args) { //显示游戏名称 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的姓名不可为空,请重新输入:"); else Console.WriteLine("B的姓名不可与A相同,请重新输入:"); 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(); DrowMap(); Console.WriteLine("游戏开始..."); //A B玩家轮流掷骰子 while(playerPos[0]<99&&playerPos[1]<99) { //A 掷骰子 if (isStop[0]) //上次走到暂停 { isStop[0] = false; }else{ #region Action(0); #endregion } if (playerPos[0] >= 99) { break; } //B 掷骰子 if (isStop[1])//上次走到暂停 { isStop[1] = false; } else { #region Action(1); #endregion } if (playerPos[1] >= 99) { break; } } //判断胜负条件 Console.Clear(); ShowUI(); if (playerPos[0] >= 99) { Console.WriteLine("{0}胜利了", names[0]); } else { Console.WriteLine("{0}胜利了", names[1]); } Console.ReadKey(); } //掷骰子方法,传0为A掷,传1为B掷, static void Action(int i) { int now = i; int other = 1 - i; //1-i为对方坐标 Console.WriteLine("{0}按任意键开始掷骰子", names[now]); Console.ReadKey(true); step = r.Next(1, 7);//产生一个随机整数 Console.WriteLine("{0}掷出了{1}", names[now], step); Console.WriteLine("{0}按任意键行动", names[now]); playerPos[now] = playerPos[now] + step; CheckPos(); if (playerPos[now] == playerPos[other]) { playerPos[other] = 0; msg = string.Format("{0}踩到了{1},{1}退回原点", names[now], names[other]); } else { switch (map[playerPos[now]]) { case 0://普通 msg = ""; break; case 1://选择盘 Console.Clear(); DrowMap(); Console.WriteLine("{0}请选择操作:", names[now]); Console.WriteLine("1 - 交换位置 2 - 炸弹"); int input = ReadInt(1, 2); if (input == 1) { int temp = playerPos[0]; playerPos[0] = playerPos[1]; playerPos[1] = temp; msg = string.Format("{0}交换了{1}", names[now], names[other]); } else { playerPos[other] -= 6; msg = string.Format("{0}选择{1}退6格", names[now], names[other]); } break; case 2://地雷 playerPos[now] -= 6; msg = string.Format("{0}踩炸弹,退6格", names[now]); break; case 3: msg = string.Format("{0}暂停一次", names[now]); isStop[now] = true; break; case 4://通道 playerPos[now] += 6; msg = string.Format("{0}进通道,进6格", names[now]); break; default: break; } } CheckPos();//越界判断 Console.ReadKey(true); Console.Clear(); DrowMap(); if (msg != "") Console.WriteLine(msg); Console.WriteLine("{0}的位置为{1},{2}的位置为{3}", names[0], playerPos[0] + 1, names[1], playerPos[1] + 1); Console.ReadKey(); } static int ReadInt(int min, int max) { while (true) { try { int num = Convert.ToInt32(Console.ReadLine()); if (num < min || num > max) { Console.WriteLine("只能输入{0}和{1}之间", min, max); continue; } return num; } catch (Exception) { Console.WriteLine("只能输入数字"); } } } /// <summary> /// 根据map数组画地图 /// </summary> static void DrowMap() { Console.WriteLine("图例:选择盘◎, 地雷☆, 暂停▲, 通道卍"); //第一行30个图形 for (int i = 0; i < 30; i++) { Console.Write(GetMapString(i)); } Console.Write("\n"); //换行 //5行开头为空格,画图形后换行 for (int i = 30; i < 35; i++) { Console.Write(" "); Console.Write(GetMapString(i)); Console.Write("\n"); } //中间30个图形 for (int i = 64; i >= 35; i--) { Console.Write(GetMapString(i)); } Console.Write("\n"); //换行 //这5行后边为空格,并换行 for (int i = 65; i < 70; i++) { Console.Write(GetMapString(i)); Console.Write("\n"); } //下边30个图形 for (int i = 70; i < 100; i++) { Console.Write(GetMapString(i)); } Console.Write("\n"); Console.ResetColor(); } //根据位置返回要画的图形 static string GetMapString(int pos) { string result = ""; if (playerPos[0] == pos && playerPos[1]==pos) { Console.ForegroundColor = ConsoleColor.White; result = "<>"; } else if (playerPos[0] == pos) { Console.ForegroundColor = ConsoleColor.White; result = "Α"; } else if (playerPos[1] == pos) { Console.ForegroundColor = ConsoleColor.White; result = "Β"; } else { switch (map[pos]) { case 0: Console.ForegroundColor = ConsoleColor.White; result = "□"; break; case 1: Console.ForegroundColor = ConsoleColor.DarkYellow; result = "◎"; break; case 2: Console.ForegroundColor = ConsoleColor.DarkRed; result = "☆"; break; case 3: Console.ForegroundColor = ConsoleColor.DarkGreen; result = "▲"; break; case 4: Console.ForegroundColor = ConsoleColor.Yellow; result = "卍"; break; default: break; } } return result; } static void CheckPos() { for (int i = 0; i < 2; i++) { if (playerPos[i] > 99) playerPos[i] = 99; else if (playerPos[i] < 0) { playerPos[i] = 0; } } } /// <summary> /// 用于绘制飞行棋名称 /// </summary> static void ShowUI() { Console.WriteLine("****************************************************"); Console.WriteLine("* *"); Console.WriteLine("* 飞行棋 *"); Console.WriteLine("* *"); Console.WriteLine("****************************************************"); } /// <summary> /// 游戏开始时初始化地图 /// </summary> static void InitialMap() { int[] selectNum = { 6, 23, 40, 55, 69, 83 }; //选择盘位置 int[] backNum = { 5, 13, 17, 33, 38, 50, 64, 80, 94 };//地雷位置 int[] parseNum = { 9, 27, 60, 93 };//暂停位置 int[] goNum = { 20, 25, 45, 63, 72, 88, 90 };//通道位置 foreach (int i in selectNum) { map[i] = 1; } foreach (int i in backNum) { map[i] = 2; } foreach (int i in parseNum) { map[i] = 3; } foreach (int i in goNum) { map[i] = 4; } } } }
时间: 2024-09-29 16:51:31