This article shares the specific code for implementing the C# console for your reference. The specific content is as follows
Introduction to the flying chess game
The flying chess I implemented this time are somewhat different from what we played when we were young. The rules are roughly similar, but the flying chess I used to learn has greatly simplified the process; the current program is suitable for two players (can also be changed to more people), and there are mainly the following levels:
Lucky Roulette*: Give players two options 1 – swap positions; 2 – retreat another player for 6 squares
Mine $: Players withdraw 6 units
Pause &: Players pause for one turn
Time and Space Tunnel #: Players advance 10 blocks
Players step on each other will cause another player to withdraw 6 squares
Player A and Player B first arrive at the end point to win
Flying chess game writing
There are 100 strings in the game. When programming, it is designed according to each horizontal and vertical line. The names of Player A and Player B cannot be empty(However, the photo statement == "" cannot implement this function, and the problem has not been resolved yet), cannot be the same
The position of each level is fixed (also random)
Specific steps are included in the comments, with code:
(If you think it is useful, please give me more likes)
using System; using ; using ; using ; using ; namespace GameDesign { class Program { //Simulate global variables--to facilitate multiple functions to use static int[] Maps = new int[100]; //Declare a static array to store the coordinates of players A and B static int[] PlayerPos = new int[2]; //Declare a static array to store the names of two players static string[] PlayNames = new string[2]; //Declare bool type to mark two players static bool[] PlayerFlags = new bool[2]; static void Main(string[] args) { GameShow(); // There is a problem: when the string is " ", the game cannot distinguish it, and the player's name is still empty at this time #region Enter player name ("Please enter player A's name"); PlayNames[0] = (); while (PlayNames[0] == " ") { ("Player A's name cannot be empty, please re-enter"); PlayNames[0] = (); } ("Please enter player B's name"); PlayNames[1] = (); while (PlayNames[1] == "" || PlayNames[0] == PlayNames[1]) { if (PlayNames[1] == "") { ("Player B's name cannot be empty, please re-enter"); PlayNames[1] = (); } else { ("The names of Player A and Player B cannot be the same, please re-enter"); PlayNames[1] = (); } } #endregion //After entering the player's name, clear the screen ();//Clear the screen GameShow(); ("{0}SoldiersAexpress", PlayNames[0]); ("{0}SoldiersBexpress", PlayNames[1]); //Before drawing the map, first initialize the map OriginalMap(Maps); DrawMap(Maps); //The game end condition while (PlayerPos[0] <= 99 && PlayerPos[1] <= 99) { if (PlayerFlags[0] == false) { PlayGame(0); } else { PlayerFlags[0] = false; } if (PlayerPos[0] >= 99) { ("Players{0}赢了Players{1}", PlayNames[0], PlayNames[1]); break; } if (PlayerFlags[1] == false) { PlayGame(1); } else { PlayerFlags[1] = false; } if (PlayerPos[1] >= 99) { ("Players{0}赢了Players{1}", PlayNames[1], PlayNames[0]); break; }//while (); } } /// <summary> /// Draw the game header /// </summary> static void GameShow() { =;// Used to set the color ("--------------------------------"); = ; ("--------------------------------"); = ; ("--------------------------------"); = ; ("-------Welcome to Laser Flying Chess------"); = ; ("--------------------------------"); = ; ("--------------------------------"); = ; ("--------------------------------"); } /// <summary> /// Initialize map settings /// </summary> /// <param name="Maps"></param> static void OriginalMap(int[] Maps) { /* * The process of initializing the map -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- * There are five special levels, which can be used to replace different numbers in the array. * 0---represents normal, 1--lucky roulette, 2--- mines, 3---pause, 4----time tunnel * Coordinates of special levels in the map * int [] luckyTurn={6,23,40,55,69,83}; * int [] landMine={5,13,17,33,38,50,64,80,94}; * int [] pause={9,27,60,93}; * int [] timeTunnel={20,25,45,63,72,83,90}; */ int[] luckyTurn = { 6, 23, 40, 55, 69, 83 }; int[] landMine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 }; int[] pause = { 9, 27, 60, 93 }; int[] timeTunnel = { 20, 25, 45, 63, 72, 83, 90 }; // Mark the positions of different levels and express them with different numbers. for (int i = 0; i < ; i++) { int n = luckyTurn[i]; Maps[n] = 1; //Maps[luckyTurn[i]]=1; } for (int i = 0; i < ; i++) { int n = landMine[i]; Maps[n] = 2; } for (int i = 0; i < ; i++) { int n = pause[i]; Maps[n] = 3; } for (int i = 0; i < ; i++) { int n = timeTunnel[i]; Maps[n] = 4; } } /// <summary> /// Draw a map /// </summary> /// <param name="Maps"></param> static void DrawMap(int[] Maps) { ("Legend: Lucky Roulette*, Mines$, Pause&, Time and Space Tunnel#"); #region Draw the first horizontal line //Draw the first horizontal line for (int i = 0; i < 30; i++) { (DrawStringMap(i)); } (); #endregion //After drawing each line, you need to change the line #region Draw the first vertical line for (int i = 30; i < 35; i++) { for (int j = 0; j <= 28 ; j++) { (" "); } (DrawStringMap(i)); (); } #endregion #region Draw the second horizontal line for (int i = 64; i>= 35; i--) { (DrawStringMap(i)); } (); #endregion #region Draw the second vertical line for (int i= 65;i<= 69 ; i++) { (DrawStringMap(i)); } #endregion #region Draw the third horizontal line for (int i = 70; i <= 99; i++) { (DrawStringMap(i)); } (); #endregion } /// <summary> /// Initialize the string in the map /// </summary> /// <param name="i"></param> /// <returns></returns> static string DrawStringMap(int i) { string str = ""; //First determine the coordinates of player A and player B. If the coordinates are the same and are both on the map (let the player coordinates equal to i), draw < if (PlayerPos[0] == PlayerPos[1] && PlayerPos[0] == i) { str="<"; } else if (PlayerPos[0] == i) { //shift+space, in the console, two half-width symbols occupy the position of a full-width symbol. str="A"; } else if (PlayerPos[1] == i) { str="B"; } else { // 0--- represents normal @, 1--- Lucky Roulette*, 2--- Mines$, 3--- Pause&, 4--- Space-time Tunnel#. switch (Maps[i]) { case 0: = ; str="@"; break; case 1: = ; str="*"; break; case 2: = ; str="$"; break; case 3: = ; str="&"; break; case 4: = ; str="#"; break; } } return str; } /// <summary> /// Game settings /// </summary> /// <param name="playernumber">Player number</param> static void PlayGame(int playernumber) { ("{0}Press any key to start rolling the dice", PlayNames[playernumber]); (true);//Compare() You can find different effects Random r = new Random(); int step = (1, 7);//The left and right open range PlayerPos[playernumber] += step; ("{0}Thrown{1}", PlayNames[playernumber],step); (true); ("{0}Press any key to start the action", PlayNames[playernumber]); (true); ("{0}The action is over", PlayNames[playernumber]); //Player A may step on Player B Lucky Roulette Mine Pause Space-Time Tunnel if (PlayerPos[playernumber] == PlayerPos[1- playernumber])//1-playernumber (make both of them opposite) { ("Players{0}踩到了Players{1},Players{2}retreat6grid", PlayNames[playernumber], PlayNames[1- playernumber], PlayNames[1 - playernumber]); } else//Step at the level { switch (Maps[PlayerPos[playernumber]]) { case 0: ("Players{0}Step on the block,Safety", PlayNames[playernumber]); (true); break; case 1: ("Players{0}Step on the lucky roulette,Please select 1--Switch location ,2--Bomb the other side(对方retreat6grid)", PlayNames[playernumber]); (true); string input = (); while (true) { if (input == "1") { ("Players{0}与Players{1}Switch location", PlayNames[playernumber], PlayNames[1- playernumber]); (true); int temp = PlayerPos[playernumber]; PlayerPos[playernumber] = PlayerPos[1- playernumber]; PlayerPos[1- playernumber] = temp; ("Switch is completed, press any key to continue the game"); (true); break; } else if (input == "2") { ("Players{0}选择轰炸Players{1},Players{2}retreat6grid", PlayNames[playernumber], PlayNames[1- playernumber], PlayNames[1 - playernumber]); (true); PlayerPos[1- playernumber] -= 6; ("Players{0}retreat6grid", PlayerPos[1- playernumber]);//Note that it is player {0}, and do not enter player {1} (true); break; } else { ("Only enter 1 or 2:1--switch position, 2--bomb the opponent (the opponent retreats 6 squares), please re-enter"); (true); input = (); } } break; case 2: ("Players{0}Stepped on a mine,retreat6grid", PlayerPos[playernumber]); PlayerPos[playernumber] -= 6; (true); break; case 3: ("Players{0}Stepped on to pause,Pause for one round", PlayerPos[playernumber]); PlayerFlags[playernumber] = true; (true); break; case 4: ("Players{0}Stepping on the space-time tunnel,"); PlayerPos[playernumber] += 10; (true); break; }//switch }//if-else ChangePos(playernumber); (); DrawMap(Maps); } /// <summary> /// Used when the player coordinates change, to limit the range /// </summary> /// <param name="playnumber"></param> static void ChangePos(int playnumber) { if(PlayerPos[playnumber] <0) { PlayerPos[playnumber] = 0; } if(PlayerPos[playnumber] >= 99) { PlayerPos[playnumber] = 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.