using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;/** 游戏规则:* 1、如果玩家A踩到了玩家B,玩家B退6格;* 2、踩到了地雷,退6格;* 3、踩到了时空隧道,进10格;* 4、踩到了幸运轮盘,1-交换位置,2-使对方退6格;* 5、踩到了暂停,暂停一回合;* 6、踩到了方块,什么都不干。*/namespace飞行棋
{class Program{// 用静态字段来模拟全局变量static int[] Maps = new int[100];// 声明一个静态数组来存储玩家A和玩家B的坐标static int[] PlayerPositions = new int[2];// 声明一个静态数组来存储玩家A和玩家B是否暂停static bool[] FlagOfPause = new bool[2];  // bool数组的默认值全是false// 声明一个静态数组来存储玩家A和玩家的名字static string[] PlayerNames = new string[2];// 声明是否游戏结束的一个Flag变量,// 值为-1时,游戏尚未结束;// 值为0时,游戏结束,胜利者为PlayerNames[0]// 值为1时,游戏结束,胜利者为PlayerNames[1]static int WinnerNumber = -1;static void Main(string[] args){GameShow();#region 输入玩家的姓名Console.ForegroundColor = ConsoleColor.White;Console.WriteLine("请输入玩家A的姓名:");PlayerNames[0] = Console.ReadLine();while (PlayerNames[0] == ""){Console.WriteLine("玩家A的姓名不能为空,请重新输入");PlayerNames[0] = Console.ReadLine();}Console.WriteLine("请输入玩家B的姓名:");PlayerNames[1] = Console.ReadLine();while (PlayerNames[1] == "" || PlayerNames[1] == PlayerNames[0]){if (PlayerNames[1] == ""){Console.WriteLine("玩家B的姓名不能为空,请重新输入玩家B的姓名:");}else{Console.WriteLine("玩家B的姓名不能与玩家A的姓名一样,请重新输入玩家B的姓名:");}PlayerNames[1] = Console.ReadLine();}#endregion// 玩家姓名输入Ok之后,我们首先应该清屏Console.Clear();  // 清屏GameShow();Console.ForegroundColor = ConsoleColor.White;Console.WriteLine("{0}的士兵用A表示\r\n{1}的士兵用B表示", PlayerNames[0], PlayerNames[1]);InitialMap();DrawMap();#region 游戏开始// 当玩家A跟玩家B没有一个人到达终点的时候,两个玩家不停地去玩游戏while (PlayerPositions[0] < 99 && PlayerPositions[1] < 99){if (FlagOfPause[0]){FlagOfPause[0] = false;}else{PlayGameOnce(0);}if (PlayerPositions[0] >= 99){//Console.WriteLine("玩家{0}已经到达了终点", PlayerNames[0]);continue;}if (FlagOfPause[1]){FlagOfPause[1] = false;}else{PlayGameOnce(1);}//if (PlayerPositions[0] >= 99)//{//    Console.WriteLine("玩家{0}已经到达了终点", PlayerNames[0]);//    continue;//}}  // while()if (Program.WinnerNumber > -1){Console.ForegroundColor = ConsoleColor.White;Console.WriteLine("{0}无耻地战胜了{1}", PlayerNames[WinnerNumber], PlayerNames[1 - WinnerNumber]);Console.WriteLine();Console.WriteLine("C#牛逼,十项全能,天下第一!");Console.WriteLine("C#牛逼,十项全能,天下第一!");Console.WriteLine("C#牛逼,十项全能,天下第一!");}#endregionConsole.WriteLine("按回车键结束");Console.ReadLine();}  // Main方法结尾/// <summary>/// 画游戏头/// </summary>public static void GameShow(){Console.ForegroundColor = ConsoleColor.Yellow;Console.WriteLine("**********************************");Console.ForegroundColor = ConsoleColor.Red;Console.WriteLine("**********************************");Console.ForegroundColor = ConsoleColor.Green;Console.WriteLine("**********************************");Console.ForegroundColor = ConsoleColor.Cyan;Console.WriteLine("*******玉田新村の飞行棋游戏*******");Console.ForegroundColor = ConsoleColor.Blue;Console.WriteLine("**********************************");Console.ForegroundColor = ConsoleColor.Magenta;Console.WriteLine("**********************************");Console.ForegroundColor = ConsoleColor.DarkRed;Console.WriteLine("**********************************");Console.WriteLine();}  // GameShow()方法结尾/// <summary>/// 初始化地图/// </summary>public static void InitialMap(){int[] luckyTurn = new int[] { 6, 23, 40, 55, 69, 83 };  // 幸运轮盘1 ◎foreach (int index in luckyTurn){Maps[index] = 1;}int[] landMind = new int[] { 5, 13, 17, 33, 38, 50, 64, 80, 94 };  // 地雷2 ☆for (int i = 0; i < landMind.Length; i++){Maps[landMind[i]] = 2;}int[] pause = new int[] { 9, 27, 60, 93 };  // 暂停3 ▲for (int i = 0; i < pause.Length; i++){Maps[pause[i]] = 3;}int[] timeTunnel = new int[] { 20, 25, 45, 63, 72, 88, 90 };  // 时空隧道4 卍for (int i = 0; i < timeTunnel.Length; i++){Maps[timeTunnel[i]] = 4;}}/// <summary>/// 绘制地图/// </summary>public static void DrawMap(){Console.ForegroundColor = ConsoleColor.White;Console.WriteLine("图例:幸运轮盘 ◎,地雷 ☆,暂停 ▲,超时空隧道 卍");Console.WriteLine();#region 第一横行for (int i = 0; i < 30; i++){Console.Write(StringOfPosition(i));}Console.WriteLine();#endregion#region 第一竖列for (int i = 30; i < 35; i++){for (int j = 0; j < 29; j++){Console.Write(" ");}Console.WriteLine(StringOfPosition(i));}#endregion#region 第二横行for (int i = 64; i > 34; i--){Console.Write(StringOfPosition(i));}Console.WriteLine();#endregion#region 第二竖列for (int i = 65; i < 70; i++){Console.WriteLine(StringOfPosition(i));}#endregion#region 第三横行for (int i = 70; i < 100; i++){Console.Write(StringOfPosition(i));}Console.WriteLine();#endregionConsole.WriteLine();}/// <summary>/// 返回地图上某一位置上的关卡/// </summary>/// <param name="i">地图的位置</param>/// <returns>从0开始,第i个位置上代表关卡的字符</returns>public static string StringOfPosition(int i){string str = null;// 如果玩家A跟玩家B的坐标相同,并且都在地图上。画一个尖括号if (PlayerPositions[0] == PlayerPositions[1] && PlayerPositions[0] == i){Console.ForegroundColor = ConsoleColor.Cyan;str = "<>";}else if (PlayerPositions[0] == i){// shift+空格Console.ForegroundColor = ConsoleColor.Cyan;str = "A";}else if (PlayerPositions[1] == i){Console.ForegroundColor = ConsoleColor.Cyan;str = "B";}else{switch (Maps[i]){case 0:Console.ForegroundColor = ConsoleColor.Yellow;str = "□";break;case 1:Console.ForegroundColor = ConsoleColor.Green;str = "◎";break;case 2:Console.ForegroundColor = ConsoleColor.Red;str = "☆";break;case 3:Console.ForegroundColor = ConsoleColor.Blue;str = "▲";break;case 4:Console.ForegroundColor = ConsoleColor.DarkMagenta;str = "卍";break;} // switch} // elsereturn str;}  // DrawOnePositin()public static void JudgeInNewPosition(int playerNumber){if (PlayerPositions[playerNumber] < 0){Console.WriteLine("玩家{0}不能退出到地图之外,返回到出发的位置", PlayerNames[playerNumber]);PlayerPositions[playerNumber] = 0;}else if (PlayerPositions[playerNumber] >= 99){WinnerNumber = playerNumber;Console.WriteLine("{0}已经到达了终点", PlayerNames[playerNumber]);PlayerPositions[playerNumber] = 99;//return;}else if (PlayerPositions[playerNumber] == PlayerPositions[1 - playerNumber]){Console.WriteLine("玩家{0}踩到了玩家{1},玩家{1}退6格", PlayerNames[playerNumber], PlayerNames[1 - playerNumber]);PlayerPositions[1 - playerNumber] -= 6;JudgeInNewPosition(1 - playerNumber);}else //踩到了关卡{switch (Maps[PlayerPositions[playerNumber]]){case 0:Console.WriteLine("玩家{0}踩到了方块,安全", PlayerNames[playerNumber]);Console.ReadKey(true);break;case 1:Console.WriteLine("玩家{0}踩到了幸运轮盘,请选择 1--交位置,2--轰炸对方", PlayerNames[playerNumber]);string input = Console.ReadLine();while (true){if (input == "1"){Console.WriteLine("玩家{0}选择与玩家{1}交换位置", PlayerNames[playerNumber], PlayerNames[1 - playerNumber]);Console.ReadKey(true);int temp;temp = PlayerPositions[playerNumber];PlayerPositions[playerNumber] = PlayerPositions[1 - playerNumber];PlayerPositions[1 - playerNumber] = temp;Console.WriteLine("交换完成,按任意键继续游戏");Console.ReadKey(true);break;}else if (input == "2"){Console.WriteLine("玩家{0}选择轰炸玩家{1},玩家{1}退6格", PlayerNames[playerNumber], PlayerNames[1 - playerNumber]);Console.ReadKey(true);PlayerPositions[1 - playerNumber] -= 6;//Console.WriteLine("玩家{0}退了6格", PlayerNames[1 - playerNumber]);JudgeInNewPosition(1 - playerNumber);Console.ReadKey(true);break;}else{Console.WriteLine("只能输入1或者2,请重新选择  1--交位置,2--轰炸对方");input = Console.ReadLine();}}//whilebreak;case 2:Console.WriteLine("玩家{0}踩到了地雷,退6格", PlayerNames[playerNumber]);PlayerPositions[playerNumber] -= 6;Console.ReadKey(true);JudgeInNewPosition(playerNumber);break;case 3:Console.WriteLine("玩家{0}踩到了暂停,暂停一回合", PlayerNames[playerNumber]);FlagOfPause[playerNumber] = true;Console.ReadKey(true);break;case 4:Console.WriteLine("玩家{0}踩到了时空隧道,前进10格", PlayerNames[playerNumber]);PlayerPositions[playerNumber] += 10;Console.ReadKey(true);JudgeInNewPosition(playerNumber);break;}//switch}//else}/// <summary>/// 玩游戏/// </summary>public static void PlayGameOnce(int playerNumber){Console.ForegroundColor = ConsoleColor.White;Console.WriteLine("玩家{0}按任意键开始掷骰子", PlayerNames[playerNumber]);Console.ReadKey(true);Random r = new Random();int steps = r.Next(1, 7);Console.WriteLine("玩家{0}掷骰子掷出了{1}", PlayerNames[playerNumber], steps);Console.WriteLine("玩家{0}按任意键开始行动", PlayerNames[playerNumber]);Console.ReadKey(true);PlayerPositions[playerNumber] += steps;JudgeInNewPosition(playerNumber);Console.WriteLine("玩家{0}行动完了", PlayerNames[playerNumber]);Console.ReadKey(true);Console.Clear();DrawMap();}}
}

学习的老赵的C#飞行棋例子代码 CSharp相关推荐

  1. 根据老赵轻量级Actor进行修改的Actor模型

    学习了老赵轻量级Actor模型,并在实际中使用,效果不错. 老赵轻量级Actor模型: ActorLite:一个轻量级Actor模型实现(上) ActorLite:一个轻量级Actor模型实现(中) ...

  2. VS 2008的JavaScript代码提示功能 (学习老赵视频的笔记)

    学习老赵的视频ASP.NET AJAX深入浅出系列课程(19):VS 2008的JavaScript代码提示功能(Level 200) 自己做的demo,记下来以便查阅!感谢赵老师! 原来js还可以像 ...

  3. 老赵的自然数分解——少侠之对象解

    自然数分解算法的起因介绍,老赵 前几天贴了个非递归的 今天继续搞一个使用对象的解 坚持用对象来解决问题的一个原因,是想证明使用面向对象不是造成算法速度慢的根本原因 例如,我这个面向对象的解,其运行速度 ...

  4. 老赵谈IL(3):IL可以看到的东西,其实大都也可以用C#来发现

    在上一篇文章中,我们通过一些示例谈论了IL与CLR中的一些特性.IL与C#等高级语言的作用类似,主要用于表示程序的逻辑.由于它同样了解太多CLR中的高级特性,因此它在大部分情况下依旧无法展现出比那些高 ...

  5. 解读网络“攻城狮”的发展---老赵带你铺一段路

    成为网络工程师要学什么.如何学.在哪儿学,网络工程师的发展如何.薪资待遇如何等问题困扰着很多网络技术人员,尤其是正在学校苦苦寻求真理的同学们.我也是一名在校大学生,深知现在同学们的苦恼.如果能掌握关于 ...

  6. 艾伟:老赵谈IL(3):IL可以看到的东西,其实大都也可以用C#来发现

    在上一篇文章中,我们通过一些示例谈论了IL与CLR中的一些特性.IL与C#等高级语言的作用类似,主要用于表示程序的逻辑.由于它同样了解太多CLR中的高级特性,因此它在大部分情况下依旧无法展现出比那些高 ...

  7. 老赵书托(1):写在前面

    最近我思考和总结地越来越多,感觉也是时候把自己许多年来的经验进行一番总结和整理.谈基础与能力的时候,我把人脑比喻为"存储器",里面存放了"知识"和"能 ...

  8. 《老赵手动整理的Python笔记(一)》

    <老赵手动整理的Python笔记(一)> 下面的内容的都是老赵在学习Python过程中记录的一些知识点,还有对于不理解的地方的一些研究.老赵有点强迫症,对于不明白的事情总想去弄明白,不然饭 ...

  9. 老赵减肥记:减肥?跟玩儿似的。

    老赵成功减肥,各中冷暖自知,与大家共勉.有志者事竟成,不是骗小孩的. 有人说程序员的生活是单调的,我觉得程序员的生活是美好的.每天写写程序.听听音乐,下班后聊聊天.看看电影,周末睡睡懒觉.四处逛逛,日 ...

最新文章

  1. 博世力士乐液压_[Event Review] Company Visit Bosch Rexroth 博世力士乐液压工厂参观
  2. web.config总结
  3. python with open 循环建立指定名字文件_Python基础——文件
  4. VMware Tools显示灰色的办法
  5. 《开源框架那点事儿33》极限挑战:用一条循环语句正确输出99表!【前两名奖图书一本】...
  6. A.2.3-猜数字游戏
  7. springboot响应结果超长(7.8M)浏览器无法接收
  8. jqgrid for asp.net 遍历所有列rowObject时不用输入编号
  9. Emacs:报错:File error: Cannot open load file,cl-lib解决
  10. [URAL]1014 The Product of Digits
  11. python中grid的用法_SVM中如何使用grid.py
  12. Java实现语音播报功能
  13. IE新功能:十大特色IE插件(转)
  14. 三极管三种基本放大电路
  15. python合并相同内容单元格_实例28_在Excel表格中将上下行相同内容的单元格自动合并...
  16. springboot整合谷歌身份验证
  17. Spring MVC+Spring+Mybatis
  18. HTML+CSS+移动端前端
  19. 鲜为人知 的 人名典故
  20. Java Service Wrapper 发布Java程序为Windows服务

热门文章

  1. 30岁男子考上上海公务员年薪20万,却为了买房想去头条,结果蒙了
  2. Linux基础命令行(tar---文件归档)
  3. 安装Matplotlib
  4. linux服务器上传文件大小被限制了,在linux服务器上更改文件上传大小
  5. 笔记本电脑有必要分盘吗?电脑是分盘好还是不分盘好
  6. 【原创】基于SSM的果蔬水果蔬菜商城(SSM鲜花果蔬商城毕业设计)
  7. Go sync.RWMutex 实现线程安全 map 读写
  8. 小程序+node获取用户openid
  9. Traditional Gomoku
  10. PDF文件批量下载爬虫