C# 飛行棋源代碼


  1  using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 
  6 namespace 飛行棋
  7 {
  8 
  9     class Program
 10     {
 11         public static int[] maps = new int[100]; //聲明一個靜態數組 用來存儲玩家A跟玩家B的坐標
 12         //兩個玩家的坐標
 13         static int[] Players = new int[2];
 14         //存儲兩個玩家的姓名
 15         static string[] playerNames = new string[2];
 16         //兩個玩家的標記,擁有某一個玩家暫停 
 17         static bool[] GamePause = new bool[2];
 18         static void Main(string[] args)
 19         {
 20             GameShow();//調用游戲提示信息
 21             #region 提示用戶輸入玩家姓名及姓名的判斷
 22             Console.WriteLine("請輸入玩家A的姓名:");
 23             playerNames[0] = Console.ReadLine();
 24             while (playerNames[0] == "")
 25             {
 26                 Console.WriteLine("玩家A的名字不能為空,請重新輸入");
 27                 playerNames[0] = Console.ReadLine();
 28             }
 29             Console.WriteLine("請輸入玩家B的姓名:");
 30             playerNames[1] = Console.ReadLine();
 31             while (playerNames[1] == "" || playerNames[1] == playerNames[0])
 32             {
 33                 if (playerNames[1] == "")
 34                 {
 35                     Console.WriteLine("玩家B的名字不能為空,請重新輸入");
 36                     playerNames[1] = Console.ReadLine();
 37                 }
 38                 else
 39                 {
 40                     Console.WriteLine("玩家B的名字不能與玩家A的名字相同,請重新輸入");
 41                     playerNames[1] = Console.ReadLine();
 42                 }
 43             }
 44             #endregion
 45             Console.Clear();//玩家姓名輸入OK 清屏
 46             GameShow();
 47             Console.WriteLine("{0}的士兵用A表示", playerNames[0]);
 48             Console.WriteLine("{0}的士兵用B表示", playerNames[1]);
 49             GameMap();//調用地圖
 50             ShowMap();//畫圖
 51             #region 玩游戲 
 52             while (Players[0] < 99 && Players[1] < 99)
 53             // 當玩家A 和玩家B 么有一個人到達終點的時候,兩個玩家繼續玩游戲
 54             {
 55                 if (GamePause[0] == false)
 56                 {
 57                     PlayGame(0);
 58                 }
 59                 else
 60                 {
 61                     GamePause[0] = false;
 62                 }
 63                 if (Players[0] >= 99)
 64                 {
 65                     Console.WriteLine("玩家{0}無恥的贏了玩家{1}", playerNames[0], playerNames[1]);
 66                     break;
 67                 }
 68                 if (GamePause[1] == false)
 69                 {
 70                     PlayGame(1);
 71                 }
 72                 else
 73                 {
 74                     GamePause[1] = false;
 75                 }
 76                 if (Players[1] >= 99)
 77                 {
 78                     Console.WriteLine("玩家{0}無恥的贏了玩家{1}", playerNames[1], playerNames[0]);
 79                     break;
 80                 }
 81             }//while
 82             #endregion
 83             Console.ReadKey();//等待
 84         }
 85         /// <summary>
 86         /// 兩個玩家玩游戲
 87         /// </summary>
 88         public static void PlayGame(int playNumber)
 89         {
 90             Random r = new Random();
 91             int rNumber = r.Next(1, 7);
 92             Console.WriteLine("玩家{0}按任意鍵開始鄭骰子", playerNames[playNumber]);
 93             Console.ReadKey(true);
 94             Console.WriteLine("玩家{0}擲骰子,擲出了{1}", playerNames[playNumber], rNumber);
 95             Players[playNumber] += rNumber;
 96             Console.ReadKey(true);
 97             Console.WriteLine("玩家{0}:請按任意鍵開始移動!!!", playerNames[playNumber]);
 98             Console.ReadKey(true);
 99             Console.WriteLine("玩家{0}:移動完了", playerNames[playNumber]);
100             Console.ReadKey(true);
101             // 玩家A有可能踩到了B ,空格,幸運輪盤,地雷,暫停,時空隧道
102             if (Players[playNumber] == Players[1 - playNumber])
103             {
104                 Console.WriteLine("{0}玩家踩到了{1}玩家,{2}玩家退6格", playerNames[playNumber], playerNames[1 - playNumber], playerNames[1 - playNumber]);
105                 Players[1 - playNumber] -= 6;
106                 Console.ReadKey(true);
107             }
108             else // 玩家A踩到了關卡
109             {
110                 // 玩家的坐標
111                 changePots();
112                 switch (maps[Players[playNumber]])
113                 {
114                     case 0:
115                         Console.WriteLine("玩家{0}踩到了方塊,安全", playerNames[playNumber]);
116                         Console.ReadKey(true);
117                         break;
118 
119                     case 1:
120                         Console.WriteLine("玩家{0}踩到了幸運輪盤,請選擇:1--交換位置 2--轟炸對方", playerNames[playNumber]);
121                         string input = Console.ReadLine();
122                         while (true)
123                         {
124                             if (input == "1")
125                             {
126                                 Console.WriteLine("玩家{0}選擇與玩家{1}交換位置", playerNames[playNumber], playerNames[1 - playNumber]);
127                                 Console.ReadKey(true);
128                                 int temp = Players[playNumber];
129                                 Players[playNumber] = Players[1 - playNumber];
130                                 Players[1 - playNumber] = temp;
131                                 Console.WriteLine("位置交換完成!!!按任意鍵繼續游戲!!!");
132                                 Console.ReadKey(true);
133                                 break;
134                             }
135                             else if (input == "2")
136                             {
137                                 Console.WriteLine("玩家{0}選擇了轟炸玩家{1},玩家{2}倒退6格", playerNames[playNumber], playerNames[1 - playNumber], playerNames[1 - playNumber]);
138                                 Console.ReadKey(true);
139                                 Players[1 - playNumber] -= 6;
140                                 Console.WriteLine("玩家B倒退6格", playerNames[1 - playNumber]);
141                                 break;
142                             }
143                             else
144                             {
145                                 Console.WriteLine("只能輸入1或者2 1--交換位置 2--轟炸對方");
146                                 input = Console.ReadLine();
147                             }
148                         }
149                         break;
150                     case 2:
151                         Console.WriteLine("玩家{0}踩到了地雷,退6格", playerNames[playNumber]);
152                         Console.ReadKey(true);
153                         Players[playNumber] -= 6;
154                         break;
155                     case 3:
156                         Console.WriteLine("玩家{0}猜到了暫停,暫停一回合", playerNames[playNumber]);
157                         GamePause[playNumber] = true;
158                         break;//暫停一回合是整個游戲最復雜的邏輯,需要最后再寫
159                     case 4:
160                         Console.WriteLine("玩家{0}踩到了時空隧道,前進10格", playerNames[playNumber]);
161                         Players[playNumber] += 10;
162                         Console.ReadKey(true);
163                         break;
164                 }//switch
165             }//else
166             changePots();
167             Console.Clear();// 清屏 
168             ShowMap();
169         }
170 
171         /// <summary>
172         /// 當玩家坐標發生改變的時候
173         /// 限定玩家不能跳出地圖的界限
174         /// </summary>
175         public static void changePots()
176         {
177             if (Players[0] < 0)
178             {
179                 Players[0] = 0;
180             }
181             if (Players[0] >= 99)
182             {
183                 Players[0] = 99;
184             }
185             if (Players[1] < 0)
186             {
187                 Players[1] = 0;
188             }
189             if (Players[1] >= 99)
190             {
191                 Players[1] = 99;
192             }
193         }
194 
195         /// <summary>
196         /// 畫游戲開頭提示信息
197         /// </summary>
198         /// 
199         public static void GameShow()
200         {
201             Console.ForegroundColor = ConsoleColor.Yellow;
202             Console.WriteLine("*****************************");
203             Console.ForegroundColor = ConsoleColor.Red;
204             Console.WriteLine("*****************************");
205             Console.ForegroundColor = ConsoleColor.Green;
206             Console.WriteLine("**** Weiterli 飛行棋V1.0 ****");
207             Console.ForegroundColor = ConsoleColor.Red;
208             Console.WriteLine("*****************************");
209             Console.ForegroundColor = ConsoleColor.Yellow;
210             Console.WriteLine("*****************************");
211         }
212 
213 
214         //int[] luckyturn={6,23,40,55,69,83};//幸運輪盤
215         //int[] lanmine ={5,13,17,33,38,50,64,80,94};//地雷☆
216         //int[]pause={9,27,60,93};//暫停
217         //int[] timeTunnel={20,25,45,63,72,88,90}//時空隧道
218         /// <summary>
219         ///初始化游戲地圖
220         ///(數組長度100,索引 0-99)
221         ///<para>luckyturn幸運輪盤lanMine地雷 pause暫停 timeTunnel時空隧道</para>
222         ///<para>注意:把數組當中的數字轉換成控制台中顯示的特殊字符串的過程就是初始化地圖</para>
223         /// </summary>
224         public static void GameMap()
225         {
226             int[] luckyturn = { 6, 23, 40, 55, 69, 83 };//幸運輪盤
227             for (int i = 0; i < luckyturn.Length; i++)
228             {
229                 //    int index = luckyturn[i];
230                 maps[luckyturn[i]] = 1;
231             }
232             int[] landmine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 };//地雷☆
233             for (int i = 0; i < landmine.Length; i++)
234             {
235                 maps[landmine[i]] = 2;
236             }
237             int[] pause = { 9, 27, 60, 93 };//暫停
238             for (int i = 0; i < pause.Length; i++)
239             {
240                 maps[pause[i]] = 3;
241             }
242             int[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 };//時空隧道
243             for (int i = 0; i < timeTunnel.Length; i++)
244             {
245                 maps[timeTunnel[i]] = 4;
246             }
247         }
248 
249 
250         /// <summary>
251         /// 畫地圖
252         /// </summary>
253         public static void ShowMap()
254         {
255             Console.WriteLine("圖例:幸運輪盤:◎  地雷:☆  暫停:▲  時空隧道:卐");
256             #region 第一橫行
257             for (int i = 0; i < 30; i++)
258             {
259                 Console.Write(DrawMaps(i));// 畫關卡
260             }//for
261             #endregion
262             Console.WriteLine();
263             #region 畫第一個豎行 
264             //第一豎行30-35
265             for (int i = 30; i < 35; i++)
266             {
267                 for (int j = 0; j <= 28; j++)
268                 {
269                     Console.Write("  ");
270                 }
271                 Console.Write(DrawMaps(i));
272                 ;// 畫關卡
273 
274                 Console.WriteLine();
275 
276             }
277             #endregion
278             #region 畫第二橫行
279             for (int i = 64; i >= 35; i--)
280             {
281                 Console.Write(DrawMaps(i));
282             }
283             Console.WriteLine();//畫完第二橫行應當換行
284             #endregion
285             #region 畫第二豎行
286             for (int i = 65; i <= 69; i++)
287             {
288                 Console.WriteLine(DrawMaps(i));
289             }
290             #endregion 
291             #region 畫第三橫行
292             for (int i = 70; i <= 99; i++)
293             {
294                 Console.Write(DrawMaps(i));
295             }
296             Console.WriteLine();//畫完第三橫行應當換行
297             #endregion
298 
299         }
300 
301 
302         /// <summary>
303         /// 畫關卡的方法
304         /// </summary>
305         /// <param name="i"></param>
306         /// <returns></returns>
307         public static string DrawMaps(int i)
308         {
309             string str = "";
310             #region 畫關卡
311             if (Players[0] == Players[1] && Players[0] == i)
312             {
313                 str = "<>";
314             }
315             else if (Players[0] == i)
316             {
317                 str = "";
318             }
319             else if (Players[1] == i)
320             {
321                 str = "";
322             }
323             else
324             {
325                 switch (maps[i])
326                 {
327                     case 0:
328 
329                         Console.ForegroundColor = ConsoleColor.White;
330                         str = "";
331                         break;
332                     case 1:
333 
334                         Console.ForegroundColor = ConsoleColor.Red;
335                         str = "";
336                         break;
337                     case 2:
338 
339                         Console.ForegroundColor = ConsoleColor.Green;
340                         str = "";
341                         break;
342                     case 3:
343                         Console.ForegroundColor = ConsoleColor.Yellow;
344                         str = "";
345                         break;
346                     case 4:
347                         Console.ForegroundColor = ConsoleColor.Blue;
348                         str = "";
349                         break;
350                 }//swicth
351             }//if
352             #endregion
353             return str;
354         }
355 
356     }
357 }
C#飛行棋

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM