首页 > 代码库 > C#面向过程项目之飞行棋

C#面向过程项目之飞行棋

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 飞行棋Ver1._0{class Program{//如果元素的值是0 代表这是1个普通//1 幸运轮盘 ◎//2 暂停 ▲//3 地雷 ●//4. 时光隧道 卍static int[] map = new int[100];static int[] pos = new int[2];//第一个元素代表的是玩家A的位置 第2个元素代表的是玩家B的位置static bool[] isStop = {false,false };static string[] names = new string[2];//用于保存玩家AB的姓名 static void Main(string[] args){ ShowUI();//显示LOGO Console.WriteLine("请输入玩家A的名字:");while (true)//进入循环 接受用户输入玩家A的名字 退出条件是 玩家A输入的不是空的字符串{names[0] = Console.ReadLine();if (names[0] == ""){Console.WriteLine("玩家的名字不能为空,请重新输入!");}else{break;//跳出循环}}Console.WriteLine("请输入玩家B的名字:");names[1] = Console.ReadLine();//接收用户的输入 并把用户的输入赋值给names数组的第1个元素while (names[1] == "" || names[1] == names[0])//循环接收用户的输入 {if (names[1] == ""){Console.WriteLine("玩家的名字不能为空,请重新输入!");}else if (names[1] == names[0]){Console.WriteLine("玩家B的名字不能与玩家A的名字一样,请重新输入");}names[1] = Console.ReadLine();}//到这里为止 我们已经接收了用户输入的玩家A和玩家B的姓名 Console.Clear();//将当前控制台上的所有的信息 清空ShowUI();Console.WriteLine(names[0] + "," + names[1] + "欢迎你们来到飞行棋乐园。。。。。。。");Console.WriteLine(names[0] + "用A表示\n" + names[1] + "用B表示\n" + "如果两个在一起用<>表示\n现在对战开始!");InitMap();DrawMap();Random r = new Random();//开始轮流掷骰子while (pos[0] < 99 && pos[1] < 99){ if (isStop[0] == false){#region 玩家A掷骰子Play(0);#endregion }else{isStop[0] = false;}if (pos[0] >= 99){break;}if (isStop[1] == false){#region 玩家B掷骰子Play(1); #endregion}else{isStop[1] = false;}//如果是普通的 则什么效果都木有//如果玩家A踩到玩家B 玩家B的位置到0//如果是幸运轮盘 1可以和对方互换位置 2.可以轰炸对方(让对方退6格)//如果是时空隧道 就让玩家A前进10格//如果是地雷 就退6格//如果是暂停 那么就让玩家A暂停一次}if (pos[0] >= 99){Console.WriteLine("恭喜你" + names[0] + "玩家胜利!");}else{Console.WriteLine("恭喜你" + names[1] + "玩家胜利!");}Console.ReadKey();} ////// 是接收用户从控制台输入指定范围的整数////// 最小值/// 最大值/// static int ReadInt(int min, int max){while (true){string str = Console.ReadLine();//接收用户从控制台输入的字符串 并将字符串赋值给str变量int i;//声明1个iint类型的变量叫做i if (int.TryParse(str, out i))//int.TryParse()这个方法将指定的字符串转换成int类型的 //如果转换成功.那么返回true 并且将转换成功的整数赋值给这个方法的第2个out参数//如果转换失败,那么这个方法就返回false{if (i < min || i > max){Console.WriteLine("输入的数字只能在{0}-{1}之间,请重新输入", min, max);continue;}else{return i;}}else{Console.WriteLine("只能输入整数数字。。。。请重新输入....");continue;}}}static void Play(int playerNumer)//传递0就是表示的是第玩家A调用 1代表的是玩家B在调用{string AorB = playerNumer == 0 ? "A" : "B"; Random r = new Random();Console.WriteLine("玩家{1}:{0}按任意键开始掷骰子....", names[playerNumer], AorB);ConsoleKeyInfo info = Console.ReadKey(true);//接收用户任意键的输入 int step = r.Next(1, 7);//返回1个1-6的随机数if (info.Key == ConsoleKey.Tab){step = 20;}Console.WriteLine("玩家{2}:{0}掷的数字是:{1}", names[playerNumer], step, AorB);pos[playerNumer] += step;CheckPos();string msg = "";//判断玩家A是否踩到了玩家Bif (pos[1] == pos[0]){if (playerNumer == 0)pos[1] = 0;elsepos[0] = 0;}else{switch (map[pos[playerNumer]]){case 0://普通的 没有效果break;case 1://幸运轮盘//如果是幸运轮盘 1可以和对方互换位置 2.可以轰炸对方(让对方退6格)Console.WriteLine("恭喜你,踩到了幸运轮盘,请选择你的运气: 1.和对方互换位置 2.轰炸对方");int userSelect = ReadInt(1, 2);if (userSelect == 1)//和对方互换位置{int temp = pos[0];//交换变量的值pos[0] = pos[1];pos[1] = temp;msg = "你选择了和对方互换位置........";}else //轰炸对方 让对方退6格{if (playerNumer == 0)pos[1] -= 6;else pos[0] -= 6;CheckPos();msg = string.Format("你轰炸了对方,{0}退6格", names[1]);}break;case 2://暂停isStop[playerNumer] = true;msg = "倒霉,暂停一次!";break;case 3://地雷pos[playerNumer] -= 6;CheckPos();msg = "真倒霉,你踩到地雷了,后退6格";break;case 4://时光隧道pos[playerNumer] += 10;CheckPos();msg = "真爽, 你穿越了, 前进10格";break;}}if (msg != ""){Console.WriteLine(msg);}Console.WriteLine("按任意键开始移动。。。");Console.ReadKey(true);Console.Clear();ShowUI();DrawMap();Console.WriteLine("**********************位置信息**********************");Console.WriteLine("玩家A的位置:" + pos[0]);Console.WriteLine("玩家B的位置:" + pos[1]);}////// 检测玩家A和玩家B的位置是否越界///static void CheckPos(){for (int i = 0; i < pos.Length; i++){if (pos[i] > 99){pos[i] = 99;}if (pos[i] < 0){pos[i] = 0;}}} static string GetMapString(int p){//1 幸运轮盘 ◎//2 暂停 ▲//3 地雷 ●//4. 时光隧道 卍 string res = "";if (pos[0] == p && pos[1] == p){Console.ForegroundColor = ConsoleColor.Red;res = ("<>");}else if (pos[0] == p){Console.ForegroundColor = ConsoleColor.Red;res = ("");}else if (pos[1] == p){Console.ForegroundColor = ConsoleColor.Red;res = ("");}else{switch (map[p]){case 0:Console.ForegroundColor = ConsoleColor.White;//是设置控制台的前景色res = "";break;case 1:Console.ForegroundColor = ConsoleColor.Green;res = "";break;case 2:Console.ForegroundColor = ConsoleColor.Yellow;res = "";break;case 3:Console.ForegroundColor = ConsoleColor.DarkRed;res = "";break;case 4:Console.ForegroundColor = ConsoleColor.Blue;res = "";break;}}return res;}static void InitMap(){int[] luckTurn = { 6, 23, 40, 55, 69, 83, 98 };//幸运轮盘int[] landMine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 };//地雷int[] pause = { 9, 27, 60, 93 };//暂停int[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 };//时空隧道//1 幸运轮盘 ◎//2 暂停 ▲//3 地雷 ●//4. 时光隧道 卍for (int i = 0; i < luckTurn.Length; i++){map[luckTurn[i]] = 1;}for (int i = 0; i < landMine.Length; i++){map[landMine[i]] = 3;}for (int i = 0; i < pause.Length; i++){map[pause[i]] = 2;}for (int i = 0; i < timeTunnel.Length; i++){map[timeTunnel[i]] = 4;}}static void DrawMap(){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.Write(GetMapString(i));Console.WriteLine();}//打印第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();}static void ShowUI(){Console.WriteLine("***************************");Console.WriteLine("* *");Console.WriteLine("* 棋 士 飞 行 棋 *");Console.WriteLine("* *");Console.WriteLine("***************************");}}}