C#和.Ne学习第九天
Posted 静梦亭
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#和.Ne学习第九天相关的知识,希望对你有一定的参考价值。
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace ConsoleApplication2 8 { 9 class Program 10 { 11 /* 12 我用0表示普通,显示给用户就是 □ 13 ....1...幸运轮盘,显示组用户就◎ 14 ....2...地雷,显示给用户就是 ☆ 15 ....3...暂停,显示给用户就是 ▲ 16 ....4...时空隧道,显示组用户就 卐 17 */ 18 static int[] Maps = new int[100]; 19 static int[] PlayPos = new int[2]; 20 static string[] PlayNames = new string[2]; 21 //bool默认false; 22 static bool[] Flags = new bool[2]; 23 static void Main(string[] args) 24 { 25 GameShow(); 26 InitNames(); 27 Console.Clear(); 28 GameShow(); 29 Console.WriteLine("{0}的士兵用A表示", PlayNames[0]); 30 Console.WriteLine("{0}的士兵用B表示", PlayNames[1]); 31 InitailMap(); 32 DrawMap(); 33 34 //当玩家 A跟玩家B没有一个人在终点时候,两个玩家不停玩游戏 35 while(PlayPos[0] < 99 && PlayPos[1] < 99) 36 { 37 #region 玩家1 38 if(Flags[0] == false) 39 { 40 PlayGame(0); 41 } 42 else 43 { 44 Flags[0] = false; 45 } 46 #endregion 47 if(PlayPos[0] == 99) 48 { 49 Console.WriteLine("玩家{0}无耻的赢了玩家{1}",PlayNames[0],PlayNames[1]); 50 Win(); 51 Console.ReadKey(); 52 break; 53 } 54 { } 55 #region 玩家2 56 if(Flags[1] == false) 57 { 58 PlayGame(1); 59 } 60 else 61 { 62 Flags[1] = false; 63 } 64 #endregion 65 if(PlayPos[1] == 99) 66 { 67 Console.WriteLine("玩家{0}无耻的赢了玩家{1}", PlayNames[1], PlayNames[0]); 68 Win(); 69 Console.ReadKey(); 70 break; 71 } 72 } 73 #region 胜利判断 74 if(PlayPos[0] > 99) 75 { 76 Console.WriteLine("玩家{0}胜利!", PlayNames[0]); 77 } 78 else 79 { 80 Console.WriteLine("玩家{0}胜利!", PlayNames[1]); 81 } 82 #endregion 83 Console.ReadKey(); 84 } 85 /// <summary> 86 /// 初始化数组内除方块所有变量 87 /// </summary> 88 private static void InitNames() 89 { 90 Console.Write("请输入玩家A的姓名:"); 91 PlayNames[0] = Console.ReadLine(); 92 while(PlayNames[0] == "") 93 { 94 Console.Write("玩家姓名不能为空,请重新输入玩家A的姓名:"); 95 PlayNames[0] = Console.ReadLine(); 96 } 97 Console.Write("请输入玩家B的姓名:"); 98 PlayNames[1] = Console.ReadLine(); 99 while(PlayNames[1] == "" || PlayNames[1] == PlayNames[0]) 100 { 101 if(PlayNames[1] == "") 102 { 103 Console.Write("玩家姓名不能为空,请重新输入玩家B的姓名:"); 104 PlayNames[1] = Console.ReadLine(); 105 } 106 else 107 { 108 Console.Write("玩家姓名不能相同,请重新输入玩家B的姓名:"); 109 PlayNames[1] = Console.ReadLine(); 110 } 111 } 112 } 113 /// <summary> 114 /// 游戏头 115 /// </summary> 116 public static void GameShow() 117 { 118 Console.ForegroundColor = ConsoleColor.Yellow; 119 Console.WriteLine("**************************"); 120 Console.ForegroundColor = ConsoleColor.Red; 121 Console.WriteLine("**************************"); 122 Console.ForegroundColor = ConsoleColor.Green; 123 Console.WriteLine("**************************"); 124 Console.ForegroundColor = ConsoleColor.Blue; 125 Console.WriteLine("****2016.Net基础飞行棋****"); 126 Console.ForegroundColor = ConsoleColor.Cyan; 127 Console.WriteLine("**************************"); 128 Console.ForegroundColor = ConsoleColor.DarkMagenta; 129 Console.WriteLine("**************************"); 130 Console.ForegroundColor = ConsoleColor.White; 131 } 132 /// <summary> 133 /// 初始化地图 134 /// </summary> 135 public static void InitailMap() 136 { 137 int[] luckyturn = { 6, 23, 40, 55, 69, 83 };//幸运轮盘◎ 138 for(int t = 0; t < luckyturn.Length; ++t) 139 { 140 Maps[luckyturn[t]] = 1; 141 } 142 int[] landMine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 };//地雷☆ 143 for(int t = 0; t < landMine.Length; ++t) 144 { 145 Maps[landMine[t]] = 2; 146 } 147 int[] pause = { 9, 27, 60, 93 };//暂停▲ 148 for(int t = 0; t < pause.Length; ++t) 149 { 150 Maps[pause[t]] = 3; 151 } 152 int[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 };//时空隧道卐 153 for(int t = 0; t < timeTunnel.Length; ++t) 154 { 155 Maps[timeTunnel[t]] = 4; 156 } 157 } 158 public static void DrawMap() 159 { 160 Console.ForegroundColor = ConsoleColor.Blue; 161 Console.WriteLine("图例:幸运轮盘:◎ 地雷:☆ 暂停:▲ 时空隧道:卐"); 162 #region 第一行 163 //第一行 164 for(int i = 0; i < 30; ++i) 165 { 166 //如果玩家A和玩家B坐标相同,并且都在地图上,画一个尖括号 167 Console.Write(DrawStringMap(i)); 168 } 169 #endregion 170 Console.Write("\n"); 171 #region 第一竖 172 for(int i = 30; i < 35; ++i) 173 { 174 for(int t = 0; t < 29; ++t) 175 { 176 Console.Write(" "); 177 } 178 Console.Write(DrawStringMap(i)); 179 Console.Write("\n"); 180 } 181 #endregion 182 #region 第二行 183 for(int i = 64; i > 34; --i) 184 { 185 Console.Write(DrawStringMap(i)); 186 } 187 #endregion 188 Console.Write("\n"); 189 #region 第二竖 190 for(int i = 65; i < 70; ++i) 191 { 192 Console.Write(DrawStringMap(i)); 193 Console.Write("\n"); 194 } 195 #endregion 196 #region 第三行 197 for(int i = 70; i <= 99; ++i) 198 { 199 Console.Write(DrawStringMap(i)); 200 } 201 #endregion 202 Console.Write("\n"); 203 } 204 /// <summary> 205 /// 返回当前位置显示的字符 206 /// </summary> 207 /// <param name="i">位置</param> 208 /// <returns>字符</returns> 209 private static string DrawStringMap(int i) 210 { 211 string str = ""; 212 if(PlayPos[0] == PlayPos[1] && PlayPos[0] == i) 213 { 214 str = "<>"; 215 } 216 else if(PlayPos[0] == i) 217 { 218 //shift+空格 219 str = "A"; 220 } 221 else if(PlayPos[1] == i) 222 { 223 str = "B"; 224 } 225 else 226 { 227 switch(Maps[i]) 228 { 229 case 0: 230 Console.ForegroundColor = ConsoleColor.Yellow; 231 str = "□"; 232 break; 233 case 1: 234 Console.ForegroundColor = ConsoleColor.Green; 235 str = "◎"; 236 break; 237 case 2: 238 Console.ForegroundColor = ConsoleColor.Red; 239 str = "☆"; 240 break; 241 case 3: 242 Console.ForegroundColor = ConsoleColor.Blue; 243 str = "▲"; 244 break; 245 case 4: 246 Console.ForegroundColor = ConsoleColor.DarkCyan; 247 str = "卐"; 248 break; 249 } 250 } 251 return str; 252 } 253 /// <summary> 254 /// 开始游戏 255 /// </summary> 256 public static void PlayGame(int PlayNumber) 257 { 258 Random r = new Random(); 259 int rNumber = r.Next(1, 7); 260 Console.WriteLine("{0}按任意键开始掷骰子", PlayNames[PlayNumber]); 261 Console.ReadKey(true); 262 Console.WriteLine("{0}掷骰子{1}", PlayNames[PlayNumber], rNumber); 263 PlayPos[PlayNumber] += rNumber; 264 ChangePos(PlayNumber); 265 Console.ReadKey(true); 266 Console.WriteLine("{0}按任意键开始行动", PlayNames[PlayNumber]); 267 Console.ReadKey(true); 268 Console.WriteLine("玩家{0}行动完了!", PlayNames[PlayNumber]); 269 Console.ReadKey(true); 270 //玩家A有可能踩到了玩家B 方块 幸运轮盘 地雷 暂停 时空隧道 271 #region 272 if(PlayPos[PlayNumber] == PlayPos[1 - PlayNumber]) 273 { 274 Console.WriteLine("玩家{0}踩到了玩家{1},玩家{2}退6格", PlayNames[PlayNumber], PlayNames[1 - PlayNumber], PlayNames[1 - PlayNumber]); 275 PlayPos[1 - PlayNumber] -= 6; 276 Console.ReadKey(); 277 } 278 else //踩到了关卡 279 { 280 switch(Maps[PlayPos[PlayNumber]]) 281 { 282 case 0: 283 #region 踩到了方块 284 Console.WriteLine("玩家{0}踩到了方块,安全。", PlayNames[PlayNumber]); 285 break; 286 #endregion 287 case 1: 288 #region 踩到了幸运轮盘 289 Console.WriteLine("玩家{0}踩到了幸运轮盘,请选择 1、交换位置 2、轰炸对方:", PlayNames[PlayNumber]); 290 string input = Console.ReadLine(); 291 while(true) 292 { 293 if(input == "1") 294 { 295 Console.WriteLine("玩家{0}选择与{1}交换位置", PlayNames[PlayNumber], PlayNames[1 - PlayNumber]); 296 Console.ReadKey(); 297 PlayPos[PlayNumber] = PlayPos[PlayNumber] + PlayPos[1 - PlayNumber]; 298 PlayPos[1 - PlayNumber] = PlayPos[PlayNumber] - PlayPos[1 - PlayNumber]; 299 PlayPos[PlayNumber] = PlayPos[PlayNumber] - PlayPos[1 - PlayNumber]; 300 Console.WriteLine("交换成功!!!按任意键继续游戏!!!"); 301 Console.ReadKey(true); 302 break; 303 } 304 else if(input == "2") 305 { 306 Console.WriteLine("玩家{0}选择轰炸玩家{1},玩家{2}退6格", PlayNames[PlayNumber], PlayNames[1 - PlayNumber], PlayNames[1 - PlayNumber]); 307 Console.ReadKey(true); 308 PlayPos[1 - PlayNumber] -= 6; 309 Console.WriteLine("玩家{0}退了6格", PlayNames[1 - PlayNumber]); 310 Console.ReadKey(true); 311 break; 312 } 313 else 314 { 315 Console.WriteLine("只能输入1或2 ,请重新输入:"); 316 input = Console.ReadLine(); 317 } 318 } 319 break; 320 #endregion 321 case 2: 322 #region 踩到了地雷 323 Console.WriteLine("玩家{0}踩到了地雷", PlayNames[PlayNumber]); 324 Console.ReadKey(true); 325 PlayPos[PlayNumber] -= 6; 326 break; 327 #endregion 328 case 3: 329 #region 踩到了暂停 330 Console.WriteLine("玩家{0}踩到了暂停,暂停一回合", PlayNames[PlayNumber]); 331 Flags[PlayNumber] = true; 332 Console.ReadKey(true); 333 break; 334 #endregion 335 case 4: 336 #region 踩到了时空隧道 337 Console.WriteLine("玩家{0}踩到了时空隧道,前进10格", PlayNames[PlayNumber]); 338 PlayPos[PlayNumber] += 10; 339 break; 340 #endregion 341 } 342 } 343 #endregion 344 ChangePos(PlayNumber); 345 ChangePos(1-PlayNumber); 346 Console.Clear(); 347 Program.GameShow(); 348 Program.DrawMap(); 349 } 350 /// <summary> 351 /// 控制位置最大最小值 352 /// </summary> 353 /// <param name="number"></param> 354 public static void ChangePos(int number) 355 { 356 if(PlayPos[number] <= 0) 357 { 358 PlayPos[number] = 0; 359 } 360 if(PlayPos[number] >= 99) 361 { 362 PlayPos[number] = 99; 363 } 364 } 365 /// <summary> 366 /// 显示胜利文字 367 /// </summary> 368 public static void Win() 369 { 370 Console.ForegroundColor = ConsoleColor.Red; 371 Console.WriteLine(" ◆ "); 372 Console.WriteLine(" ■ ◆ ■ ■"); 373 Console.WriteLine(" ■■■■ ■ ■ ◆■ ■ ■ ■"); 374 Console.WriteLine(" ■ ■ ■ ■ ◆ ■ ■ ■ ■"); 375 Console.WriteLine(" ■ ■ ■■■■■■ ■■■■■■■ ■ ■ ■"); 376 Console.WriteLine(" ■■■■ ■ ■ ●■● ■ ■ ■"); 377 Console.WriteLine(" ■ ■ ■ ● ■ ● ■ ■ ■"); 378 Console.WriteLine(" ■ ■ ■■■■■■ ● ■ ● ■ ■ ■"); 379 Console.WriteLine(" ■■■■ ■ ● ■ ■ ■ ■ ■"); 380 Console.WriteLine(" ■ ■ ■ ■ ■ ■ ■ ■"); 381 Console.WriteLine(" ■ ■ ■ ■ ■ ■ "); 382 Console.WriteLine(" ■ ■ ■ ■ ● ■ "); 383 Console.WriteLine(" ■ ■■ ■■■■■■ ■ ● ●"); 384 Console.ResetColor(); 385 } 386 } 387 }
这个是花了几天看着视频写好的。
以上是关于C#和.Ne学习第九天的主要内容,如果未能解决你的问题,请参考以下文章