首页 > 代码库 > 纠正飞行棋的错误

纠正飞行棋的错误

错误1:在运行暂停功能时,这个暂停功能可以实现,但无法显示提示信息。

改正如下:

case 3:
                                Console.Clear();
                                Program.drawmap();
                                isstop[0] = true;
                                Console .WriteLine ("{0}走到了暂停,暂停一次!", name[0]);
                                Console.WriteLine("按任意键继续...");
  Console.ReadKey(true);

原因:缺少Console.WriteLine("按任意键 继续...");

Console.ReadKey(true);

错误二:有一段代码没有效果

改正如下:

                    Console.Clear();
                    Program.chushiMap();

                    Program.drawmap();
                    Console.WriteLine("{0}掷出了{1}", name[0], playpost[0]);
                    Console.WriteLine("*******************************************");
                    Console.WriteLine("{0}的位置{1}", name[0], playpost[0] + 1);
                    Console.WriteLine("{0}的位置{1}", name[1], playpost[1] + 1);

原因:清屏的位置错误,应放在输出的前面。

错误三:玩家走不出去

改正如下:

int[] Landmine = new int[] { 45, 58, 62, 79, 88, 98 };//地雷

原因:将最后一个99设置为了一个地雷,踩了就后退六步,所以无法走出去。

下面是完整代码:

技术分享
  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 
  6 namespace 骑士飞行棋
  7 {
  8     class Program
  9     {
 10         static int[] map = new int[100];
 11         static int[] playpost = new int[]{0,0};
 12         /// <summary>
 13         /// 界面函数
 14         /// </summary>
 15         static void Menus()
 16         {
 17             Console.WriteLine("\t************************************************\t");
 18             Console.WriteLine("\t*                                              *\t");
 19             Console.WriteLine("\t*            骑  士  飞  行  棋                *\t");
 20             Console.WriteLine("\t************************************************\t");
 21         }
 22         /// <summary>
 23         /// 地图
 24         /// </summary>
 25         static void chushiMap()
 26         {
 27             
 28             int[] luckkey = new int[] { 6, 12, 19, 28, 46, 66 };//幸运轮盘
 29             int[] Landmine = new int[] { 45, 58, 62, 79, 88, 98 };//地雷
 30             int[] Pause = new int[] { 33, 51, 68, 70, 80, 95 };//暂停
 31             int[] Timeturn = new int[] { 25, 39, 47, 59, 71 };//时空隧道
 32             for (int i = 0; i < luckkey.Length; i++)
 33             {
 34                 int pos = luckkey[i];
 35                 map[pos] = 1;
 36             }
 37             for (int i = 0; i < Landmine.Length; i++)
 38             {
 39                 int pos = Landmine [i];
 40                 map[pos] = 2;
 41             }
 42             for (int i = 0; i < Pause.Length; i++)
 43             {
 44                 int pos = Pause [i];
 45                 map[pos] = 3;
 46             }
 47             for (int i = 0; i < Timeturn.Length; i++)
 48             {
 49                 int pos = Timeturn [i];
 50                 map[pos] = 4;
 51             }
 52            
 53         }
 54         /// <summary>
 55         /// 判断AB的位置
 56         /// </summary>
 57         /// <param name="pos"></param>
 58         /// <returns></returns>
 59         static string panduan(int pos)
 60          {
 61              string result = "";
 62              if (playpost[0] == pos && playpost[1] == pos)
 63              {
 64                  Console.ForegroundColor = ConsoleColor.Red;
 65                  result = "<>";
 66              }
 67              else if (playpost[0] == pos)
 68              {
 69                  Console.ForegroundColor = ConsoleColor.Red;
 70                  result = "Α";
 71              }
 72              else if (playpost[1] == pos)
 73              {
 74                  Console.ForegroundColor = ConsoleColor.Red;
 75                 result = "Β";
 76              }
 77              else
 78              {
 79                  switch (map[pos])
 80                  {
 81                      case 0:
 82                          result = ""; break;
 83                      case 1:
 84                          Console.ForegroundColor = ConsoleColor.Yellow;
 85                         result = ""; break;
 86                      case 2:
 87                          Console.ForegroundColor = ConsoleColor.Green;
 88                          result = ""; break;
 89                      case 3:
 90                          Console.ForegroundColor = ConsoleColor.DarkRed;
 91                         result = ""; break;
 92                      case 4:
 93                          Console.ForegroundColor = ConsoleColor.Blue;
 94                         result = ""; break;
 95 
 96                  }
 97              }
 98              return result;
 99          }
100         /// <summary>
101         /// 绘制AB的位置,在地图上
102         /// </summary>
103         static void drawmap()
104         {
105             for (int i = 0; i <= 29; i++)
106             {
107                 Console.Write(Program.panduan(i));
108                 
109             }
110             Console.WriteLine();
111             for (int i = 30; i <= 34; i++)
112             {
113                 for (int j = 0; j < 29; j++)
114                 {
115                     Console.Write ("  ");
116                 }
117                 Console.WriteLine(panduan(i));
118                
119             }
120             for (int i = 64; i >= 35; i--)
121             {
122                 Console.Write(panduan(i));
123             }
124             Console.WriteLine();
125             for (int i = 65; i <= 69; i++)
126             {
127                 Console.WriteLine(panduan(i));
128             }
129             for (int i = 70; i <= 99; i++)
130             {
131                 Console.Write(panduan(i));
132             }
133             Console.WriteLine();
134         }
135        
136         /// <summary>
137         /// 作为玩家A和玩家B坐标越界的一个判断
138         /// </summary>
139         /// <param name="args"></param>
140         static void check()
141         {
142             for (int i = 0; i <= 1; i++)
143             {
144                 if (playpost[i] > 99)
145                 {
146                     playpost[i] = 99;
147                 }
148                 else if (playpost[i] < 0)
149                 {
150                     playpost[i] = 0;
151                 }
152             }
153         }
154         static int ReadInt()
155         {
156             int i = ReadInt(int.MinValue, int.MaxValue);
157             return i;
158         }
159         static int ReadInt(int min , int max)
160        {
161            while (true)
162            {
163                try
164                {
165                    int number = Convert.ToInt32(Console.ReadLine());
166                    if (number < min || number > max)
167                    {
168                        Console.WriteLine("只能输入0-1之间的数字,从新输入!");
169                        continue;
170                    }
171                    return number;
172                }
173                catch
174                {
175                    Console.WriteLine("只能输入数字,从新输入!");
176                }
177            }
178            
179        }
180         /// <summary>
181         /// 主函数
182         /// </summary>
183         /// <param name="args"></param>
184         static void Main(string[] args)
185         {
186             
187             Random random = new Random();
188             bool[] isstop = { false, false };//定义一个布尔类型,false表示运行、true表示暂停
189             int step = 0;
190             string[] name = new string[2];//定义AB两玩家的名称(name[0]为A玩家name[1]为B玩家)
191             Program.Menus();//显示游戏名称
192             Console.WriteLine("请输入玩家A的名称:");
193             name[0] = Console.ReadLine();
194             //判断用户输入的内容是否为空
195             while (name[0] == "")
196             {
197                 Console.WriteLine("玩家名称不能为空,请重新输入!");
198                 name[0] = Console.ReadLine();
199             }
200             Console.WriteLine("请输入玩家B的名称:");
201             name[1] = Console.ReadLine();
202             //判断用户输入的内容是否为空或是否与玩家A的名称相同
203             while (name[1] == "" || name [1] == name [0])
204             {
205                 if (name[1] == "")
206                 {
207                     Console.WriteLine("玩家名称不能为空,请重新输入!");
208                     name[1] = Console.ReadLine();
209                 }
210                 else if (name[1] == name[0])
211                 {
212                     Console.WriteLine("玩家B的名称不能与玩家A的名称相同,请重新输入!");
213                     name[1] = Console.ReadLine();
214                 }
215             }
216             Console.Clear();
217             Program.Menus();
218             Console.WriteLine("对战开始!");
219             Console.WriteLine("{0}玩家使用A进行游戏", name[0]);
220             Console.WriteLine("{0}玩家使用B进行游戏", name[1]);
221             Console.WriteLine("当AB处在相同位置上时,显示<>");
222             Console.WriteLine("幸运轮盘◎ 炸弹※ 暂停▲ 时光隧道卍 ");
223             Console.WriteLine(); 
224             Program.chushiMap();
225             Program.drawmap();
226             Console.Clear();
227              Program.drawmap();
228             while(playpost [0] < 99 && playpost [1] < 99)
229             {
230                 if (isstop[0] == false)
231                 {
232                     #region 玩家A之色子
233 
234                     Console.WriteLine("{0}按任意键开始游戏", name[0]);
235                     Console.ReadKey(true);
236                     step = random.Next(1, 7);
237                     Console.WriteLine("{0}执出了:{1}", name[0], step);
238                     Console.WriteLine("按任意键执行。。。。。");
239                     Console.ReadKey(true);
240                     playpost[0] += step;
241                     Program.check();
242                     if (playpost[0] == playpost[1])
243                     {
244                         Console.Clear();
245                         Program.drawmap();
246                         Console.WriteLine("{0}撞到了{1},把{1}撞到了起点!", name [0],name [1]);
247                         playpost[1] = 0;
248                         Console.WriteLine("按任意键继续...");
249                         Console.ReadKey(true);
250                     }
251                     else
252                     {
253                         switch (map[playpost[0]])
254                         {
255                             case 0:
256                                 break;
257                             case 1:
258                                 Console.Clear();
259                                 Program.drawmap();
260                                 Console.WriteLine("恭喜您!走到了幸运轮盘,请您选择您的幸运数(1-2):");
261                                 Console.WriteLine("---------------------------------------------------");
262                                 Console.WriteLine("        1. 交换位置          2.轰炸对方          ");
263                                 int userselect = ReadInt(1, 2);
264                                 if (userselect == 1)
265                                 {
266                                     int temp = playpost[0];
267                                     playpost[0] = playpost[1];
268                                     playpost[1] = temp;
269                                 }
270                                 else
271                                 {
272                                     playpost[1] = playpost[1] - 6;
273                                     Program.check();
274                                 }
275                                 break;
276                             case 2:
277                                  Console.Clear();
278                                 Program.drawmap();
279                                 Console.WriteLine("糟糕了!您踩到地雷了,后退六步。");
280                                 playpost[0] = playpost[0] - 6;
281                                 Program.check();
282                                 Console.WriteLine("按任意键继续...");
283                         Console.ReadKey(true);
284                                 break;
285                             case 3:
286                                 Console.Clear();
287                                 Program.drawmap();
288                                 isstop[0] = true;
289                                 Console .WriteLine ("{0}走到了暂停,暂停一次!", name[0]);
290                                 Console.WriteLine("按任意键继续...");
291                         Console.ReadKey(true);
292                                 break;
293                             case 4:
294                                  Console.Clear();
295                                 Program.drawmap();
296                                 Console.WriteLine("恭喜您,走到了时空隧道!前进十步");
297                                 playpost[0] = playpost[0] + 10;
298                                 Program.check();
299                                 Console.WriteLine("按任意键继续...");
300                         Console.ReadKey(true);
301                                 break;
302                         }
303 
304                     }
305                     Console.Clear();
306                     Program.chushiMap();
307 
308                     Program.drawmap();
309                     Console.WriteLine("{0}掷出了{1}", name[0], playpost[0]);
310                     Console.WriteLine("*******************************************");
311                     Console.WriteLine("{0}的位置{1}", name[0], playpost[0] + 1);
312                     Console.WriteLine("{0}的位置{1}", name[1], playpost[1] + 1);
313                     #endregion
314                 }
315                 else
316                 {
317                     isstop[0] = false;
318                 }
319                 if (playpost[0] >= 99)
320                 {
321                     break;
322                 }
323                 if (isstop[1] == false)
324                 {
325                     #region 玩家B之色子
326                     Console.WriteLine("{0}按任意键开始游戏", name[1]);
327                     Console.ReadKey(true);
328                     step = random.Next(1, 7);
329                     Console.WriteLine("{0}执出了:{1}", name[1], step);
330                     Console.WriteLine("按任意键执行。。。。。");
331                     Console.ReadKey(true);
332                     playpost[1] += step;
333                     Program.check();
334                     if (playpost[0] == playpost[1])
335                     {
336                         Console.Clear();
337                         Program.drawmap();
338                         Console.WriteLine("{0}撞到了{1},把{1}撞到了起点!", name[1], name[2]);
339                         playpost[0] = 0;
340                         Console.WriteLine("按任意键继续...");
341                         Console.ReadKey(true);
342                     }
343                     else
344                     {
345                         switch (map[playpost[1]])
346                         {
347                             case 0:
348                                 break;
349                             case 1:
350                                 Console.Clear();
351                                 Program.drawmap();
352                                 Console.WriteLine("恭喜您!走到了幸运轮盘,请您选择您的幸运数(1-2):");
353                                 Console.WriteLine("---------------------------------------------------");
354                                 Console.WriteLine("        1. 交换位置          2.轰炸对方          ");
355                                 int userselect = ReadInt(1, 2);
356                                 if (userselect == 1)
357                                 {
358                                     int temp = playpost[1];
359                                     playpost[1] = playpost[0];
360                                     playpost[0] = temp;
361                                 }
362                                 else
363                                 {
364                                     playpost[0] -= 6;
365                                     Program.check();
366                                 }
367                                 break;
368                             case 2:
369                                  Console.Clear();
370                                 Program.drawmap();
371                                 Console.WriteLine("糟糕了!您踩到地雷了,后退六步。");
372                                 playpost[1] -= 6;
373                                 Program.check();
374                                 Console.WriteLine("按任意键继续...");
375                         Console.ReadKey(true);
376                                 break;
377                             case 3:
378                                 Console.Clear();
379                                 Program.drawmap();
380                                  isstop[1] = true;
381                                  Console.WriteLine("{0}走到了暂停,暂停一次!", name[0]);
382                                 Console.WriteLine("按任意键继续...");
383                         Console.ReadKey(true);
384                                 break;
385                             case 4:
386                                  Console.Clear();
387                                 Program.drawmap();
388                                 Console.WriteLine("恭喜您,走到了时空隧道!前进十步");
389                                 playpost[1] += 10;
390                                 Program.check();
391                                 Console.WriteLine("按任意键继续...");
392                         Console.ReadKey(true);
393                                 break;
394                         }
395                     }
396                     Console.Clear();
397                     Program.chushiMap();
398                     Program.drawmap();
399                     Console.WriteLine("{0}掷出了{1}", name[1], playpost[1]);
400                     Console.WriteLine("*******************************************");
401                     Console.WriteLine("{0}的位置{1}", name[1], playpost[1] + 1);
402                     Console.WriteLine("{0}的位置{1}", name[0], playpost[0] + 1);
403                     /*Console.Clear();
404                     Program.chushiMap();
405                     Program.drawmap();*/ 
406                     #endregion
407                 }
408                 else
409                 {
410                     isstop[1] = false;
411                 }
412                 
413             }
414             if (playpost[0] >= 99)
415             {
416                 Console.WriteLine("{0}玩家胜利", name[0]);
417             }
418             else
419             {
420                 Console.WriteLine("{0}玩家胜利", name[1]);
421             }
422             Console.ReadKey();
423         }
424     }
425 }
View Code

 

纠正飞行棋的错误