SoFunction
Updated on 2025-03-06

C# Knight Flying Chess Source Code (Share)


using System;
using ;
using ;
using ;
using ;


namespace Knight flying chess
{
    class Program
    {
//Storage our game map level in the array below
//The element with subscript of the array is 0 corresponding to the first grid on the map. The element with subscript is 1 corresponding to the second grid... The element with subscript is n corresponding to n+1 grid
//In the array 1: indicates lucky roulette ◎
//                                                                                                                              �
//                                                                                                                              �
//                4: Indicates the space-time tunnel
//                                                              �
        static int[] map = new int[100];

static string[] names = new string[2]; //names[0]Storage the name of player A   name[1]Storage the name of player B

static int[] playerPos = { 0, 0 };//playPos[0] save the position of player A, playPos[1] save the position of player B

static int step = 0; // Used to store the generated random numbers
       
static string input = ""; //The user stores the user's input
       
static string msg = ""; // Used to store if the user steps on a certain level and output it
       
static bool[] isStop = { false, false };//isStop[0] indicates whether player A has reached a pause last time. It seems to be true, not false

static Random r = new Random();//r is the random number generated
       
        static void Main( string[] args)
        {
           


ShowUI(); //Show the game
            InitialName();
            ();
            ShowUI();
("The battle begins...");
("{0} is represented by A", names[0]);
("{0} is represented by B", names[1]);
("If AB is in the same position, use <>");
InitialMap();//Initialize the map
drawMap();//Draw the map
("Start the game...");

//In this loop, player A and player B roll dice in turn. When the coordinates of player A or player B are >=99, the loop ends.
            while (playerPos[0] < 99 && playerPos[1] < 99)
            {
Action(0);//A throw sieve
Action(1);//B Throw sieve
            }
            ();
        }

        /// <summary>
/// The name used to draw the flying chess
        /// </summary>
        static void ShowUI()
        {
            ("*******************************************************");
            ("*                                                     *");
("*
            ("*                                                     *");
            ("*******************************************************");
        }

        static void InitialName()
        {
("Please enter the name of Player A");
            names[0] = ();
//Judge whether the content entered in the book is empty. If it is empty, ask the user to re-enter
            while (names[0] == "")
            {
("Player A's name cannot be empty, please re-enter!");
                names[0] = ();
            }
("Please enter player B's name");
            names[1] = ();
//Distinguish whether the content entered by the user is empty. If it is empty, ask the user to re-enter
            while (names[1] == "" || names[1] == names[0])
            {
                if (names[1] == "")
                {
("Player B's name cannot be empty, please re-enter!");
                    names[1] = ();
                }
                else
                {
("The name you entered is the same as the name {0} of player A, please re-enter", names[0]);
                    names[1] = ();
                }

            }
        }

        static void InitialMap()
        {
//Subscript used to store landmines in the map
int[] luckyTurn = { 6, 23, 40, 55, 69, 83 };//Lucky Roulette 1
int[] landMine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 };// Landmines 2
int[] pause = { 9, 27, 60, 93 };//Pause coordinates 3
int[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 };//Spatial-time tunnel  4

for (int i = 0; i < 100; i++) // Initialize the data of the map array
                map[i] = 0;

//Fill in the lucky roulette position into the map
            for (int i = 0; i < ; i++)
                map[luckyTurn[i]] = 1;
//Fill the mine into the map
            for (int i = 0; i < ; i++)
                map[landMine[i]] = 2;
//Fill in the map
            for (int i = 0; i < ; i++)
                map[pause[i]] = 3;
//Fill the space-time tunnel into the map
            for (int i = 0; i < ; i++)
                map[timeTunnel[i]] = 4;
        }

        /// <summary>
///  Obtain the pattern that should be drawn on the pos coordinate
        /// </summary>
/// <param name="pos">Coordinates to be drawn</param>
        /// <returns></returns>
        static string getMapString(int pos)
        {
            string result = "";
            if (playerPos[0] == pos && playerPos[1] == pos)
            {
                = ;
                result = "<>";
            }
            else if (playerPos[0] == pos)
            {
                = ;
                result = "A";
            }
            else if (playerPos[1] == pos)
            {
                = ;
                result = "B";
            }
            else
            {
                switch (map[pos])
                {
                    case 0:
                        = ;
                        result = "□";
                        break;
                    case 1:
                        = ;
                        result = "◎";
                        break;
                    case 2:
                        = ;
                        result = "☆";
                        break;
                    case 3:
                        = ;
                        result = "▲";
                        break;
                    case 4:
                        = ;
result = "swastika";
                        break;
                }
            }
            return result;
        }

        static void drawMap()
        {
("Legend: Lucky Roulette: ◎   Landmine: ☆   Pause: ▲    Time and Space Tunnel: swastika  ");
//Draw the first line
            for (int i = 0; i < 30; i++)
                (getMapString(i));
            ();

//The first column on the left
            for (int i = 30; i < 35; i++)
            {
                for (int j = 0; j < 29; j++)
                    (" ");
                (getMapString(i));
                ();
            }

//The second line
            for (int i = 64; i >= 35; i--)
                (getMapString(i));
            ();

//The column on the right
            for (int i = 65; i < 70; i++)
            {
                (getMapString(i));
                ();
            }

//The third line
            for (int i = 70; i < 100; i++)
                (getMapString(i));
            ();
            ();
        }

        static void checkPos()
        {
            for (int i = 0; i <= 1; i++)
            {
                if (playerPos[i] > 99)
                {
                    playerPos[i] = 99;
                }
                if (playerPos[i] < 0)
                {
                    playerPos[i] = 0;
                }
            }
        }

static int ReadInt()//produce an integer
        {
            int i = ReadInt(, );
            return i;
        }

static int ReadInt(int min, int max)//Generate the number between min--max
        {
            while (true)
            {
                try
                {
                    int number = Convert.ToInt32(());
                    if (number < min || number > max)
                    {
("You can only enter numbers between {0}--{1}, please re-enter", min, max);
                        continue;
                    }
                    return number;
                }
                catch
                {
("You can only enter numbers, please re-enter!");
                }

            }

        }

        /// <summary>
/// Method of throwing sieve A or B
        /// </summary>
/// <param name="playerNumber">A throw sieve is transmitted from 0  B throw sieve is transmitted from 1</param>
        static void Action(int playerNumber)
        {
            if (isStop[playerNumber] == false)
            {
("{0}Press any key to start throwing the sieve...", names[playerNumber]);
                ConsoleKeyInfo sec = (true);
step = (1, 7);//Create a random number between 1 and 6
                if ( == )
                {
                    ConsoleKeyInfo sec1 = (true);
                    if ( == ConsoleKey.F1)
                    {
                        step = ReadInt(1, 100);
                    }
                }

("{0}rolled {1}", names[playerNumber], step);
("{0}Press any key to start the action...", names[playerNumber]);
                (true);
playerPos[playerNumber] += step; //Note that once the coordinates change, you must determine whether the coordinate value is >99||<0
checkPos();//Check whether the coordinates are out of bounds

if (playerPos[playerNumber] == playerPos[1 - playerNumber]) //Player A picks player B
                {
                    playerPos[1 - playerNumber] = 0;
msg = ("{0} stepped on {1}, {1} returned to the origin", names[playerNumber], names[1 - playerNumber]);
                }
                else
{//If you didn't step on it, you need to determine whether there are other levels in the current location of Player A.
                    switch (map[playerPos[playerNumber]])
                    {
                        case 0:
//On average, no effect
                            msg = "";
                            break;
                        case 1:
//Go to the Lucky Roulette Level
                            ();
("You have reached the lucky roulette, please choose luck?");
("1 ---Swap positions 2--Bomb the opponent");
                            int userSelect = ReadInt(1, 2);
                            if (userSelect == 1)
{//Try to exchange positions with the other party
                                int temp = playerPos[playerNumber];
                                playerPos[playerNumber] = playerPos[1 - playerNumber];
                                playerPos[1 - playerNumber] = temp;
msg = ("{0}Selected to exchange position with the other party", names[playerNumber]);
                            }
                            else
{//Bomb the other side
                                playerPos[1 - playerNumber] -= 6;
msg = ("{0} bombed {1}, {1} returned 6 blocks", names[playerNumber], names[1 - playerNumber]);
                                checkPos();
                            }
                            break;
                        case 2:
//Step on a mine
                            playerPos[playerNumber] -= 6;
                            checkPos();
msg = ("{0} stepped on the mine, {0} retreated 6 blocks", names[playerNumber]);
                            break;
                        case 3:
//Pause once
                            isStop[playerNumber] = true;
msg = ("{0} has reached the red light, pause once next time", names[playerNumber]);
                            break;
                        case 4:
//Step on the space-time tunnel
                            playerPos[playerNumber] += 10;
msg = ("{0} entered the space-time tunnel and entered 10 blocks", names[playerNumber]);
                            break;
                    }

                }
            }
            else
            {
                isStop[playerNumber] = false;
            }

            if (playerPos[playerNumber] >= 99)
            {
//Judge who wins and who fails
                ();
                if (playerPos[0] >= 99)
                {
("{0} won!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", names[0]);
                }
                else
                {
("{0}Victed!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", names[1]);
                }
            }

            ();
            drawMap();
            if (msg != "")
            {
                (msg);
            }
("{0} rolled {1}, the action was completed!", names[playerNumber], step);
("************The locations of player A and player B*************);
(The position of "{0} is: {1}", names[0], playerPos[0] + 1);
(The position of "{0} is: {1}", names[1], playerPos[1] + 1);
         
        }
    }
}