This article shares the specific code for C# to implement a simple flying chess game for your reference. The specific content is as follows
The complete code is shown below:
namespace Flying chess { class Program { public static int[] Maps = new int[100]; public static int[] PlayerPos = new int[2]; public static string[] PlayerName = new string[2]; public static bool[] Flags = new bool[2];//The initial value is false static void Main(string[] args) { GameStart(); Mes(); (); GameStart(); ("{0}SoldiersAexpress\n{1}SoldiersAexpress", PlayerName[0], PlayerName[1]); InitailMap(); DrawMap(); while (PlayerPos[0] < 99 && PlayerPos[1] < 99) { for (int i = 0; i < 2; i++) { if (!Flags[i]) PlayGame(i); else Flags[i] = false; if (PlayerPos[i] >= 99) ("Players{0}victory",PlayerName[i]); } } Win(); (); } /// <summary> /// Game start prompt /// </summary> public static void GameStart() { = ; ("************************"); = ; ("************************"); = ; ("*********Flying Chess*********"); = ; ("************************"); = ; ("************************"); } /// <summary> /// Enter player information /// </summary> public static void Mes() { ("Please enter player A's name"); PlayerName[0] = (); while (PlayerName[0] == " ") { ("The player's name cannot be empty, please re-enter the name of player A"); PlayerName[0] = (); } ("Please enter player B's name"); PlayerName[1] = (); while (PlayerName[1] == " " || PlayerName[0] == PlayerName[1]) { if (PlayerName[1] == " ") ("The player's name cannot be empty, please re-enter"); else ("The names of the two players cannot be consistent, please re-enter the name of player B."); PlayerName[0] = (); } } /// <summary> /// Initialize the map /// </summary> public static void InitailMap() { int[] luckyturn = { 6, 23, 40, 55, 69, 83 }; int[] landMine = { 5, 13, 17, 33, 38, 50, 55, 80, 94 }; int[] pause = { 9, 27, 60, 93 }; int[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 }; foreach (int i in luckyturn) Maps[i] = 1; foreach (int i in landMine) Maps[i] = 2; foreach (int i in pause) Maps[i] = 3; foreach (int i in timeTunnel) Maps[i] = 4; } /// <summary> /// Implement the conversion of numbers and special characters /// </summary> public static void DrawMap() { int i; ("Lucky Disc: ①\tBomb: ★\tPause: ▲\tSpace Tunnel: ﹌"); #region First Random for (i = 0; i < 30; i++) (Draw(i)); (); #endregion #region First vertical line for (; i <35; i++) { for(int j = 0; j < 29; j++) (" "); (Draw(i)); } #endregion #region Second Random for (i = 64; i >= 35; i--) (Draw(i)); (); #endregion #region Second vertical line for (i=65; i < 70; i++) { (Draw(i)); //(); } #endregion #region The third strait for (; i<100; i++) (Draw(i)); #endregion (); } /// <summary> /// Convert array to special characters /// </summary> /// <param name="i"></param> /// <returns></returns> public static string Draw(int i) { string str=" "; if (PlayerPos[0] == PlayerPos[1] && PlayerPos[1] == 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;//Lucky Disk case 2: = ; str = "★"; break;// Landmines case 3: = ; str = "▲"; break;//pause case 4: = ; str = "﹌"; break;//Space-time tunnel } } return str; } /// <summary> /// The game is in code segment /// </summary> /// <param name="playerNumber"></param> public static void PlayGame(int playerNumber) { Random r = new Random(); ("{0}Press any key to start rolling the dice", PlayerName[playerNumber]); (true); int n = (1,7); ("{0}Thrown{1}", PlayerName[playerNumber], n); PlayerPos[playerNumber] += n; ChangePos(); (true); ("{0}Press any key to start the action", PlayerName[playerNumber]); (true); ("{0}The action ends", PlayerName[playerNumber]); (true); if (PlayerPos[playerNumber] == PlayerPos[1- playerNumber]) { ("Players{0}踩到了Players{1},Players{2}Back six squares", PlayerName[playerNumber], PlayerName[1- playerNumber], PlayerName[1- playerNumber]); PlayerPos[1] -= 6; (true); } else { switch (Maps[PlayerPos[playerNumber]]) { case 0: ("Players{0}normal", PlayerName[playerNumber]); (true); break; case 1: ("Players{0}Stepping on the lucky disc,There are two options below:1.Both parties exchange positions,2.对方Back six squares",PlayerName[playerNumber]); while (true) { string input = (); if (input == "1") { ("Players{0}Select the swap location", PlayerName[playerNumber]); (true); int temp; temp = PlayerPos[playerNumber]; PlayerPos[playerNumber] = PlayerPos[1- playerNumber]; PlayerPos[1- playerNumber] = temp; ("Switch successfully, press any key to continue the game"); (true); break; } else if (input == "2") { ("Players{0}Choose to bomb the other side", PlayerName[playerNumber]); (true); PlayerPos[1- playerNumber] -= 6; (true); break; } else { ("Only enter 1 or 2"); (true); } } break; case 2: ("Players{0}Stepped on a mine,Retreat six units", PlayerName[playerNumber]); (true); PlayerPos[playerNumber] -= 6; break; case 3: ("Players{0}Stepped on to pause,Pause for one round", PlayerName[playerNumber]); Flags[playerNumber] = true; (true); break; case 4: ("Players{0}Stepping on the space-time tunnel,Advance ten squares", PlayerName[playerNumber]); PlayerPos[playerNumber] += 10; (true); break; } } ChangePos(); (); DrawMap(); } /// <summary> /// Called when the player coordinates change /// </summary> public static void ChangePos() { 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; } public static void Win() { ("Virectory"); } } }
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.