C # level operations of small programs Flying chess,
Flight level operations
1. Analysis and Design
The basic rule of Flying chess is to roll the dice in turn. Here it is a loop structure, and then move forward based on the number of points thrown. <Note: after each step advances, you should determine whether the game has been completed.>, perform corresponding operations when a level is encountered. There are four levels, each of which has different operations, therefore, the switch-case loop structure can be used to determine whether to step on the level after each step of operation, and then perform the next operation based on the judgment result. After each operation is completed, redraw the latest map.
2. Code Implementation
I. players roll the dice and the two take turns to throw the dice. When one wins, the game ends. The throwing dice function Random () is used to generate Random numbers. After each advance step, you must check whether the game is successful. The method is as follows:
1 /// <summary> 2 // check whether the coordinates are out of bounds 3 /// </summary> 4 static void CheckPos () 5 {6 for (int I = 0; I <= 1; I ++) 7 {8 if (playerPos [I]> 99) 9 {10 playerPos [I] = 99; 11} 12 if (playerPos [I] <0) 13 {14 playerPos [I] = 0; 15} 16} 17}View Code
When you encounter a level, perform operations based on the nature of the level settings. When you encounter a lucky turntable, you can select 1: Switch location with the other party; 2: bomb the other party, let the recipient return 6 cells. here we need to let the user enter 1 or 2. During reading, we need to check the value typed by the user. This can be achieved through the following methods:
1 static int ReadInt (int min, int max) 2 {3 while (true) 4 {5 try 6 {7 int number = Convert. toInt32 (Console. readLine (); 8 if (number <min | number> max) 9 {10 Console. writeLine ("only numbers between {0}-{1} can be entered. Please try again! ", Min, max); 11 continue; 12} 13 return number; 14} 15 catch16 {17 Console. WriteLine (" only numbers are allowed. Please enter it again! "); 18} 19} 20}View Code
When Level 3 is paused, the user needs to pause the next action. Here, an array of bool types can be defined to indicate whether the user should suspend the operation in this step, the following code throws user:
1 // players roll the dice in turn in this loop. When any one person's coordinates> = 99, the game ends 2 while (playerPos [0] <99 & playerPos [1] <99) 3 {4 Random r = new Random (); // generate Random number 5 int Step; // store the generated Random number; 6 7 if (isStop [0] = false) 8 {9 # region // player A dice 10 Console. writeLine ("{0} press any key to roll the dice .... ", names [0]); 11 ConsoleKeyInfo rec = Console. readKey (true); 12 if (rec. key = ConsoleKey. tab) 13 {14 Step = 6; 15} 16 else 17 {18 Step = r. next (1, 7); 19} 20 Console. write Line ("{0} threw {1}", names [0], Step); 21 Console. writeLine ("{0} press any key to act... ", names [0]); 22 Console. readKey (true); // do not display the value of the pressed key 23 24 playerPos [0] = playerPos [0] + Step; // change the coordinates <once the coordinates change, determine if it is greater than 99 or less than 0> 25 CheckPos (); 26 if (playerPos [0] = playerPos [1]) // player A step on Player B 27 {28 playerPos [1] = 0; 29 msg = string. format ("{0} stepped on {1} and returned to origin", names [0], names [1]); // The Format function is used to concatenate character segments 30} 31 32 # region 33 else // if this character is not entered Set whether there are other levels 34 {35 switch (Map [playerPos [0]) 36 {37 case 0: 38 // normal location, no effect 39 msg = ""; 40 break; 41 case 1: 42 // lucky turntable 43 Console. clear (); 44 DrownMap (); 45 Console. writeLine ("{0} has arrived at the Lucky turntable. Please select luck? ", Names [0]); 46 Console. writeLine ("1: Switching Location 2: bombing"); 47 int userSelect = ReadInt (1, 2); 48 if (userSelect = 1) 49 {50 int temp; 51 temp = playerPos [0]; 52 playerPos [0] = playerPos [1]; 53 playerPos [1] = temp; 54 msg = string. format ("{0} selected a location to exchange with {1}, haha, before liberation all night", names [0], names [1]); 55} 56 else 57 {58 playerPos [1] = playerPos [1]-6; 59 CheckPos (); 60 msg = string. format ("{0} selected and let {1} Step back, please do it yourself ", Names [0], names [1]); 61} 62 break; 63 case 2: 64 // mine 65 playerPos [0] = playerPos [0]-6; 66 CheckPos (); 67 msg = string. format ("{0} stepped on a mine, step 6 back, Amitabha", names [0]); 68 break; 69 case 3: 70 // pause 71 isStop [0] = true; 72 msg = string. format ("{0} paused once! ", Names [0]); 73 break; 74 case 4: 75 // time-space tunnel 76 playerPos [0] = playerPos [0] + 6; 77 CheckPos (); 78 msg = string. format ("{0} enters the time-space tunnel. Proceed! ", Names [0]); 79 break; 80} 81} 82 # endregion 83 Console. writeLine ("press any key to start the action... "); 84 Console. readKey (true); 85 Console. clear (); 86 DrownMap (); 87 if (msg! = "") 88 {89 Console. WriteLine (msg); 90} 91 Console. WriteLine ("{0} threw {1}. The action is complete! ", Names [0], Step); 92 Console. writeLine ("********* w the positions of Player A and player B are as follows ************"); 93 Console. writeLine ("player {0} location: {1}", names [0], playerPos [0] + 1); 94 Console. writeLine ("player {0} location: {1}", names [1], playerPos [1] + 1 ); 95 # endregion 96} 97 else 98 {99 // note that A suspends A 100 isStop [0] = false; 101} 102 if (playerPos [0]> = 99) 103 {104 break; 105} 106 # region player B dice 107 # endregion108 109}View Code
Learning self-propagation intelligence podcast tutorial
--- Pole