This article shares the specific code for implementing a simple flying chess game on the C# console for your reference. The specific content is as follows
Requirements Analysis
1. Make a game head: Introduction to game head
2. Draw a map
Use a one-dimensional array to assemble the route of the entire map
If this position is 0, draw a normal grid □
If this position is 1, draw the lucky roulette◎
If this position is 2, draw the mine★
If this position is 3, draw pause ▲
If this position is 4, draw the space-time tunnel
Planning the lucky roulette position
int[] luckyturn = { 6, 23, 40, 55, 69, 83 };
Plan the location of mines
int[] landMine = { 5,13,17,33,38,50,64,80,94};
Planning a suspended position
int[] pause = {9,27,60,93 };
Plan the location of the space-time tunnel
int[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 };
3. Set up special levels
The code is as follows:
using System; using ; using ; using ; using ; namespace _01Flying chess { class Program { /// <summary> /// The entire map array /// </summary> static int[] Maps = new int[100]; /// <summary> ///Storage the player's array /// </summary> static int[] PlayerPos = new int[2]; /// <summary> /// Array of player names /// </summary> static string[] PlayerName = new string[2]; static bool[] PlayerFlage = new bool[2]; static void Main(string[] args) { //Draw the game title ShowTitle(); //Enter player name ("Please enter player A's name:"); PlayerName[0] = (); while (PlayerName[0] == "") { ("Player A's name cannot be empty, please re-enter!"); PlayerName[0]=(); } ("Please enter player B's name:"); PlayerName[1] = (); while (PlayerName[1]==""||PlayerName[1]==PlayerName[0]) { if (PlayerName[1]=="") { ("Player B's name cannot be empty, please re-enter!"); PlayerName[1]= (); } if (PlayerName[1]==PlayerName[0]) { ("Player B's name and A are repeated, please re-enter!"); PlayerName[1] = (); } } //Enter your name and clear the screen (); ShowTitle(); //Initialize the map level InitialMap(); //Draw a map DrawMap(); (); while (PlayerPos[0]<99&&PlayerPos[1]<99) { if (PlayerFlage[0]==false) { PlayGame(0); } else { PlayerFlage[0] = false; } if(PlayerFlage[1]==false) { PlayGame(1); } else { PlayerFlage[1] = false; } if (PlayerPos[0] == 99) { ("Congratulations to the players[{0}]Win", PlayerName[0]); } if (PlayerPos[1] == 99) { ("Congratulations to the players[{0}]Win", PlayerName[1]); } } } /// <summary> /// Set the game title /// </summary> static void ShowTitle() { //Set the color = ; ("************************************"); = ; ("************************************"); = ; ("************************************"); = ; ("*********************Flying Chess******************); = ; ("************************************"); = ; ("************************************"); = ; ("************************************"); } /// <summary> /// Initialize the map level /// </summary> static void InitialMap() { //Determine the position of the lucky roulette ◎==1 int[] luckyturn = { 6, 23, 40, 55, 69, 83 }; for (int i=0;i<;i++) { Maps[luckyturn[i]] = 1; } //Determine the location of the mine★==2 int[] landMine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 }; for (int i=0;i<;i++) { Maps[landMine[i]] = 2; } //Determine the pause position ▲==3 int[] pause = { 9, 27, 60, 93 }; for (int i=0;i<;i++) { Maps[pause[i]] = 3; } //Determine the location of the space-time tunnel swastika==4 int[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 }; for (int i=0;i<;i++) { Maps[timeTunnel[i]] = 4; } } /// <summary> /// Draw a map /// </summary> static void DrawMap() { = ; ("Players[{0}]useAexpress", PlayerName[0]); ("Players[{0}]useBexpress", PlayerName[1]); ("Rules of the game:"); ("1. Two players take turns to roll dice, and player A is required to roll it first."); ("2. It is safe to step on the □ grid, and there is no reward or punishment!"); ("3. Step on the lucky roulette, you can make two choices: a. Change the position of the opponent's player; b. Bomb the opponent and make the opponent retreat 6 steps"); ("4. Step on the ★ landmines and take 6 steps back!"); ("5. If you step on ▲, the operation will be paused in the next round!"); ("6. Step on the time and space tunnel and go 10 steps directly!"); ("7. If you step on the other party, the other party will take 6 steps directly!"); ///The first strait for (int i=0;i<30;i++) { //Judge the position of the two players the same, and make sure that both players are still in the map (DrawString(i)); } (); ///The first vertical column for (int i=30;i<35;i++) { for (int j=0;j<29;j++) { (" "); } (DrawString(i)); } ///The second strait for (int i=64;i>34;i--) { (DrawString(i)); } (); ///The second vertical column for (int i=65;i<70;i++) { (DrawString(i)); } ///The third horizontal line for (int i=70;i<100;i++) { (DrawString(i)); } (); } /// <summary> /// How to judge the map /// </summary> /// <param name="pos"></param> private static string DrawString(int pos) { string str = ""; if (PlayerPos[0] == PlayerPos[1] && PlayerPos[0] == pos) { = ; str ="<>"; } else if (PlayerPos[0] == pos) { = ; str="A"; } else if (PlayerPos[1] == pos) { = ; str ="B"; } else { switch (Maps[pos]) { case 0: = ; str ="□"; break; case 1: = ; str ="◎"; break; case 2: = ; str ="★"; break; case 3: = ; str ="▲"; break; case 4: = ; str ="swastika"; break; default: break; } } return str; } //Game session static void PlayGame(int playerNum) { Random r = new Random(); ("Players[{0}]Press any key to roll the dice.",PlayerName[playerNum]); (true); int number = (1, 7); ("Players[{0}]Throw out<{1}>point.",PlayerName[playerNum],number); ("Players[{0}]Press any key to move.",PlayerName[playerNum]); (true); PlayerPos[playerNum] += number; ("Players[{0}]Move completed!",PlayerName[playerNum]); //The player stepped on the other party ChangedCheck(); if (PlayerPos[playerNum]==PlayerPos[1-playerNum]) { ("Players[{0}]踩到Players[{1}],Players[{1}]retreat6step", PlayerName[playerNum], PlayerName[1 - playerNum]); PlayerPos[1 - playerNum] -= 6; } else { switch (Maps[PlayerPos[playerNum]]) { //When step on ordinary floors, there is no reward or punishment for safety. case 0: ("Players[{0}]Step into a safe area,No rewards and punishments!Press any key to continue the game", PlayerName[playerNum]); (true); break; //Step on 1 lucky roulette and choose a reward case 1: ("Players[{0}]Step on the lucky roulette,Please select:a--Switch location b--Bomb the other side.", PlayerName[playerNum]); string input = (); while (true) { if (input =="a") { ("Players[{0}]选择与Players[{1}]Switch location.",PlayerName[playerNum],PlayerName[1-playerNum]); int temp = PlayerPos[playerNum]; PlayerPos[playerNum] = PlayerPos[1 - playerNum]; PlayerPos[1 - playerNum] = temp; ("Players[{0}]与Players[{1}]Switch location完成!Press any key to continue the game", PlayerName[playerNum], PlayerName[1 - playerNum]); (true); break; } else if (input == "b") { ("Players[{0}]选择轰炸Players[{1}]", PlayerName[playerNum], PlayerName[1 - playerNum]); PlayerPos[1 - playerNum] -= 6; ("Players[{0}]被轰炸倒retreat6step!Press any key to continue the game",PlayerName[1-playerNum]); (true); break; } else { input = (); } } break; // Step on 2 landmines and go back 6 squares directly case 2: ("Players[{0}]Step on a mine,retreat6grid! Press any key to continue the game", PlayerName[playerNum]); PlayerPos[playerNum] -= 6; (true); break; //Step 3 and pause in the next round case 3: ("Players[{0}]Step on pause,Pause operation for the next round!Press any key to continue the game", PlayerName[playerNum]); PlayerFlage[playerNum] = true; (true); break; // Step into the 4 time and space tunnel and go directly to 10 steps case 4: ("Players[{0}]Step into the space-time tunnel,go ahead10step!Press any key to continue the game", PlayerName[playerNum]); PlayerPos[playerNum] += 10; (true); break; } } ChangedCheck(); (); ShowTitle(); DrawMap(); } static void ChangedCheck() { if (PlayerPos[0]<0) { PlayerPos[0] = 0; } if (PlayerPos[0]>99) { PlayerPos[0] = 99; } if (PlayerPos[1] < 0) { PlayerPos[1] = 0; } 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.