This article shares the specific code for C# implementation of flying chess for your reference. The specific content is as follows
Game rules
If Player A step on Player B, Player B will withdraw from 6 squares
Step on 1 lucky roulette, a exchange position, b bombs the opponent and makes the opponent retreat 6 squares
Step on 2 landmines, 6 spaces back
Step on 3 pauses, pause for one round
Stepping into 4 time tunnels, entering 10 grids
Step on the block and do nothing
0 means normal level
1 means lucky roulette◎
2 means landmines★
3 means pause ▲
4 means space-time tunnel
The next article will be published on the analysis of the source code of the flying chess.
Source code
using System; using ; using ; using ; using ; namespace Flying chess { class Program { //We use static fields to simulate global variables static int[] Maps=new int[100]; //Declare an array of static fields to store the coordinates of player A and player B static int[] playerPos = new int[2]; //Storing the names of two players static string[] playerNames = new string[2]; //Tags for two players static bool[] flags = new bool[2]; //flags[0]Player A and flags[1]Player B are both false by default static void Main(string[] args) { GameShow(); //Load the game header #region Enter player name ("Please enter player A's name"); playerNames[0] = (); while (playerNames[0]=="") { ("Player A's name cannot be empty, please re-enter"); playerNames[0] = (); } ("Please enter player B's name"); playerNames[1] = (); while (playerNames[1] == ""||playerNames[1]==playerNames[0]) { if (playerNames[1] == "") { ("The name cannot be empty, please re-enter"); playerNames[1] = (); } else { ("Player B's name cannot be repeated, please re-enter"); playerNames[1] = (); } } #endregion //After the player's name is written, the screen will be cleared (); //Clear the screen GameShow(); ("{0}SoldiersAexpress", playerNames[0]); ("{0}SoldiersBexpress", playerNames[1]); Initailmap(); //Initialize the map DrowMap(); //Draw the map---Note: Before drawing the map, you should first initialize the map. //When neither player A nor player B reaches the end point, they are in the game while (playerPos[0] < 99 && playerPos[1] < 99) { if (flags[0] == false) { PlayGame(0); } else { flags[0] = false; } if (playerPos[0] >= 99) { ("Players{0}赢了Players{1}", playerNames[0], playerNames[1]); break; } if (flags[1] == false) { PlayGame(1); } else { flags[1] = false; } if (playerPos[1] >= 99) { ("Players{0}赢了Players{1}", playerNames[1], playerNames[0]); break; } } (); } /// <summary> /// Set the color of the game head and output content /// </summary> public static void GameShow() { = ; //Set the foreground color of the output content ("**********************"); = ; ("**********************"); = ; ("******Flying Chess War******"); = ; ("**********************"); = ; ("**********************"); = ; ("**********************"); } /// <summary> /// Initialize the map /// </summary> public static void Initailmap() { int[] lucklyturn = { 6, 23, 40, 55, 69, 83 }; //Lucky Roulette for (int i = 0; i < ; i++) { int index = lucklyturn[i]; Maps[index] = 1; //Set the position of the lucky roulette to 1 } int[] landMine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 };// Landmines for (int i = 0; i < ; i++) { int index = landMine[i]; Maps[index] = 2; //Set the location of the mine to 2 } int[] pause = { 9, 27, 60, 93 }; //pause for (int i = 0; i < ; i++) { int index = pause[i]; Maps[index] = 3; //Set the values at the pause position to 3 } int[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 };//Space-time tunnel for (int i = 0; i <; i++) { int index=timeTunnel[i]; Maps[index] = 4; //Set the values at the location of the space-time tunnel to 4 } } /// <summary> /// Draw a map /// </summary> public static void DrowMap() { ("Legend: Normal Block: □ Lucky Roulette: ◎ Mine: ★ Pause: ▲ Time and Space Tunnel: swastika"); //The first 0--29 for (int i = 0; i <= 29; i++) { (DrowStringMap(i)); //Output the graph of the current cell returned } //Win the line after drawing the first line (); //The first vertical line 30--34 for (int i = 30; i <= 34; i++) { for (int j = 0; j < 29; j++) { (" "); //Two spaces } (DrowStringMap(i)); (); } //The second rampant 35--64 for (int i = 64; i >=35; i--) { (DrowStringMap(i)); } //Win the second horizontal line after printing (); //The second vertical line 65--69 for (int i = 65; i <= 69; i++) { (DrowStringMap(i)); (); } //The third 70--99 for (int i = 70; i <= 99; i++) { (DrowStringMap(i)); } (); //After drawing the last line, you should change the line } /// <summary> /// Draw the current cell pattern /// </summary> /// <param name="i">Passing the index of the current cell</param> /// <returns>Return the graph of the current cell</returns> public static string DrowStringMap(int i) { string str = " "; //If the coordinates of player A and player B are the same and both draw a <> on the map (because the player may go outside the map at the beginning and end) if (playerPos[0] == playerPos[1] && playerPos[0] == i) { = ; str="<>"; } else if (playerPos[0] == i) { = ; str="A"; } else if (playerPos[1] == i) { = ; str="B"; } else { switch (Maps[i]) { case 0: = ; str="□"; break; case 1: = ; str="◎"; break; case 2: = ; str="★"; break; case 3: = ; str="▲"; break; case 4: = ; str="swastika"; break; } } //else return str; } /// <summary> /// Play the game /// </summary> public static void PlayGame(int playerNumber) { Random r = new Random(); int rNumber = (1, 7); ("{0}Players开始掷骰子", playerNames[playerNumber]); (true); //ReadKey is an overloaded function, and the parameter inside is true to indicate that the pressed key is not displayed. ("{0}Thrown{1}", playerNames[playerNumber], rNumber); (true); ("{0}Press any key to start the action", playerNames[playerNumber]); (true); playerPos[playerNumber] += rNumber; ChangePos(); ("Players{0}The action is over", playerNames[playerNumber]); (true); //Player A may step on Player B if (playerPos[playerNumber] == playerPos[1 - playerNumber]) { ("Players{0}踩到了Players{1},{2}retreat6grid", playerNames[0], playerNames[1], playerNames[1]); playerPos[1 - playerNumber] -= 6; //If Player A step on Player B, Player B will withdraw from 6 squares ChangePos(); (true); } // Player A may step on blocks, time tunnels, pauses, mines, lucky roulettes else { switch (Maps[playerPos[playerNumber]]) { case 0: ("Players{0}Step on the block,Safety", playerNames[playerNumber]); (true); break; case 1: ("Players{0}Step on the lucky roulette,Please selectaChange position;bBomb the other side,让对方retreat6grid", playerNames[playerNumber]); string input = (); while (true) { if (input == "a") { ("Players{0}和Players{1}交Change position", playerNames[playerNumber], playerNames[1 - playerNumber]); //******* (true); int temp = playerPos[playerNumber]; playerPos[playerNumber] = playerPos[1 - playerNumber]; playerPos[1 - playerNumber] = temp; ("Players{0}和Players{1}Location exchange succeeded,Press any key to continue the game。", playerNames[playerNumber], playerNames[1 - playerNumber]); (true); break; } else if (input == "b") { ("Players{0}选择轰炸Players{1},Players{2}的位置retreat6grid", playerNames[playerNumber], playerNames[1 - playerNumber], playerNames[1 - playerNumber]); (true); playerPos[1 - playerNumber] -= 6; ChangePos(); ("Players{0}的位置retreat6grid", playerNames[1 - playerNumber]); (true); break; } else { ("Only enter a or b, if the input is incorrect, please re-enter"); input = (); } } break; case 2: ("Players{0}Stepped on a mine,retreat6grid", playerNames[playerNumber]); (true); playerPos[playerNumber] -= 6; ChangePos(); break; case 3: ("Players{0}Stepped on to pause,Pause for one round", playerNames[playerNumber]); flags[playerNumber] = true; (true); break; case 4: ("Players{0}Stepping on the space-time tunnel,go ahead10grid", playerNames[playerNumber]); playerPos[playerNumber] += 10; ChangePos(); break; } //switch } //else ChangePos(); (); //Clear the screen DrowMap(); //Re-draw } /// <summary> /// Called when the player coordinates change /// </summary> public static void ChangePos() { if (playerPos[0] < 0) { playerPos[0] = 0; } else if (playerPos[0] >99) { playerPos[0] = 99; } else if (playerPos[1] < 0) { playerPos[1] = 0; } else if (playerPos[1] > 99) { playerPos[1] = 99; } } } }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.