C\#기사 비행 기의 원본 코드(공유)

코드 는 다음 과 같다.

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


namespace
{
    class Program
    {
        //
        // 0 1     1 2 ... n n+1
        //    1:   ◎
        //           2: ☆
        //           3: ▲
        //           4: d
        //           0:   □
        static int[] map = new int[100];

        static string[] names = new string[2]; //names[0] A     name[1] B

        static int[] playerPos = { 0, 0 };//playPos[0] A ,playPos[1] B

        static int step = 0; //

        static string input = ""; //

        static string msg = ""; // ,

        static bool[] isStop = { false, false };//isStop[0] A , true, false

        static Random r = new Random();//r

        static void Main( string[] args)
        {
           


            ShowUI(); //
            InitialName();
            Console.Clear();
            ShowUI();
            Console.WriteLine(" ......");
            Console.WriteLine("{0} A ", names[0]);
            Console.WriteLine("{0} B ", names[1]);
            Console.WriteLine(" AB , <> ");
            InitialMap();//
            drawMap();//
            Console.WriteLine(" ......");

            // A B   A B >=99 ,
            while (playerPos[0] < 99 && playerPos[1] < 99)
            {
                Action(0);//A
                Action(1);//B  
            }
            Console.ReadKey();
        }

        /// <summary>
        ///
        /// </summary>
        static void ShowUI()
        {
            Console.WriteLine("*******************************************************");
            Console.WriteLine("*                                                     *");
            Console.WriteLine("*                                           *");
            Console.WriteLine("*                                                     *");
            Console.WriteLine("*******************************************************");
        }

        static void InitialName()
        {
            Console.WriteLine(" A ");
            names[0] = Console.ReadLine();
            // , ,
            while (names[0] == "")
            {
                Console.WriteLine(" A , !");
                names[0] = Console.ReadLine();
            }
            Console.WriteLine(" B ");
            names[1] = Console.ReadLine();
            // , ,
            while (names[1] == "" || names[1] == names[0])
            {
                if (names[1] == "")
                {
                    Console.WriteLine(" B , !");
                    names[1] = Console.ReadLine();
                }
                else
                {
                    Console.WriteLine(" A {0} , ", names[0]);
                    names[1] = Console.ReadLine();
                }

            }
        }

        static void InitialMap()
        {
            //
            int[] luckyTurn = { 6, 23, 40, 55, 69, 83 };// 1
            int[] landMine = { 5, 13, 17, 33, 38, 50, 64, 80, 94 };// 2
            int[] pause = { 9, 27, 60, 93 };// 3
            int[] timeTunnel = { 20, 25, 45, 63, 72, 88, 90 };//   4

            for (int i = 0; i < 100; i++) // map
                map[i] = 0;

            // map
            for (int i = 0; i < luckyTurn.Length; i++)
                map[luckyTurn[i]] = 1;
            // map
            for (int i = 0; i < landMine.Length; i++)
                map[landMine[i]] = 2;
            // map
            for (int i = 0; i < pause.Length; i++)
                map[pause[i]] = 3;
            // map
            for (int i = 0; i < timeTunnel.Length; i++)
                map[timeTunnel[i]] = 4;
        }

        /// <summary>
        ///  pos
        /// </summary>
        /// <param name="pos"> </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 = "◎";
                        break;
                    case 2:
                        Console.ForegroundColor = ConsoleColor.Green;
                        result = "☆";
                        break;
                    case 3:
                        Console.ForegroundColor = ConsoleColor.Blue;
                        result = "▲";
                        break;
                    case 4:
                        Console.ForegroundColor = ConsoleColor.DarkBlue;
                        result = "d";
                        break;
                }
            }
            return result;
        }

        static void drawMap()
        {
            Console.WriteLine(" : :◎   :☆   :▲     :d  ");
            //
            for (int i = 0; i < 30; i++)
                Console.Write(getMapString(i));
            Console.WriteLine();

            //
            for (int i = 30; i < 35; i++)
            {
                for (int j = 0; j < 29; j++)
                    Console.Write(" ");
                Console.Write(getMapString(i));
                Console.WriteLine();
            }

            //
            for (int i = 64; i >= 35; i--)
                Console.Write(getMapString(i));
            Console.WriteLine();

            //
            for (int i = 65; i < 70; i++)
            {
                Console.Write(getMapString(i));
                Console.WriteLine();
            }

            //
            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()//
        {
            int i = ReadInt(int.MaxValue, int.MinValue);
            return i;
        }

        static int ReadInt(int min, int max)// min--max
        {
            while (true)
            {
                try
                {
                    int number = Convert.ToInt32(Console.ReadLine());
                    if (number < min || number > max)
                    {
                        Console.WriteLine(" {0}--{1} , ", min, max);
                        continue;
                    }
                    return number;
                }
                catch
                {
                    Console.WriteLine(" , !");
                }

            }

        }

        /// <summary>
        /// A B
        /// </summary>
        /// <param name="playerNumber">A 0    B 1 </param>
        static void Action(int playerNumber)
        {
            if (isStop[playerNumber] == false)
            {
                Console.WriteLine("{0} ......", names[playerNumber]);
                ConsoleKeyInfo sec = Console.ReadKey(true);
                step = r.Next(1, 7);// 1 6
                if (sec.Key == ConsoleKey.Tab)
                {
                    ConsoleKeyInfo sec1 = Console.ReadKey(true);
                    if (sec1.Key == ConsoleKey.F1)
                    {
                        step = ReadInt(1, 100);
                    }
                }

                Console.WriteLine("{0} {1}", names[playerNumber], step);
                Console.WriteLine("{0} ......", names[playerNumber]);
                Console.ReadKey(true);
                playerPos[playerNumber] += step; // , , >99||<0
                checkPos();//

                if (playerPos[playerNumber] == playerPos[1 - playerNumber]) // A B
                {
                    playerPos[1 - playerNumber] = 0;
                    msg = string.Format("{0} {1},{1} ", names[playerNumber], names[1 - playerNumber]);
                }
                else
                {// , A
                    switch (map[playerPos[playerNumber]])
                    {
                        case 0:
                            // ,
                            msg = "";
                            break;
                        case 1:
                            //
                            Console.Clear();
                            Console.WriteLine(" , ?");
                            Console.WriteLine("1 ---   2--- ");
                            int userSelect = ReadInt(1, 2);
                            if (userSelect == 1)
                            {//
                                int temp = playerPos[playerNumber];
                                playerPos[playerNumber] = playerPos[1 - playerNumber];
                                playerPos[1 - playerNumber] = temp;
                                msg = string.Format("{0} ", names[playerNumber]);
                            }
                            else
                            {//
                                playerPos[1 - playerNumber] -= 6;
                                msg = string.Format("{0} {1},{1} 6 ", names[playerNumber], names[1 - playerNumber]);
                                checkPos();
                            }
                            break;
                        case 2:
                            //
                            playerPos[playerNumber] -= 6;
                            checkPos();
                            msg = string.Format("{0} ,{0} 6 ", names[playerNumber]);
                            break;
                        case 3:
                            //
                            isStop[playerNumber] = true;
                            msg = string.Format("{0} , ", names[playerNumber]);
                            break;
                        case 4:
                            //
                            playerPos[playerNumber] += 10;
                            msg = string.Format("{0} , , 10 ", names[playerNumber]);
                            break;
                    }

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

            if (playerPos[playerNumber] >= 99)
            {
                // ,
                Console.Clear();
                if (playerPos[0] >= 99)
                {
                    Console.WriteLine("{0} !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", names[0]);
                }
                else
                {
                    Console.WriteLine("{0} !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", names[1]);
                }
            }

            Console.Clear();
            drawMap();
            if (msg != "")
            {
                Console.WriteLine(msg);
            }
            Console.WriteLine("{0} {1}, !", names[playerNumber], step);
            Console.WriteLine("************* A B *********");
            Console.WriteLine("{0} :{1}", names[0], playerPos[0] + 1);
            Console.WriteLine("{0} :{1}", names[1], playerPos[1] + 1);

        }
    }
}

좋은 웹페이지 즐겨찾기