C#Learning Notes 4-Flying Chess Project

Posted by hassanz25 on Mon, 03 Feb 2020 03:59:42 +0100

Flying Chess Items

Preface

The last genius said that he studied eight hours a day. Unexpectedly, he broke his word yesterday and learned four hours. He got up at 7:30 this morning to continue learning the flying chess project, and finally compiled it successfully.Due to the problem of computer characters, it turned out to be a bit ugly. You can learn to beautify it by learning QT after you have finished C#.

Introduction to Flying Chess Game

This program achieves flying chess which is different from what we played as a kid. The rules are similar, but flying chess in my learning process greatly simplifies the process. The current program is suitable for two-player play (or more people). There are mainly the following levels:
Lucky Wheel*: Give players two choices 1 - to swap positions; 2 - to take another player back 6 spaces
Mine$: Player returns 6 places
Pause &: Player pauses a round
Space Time Tunnel#: Players advance 10 spaces
Players trampling on each other will cause another player to retreat 6 spaces
Player A and Player B win at the end

Flying Chess Game Writing

The game has 100 strings, designed against each horizontal and vertical line at the time of programming, and player A and player B cannot have empty names (but light declaration="" does not do that and has not yet been resolved), and cannot be the same
The position of each level is fixed (or random)
Specific steps are included in the notes, with code:
(Give me more credit for finding it useful)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GameDesign
{
    class Program
    {
        //Simulate global variables--for use by multiple functions
        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();
            // One problem is that when the string is "", the game is not recognizable and the player name is still empty.
            #region Enter Player Name
            Console.WriteLine("Please enter a player A Name of");
            PlayNames[0] = Console.ReadLine();
            while (PlayNames[0] == " ")
            {
                Console.WriteLine("Game player A Name cannot be empty, please re-enter");
                PlayNames[0] = Console.ReadLine();
            }
            Console.WriteLine("Please enter a player B Name of");
            PlayNames[1] = Console.ReadLine();
            while (PlayNames[1] == "" || PlayNames[0] == PlayNames[1])
            {
                if (PlayNames[1] == "")
                {
                    Console.WriteLine("Game player B Name cannot be empty, please re-enter");
                    PlayNames[1] = Console.ReadLine();
                }
                else
                {
                    Console.WriteLine("Game player A With Players B The names cannot be the same, please re-enter");
                    PlayNames[1] = Console.ReadLine();
                }
            }
            #endregion
            //Player's name is cleared after Ok is entered
            Console.Clear();//Clear screen
            GameShow();
            Console.WriteLine("{0}For soldiers A Express", PlayNames[0]);
            Console.WriteLine("{0}For soldiers B Express", PlayNames[1]);
            //Initialize the map before drawing it
            OriginalMap(Maps);
            DrawMap(Maps);
            //Game Termination Conditions
            while (PlayerPos[0] <= 99 && PlayerPos[1] <= 99)
            {
                if (PlayerFlags[0] == false)
                {
                    PlayGame(0);
                }
                else
                {
                    PlayerFlags[0] = false;
                }
                if (PlayerPos[0] >= 99)
                {
                    Console.WriteLine("Game player{0}Win Player{1}", PlayNames[0], PlayNames[1]);
                    break;
                }
                if (PlayerFlags[1] == false)
                {
                    PlayGame(1);
                }
                else
                {
                    PlayerFlags[1] = false;
                }
                if (PlayerPos[1] >= 99)
                {
                    Console.WriteLine("Game player{0}Win Player{1}", PlayNames[1], PlayNames[0]);
                    break;

                }//while
                Console.ReadKey();
            }
        }
        /// <summary>
        ///Draw game head
        /// </summary>
        static void GameShow()
        {
            
            Console.ForegroundColor=ConsoleColor.Blue;//Used to set colors
            Console.WriteLine("--------------------------------");
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("--------------------------------");
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("--------------------------------");
            Console.ForegroundColor = ConsoleColor.DarkMagenta;
            Console.WriteLine("-------Welcome Laser Flight chess------");
            Console.ForegroundColor = ConsoleColor.DarkGray;
            Console.WriteLine("--------------------------------");
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine("--------------------------------");
            Console.ForegroundColor = ConsoleColor.DarkRed;
            Console.WriteLine("--------------------------------");
        }
        /// <summary>
        ///Initialize map settings
        /// </summary>
        /// <param name="Maps"></param>
        static void OriginalMap(int[] Maps)
        {
            /*
             * The process of initializing a map - the process of turning numbers into strings in an array
             * There are five special levels that can be replaced by different numbers in the array
             * 0---Represents normal, 1 - lucky wheel, 2 - mines, 3 - pause, 4 - space-time tunnel
             * Coordinates of a particular level on 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 with different numbers
            for (int i = 0; i < luckyTurn.Length; i++)
            {
                int n = luckyTurn[i];
                Maps[n] = 1;
                //Maps[luckyTurn[i]]=1;
            }
            for (int i = 0; i < landMine.Length; i++)
            {
                int n = landMine[i];
                Maps[n] = 2;
            }
            for (int i = 0; i < pause.Length; i++)
            {
                int n = pause[i];
                Maps[n] = 3;
            }
            for (int i = 0; i < timeTunnel.Length; i++)
            {
                int n = timeTunnel[i];
                Maps[n] = 4;
            }
        }
        /// <summary>
        ///Mapping
        /// </summary>
        /// <param name="Maps"></param>
        static void DrawMap(int[] Maps)
        {
            Console.WriteLine("Legend: Lucky Wheel*,mine $,suspend&,Space Time Tunnel#");
            #region Draw the first horizontal line
            //Draw the first horizontal line
            for (int i = 0; i < 30; i++)
            {
                Console.Write(DrawStringMap(i));
            }
            Console.WriteLine();
            #endregion
            //Need to wrap after each line is drawn
            #region Draw the first vertical line
            for (int i = 30; i < 35; i++)
            {
                for (int j = 0; j <= 28 ; j++)
                {
                    Console.Write(" ");
                }
                Console.Write(DrawStringMap(i));
                Console.WriteLine();
            }

            #endregion
            #region Draw the second horizontal line
            for (int i = 64; i>= 35; i--)
            {
                Console.Write(DrawStringMap(i));
            }
            Console.WriteLine();
            #endregion
            #region Draw the second vertical line
            for (int i= 65;i<= 69 ; i++)
            {
                Console.WriteLine(DrawStringMap(i));
            }
            #endregion
            #region Draw the third line
            for (int i = 70; i <= 99; i++)
            {
                Console.Write(DrawStringMap(i));
            }
            Console.WriteLine();
            #endregion
        }
        /// <summary>
        ///Initialize string in 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 both are on the map (make the player coordinates equal to i), draw <
            if (PlayerPos[0] == PlayerPos[1] && PlayerPos[0] == i)
            {
                str="<";
            }
            else if (PlayerPos[0] == i)
            {
                //shift + space, two half-angle symbols occupy the position of one full-angle symbol in the console
                str="A";
            }
            else if (PlayerPos[1] == i)
            {
                str="B";
            }
            else
            {
                // 0--- for Normal@, 1--- Lucky Wheel*, 2--- Mine$, 3--- Pause &, 4--- Space Time Tunnel#.
                switch (Maps[i])
                {
                    case 0:
                        Console.ForegroundColor = ConsoleColor.Blue;
                        str="@";
                        break;
                    case 1:
                        Console.ForegroundColor = ConsoleColor.Cyan;
                        str="*";
                        break;
                    case 2:
                        Console.ForegroundColor = ConsoleColor.Green;
                        str="$";
                        break;
                    case 3:
                        Console.ForegroundColor = ConsoleColor.Red;
                        str="&";
                        break;
                    case 4:
                        Console.ForegroundColor = ConsoleColor.Magenta;
                        str="#";
                        break;
                }
            }
            return str;
        }
        /// <summary>
        ///Game Settings
        /// </summary>
        /// <param name="playernumber">player number</param>
        static void PlayGame(int playernumber)
        {
            Console.WriteLine("{0}Press any key to start dicing", PlayNames[playernumber]);
            Console.ReadKey(true);//You can see different effects by comparing Console.Readkey()
            Random r = new Random();
            int step = r.Next(1, 7);//Left Closed Right Open Interval
            PlayerPos[playernumber] += step;
            Console.WriteLine("{0}Thrown{1}", PlayNames[playernumber],step);
            Console.ReadKey(true);
            Console.WriteLine("{0}Press any key to start the action", PlayNames[playernumber]);
            Console.ReadKey(true);
            Console.WriteLine("{0}Action finished", PlayNames[playernumber]);
            //Player A may step on Player B Lucky Wheel Mine to pause space-time tunnel
            if (PlayerPos[playernumber] == PlayerPos[1- playernumber])//1-playernumber (make them the opposite number)
            {
                Console.WriteLine("Game player{0}Stepped on the player{1},Game player{2}Back 6", PlayNames[playernumber], PlayNames[1- playernumber], PlayNames[1 - playernumber]);
            }
            else//Step on a level
            {
                switch (Maps[PlayerPos[playernumber]])
                {
                    case 0:
                        Console.WriteLine("Game player{0}Step on a square, safe", PlayNames[playernumber]);
                        Console.ReadKey(true);
                        break;
                    case 1:
                        Console.WriteLine("Game player{0}Step on the lucky wheel, please choose 1--Swap locations, 2--Bombing the other side (6 spaces back)", PlayNames[playernumber]);
                        Console.ReadKey(true);
                        string input = Console.ReadLine();
                        while (true)
                        {
                            if (input == "1")
                            {
                                Console.WriteLine("Game player{0}With Players{1}Change of position", PlayNames[playernumber], PlayNames[1- playernumber]);
                                Console.ReadKey(true);
                                int temp = PlayerPos[playernumber];
                                PlayerPos[playernumber] = PlayerPos[1- playernumber];
                                PlayerPos[1- playernumber] = temp;
                                Console.WriteLine("Exchange complete, press any key to continue the game");
                                Console.ReadKey(true);
                                break;
                            }
                            else if (input == "2")
                            {
                                Console.WriteLine("Game player{0}Select Bomber Player{1},Game player{2}Back 6", PlayNames[playernumber], PlayNames[1- playernumber], PlayNames[1 - playernumber]);
                                Console.ReadKey(true);
                                PlayerPos[1- playernumber] -= 6;
                                Console.WriteLine("Game player{0}Back 6", PlayerPos[1- playernumber]);//Note that it is player {0} and not player {1}
                                Console.ReadKey(true);
                                break;
                            }
                            else
                            {
                                Console.WriteLine("Only 1 or 2:1 can be entered--Swap locations, 2--Bombing each other (they backed 6 spaces), please re-enter");
                                Console.ReadKey(true);
                                input = Console.ReadLine();
                            }
                        }
                        break;
                    case 2:
                        Console.WriteLine("Game player{0}Stepped on a mine and backed 6 spaces", PlayerPos[playernumber]);
                        PlayerPos[playernumber] -= 6;
                        Console.ReadKey(true);
                        break;
                    case 3:
                        Console.WriteLine("Game player{0}Step on the pause, pause a round", PlayerPos[playernumber]);
                        PlayerFlags[playernumber] = true;
                        Console.ReadKey(true);
                        break;
                    case 4:
                        Console.WriteLine("Game player{0}Stepping on the space-time tunnel,");
                        PlayerPos[playernumber] += 10;
                        Console.ReadKey(true);
                        break;
                }//switch
            }//if-else
            ChangePos(playernumber);
            Console.Clear();
            DrawMap(Maps);

        }
        /// <summary>
        ///Used when player coordinates change to limit 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;
            }
        }
    }
}

Published 5 original articles, received 0 reviews, and received 145 visits
Private letter follow

Topics: Qt Programming