SoFunction
Updated on 2025-03-06

C# Flying Chess Mini Program Design Code

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;
  }
  /// &lt;summary&gt;
  /// Initialize the map and change the default map coordinate type  /// 0: Blocks  /// 1: Roulette  /// 2: Landmines  /// 3: Pause  /// 4: Tunnel  /// &lt;/summary&gt;
  public static void InitailMap()
  {
   #region Roulette   int[] luckTrun = { 6, 23, 40, 55, 69, 83 };
   for (int i = 0; i &lt; ; i++)
   {
    Maps[luckTrun[i]] = 1;
   }
   #endregion

   #region Landmines   int[] landMine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 };
   for (int i = 0; i &lt; ; i++)
   {
    Maps[landMine[i]] = 2;
   }
   #endregion

   #region Pause   int[] pause = { 9, 27, 60, 93 };
   for (int i = 0; i &lt; ; i++)
   {
    Maps[pause[i]] = 3;
   }
   #endregion

   #region Tunnel   int[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 };
   for (int i = 0; i &lt; ; i++)
   {
    Maps[timeTunnel[i]] = 4;
   }
   #endregion
  }
  /// &lt;summary&gt;
  /// Set the current coordinate type  /// &lt;/summary&gt;
  /// <param name="i">Coordinates</param>  /// <returns>Coordinate Type</returns>  public static string DrawStringMap(int i)
  {
   string str = null;
   if (PlayerPos[0] == PlayerPos[1] &amp;&amp; PlayerPos[0] == i)
   {
    str = "&lt;&gt;";
   }
   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;
  }
  /// &lt;summary&gt;
  /// Generate all coordinates  /// &lt;/summary&gt;
  public static void DrawMap()
  {
   ("Legend: LuckTrun<◎> landMine<☆> Pause<▲> timeTunnel<N>");

   #region First Step   for (int i = 0; i &lt; 30; i++)
   {
    (DrawStringMap(i));
   }
   ();
   #endregion

   #region First vertical line   for (int i = 30; i &lt; 35; i++)
   {
    for (int j = 0; j &lt;= 28; j++)
    {
     (" ");
    }
    (DrawStringMap(i));
    ();
   }
   #endregion

   #region Second Order   for (int i = 64; i &gt;= 35; i--)
   {
    (DrawStringMap(i));
   }
   ();
   #endregion

   #region Second vertical line   for (int i = 65; i &lt; 70; i++)
   {
    (DrawStringMap(i));
   }
   #endregion

   #region The third stance   for (int i = 70; i &lt;= 99; i++)
   {
    (DrawStringMap(i));
   }
   ();
   #endregion
  }
  /// &lt;summary&gt;
  /// Determine whether the coordinates are out of range  /// &lt;/summary&gt;
  public static void ChangePos()
  {
   #region Player A
   if (PlayerPos[0] &lt; 0)
   {
    PlayerPos[0] = 0;
   }
   if (PlayerPos[0] &gt; 99)
   {
    PlayerPos[0] = 99;
   }
   #endregion

   #region Player B
   if (PlayerPos[1] &lt; 0)
   {
    PlayerPos[1] = 0;
   }
   if (PlayerPos[1] &gt; 99)
   {
    PlayerPos[1] = 99;
   }
   #endregion
  }
  /// &lt;summary&gt;
  /// When stepping on the wheel, select function  /// &lt;/summary&gt;
  /// <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 = ();
    }
   }
  }
  /// &lt;summary&gt;
  /// Play games  /// &lt;/summary&gt;
  /// <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] &lt; 99 &amp;&amp; PlayerPos[1] &lt; 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.