C knight flying chess source of share

  • 2020-05-10 18:40:16
  • OfStack

The code is as follows:


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

namespace  Knight flying chess 
{
    class Program
    {
        // The following array stores the levels of our game map 
        // The index of the array is zero 0 The element corresponds to the number on the map 1 "      The subscript for 1 The element corresponds to the element number 2 " ... The subscript for n The element corresponding to n+1 " 
        // In the array    1 : stands for wheel of fortune    Thecountry. 
        //           2:  Said mines   Do things 
        //           3:  Said to suspend   Bring about 
        //           4:  Represents a space-time tunnel   � 
        //           0:  Said ordinary    - 
        static int[] map = new int[100];
        static string[] names = new string[2]; //names[0] Store the player A The names of the     name[1] Save the player B The names of the 
        static int[] playerPos = { 0, 0 };//playPos[0] Save the player A The location of the, playPos[1] Save the player B The location of the 
        static int step = 0; // Used to store the generated random Numbers 

        static string input = ""; // The user stores the user's input 

        static string msg = ""; // It is used to store the output when the user steps on a level 

        static bool[] isStop = { false, false };//isStop[0] Said the player A Whether or not the last time 1 Walk to a pause, as if to say true That are not for false
        static Random r = new Random();//r Is the random number generated 

        static void Main( string[] args)
        {
            

            ShowUI(); // According to the game 
            InitialName();
            Console.Clear();
            ShowUI();
            Console.WriteLine(" Against began ......");
            Console.WriteLine("{0} with A To represent the ", names[0]);
            Console.WriteLine("{0} with B To represent the ", names[1]);
            Console.WriteLine(" if AB In the same 1 Location, <> said ");
            InitialMap();// Initialize map 
            drawMap();// map 
            Console.WriteLine(" Start the game ......");
            // Let the player in this loop A And the player B Roll the dice    When a player A Or the player B The coordinates of the >=99 When, the loop ends 
            while (playerPos[0] < 99 && playerPos[1] < 99)
            {
                Action(0);//A Rolling dice 
                Action(1);//B Rolling dice   
            }
            Console.ReadKey();
        }
        /// <summary>
        ///  Used to draw the name of the flight chess 
        /// </summary>
        static void ShowUI()
        {
            Console.WriteLine("*******************************************************");
            Console.WriteLine("*                                                     *");
            Console.WriteLine("*          Riding a       "       fly       line        chess              *");
            Console.WriteLine("*                                                     *");
            Console.WriteLine("*******************************************************");
        }
        static void InitialName()
        {
            Console.WriteLine(" Please enter player A The names of the ");
            names[0] = Console.ReadLine();
            // Determine if the content entered with the book is empty, and if so, ask the user to re-enter it 
            while (names[0] == "")
            {
                Console.WriteLine(" The player A The name cannot be empty, please enter again! ");
                names[0] = Console.ReadLine();
            }
            Console.WriteLine(" Please enter player B The names of the ");
            names[1] = Console.ReadLine();
            // Determine if the user has entered anything that is empty, and if so, have the user re-enter it 
            while (names[1] == "" || names[1] == names[0])
            {
                if (names[1] == "")
                {
                    Console.WriteLine(" The player B The name cannot be empty, please enter again! ");
                    names[1] = Console.ReadLine();
                }
                else
                {
                    Console.WriteLine(" You enter the name with the player A The names of the {0} The same , Please re-enter ", names[0]);
                    names[1] = Console.ReadLine();
                }
            }
        }
        static void InitialMap()
        {
            // Used to store the subscript of a mine on a map 
            int[] luckyTurn = { 6, 23, 40, 55, 69, 83 };// Wheel of fortune  1 
            int[] landMine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 };// mines  2
            int[] pause = { 9, 27, 60, 93 };// Suspended coordinates  3 
            int[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 };// Time and space tunnel   4
            for (int i = 0; i < 100; i++) //  Initialize the map Array data 
                map[i] = 0;
            // Fill in the position of the wheel of fortune map In the 
            for (int i = 0; i < luckyTurn.Length; i++)
                map[luckyTurn[i]] = 1;
            // Fill in the mines map In the 
            for (int i = 0; i < landMine.Length; i++)
                map[landMine[i]] = 2;
            // Let's put in the pause map In the 
            for (int i = 0; i < pause.Length; i++)
                map[pause[i]] = 3;
            // Fill in the time tunnel map In the 
            for (int i = 0; i < timeTunnel.Length; i++)
                map[timeTunnel[i]] = 4;
        }
        /// <summary>
        ///   For the first pos The pattern that should be drawn on the coordinates 
        /// </summary>
        /// <param name="pos"> The coordinates to draw </param>
        /// <returns></returns>
        static string getMapString(int pos)
        {
            string result = "";
            if (playerPos[0] == pos && playerPos[1] == pos)
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                result = "<>";
            }
            else if (playerPos[0] == pos)
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                result = " a. ";
            }
            else if (playerPos[1] == pos)
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                result = " B ";
            }
            else
            {
                switch (map[pos])
                {
                    case 0:
                        Console.ForegroundColor = ConsoleColor.White;
                        result = " - ";
                        break;
                    case 1:
                        Console.ForegroundColor = ConsoleColor.Red;
                        result = " Thecountry. ";
                        break;
                    case 2:
                        Console.ForegroundColor = ConsoleColor.Green;
                        result = " Do things ";
                        break;
                    case 3:
                        Console.ForegroundColor = ConsoleColor.Blue;
                        result = " Bring about ";
                        break;
                    case 4:
                        Console.ForegroundColor = ConsoleColor.DarkBlue;
                        result = " � ";
                        break;
                }
            }
            return result;
        }
        static void drawMap()
        {
            Console.WriteLine(" legend : Wheel of fortune:  Thecountry.      Mine: nurture     Suspended: bring about       Time and space tunnel: weng   ");
            // Draw the first 1 line 
            for (int i = 0; i < 30; i++)
                Console.Write(getMapString(i));
            Console.WriteLine();
            // On the left side of the first 1 column 
            for (int i = 30; i < 35; i++)
            {
                for (int j = 0; j < 29; j++)
                    Console.Write(" ");
                Console.Write(getMapString(i));
                Console.WriteLine();
            }
            // The first 2 line 
            for (int i = 64; i >= 35; i--)
                Console.Write(getMapString(i));
            Console.WriteLine();
            // The column on the right 
            for (int i = 65; i < 70; i++)
            {
                Console.Write(getMapString(i));
                Console.WriteLine();
            }
            // The first 3 line 
            for (int i = 70; i < 100; i++)
                Console.Write(getMapString(i));
            Console.ResetColor();
            Console.WriteLine();
        }
        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 1 An integer 
        {
            int i = ReadInt(int.MaxValue, int.MinValue);
            return i;
        }
        static int ReadInt(int min, int max)// produce min--max  Between the number of 
        {
            while (true)
            {
                try
                {
                    int number = Convert.ToInt32(Console.ReadLine());
                    if (number < min || number > max)
                    {
                        Console.WriteLine(" Can only enter {0}--{1} Please re-enter the number between ", min, max);
                        continue;
                    }
                    return number;
                }
                catch
                {
                    Console.WriteLine(" Only Numbers can be entered, please re-enter! ");
                }
            }
        }
        /// <summary>
        /// A or B The way you throw a sieve 
        /// </summary>
        /// <param name="playerNumber">A Rolling dice preach 0 To come over    B Rolling dice preach 1 To come over </param>
        static void Action(int playerNumber)
        {
            if (isStop[playerNumber] == false)
            {
                Console.WriteLine("{0} Press any key to start throwing the sieve ......", names[playerNumber]);
                ConsoleKeyInfo sec = Console.ReadKey(true);
                step = r.Next(1, 7);// produce 1 a 1 to 6 Random number between 
                if (sec.Key == ConsoleKey.Tab)
                {
                    ConsoleKeyInfo sec1 = Console.ReadKey(true);
                    if (sec1.Key == ConsoleKey.F1)
                    {
                        step = ReadInt(1, 100);
                    }
                }
                Console.WriteLine("{0} Throw out of the {1}", names[playerNumber], step);
                Console.WriteLine("{0} Press any key to start action ......", names[playerNumber]);
                Console.ReadKey(true);
                playerPos[playerNumber] += step; // Pay attention to, 1 Once the coordinate is changed, it is necessary to judge whether the coordinate value is >99||<0
                checkPos();// Check coordinates are out of bounds 
                if (playerPos[playerNumber] == playerPos[1 - playerNumber]) // The player A To the player B
                {
                    playerPos[1 - playerNumber] = 0;
                    msg = string.Format("{0} Stepped on {1},{1} Back to the origin ", names[playerNumber], names[1 - playerNumber]);
                }
                else
                {// You didn't step on it. Judge the player A Are there any other levels in the current location 
                    switch (map[playerPos[playerNumber]])
                    {
                        case 0:
                            // Ordinary, no effect 
                            msg = "";
                            break;
                        case 1:
                            // Go to the   Wheel of fortune level 
                            Console.Clear();
                            Console.WriteLine(" You go to the wheel of fortune, please choose luck? ");
                            Console.WriteLine("1 --- Swap places   2--- Bombing of the other party ");
                            int userSelect = ReadInt(1, 2);
                            if (userSelect == 1)
                            {// Switch places with each other 
                                int temp = playerPos[playerNumber];
                                playerPos[playerNumber] = playerPos[1 - playerNumber];
                                playerPos[1 - playerNumber] = temp;
                                msg = string.Format("{0} Chose to switch places with each other ", names[playerNumber]);
                            }
                            else
                            {// Bombing of the other party 
                                playerPos[1 - playerNumber] -= 6;
                                msg = string.Format("{0} bombed {1},{1} Back to the 6 " ", names[playerNumber], names[1 - playerNumber]);
                                checkPos();
                            }
                            break;
                        case 2:
                            // Step on a mine 
                            playerPos[playerNumber] -= 6;
                            checkPos();
                            msg = string.Format("{0} Step on a mine ,{0} retired 6 " ", names[playerNumber]);
                            break;
                        case 3:
                            // suspended 1 time 
                            isStop[playerNumber] = true;
                            msg = string.Format("{0} Go to the red light, stop next time 1 Time! ", names[playerNumber]);
                            break;
                        case 4:
                            // Step into the time tunnel 
                            playerPos[playerNumber] += 10;
                            msg = string.Format("{0} Into the time tunnel, cool dead, into 10 " ", names[playerNumber]);
                            break;
                    }
                }
            }
            else
            {
                isStop[playerNumber] = false;
            }
            if (playerPos[playerNumber] >= 99)
            {
                // Judge who wins and who loses 
                Console.Clear();
                if (playerPos[0] >= 99)
                {
                    Console.WriteLine("{0} The victory !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", names[0]);
                }
                else
                {
                    Console.WriteLine("{0} The victory !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", names[1]);
                }
            }
            Console.Clear();
            drawMap();
            if (msg != "")
            {
                Console.WriteLine(msg);
            }
            Console.WriteLine("{0} Throw out of the {1} Action accomplished! ", names[playerNumber], step);
            Console.WriteLine("************* The player A And the player B The location of the *********");
            Console.WriteLine("{0} Is: {1}", names[0], playerPos[0] + 1);
            Console.WriteLine("{0} Is: {1}", names[1], playerPos[1] + 1);

        }
    }
}


Related articles: