飞行棋程序(附源码)
下面是自己写的飞行棋的小程序,代码写的简单,希望各路大神多多指教----话不多说,直接上代码
一共有三个类,第一个GameManager:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace GameDemo 7 { 8 class GameManager 9 { 10 Random r = new Random(); 11 int[] playerPos = { 0,0 }; 12 13 string[] playerName = new string[2]; 14 public void Start() 15 { 16 17 GameMap myMap = new GameMap(); 18 int step = 0; //这个变量用于存放要走的步数 19 20 myMap.GameSet(); 21 22 myMap.ShowTitle(); 23 24 Console.WriteLine("请输入玩家A的姓名:"); 25 while ((playerName[0] = Console.ReadLine()) == "") 26 { 27 Console.WriteLine("您输入的玩家A的姓名无效,请重新输入!"); 28 } 29 30 Console.WriteLine ("请输入玩家B的姓名:"); 31 while ((playerName[1] = Console.ReadLine()) == "") 32 { 33 Console.WriteLine("您输入的玩家A的姓名无效,请重新输入!"); 34 } 35 36 while (playerName[0] == playerName[1]) 37 { 38 Console.WriteLine("你输入的玩家B的姓名与玩家A的姓名相同,请重新输入!"); 39 playerName[1] = Console.ReadLine(); 40 } 41 42 myMap.ShowTitle(); 43 Console.WriteLine("对战开始:"); 44 Console.WriteLine("{0}士兵用A来表示",playerName[0]); 45 Console.WriteLine("{0}士兵用A来表示", playerName[1]); 46 myMap.DrawMap(playerPos[0], playerPos[1]); 47 48 //假如我们的游戏谁先到最后一格,就胜利 49 50 bool isStopA = false; 51 bool isStopB = false; 52 while (playerPos[0] < 99 && playerPos[1] < 99) //玩家A所走的位置和玩家B所走的位置都要小于99 53 { 54 string msg = ""; 55 //A开始掷骰子的代码 56 #region//A开始掷骰子的代码 57 if (isStopA==false ) 58 { 59 Console.WriteLine("{0}请按任意键开始掷骰子。。。", playerName[0]); 60 Console.ReadKey(true); 61 step = r.Next(1, 7); 62 Console.WriteLine("{0}掷出了:{1}", playerName[0], step); 63 Console.WriteLine("按任意键开始行动。。。"); 64 Console.ReadKey(true); 65 66 playerPos[0] = playerPos[0] + step; 67 68 if (playerPos[0] == playerPos[1]) 69 { 70 msg = string.Format("{0}踩到{1}了,{1}退回到原点", playerName[0], playerName[1]); 71 playerPos[1] = 0; 72 } 73 else 74 { 75 switch (myMap.Map[playerPos[0]]) 76 { 77 case 1://幸运轮盘 78 Console.WriteLine("1----交换位置 2----轰炸"); 79 int userSelect = myMap.ReadInt(1, 2); 80 switch (userSelect) 81 { 82 case 1: 83 int temp = 0; 84 temp = playerPos[0]; 85 playerPos[0] = playerPos[1]; 86 playerPos[1] = temp; 87 msg = string.Format("{0}和{1}交换位置", playerName[0], playerName[1]); 88 break; 89 case 2: 90 playerPos[1] = playerPos[1] - 6; 91 if (playerPos[1] < 0) 92 playerPos[1] = 0; 93 msg = string.Format("{0}轰炸了{1},{1}向后倒退6步", playerName[0], playerName[1]); 94 break; 95 } 96 break; 97 case 2://地雷 98 playerPos[0] = playerPos[0] - 10; 99 if (playerPos[0] < 0) 100 playerPos[0] = 0; 101 msg = string.Format("{0}踩到地雷了!退后10步,oh~~no", playerName[0]); 102 break; 103 case 3://暂停 104 isStopA = true; 105 msg = string.Format("{0}玩家停止一次掷骰子",playerName[0]); 106 break; 107 case 4://时空隧道 108 playerPos[0] = playerPos[0] + 10; 109 msg = string.Format("{0}玩家进入了时空隧道,向前飞了10步", playerName[0]); 110 break; 111 default://普通 112 break; 113 } 114 } 115 126 Console.Clear(); 127 128 myMap.DrawMap(playerPos[0], playerPos[1]); 129 Console.WriteLine(msg); 130 Console.WriteLine("{0}玩家的位置为:{1}", playerName[0], playerPos[0]); 131 Console.WriteLine("{0}玩家的位置为:{1}", playerName[1], playerPos[1]); 132 133 Console.WriteLine(); 134 } 135 else 136 { 137 isStopA = false ; 138 } 139 #endregion 140 endGame(); 141 142 143 //B开始掷骰子的代码 144 #region//B开始掷骰子的代码 145 if (isStopB == false) 146 { 147 msg = ""; 148 Console.WriteLine("{0}请按任意键开始掷骰子。。。", playerName[1]); 149 Console.ReadKey(true); 150 step = r.Next(1, 7); 151 Console.WriteLine("{0}掷出了:{1}", playerName[1], step); 152 Console.WriteLine("按任意键开始行动。。。"); 153 Console.ReadKey(true); 154 155 playerPos[1] = playerPos[1] + step; 156 // if (playerPos[1] >= 99) 157 // {158 endGame(); 159 //} 160 if (playerPos[0] == playerPos[1]) 161 { 162 msg = string.Format("{0}踩到{1}了,{1}退回到原点", playerName[1], playerName[0]); 163 playerPos[0] = 0; 164 } 165 else 166 { 167 switch (myMap.Map[playerPos[1]]) 168 { 169 case 1://幸运轮盘 170 Console.WriteLine("1----交换位置 2----轰炸"); 171 int userSelect = myMap.ReadInt(1, 2); 172 switch (userSelect) 173 { 174 case 1: 175 int temp = 0; 176 temp = playerPos[0]; 177 playerPos[0] = playerPos[1]; 178 playerPos[1] = temp; 179 msg = string.Format("{0}和{1}交换位置", playerName[1], playerName[0]); 180 break; 181 case 2: 182 playerPos[0] = playerPos[0] - 6; 183 if (playerPos[0] < 0) 184 playerPos[0] = 0; 185 msg = string.Format("{0}轰炸了{1},{1}向后倒退6步", playerName[1], playerName[0]); 186 break; 187 } 188 break; 189 case 2://地雷 190 playerPos[1] = playerPos[1] - 10; 191 if (playerPos[1] < 0) 192 playerPos[1] = 0; 193 msg = string.Format("{0}踩到地雷了!退后10步,oh~~no", playerName[1]); 194 break; 195 case 3://暂停 196 isStopB = true; 197 msg = string.Format("{0}玩家停止一次掷骰子", playerName[1]); 198 break; 199 case 4://时空隧道 200 playerPos[1] = playerPos[1] + 10; 201 msg = string.Format("{0}玩家进入了时空隧道,向前飞了10步", playerName[1]); 202 break; 203 default://普通 204 break; 205 } 206 } 207 #region//没用的代码 208 //playerPos[0] = playerPos[0] + JudgeGame(step,playerPos[0],playerPos[1]); 209 //if (step!= JudgeGame(step, playerPos[0], playerPos[1])) 210 //{ 211 // playerPos[0] = playerPos[0] + JudgeGame(step, playerPos[0], playerPos[1]); 212 // if (playerPos[0] < 0) 213 // playerPos[0] = 0; 214 //} 215 216 //playerPos[1]=stepOtherAtoB(ref playerPos[0], ref playerPos[1]); 217 #endregion 218 Console.Clear(); 219 220 myMap.DrawMap(playerPos[0], playerPos[1]); 221 Console.WriteLine(msg); 222 Console.WriteLine("{0}玩家的位置为:{1}", playerName[0], playerPos[0]); 223 Console.WriteLine("{0}玩家的位置为:{1}", playerName[1], playerPos[1]); 224 225 Console.WriteLine(); 226 } 227 else 228 { 229 isStopB = false; 230 } 231 #endregion 232 233 endGame(); 234 235 } 236 237 238 } 239 /// <summary> 240 /// 游戏结束的代码 241 /// </summary> 242 public void endGame() 243 { 244 if (playerPos[0] >= 99 || playerPos[1] >= 99) 245 { 246 if (playerPos[0] >= 99) 247 { 248 Console.Clear(); 249 Console.WriteLine("{0}玩家赢得了比赛!", playerName[0]); 250 } 251 else 252 { 253 Console.Clear(); 254 Console.WriteLine("{0}玩家赢得了比赛!", playerName[1]); 255 } 256 Console.WriteLine("*************************************************"); 257 Console.WriteLine("* *"); 258 Console.WriteLine("* GAME OVER *"); 259 Console.WriteLine("* *"); 260 Console.WriteLine("*************************************************"); 261 return; 262 } 263 264 } 265 357 } 358 }
第二个类:GameMap
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace GameDemo 7 { 8 class GameMap 9 { 10 //在这个数组,存储我们的每一格的地图类型(机关) 11 //数组中的每一格,对应游戏地图上的一格. 12 //下标为[0]的数组元素,对应游戏地图上的第一格 13 14 // 1:幸运轮盘◎ 2:地雷☆ 3: 暂停▲ 4:时空隧道卐 0:普通□ 15 16 int[] map = new int[100]; 17 18 public int[] Map 19 { 20 get { return map; } 21 set { map = value; } 22 } 23 24 //public int[] Map 25 //{ 26 // get { return map; } 27 // set { map = value; } 28 //} 29 30 31 public GameMap() 32 { 33 this.InitalMap(); 34 } 35 public int[] luckTurn = new int[6] { 6, 23, 40, 55, 69, 83 }; //声明数组 36 public int[] landMine = new int[9] { 5, 13, 17, 33, 38, 50, 64, 80, 94 }; 37 public int[] pause = new int[4] { 9, 27, 60, 93 }; 38 public int[] timeTunnel = new int[7] { 20, 25, 45, 63, 72, 88, 90 }; 39 public int InitalMap() 40 { 41 //用于存储在地图中为地雷的下标 42 //int[] luckTurn = { 6, 23, 40, 55, 69, 83 }; // 幸运轮盘◎ 43 //int[] landMine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 };//地雷☆ 44 //int[] pause = { 9, 27, 60, 93 };//暂停▲ 45 //int[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 };//时空隧道卐 46 47 //初始化地图中的幸运轮盘位置 48 for (int i = 0; i < luckTurn.Length; i++) 49 { 50 Map[luckTurn[i]]=1; 51 } 52 //初始化地图中的地雷位置 53 for (int i = 0; i < landMine.Length; i++) 54 { 55 Map[landMine[i]]=2; 56 } 57 //初始化地图中的暂停位置 58 for (int i = 0; i < pause.Length; i++) 59 { 60 Map[pause[i]] = 3; 61 } 62 //初始化地图中的时空隧道位置 63 for (int i = 0; i < timeTunnel.Length; i++) 64 { 65 Map[timeTunnel[i]] = 4; 66 } 67 return 0; 68 } 69 70 71 // //画地图时注意,我们是从0-99依次画地图. 72 //以画第一格为例,需要考虑到的情况如下: 73 //1. A和B 是否同时都在这一格上.如果在,则画一个 <> 74 //2. A是否在当前要画的这一格上,如果在,则画一个 A 75 //3. B是否在当前要画的这一格上,如果在,则画一个 B 76 //4.如果上面的都不成立,我们就要考虑这一格是什么机关.比如我们当前画的是第一格,那么我们就看一下 77 //map[0]中存的值是几.如果是0,表示普通,则画一个□ 如果是1,表示轮盘,则画一个◎ 78 // 如果是2,则表示地雷,画一个☆ 如果是3,则表示暂停,画一个▲ 79 //如果是4,则表示时空隧道,则画一个卐 80 81 82 /// <summary> 83 /// 得到第pos个位置应该画的图型 84 /// </summary> 85 /// <param name="pos">我们要画的地图的那一格的下标</param> 86 /// <param name="player1Pos">玩家A当前所在的地址位置,下标</param> 87 /// <param name="player2Pos">玩家B当前所在的地址位置,下标</param> 88 /// <returns></returns> 89 90 public string GetMapString(int pos, int player1Pos, int player2Pos) 91 { 92 string mystring = ""; 93 if (player1Pos == player2Pos && player1Pos == pos) 94 { 95 mystring="<>"; 96 } 97 else if (player1Pos == pos) 98 { 99 mystring="A"; 100 } 101 else if (player2Pos == pos) 102 { 103 mystring = "B"; 104 } 105 else 106 { 107 switch (Map[pos]) 108 { 109 case 1: 110 mystring = "◎"; 111 break; 112 case 2: 113 mystring = "☆"; 114 break; 115 case 3: 116 mystring = "▲"; 117 break; 118 case 4: 119 mystring = "卐"; 120 break; 121 default: 122 mystring = "□"; 123 break; 124 } 125 } 126 return mystring; 127 128 } 129 130 /// <summary> 131 /// 132 /// </summary> 133 /// <param name="player1Pos">A所在地图的下标</param> 134 /// <param name="player2Pos">B所在地图的下标</param> 135 public void DrawMap(int player1Pos, int player2Pos) 136 { 137 //输出示例 138 Console.WriteLine("图例:幸运轮盘--◎ 地雷--☆ 暂停--▲ 时空隧道--卐 普通--□"); 139 //输出第一行 140 for (int i = 0; i <= 29; i++) 141 { 142 Console.Write(GetMapString(i,player1Pos,player2Pos)); 143 } 144 Console.WriteLine(); 145 146 //输出第一列 147 for(int i=30;i<=34;i++) 148 { 149 for(int j=0;j<29;j++) 150 { 151 Console.Write(" "); 152 } 153 Console.WriteLine(GetMapString(i,player1Pos,player2Pos)); 154 } 155 156 157 //输出第二行 158 for (int i = 64; i >= 35; i--) 159 { 160 Console.Write(GetMapString(i,player1Pos,player2Pos)); 161 } 162 Console.WriteLine(); 163 164 //输出第二列 165 for (int j = 65; j <= 69; j++) 166 { 167 Console.WriteLine(GetMapString(j, player1Pos, player2Pos)); 168 } 169 170 171 //输出第三行 172 for (int i = 70; i <= 99; i++) 173 { 174 Console.Write(GetMapString(i,player1Pos,player2Pos)); 175 } 176 Console.WriteLine(); 177 } 178 179 /// <summary> 180 /// 调用此方法,清屏,然后输出有戏名称 181 /// </summary> 182 public void ShowTitle() 183 { 184 //绘制欢迎界面 185 Console.Clear(); 186 Console.WriteLine("*************************************************"); 187 Console.WriteLine("* *"); 188 Console.WriteLine("* 骑 士 飞 行 棋 *"); 189 Console.WriteLine("* *"); 190 Console.WriteLine("*************************************************"); 191 } 192 193 /// <summary> 194 ///游戏设置,包括开始,背景色,前景色,字体等等 195 /// </summary> 196 public void GameSet() 197 { 198 int userInput = 0; 199 Console.WriteLine("1-----------游戏开始"); 200 Console.WriteLine("2-----------设置背景色"); 201 Console.WriteLine("3-----------设置字体"); 202 Console.WriteLine("请选择:"); 203 userInput = ReadInt(1, 3); 204 //userInput = ReadInt(Console.Read(),Console.Read()); 205 switch (userInput) 206 { 207 case 2: 208 Console.WriteLine("1-------------白色"); 209 Console.WriteLine("2-------------红色"); 210 Console.WriteLine("3-------------黑色"); 211 userInput = ReadInt(1, 3); 212 //userInput = ReadInt(Console.Read(), Console.Read()); 213 switch (userInput) 214 { 215 case 1: 216 Console.BackgroundColor = ConsoleColor.White; 217 break; 218 case 2: 219 Console.BackgroundColor = ConsoleColor.Red; 220 break; 221 case 3: 222 Console.BackgroundColor = ConsoleColor.Black; 223 break; 224 } 225 break; 226 case 3: 227 Console.WriteLine("1-------------白色"); 228 Console.WriteLine("2-------------红色"); 229 Console.WriteLine("3-------------黑色"); 230 userInput=ReadInt(1,3); 231 //userInput = ReadInt(Console.Read(), Console.Read()); 232 userInput = ReadInt(Console.Read(), Console.Read()); 233 switch (userInput) 234 { 235 case 1: 236 Console.ForegroundColor = ConsoleColor.White; 237 break; 238 case 2: 239 Console.ForegroundColor = ConsoleColor.Red; 240 break; 241 case 3: 242 Console.ForegroundColor = ConsoleColor.Black; 243 break; 244 } 245 break; 246 default : 247 break; 248 249 } 250 251 } 252 253 public int ReadInt(int min, int max) 254 { 255 int userInput = 0; 256 while (int.TryParse(Console.ReadLine(), out userInput) == false //把输入的字符转换成32位有符号整数,并把值赋给userInput,然后返回它。 257 || (userInput >= min && userInput <= max) == false) 258 { 259 Console.WriteLine("输入有误,请重新输入!"); 260 } 261 return userInput; 262 263 } 264 265 266 } 267 }
第三个类Program
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace GameDemo 7 { 8 class Program 9 { 10 static void Main(string[] args) 11 { 12 GameManager gm = new GameManager(); 13 gm.Start(); 14 Console.ReadKey(); 15 16 } 17 } 18 }
转载于:https://www.cnblogs.com/daban/archive/2012/06/12/2546634.html
飞行棋程序(附源码)相关推荐
- 关于美团:零基础搭建获利的美团饿了么优惠券CPS小程序附源码
关于美团:零基础搭建获利的美团饿了么优惠券CPS小程序附源码 下面是无裂变版本的搭建教程. 源码地址:http://y.mybei.cn 部署步骤 部署后台,填资料 登录后台 http://q.myb ...
- 手把手教你搭建美团饿了么电影票外卖cps小程序 附源码
手把手教你搭建美团饿了么电影票外卖cps小程序 附源码 外卖红包项目的优势 1.刚需:吃饭是每日刚需,尤其是城市白领,上班族,宅男宅女,等群体点外卖的需求,需求更是高频率(每天). 2.门槛低:平台门 ...
- 世界上最短的DVD解码程序(附源码) (转)
世界上最短的DVD解码程序(附源码) (转)[@more@] 前些天麻省理工学院的两位学生编写出世界上最短的 dvd解码 程序,而这个纪录最近被一位名为Charles M Hannum的程序员打破,他 ...
- 海底飞行棋(含源码)
飞行棋玩过吗?玩过python制作的海底飞行棋玩过吗?额...... 今天就来教制作海底飞行棋 核心玩法 两名玩家通过→和←操控游戏角色,最终全部到达终点,(本游戏适合全年龄段,不要太较真)谁的分数越 ...
- Python程序打包成.exe文件(弹窗恶搞小程序附源码)
0.先来张效果图: 1.安装pyinsatller 打开命令行窗口,输入如下指令:pip3 install pyinstaller 我的已经安装过,所以这样显示. 2.使用pyinstaller打包P ...
- 零基础搭建获利的美团饿了么优惠券CPS小程序(附源码)
1.先上图 2.前言 某天下午,我正在公司认真的写着代码,突然我的手机弹了一个通知,我赶紧抓起手机看看(给自己一个摸鱼的理由) 让我看看到底是谁发消息打扰我认真的工作啊. 害,原来是某个群转发了一个外 ...
- 拟物风格的小程序附源码
1. 前言 对于拟物风格这种UI研究良久,这里把我收集到的一些好用的组件.UI.源码发出来供大家使用.我个人也开源了一款拟物风格的小程序,欢迎借鉴. 各个风格简介 Material Design,物料 ...
- Tkinter超市收银小程序(附源码),看完这一篇你还不会Tkinter吗,快来试试吧
Tkinter超市收银小程序 引言 超市收银小程序,一个小例子快速上手Tkiner使用,虽然最近有好多实验报告要写,但是我还是给大家写一个小例子,之前就用C.C++,java.特别是C#最好用,做过图 ...
- C#中实现视频播放器窗体程序(附源码下载)
场景 效果 实现 新建窗体程序,然后从工具箱中拖拽DataGridView控件,然后在控件右上角点击新增列,设置好每列 的Name属性和Headertext属性. 新建Video类 项目-右击-新增- ...
- MFC Windows 程序设计[319]之渐变标题栏程序(附源码)
MFC Windows 程序设计[319]之渐变标题栏程序 程序之美 前言 主体 运行效果 核心代码 逻辑分析 结束语 程序之美 前言 MFC是微软公司提供的一个类库(class libraries) ...
最新文章
- 第十一届蓝桥杯省赛 A组 试题H:子串分值
- openstack-9:安装neutron网络服务
- 云计算会杀死开源吗?
- Java TCP/UDP socket 编程流程总结
- Kafka创建Topic时如何将分区放置到不同的Broker中
- 希尔排序的详细过程_算法系列: 10大常见排序算法(4)希尔排序
- 图案、标签、logo
- 你还在烦U盘记录该如何消除吗
- ant design vue table 高度自适应_Vue.JS 实现拼拼乐小游戏项目
- Matlab中TCP通讯-实现外部程序提供优化目标函数解
- WINDOWS用VS2010开发NPAPI插件步骤
- 翻译: 2深度学习的基础技能 概览
- 软件开发项目管理经验总结
- CocosBuilder 的使用
- 揭秘淘宝286亿海量图片存储与处理架构(转)
- phpStudy安装框架教程
- 淮师计算机网络题库,淮阴师范学院(淮师)计算机基础一至六章习题
- android监听点击通知栏,android使用NotificationListenerService监听通知栏消息
- 重新定义团队 谷歌的自由精神对所有公司都适用
- C++ 时间函数整理详解