C# console to achieve flying chess games

Time:2021-9-22

This example shares the specific code of flying chess game on c# console for your reference. The specific contents are as follows

Game title

static void ShowTitle()
        {
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine("****************************************");
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("****************************************");
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.WriteLine("****************************************");
            Console.ForegroundColor = ConsoleColor.Red;
            Console. Writeline ("********************************************");
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.WriteLine("****************************************");
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("****************************************");
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine("****************************************");
        }

Game rules description

1. Player a and player B must enter a non empty user name that is not a pure number
2. Player a rolls the dice first, and player AB rolls the dice in turn
3 in the game, “□” represents ordinary grid, “◎” represents lucky roulette, “★” represents mine, “▲” represents pause, “卍” represents space-time tunnel
4. “□” there is no reward or punishment for players!
5. “◎” players have two choices: A. choose to exchange positions with each other; b. Choose to bomb the other party and make the other party step back by 6 steps
6. “★” punish the player and make the player step backward by 6 steps
7. “▲” punish the player to suspend the operation in the next round
8. “Yi” rewards players to advance 10 steps directly
9. If you step on the other party, you will punish the other party by 6 steps

Game map

The map consists of 100 grids in total. It is implemented in Z-type. The first row has 30 grids from left to right, 5 grids from the first column, 30 grids from right to left in the second row, 5 grids from the second column, and 30 grids from left to right in the third row

1. Declare a one-dimensional array of type int, the length is 100, and the default value of each position is 0
2. “□” ordinary grid, that is, the default value of 0 represents
3. “◎” lucky roulette uses the number 1 to represent {6,23,40,55,69,83}
4. ★ “the number 2 used in mines represents {5,13,17,33,38,50,64,80,94}
5. “▲” stop using the number 3 to represent {9,27,60,93}
6. The “卍” space-time tunnel uses the number 4 to represent {20,25,45,63,88,90}

Draw game rules

static void ShowRule()
        {
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("------------------------------------------------------------");
            Console. Writeline ("player [1] is represented by a");
            Console. Writeline ("player [2] is represented by B");
            Console.writeline ("Rule Description:");
            Console. Writeline ("1. Player a rolls the dice first, and players a and B roll the dice in turn");
            Console. Writeline ("2." □ "has no reward or punishment for players!");
            Console. Writeline ("3." ◎ "players have two choices: A. choose to exchange positions with the other party; B. choose to bomb the other party to make the other party step backward by 6 steps");
            Console. Writeline ("4." ★ "punish the player and make the player backward by 6 steps");
            Console. Writeline ("5." ▲ "punish the player to pause the next round of operation");
            Console. Writeline ("6. Reward players with 10 steps forward directly");
            Console. Writeline ("7. If you step on the other party, punish the other party to directly step back by 6 steps");
            Console.WriteLine("------------------------------------------------------------");
        }

Initialize map

static void InitialMap()
        {
            //Determine "◎" = 1
            int[] lunckturn = { 6, 23, 40, 55, 69, 83 };
            for (int i = 0; i < lunckturn.Length; i++)
            {
                int index = lunckturn[i];
                Maps[index] = 1;
            }
            //Confirm "★" = 2
            int[] landmine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 };
            for (int i = 0; i < landmine.Length; i++)
            {
                Maps[landmine[i]] = 2;
            }
            //Confirm "▲" = 3
            int[] pause = { 9, 27, 60, 93 };
            for (int i = 0; i < pause.Length; i++)
            {
                Maps[pause[i]] = 3;
            }
            //Determine "卍" = 4
            int[] timeTunnel = { 20, 25, 45, 63, 88, 90 };
            for (int i = 0; i < timeTunnel.Length; i++)
            {
                Maps[timeTunnel[i]] = 4;
            }
        }

draw a map

/// <summary>
        ///Draw a map
        /// </summary>
        static void DrawMap()
        {
            //First line
            for (int i = 0; i < 30; i++)
            {
                Console.Write(DrawString(i));
            }
            Console.WriteLine();
            //First column
            for (int i = 30; i < 35; i++)
            {
                for (int k = 0; k < 29; k++)
                {
                    Console.Write(" ");
                }
                Console.Write(DrawString(i));
                Console.WriteLine();
            }
            //Second line
            for (int i = 64; i > 34; i--)
            {
                Console.Write(DrawString(i));
            }
            Console.WriteLine();
            //Second column
            for (int i = 65; i <70; i++)
            {
                Console.WriteLine(DrawString(i));
            }
            //Third line
            for (int i = 70; i < 100; i++)
            {
                Console.Write(DrawString(i));
            }
            Console.WriteLine();
        }

        /// <summary>
        ///Select the drawn pattern according to the value of each position in the map array
        /// </summary>
        ///< param name = "type" > the location number in the map array represents the map location index < / param >
        ///< returns > returns the final selected pattern < / returns >
        static string DrawString(int index)
        {
            string str = "";
            //The positions of two players coincide, and ensure that the positions of both players are in the map
            if (playerPosition[0]==playerPosition[1]&&playerPosition[0]==index)
            {
                Console.ForegroundColor = ConsoleColor.White;
                str = "<>";
            }
            else if (playerPosition[0]==index)
            {
                Console.ForegroundColor = ConsoleColor.DarkYellow;
                str = "A";
            }
            else if (playerPosition[1]==index)
            {
                Console.ForegroundColor = ConsoleColor.DarkCyan;
                str = "B";
            }
            else
            {
                switch (Maps[index])
                {
                    case 0:
                        Console.ForegroundColor = ConsoleColor.Cyan;
                        str = "□";
                        break;
                    case 1:
                        Console.ForegroundColor = ConsoleColor.Magenta;
                        str = "◎";
                        break;
                    case 2:
                        Console.ForegroundColor = ConsoleColor.Red;
                        str = "★";
                        break;
                    case 3:
                        Console.ForegroundColor = ConsoleColor.DarkBlue;
                        str = "▲";
                        break;
                    case 4:
                        Console.ForegroundColor = ConsoleColor.Green;
                        STR = "卍";
                        break;
                    default:
                        break;
                }
            }
            return str;
        }

Implement user registration

/// <summary>
        ///Maps
        /// </summary>
        static int[] Maps = new int[100];
        /// <summary>
        ///User name
        /// </summary>
        static string[] playerName = new string[2];
        /// <summary>
        ///Position of two players
        /// </summary>
        static int[] playerPosition = new int[2];
        static void Main(string[] args)
        {
            //Game title
            ShowTitle();
            #Region // / user name registration
            Console. Writeline ("please register two players first!");
            Console.writeline ("registration rule: cannot be empty, and cannot be a pure number, player AB name cannot be the same");
            Console. Writeline ("please enter player a's name:");
            string pattern = @"^\d+$";
            bool isok = true;
            while (isok)
            {
                playerName[0] = Console.ReadLine();
                if (string.IsNullOrWhiteSpace(playerName[0]))
                {
                    Console.writeline ("player a's name cannot be empty, please re-enter");
                }
                else if (Regex.IsMatch(playerName[0], pattern))
                {
                    Console.writeline ("player a's name cannot be a pure number, please re-enter");
                }
                else
                {
                    isok = false;
                }
            }
            isok = true;
            Console. Writeline ("please enter player B's name:");
            while (isok)
            {
                playerName[1] = Console.ReadLine();
                if (string.IsNullOrWhiteSpace(playerName[1]))
                {
                    Console.writeline ("player B's name cannot be empty, please re-enter");
                }
                else if(Regex.IsMatch(playerName[1], pattern))
                {
                    Console.writeline ("player B's name cannot be a pure number, please re-enter");
                }
                else if (playerName[0]==playerName[1])
                {
                    Console.writeline ("player B's name has been occupied, please re-enter");
                }
                else
                {
                    isok = false;
                }
            }
            #endregion
            Console.Clear();
            ShowTitle();
            ShowRule();
            InitialMap();
            DrawMap();
            Console.ReadLine();
        }

Game logic

1. Player a presses any key to roll the dice first
2. Random 1-6 steps after rolling the dice
3. Press any key to move the corresponding steps

/// <summary>
        ///Maps
        /// </summary>
        static int[] Maps = new int[100];
        /// <summary>
        ///User name
        /// </summary>
        static string[] playerName = new string[2];
        /// <summary>
        ///Position of two players
        /// </summary>
        static int[] playerPosition = new int[2];
        /// <summary>
        ///Mark two players who should throw the sieve
        /// </summary>
        static bool[] playerFlag = new bool[2];
        static void Main(string[] args)
        {
            //Game title
            ShowTitle();
            #Region // / user name registration
            Console. Writeline ("please register two players first!");
            Console.writeline ("registration rule: cannot be empty, and cannot be a pure number, player AB name cannot be the same");
            Console. Writeline ("please enter player a's name:");
            string pattern = @"^\d+$";
            bool isok = true;
            while (isok)
            {
                playerName[0] = Console.ReadLine();
                if (string.IsNullOrWhiteSpace(playerName[0]))
                {
                    Console.writeline ("player a's name cannot be empty, please re-enter");
                }
                else if (Regex.IsMatch(playerName[0], pattern))
                {
                    Console.writeline ("player a's name cannot be a pure number, please re-enter");
                }
                else
                {
                    isok = false;
                }
            }
            isok = true;
            Console. Writeline ("please enter player B's name:");
            while (isok)
            {
                playerName[1] = Console.ReadLine();
                if (string.IsNullOrWhiteSpace(playerName[1]))
                {
                    Console.writeline ("player B's name cannot be empty, please re-enter");
                }
                else if(Regex.IsMatch(playerName[1], pattern))
                {
                    Console.writeline ("player B's name cannot be a pure number, please re-enter");
                }
                else if (playerName[0]==playerName[1])
                {
                    Console.writeline ("player B's name has been occupied, please re-enter");
                }
                else
                {
                    isok = false;
                }
            }
            #endregion
            Console.Clear();
            ShowTitle();
            ShowRule();
            InitialMap();
            DrawMap();
            //Start entering the game
            //If it is judged that neither player has reached the end, the game continues
            while (playerPosition[0]<99&&playerPosition[1]<99)
            {
                if (playerFlag[0]==false)
                {
                    PlayGame(0);
                }
                else
                {
                    playerFlag[0] = false;
                }

                if (playerFlag[1]==false)
                {
                    PlayGame(1);
                }
                else
                {
                    playerFlag[1] = false;
                }

                if (playerPosition[0]>=99)
                {
                    Console. Writeline ("congratulations on player [{0}] winning!", playername [0]);
                    break;
                }
                else if (playerPosition[1] >= 99)
                {
                    Console. Writeline ("congratulations on player [{0}] winning!", playername [1]);
                    break;
                }
            }
            Console.ReadLine();
        }

4. After moving, judge the function of this grid and realize the function of special grid
5. Press any key to complete the interface

/// <summary>
        ///Method for realizing game function
        /// </summary>
        ///< param name = "playerindex" > judge which player initiated the game operation through the player index < / param >
        static void PlayGame(int playerIndex)
        {
            #Region // in order to make the player roll the dice and calculate the number of moving steps
            Random r = new Random();
            Console. Writeline ("player [{0}] presses any key to roll dice this round!", playername [playerindex];
            Console.ReadKey(true);
            int number=r.Next(1, 7);
            Console.writeline ("player [{0}] throws < {1} > point", playername [playerindex], number);
            Console. Writeline ("player [{0}] presses any key to start moving!", playername [playerindex];
            Console.ReadKey(true);
            playerPosition[playerIndex] += number;
            Console. Writeline ("player [{0}] has finished moving!", playername [playerindex];
            #endregion
            CheckPosition();
            #Region // / judge the grid rewards and punishments of the moving results
            //First judge whether two players step on each other
            if (playerPosition[0]==playerPosition[1])
            {
                Console. Writeline ("player [{0}] steps on player [{1}], player [{1}] retreats 6 spaces!", playername [playerindex], playername [1-playerindex]);
                playerPosition[1-playerIndex] -= 6;
            }
            else
            {
                switch (Maps[playerPosition[playerIndex]])
                {
                    //Step on the normal grid
                    case 0:
                        Console. Writeline ("player [{0}] steps on a safe zone! No rewards or punishments! Press any key to refresh the interface!", playername [playerindex];
                        Console.ReadKey(true);
                        break;
                    //Step on the wheel of luck
                    case 1:
                        Console. Writeline ("player [{0}] stepped on the lucky wheel! Please select: a -- exchange position, B -- bomb the other party", playername [playerindex]);
                        string type = Console.ReadLine();
                        while (true)
                        {
                            if (type=="a")
                            {
                                Console.writeline ("player [{0}] chooses to exchange location with player [{1}], playername [playerindex], playername [1-playerindex]);
                                int temp = playerPosition[0];
                                playerPosition[0] = playerPosition[1];
                                playerPosition[1] = temp;
                                Console.writeline ("player [{0}] and player [{1}] exchange positions, press any key to refresh the interface!", playername [playerindex], playername [1 - playerindex]);
                                Console.ReadKey(true);
                                break;
                            }
                            else if (type=="b")
                            {
                                Console.writeline ("player [{0}] chooses to bomb player [{1}]", playername [playerindex], playername [1 - playerindex]);
                                playerPosition[1-playerIndex] -= 6;
                                Console.writeline ("player [{0}] bombards player [{1}], press any key to refresh the interface!", playername [playerindex], playername [1 - playerindex]);
                                Console.ReadKey(true);
                                break;
                            }
                            else
                            {
                                Console. Writeline ("please enter the correct command! Please select: a -- exchange location, B -- bomb each other");
                                type = Console.ReadLine();
                            }
                        }
                        break;
                        //Step on a mine
                    case 2:
                        Console. Writeline ("player [{0}] steps on a mine, step back 6! Press any key to refresh the interface", playername [playerindex]);
                        playerPosition[playerIndex] -= 6;
                        Console.ReadKey(true);
                        break;
                        //Step on pause
                    case 3:
                        Console. Writeline ("player [{0}] steps on pause, the next round will be paused! Press any key to refresh the interface", playername [playerindex]);
                        playerFlag[playerIndex] = true;
                        break;
                        //Step on the space-time tunnel
                    case 4:
                        Console. Writeline ("player [{0}] steps on the space-time tunnel, advance 10 steps! Press any key to refresh the interface", playername [playerindex]);
                        playerPosition[playerIndex] += 10;
                        Console.ReadKey(true);
                        break;
                    default:
                        break;
                }
            }
            CheckPosition();
            Console.Clear();
            ShowTitle();
            ShowRule();
            InitialMap();
            DrawMap();
            #endregion
        }
        /// <summary>
        ///Does the break exceed the end point or the start point
        /// </summary>
        static void CheckPosition()
        {
            if (playerPosition[0] < 0)
            {
                playerPosition[0] = 0;
            }
            if (playerPosition[0] > 99)
            {
                playerPosition[0] = 99;
            }
            if (playerPosition[1] < 0)
            {
                playerPosition[1] = 0;
            }
            if (playerPosition[1] > 99)
            {
                playerPosition[1] = 99;
            }
        }

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support developpaer.