Everyone should have played the flying chess game. How to write it in C# language? This example will share with you the C# implementation code of the flying chess C# for your reference. The specific content is as follows
using System; using ; using ; using ; using ; namespace Pachee { class Program { #region Static fields // Number of levels public static int[] Maps = new int[100]; // Player coordinates public static int[] PlayerPos = new int[2]; // Player name public static string[] PlayerNames = new string[2]; // Determine whether the player is suspended public static bool[] Flags = new bool[2]; #endregion /// <summary> /// Output the game header /// </summary> public static void ShowGame() { = ; ("****************************"); = ; ("****************************"); = ; ("***C#Basic exercises: Flying chess project***"); = ; ("****************************"); = ; ("****************************"); } /// <summary> /// Accept the game name entered by the user to determine whether it is legal /// </summary> /// <returns>Game name</returns> public static string[] InputPlayerNames() { PlayerNames[0] = ""; PlayerNames[1] = ""; = ; while (PlayerNames[0] == "") { ("Please enter the name of game A player: "); PlayerNames[0] = ().Trim(); if (PlayerNames[0] == "") { ("A player name cannot be empty, please enter again."); continue; } break; } while (PlayerNames[1] == "" || PlayerNames[0] == PlayerNames[1]) { ("Please enter the name of game B player: "); PlayerNames[1] = ().Trim(); if (PlayerNames[1] == "") { ("B player name cannot be empty, please enter again."); continue; } else if (PlayerNames[1] == PlayerNames[0]) { ("The player name cannot be the same as the player A B, please enter again."); continue; } break; } return PlayerNames; } /// <summary> /// Initialize the map and change the default map coordinate type /// 0: Blocks /// 1: Roulette /// 2: Landmines /// 3: Pause /// 4: Tunnel /// </summary> public static void InitailMap() { #region Roulette int[] luckTrun = { 6, 23, 40, 55, 69, 83 }; for (int i = 0; i < ; i++) { Maps[luckTrun[i]] = 1; } #endregion #region Landmines int[] landMine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 }; for (int i = 0; i < ; i++) { Maps[landMine[i]] = 2; } #endregion #region Pause int[] pause = { 9, 27, 60, 93 }; for (int i = 0; i < ; i++) { Maps[pause[i]] = 3; } #endregion #region Tunnel int[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 }; for (int i = 0; i < ; i++) { Maps[timeTunnel[i]] = 4; } #endregion } /// <summary> /// Set the current coordinate type /// </summary> /// <param name="i">Coordinates</param> /// <returns>Coordinate Type</returns> public static string DrawStringMap(int i) { string str = null; 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 = "Blog"; break; } } return str; } /// <summary> /// Generate all coordinates /// </summary> public static void DrawMap() { ("Legend: LuckTrun<◎> landMine<☆> Pause<▲> timeTunnel<N>"); #region First Step for (int i = 0; i < 30; i++) { (DrawStringMap(i)); } (); #endregion #region First vertical line for (int i = 30; i < 35; i++) { for (int j = 0; j <= 28; j++) { (" "); } (DrawStringMap(i)); (); } #endregion #region Second Order for (int i = 64; i >= 35; i--) { (DrawStringMap(i)); } (); #endregion #region Second vertical line for (int i = 65; i < 70; i++) { (DrawStringMap(i)); } #endregion #region The third stance for (int i = 70; i <= 99; i++) { (DrawStringMap(i)); } (); #endregion } /// <summary> /// Determine whether the coordinates are out of range /// </summary> public static void ChangePos() { #region Player A if (PlayerPos[0] < 0) { PlayerPos[0] = 0; } if (PlayerPos[0] > 99) { PlayerPos[0] = 99; } #endregion #region Player B if (PlayerPos[1] < 0) { PlayerPos[1] = 0; } if (PlayerPos[1] > 99) { PlayerPos[1] = 99; } #endregion } /// <summary> /// When stepping on the wheel, select function /// </summary> /// <param name="input">Player's Choice</param> /// <param name="player">Player Mark</param> public static void PlayerSelect(string input, int player) { while (true) { if (input == "1") { ("Player {0} select and {1} swap places.", PlayerNames[player], PlayerNames[1 - player]); int temp = PlayerPos[player]; PlayerPos[player] = PlayerPos[1 - player]; PlayerPos[1 - player] = temp; ("Swap complete, press any key continue."); (true); break; } else if (input == "2") { ("Player {0} select bombing {1}, Player {2} back to 6.", PlayerNames[player], PlayerNames[1 - player], PlayerNames[1 - player]); PlayerPos[1 - player] -= 6; (true); break; } else { ("Can only select: 1--Swap places 2--bombing: "); input = (); } } } /// <summary> /// Play games /// </summary> /// <param name="player">Player label</param> public static void PlayGame(int player) { Random r = new Random(); int next = (1, 7); ("{0} press any key to start rolling the dice.", PlayerNames[player]); (true); ("{0} Throw out of {1}", PlayerNames[player], next); PlayerPos[player] += next; ChangePos(); (true); ("{0} press any key to start action.", PlayerNames[player]); (true); ("{0} action complete.", PlayerNames[player]); (true); // Player A may step on: Player B, blocks, roulettes, mines, pauses, tunnels if (PlayerPos[player] == PlayerPos[1 - player]) { ("Player {0} step on {1}, {2} back to 6.", PlayerNames[player], PlayerNames[1 - player], PlayerNames[1 - player]); PlayerPos[1 - player] -= 6; (true); } else { switch (Maps[PlayerPos[player]]) { case 0: ("Player {0} step on Square, safe.", PlayerNames[player]); (true); break; case 1: ("Player {0} step on a LuckTrun, please select: 1--Swap places 2--bombing: ", PlayerNames[player]); string input = ().Trim(); PlayerSelect(input, player); (true); break; case 2: ("Player {0} step on a LandMine, back to 6", PlayerNames[player]); PlayerPos[player] -= 6; (true); break; case 3: ("Player {0} step on a Pause, to suspend a round.", PlayerNames[player]); (true); Flags[player] = true; break; case 4: ("Player {0} step on a TimeTunnel, forward 10.", PlayerNames[player]); PlayerPos[player] += 10; (); break; } } ChangePos(); (); DrawMap(); } static void Main(string[] args) { ShowGame(); InputPlayerNames(); ("Player {0} is A.", PlayerNames[0]); ("Player {0} is B.", PlayerNames[1]); InitailMap(); DrawMap(); while (PlayerPos[0] < 99 && PlayerPos[1] < 99) { #region A if (Flags[0] == false) { PlayGame(0); } else { Flags[0] = false; } #endregion #region B if (Flags[1] == false) { PlayGame(1); } else { Flags[1] = false; } #endregion } #region judges player victory if (PlayerPos[0] == 99) { (); ("Player {0} Win.", PlayerNames[0]); } if (PlayerPos[1] == 99) { (); ("Player {0} Win.", PlayerNames[1]); } #endregion (); } } }
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.