C#飞行棋游戏

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Threading.Tasks;
  6 
  7 namespace _05.飞行棋游戏
  8 {
  9     class Program
 10     {
 11         //用静态字段来模拟全局变量
 12         static int[] Maps = new int[100];
 13         //声明一个静态数组来存储玩家A和玩家B的坐标
 14         static int[] PlayerPos = new int[2];
 15         //存储两个玩家的姓名
 16         static string[] PlayerNames = new string[2];
 17 
 18         static void Main(string[] args)
 19         {
 20             //1.画游戏头
 21             //2.初始化地图(加载地图所需要的资源)
 22             //将整数数组中的数字编成控制台中显示的特殊字符串的过程--初始化地图
 23             //3.画地图
 24             //4.玩游戏
 25             GameShow();
 26             #region 玩家姓名输入
 27             Console.WriteLine("请输入玩家A的姓名");
 28             PlayerNames[0] = Console.ReadLine();
 29             while (PlayerNames[0] == "")
 30             {
 31                 Console.WriteLine("玩家A的姓名不能为空,请重新输入");
 32                 PlayerNames[0] = Console.ReadLine();
 33             }
 34 
 35             Console.WriteLine("请输入玩家B的姓名");
 36             PlayerNames[1] = Console.ReadLine();
 37             while (PlayerNames[1] == "" || PlayerNames[1] == PlayerNames[0])
 38             {
 39                 if (PlayerNames[1] == "")
 40                 {
 41                     Console.WriteLine("玩家B的姓名不能为空,请重新输入");
 42                     PlayerNames[1] = Console.ReadLine();
 43                 }
 44                 else
 45                 {
 46                     Console.WriteLine("玩家B的姓名不能和玩家A的姓名相同,请重新输入");
 47                     PlayerNames[1] = Console.ReadLine();
 48                 }
 49             }
 50             #endregion
 51 
 52             //玩家输入姓名OK后,首先应该清屏
 53             Console.Clear();//清屏
 54             GameShow();
 55             Console.WriteLine("{0}的士兵用A表示", PlayerNames[0]);
 56             Console.WriteLine("{0}的士兵用B表示", PlayerNames[1]);
 57 
 58             InitailMap();
 59             DrawMap();
 60 
 61             //玩家A和玩家B都没到终点,游戏继续
 62             while (PlayerPos[0] < 99 && PlayerPos[1] < 99)
 63             {
 64                 Console.WriteLine("{0}按任意键开始掷骰子", PlayerNames[0]);
 65                 Console.ReadKey(true);
 66                 Console.WriteLine("{0}掷出了4",PlayerNames[0]);
 67                 PlayerPos[0] += 4;
 68                 Console.ReadKey(true);
 69                 Console.WriteLine("{0}按任意键开始行动",PlayerNames[0]);
 70                 Console.ReadKey(true);
 71                 Console.WriteLine("{0}行动完了",PlayerNames[0]);
 72                 Console.ReadKey(true);
 73                 //玩家A有可能踩到了玩家B、方块、幸运轮盘、地雷、暂停、时空隧道
 74                 if (PlayerPos[0]==PlayerPos[1])
 75                 {
 76                     Console.WriteLine("玩家{0}踩到了玩家{1},玩家{2}退6格",PlayerNames[0],PlayerNames[1],PlayerNames[1]);
 77                     PlayerPos[1] -= 6;
 78                     Console.ReadKey(true);
 79                 }
 80                 else//踩到关卡
 81                 {
 82                     //玩家坐标
 83                     
 84                     switch (Maps[PlayerPos[0]])//0 1 2 3 4
 85                     {
 86                         case 0:Console.WriteLine("玩家{0}踩到了方块,安全", PlayerNames[0]);
 87                             Console.ReadKey(true);
 88                             break;
 89                         case 1:Console.WriteLine("玩家{0}踩到了幸运轮盘,请选择:1--交换位置;2--轰炸对方退6格",PlayerNames[0]);
 90                             string input = Console.ReadLine();
 91                             Console.ReadKey(true);
 92                             break;
 93                     }
 94                 }
 95             }
 96 
 97             Console.ReadKey();
 98         }
 99 
100         /// <summary>
101         /// 画游戏头
102         /// </summary>
103         public static void GameShow()
104         {
105             Console.ForegroundColor = ConsoleColor.DarkGreen;
106             Console.WriteLine("*************************************************************");
107             Console.ForegroundColor = ConsoleColor.DarkBlue;
108             Console.WriteLine("*************************************************************");
109             Console.ForegroundColor = ConsoleColor.Cyan;
110             Console.WriteLine("*************************************************************");
111             Console.ForegroundColor = ConsoleColor.Red;
112             Console.WriteLine("**********************飞行棋游戏*****************************");
113             Console.ForegroundColor = ConsoleColor.DarkGray;
114             Console.WriteLine("*************************************************************");
115             Console.ForegroundColor = ConsoleColor.Blue;
116             Console.WriteLine("*************************************************************");
117             Console.ForegroundColor = ConsoleColor.Green;
118             Console.WriteLine("*************************************************************");
119         }
120 
121         /// <summary>
122         /// 初始化地图
123         /// </summary>
124         public static void InitailMap()
125         {
126             int[] luckyturn = { 6, 23, 40, 55, 69, 83 };//幸运轮盘○
127             for (int i = 0; i < luckyturn.Length; i++)
128             {
129                 Maps[luckyturn[i]] = 1;
130             }
131 
132             int[] landMine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 };//地雷☆
133             for (int i = 0; i < landMine.Length; i++)
134             {
135                 Maps[landMine[i]] = 2;
136             }
137 
138             int[] pause = { 9, 27, 63, 93 };//暂停△
139             for (int i = 0; i < pause.Length; i++)
140             {
141                 Maps[pause[i]] = 3;
142             }
143 
144             int[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 };//时空隧道##
145             for (int i = 0; i < timeTunnel.Length; i++)
146             {
147                 Maps[timeTunnel[i]] = 4;
148             }
149         }
150 
151         /// <summary>
152         /// 画地图
153         /// </summary>
154         public static void DrawMap()
155         {
156             Console.WriteLine("图例:幸运轮盘:○    地雷:☆    暂停:△    时空隧道:##");
157 
158             //第一横行
159             for (int i = 0; i < 30; i++)
160             {
161                 Console.Write(DrowStringMap(i));
162             }
163 
164             //画完第一横行后应该换行
165             Console.WriteLine();
166 
167             //第一竖行
168             for (int i = 30; i < 35; i++)
169             {
170                 for (int j = 0; j <= 28; j++)
171                 {
172                     Console.Write("  ");
173                 }
174                 Console.Write(DrowStringMap(i));
175                 Console.WriteLine();
176             }
177 
178             //第一横行:倒序
179             for (int i = 64; i >= 35; i--)
180             {
181                 Console.Write(DrowStringMap(i));
182             }
183 
184             //画完第二横行后应该换行
185             Console.WriteLine();
186 
187             //第二竖行
188             for (int i = 65; i <= 69; i++)
189             {
190                 Console.WriteLine(DrowStringMap(i));
191             }
192 
193             //第二横行
194             for (int i = 70; i <= 99; i++)
195             {
196                 Console.Write(DrowStringMap(i));
197             }
198 
199             //画完最后一行应该换行
200             Console.WriteLine();
201 
202         }
203 
204         /// <summary>
205         /// 从画地图方法中抽象出来的方法
206         /// </summary>
207         /// <param name="i">地图中的位置</param>
208         /// <returns>要打印的字符串</returns>
209         public static string DrowStringMap(int i)
210         {
211             //声明str接收要打印的值
212             string str = "";
213             //玩家A和玩家B的坐标相同,并且都在地图上,则画<>
214             if (PlayerPos[0] == PlayerPos[1] && PlayerPos[0] == i)
215             {
216                 str = "<>";
217             }
218             else if (PlayerPos[0] == i)
219             {
220                 str = "A";
221             }
222             else if (PlayerPos[1] == i)
223             {
224                 str = "B";
225             }
226             else
227             {
228                 switch (Maps[i])
229                 {
230                     case 0:
231                         Console.ForegroundColor = ConsoleColor.Yellow;
232                         str = "□";
233                         break;
234                     case 1:
235                         Console.ForegroundColor = ConsoleColor.Green;
236                         str = "○";
237                         break;
238                     case 2:
239                         Console.ForegroundColor = ConsoleColor.Red;
240                         str = "☆";
241                         break;
242                     case 3:
243                         Console.ForegroundColor = ConsoleColor.Blue;
244                         str = "△";
245                         break;
246                     case 4:
247                         Console.ForegroundColor = ConsoleColor.DarkCyan;
248                         str = "##";
249                         break;
250                 }
251             }
252 
253             return str;
254         }
255     }
256 }
257