飞行棋程序(附源码)

摘要:
“);34}3536while37{38Console.WriteLine(“您输入的玩家B的名称与玩家A的名称相同,请重新输入!”);39 playerName[1]=Console.ReadLine();40}4142myMap.ShowTitle() ; 43控制台。WriteLine;44控制台。WriteLine;45控制台。WriteLine;46my地图。绘图地图;4748//如果我们的游戏先到达最后一个格子,4950boolisStopA=false;51boolisStopB=false;52while//玩家A和玩家B所处的位置应小于9953{54stringmsg=“”;55//开始掷骰子的代码56#区域//开始掷骰子代码57if58{59Console.WriteLine(“{0}按任意键开始掷骰子…”,playerName[0]);60控制台。ReadKey;61步=右。下一个(1,7);62控制台。WriteLine;63控制台。WriteLine(“按任意键开始操作…”);64控制台。ReadKey;6566playerPos[0]=playerPos[0]+步进;6768if69{70msg=字符串。格式;71playerPos[1]=0;72}73else74{75switch76{77case1://Lucky Wheel 78控制台。WriteLine;79intuserSelect=myMap.ReadInt(1,2);80switch81{82case1:83inttemp=0;84temp=playerPos[0];85playerPos[0]=playerPos[1]=temp;87msg=字符串。[1]=0;93msg=字符串。格式;94break;95}96break;97case2://地雷98playerPos[0]=playerPos[0]-10;99if100playerPos[0]=0;101msg=字符串。格式(“{0}踩雷!

下面是自己写的飞行棋的小程序,代码写的简单,希望各路大神多多指教----话不多说,直接上代码

一共有三个类,第一个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="";
100             }
101             else if (player2Pos == pos)
102             {
103                 mystring = "";
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 }

飞行棋程序(附源码)第1张

免责声明:文章转载自《飞行棋程序(附源码)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇并发模式与 RPS 模式之争,性能压测领域的星球大战spring怎么设置定时任务为每天凌晨2点执行和每小时执行一次?下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

eclipse反编译插件jad安装

几年前就安装过,不过有个独立运行的反编译工具用,所以这个没安装成功也就放弃了。现在发现用独立工具确实麻烦,先要打开工具,在把jar文件拖进来。 不如使用插件方便快捷,一键就可以看到源码。于是再来一次。。。 记得当年出现的问题就是点要反编译的类出现错误:java.io.IOException: Cannot run program "jad" 。。。 此次也...

linux中patch命令 -p 选项

 patch命令和diff命令是linux打补丁的成对命令,diff 负责生产xxxxx.patch文件,patch命令负责将补丁打到要修改的源码上。但是patch命令的参数-p很容易使人迷惑,因为对-p 后面的数字理解不清晰,造成patch打不上,项目时间拖延,很是郁闷。后来仔细实践了一下,弄清楚了-p实际的含义。        举例说明更加容易看懂。比...

PureMVC源码分析

PureMVC 是在基于模型、视图和控制器 MVC 模式建立的一个轻量级的开源应用框架,具有跨平台语言无关性。最初被应用于adobe flex,actionScript开发中,现已被移植到包括c++,java,c#,php等主要语言平台上,在各平台上的实现方式也几乎一样,降低了用户学习成本。 本文从PureMVC actionScript版源码角度分析P...

Java中生成随机数的4种方式!

在 Java 中,生成随机数的场景有很多,所以本文我们就来盘点一下 4 种生成随机数的方式,以及它们之间的区别和每种生成方式所对应的场景。 1.Random Random 类诞生于 JDK 1.0,它产生的随机数是伪随机数,也就是有规则的随机数。Random 使用的随机算法为 linear congruential pseudorandom number...

Android 开 发 资 源 分 享

一、精编教程贴 1、安卓模拟器BlueStacks安装使用教程 http://www.apkbus.com/android-64318-1-1.html 2、Mkey libgdx游戏引擎教程 http://www.apkbus.com/android-57355-1-1.html 3、java基础 与 android学习指南——实例教程 http://w...

【原创】简单快速软件开发平台,C/S架构二次开发平台

简单快速软件开发平台,二次开发平台 二次开发平台可根据企业的需求,灵活快速搭建企业业务管理系统,充分满足企业个性化的需求。C/S系统快速开发框架为企业和个人提供快速开发能力,能快速搭建C/S架构模式的企业管理应用系统。C/S系统快速开发框架提供开发大型软件系统的底层模板,基于开发框架的VS解决方案源码,改名为自己的软件系统名称,如:MyERP,MyMES等...