C# 숫자 달리기

728x90

기본 방법

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

namespace CheckPoint01
{
    class Program
    {
        static void Main(string[] args)
        {
            Random rnd = new Random();
            const string LINE = "--------------------------------------------";
            const int END_LINE = 42;
            const int DELAY_TIME = 200;

            int runA = 0;
            int runB = 0;
            int runC = 0;
            int runD = 0;

            while (true)
            {
                Thread.Sleep(DELAY_TIME); //딜레이 (200 = 1초)
                Console.Clear();  //화면 지우기

                ++runA;
                ++runB;
                ++runC;
                ++runD;

                int rndNum = rnd.Next(0, 4);  // 0~3
                int runRndNum = rnd.Next(0, 2); //0~1

                switch(rndNum)
                {
                    case 0:
                        runA += runRndNum; //++runA;로만 해도 가능
                        break;
                    case 1:
                        runB += runRndNum;
                        break;
                    case 2:
                        runC += runRndNum;
                        break;
                    case 3:
                        runD += runRndNum;
                        break;
                }
                Console.WriteLine(LINE);
                
                for(int i = 0; i < runA; i++)
                    Console.Write(" ");
                Console.Write("1");

                for (int i = (END_LINE - 3) - runA; i >= 0; i--)
                    Console.Write(" ");
                Console.WriteLine("|");

                for (int i = 0; i < runB; i++)
                    Console.Write(" ");
                Console.Write("2");

                for (int i = (END_LINE - 3) - runB; i >= 0; i--)
                    Console.Write(" ");
                Console.WriteLine("|");

                for (int i = 0; i < runC; i++)
                    Console.Write(" ");
                Console.Write("3");

                for (int i = (END_LINE - 3) - runC; i >= 0; i--)
                    Console.Write(" ");
                Console.WriteLine("|");

                for (int i = 0; i < runD; i++)
                    Console.Write(" ");
                Console.Write("4");

                for (int i = (END_LINE - 3) - runD; i >= 0; i--)
                    Console.Write(" ");
                Console.WriteLine("|");

                Console.WriteLine(LINE);

                if(runA >= END_LINE || runB >= END_LINE || runC >= END_LINE || runD >= END_LINE)
                {
                    int runNum = 0;
                    string strResult = "결과 :  !!{0} 선수 우승!!";

                    if (runA >= END_LINE)
                        runNum = 1;
                    else if (runB >= END_LINE)
                        runNum = 2;
                    else if (runC >= END_LINE)
                        runNum = 3;
                    else
                        runNum = 4;

                    Console.WriteLine(strResult, runNum);

                    Console.Write("다시 하시려면 0번 입력 : ");
                    if (0 == int.Parse(Console.ReadLine()))  // "0" == Console.ReadLine()
                    {
                        runA = 0;
                        runB = 0;
                        runC = 0;
                        runD = 0;                           
                    }
                }
            }
            Console.ReadKey();
        }
    }
}

메소드 활용 방법

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

namespace CheckPoint002
{
    class Program
    {
        const string LINE = "--------------------------------------------";
        const int END_LINE = 42;
        const int DELAY_TIME = 200;

        static int runA = 0;
        static int runB = 0;
        static int runC = 0;
        static int runD = 0;

        static void ClearScreen()
        {
            Thread.Sleep(DELAY_TIME); //딜레이 (200 = 1초)
            Console.Clear();  //화면 지우기
        }
        static void Process(Random rnd)
        {
            ++runA;
            ++runB;
            ++runC;
            ++runD;

            int rndNum = rnd.Next(0, 4);  // 0~3
            int runRndNum = rnd.Next(0, 2); //0~1

            switch (rndNum)
            {
                case 0:
                    runA += runRndNum; //++runA;로만 해도 가능
                    break;
                case 1:
                    runB += runRndNum;
                    break;
                case 2:
                    runC += runRndNum;
                    break;
                case 3:
                    runD += runRndNum;
                    break;
            }
        }
        static void UpdateScreen()
        {
            Console.WriteLine(LINE);

            for (int i = 0; i < runA; i++)
                Console.Write(" ");
            Console.Write("1");

            for (int i = (END_LINE - 3) - runA; i >= 0; i--)
                Console.Write(" ");
            Console.WriteLine("|");

            for (int i = 0; i < runB; i++)
                Console.Write(" ");
            Console.Write("2");

            for (int i = (END_LINE - 3) - runB; i >= 0; i--)
                Console.Write(" ");
            Console.WriteLine("|");

            for (int i = 0; i < runC; i++)
                Console.Write(" ");
            Console.Write("3");

            for (int i = (END_LINE - 3) - runC; i >= 0; i--)
                Console.Write(" ");
            Console.WriteLine("|");

            for (int i = 0; i < runD; i++)
                Console.Write(" ");
            Console.Write("4");

            for (int i = (END_LINE - 3) - runD; i >= 0; i--)
                Console.Write(" ");
            Console.WriteLine("|");

            Console.WriteLine(LINE);
        }
        static bool CheckResult()
        {
            if (runA >= END_LINE || runB >= END_LINE || runC >= END_LINE || runD >= END_LINE)
            {
                int runNum = 0;
                string strResult = "결과 :  !!{0} 선수 우승!!";

                if (runA >= END_LINE)
                    runNum = 1;
                else if (runB >= END_LINE)
                    runNum = 2;
                else if (runC >= END_LINE)
                    runNum = 3;
                else
                    runNum = 4;

                Console.WriteLine(strResult, runNum);

                Console.Write("다시 하시려면 0번 입력 : ");
                if (0 == int.Parse(Console.ReadLine()))  // "0" == Console.ReadLine()
                {
                    runA = 0;
                    runB = 0;
                    runC = 0;
                    runD = 0;

                    return true;  //break랑 같은 개념, bool타입
                }
                else
                {
                    return false;
                }
            }
            return true;
        }
        static void Main(string[] args)
        {
            Random rnd = new Random();

            while (true)
            {
                ClearScreen();
                
                Process(rnd);

                UpdateScreen();

                if (CheckResult() == false)
                    break;
            }
            Console.ReadKey();
        }
    }
}

배열 사용 방법

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

namespace _CheckPoint03
{
    class Program
    {
        const int MAP_X = 7;
        const int MAP_Y = 22;
        const int DELAY_TIME = 300;
        static void UpdateView(char[] _tile, int[,] _map) //call by reference
        {
            for (int i = 0; i < MAP_X; i++)
            {
                for (int j = 0; j < MAP_Y; j++)
                {
                    int tileIndex = _map[i, j];
                    Console.Write(_tile[tileIndex]);

                    if (j == MAP_Y - 1)
                        Console.WriteLine();
                }
            }
        }
        static void ClearView()
        {
            Thread.Sleep(DELAY_TIME);
            Console.Clear();
        }
        static void UpdateGO(int[] _arrIndexX, int[,] _map)
        {
            for (int i = 0; i < _arrIndexX.Length; i++) //22 전까지 돌림
            {
                int indexMaxX = i + 1;
                int indexY = _arrIndexX[i]; //숫자가 앞으로 이동하면서 이전 인덱스와 Swap(0 <-> 1)

                int temp = _map[indexMaxX, indexY];  //temp = 3;
                _map[indexMaxX, indexY + 1] = temp;
                _map[indexMaxX, indexY] = 0;

                _arrIndexX[i]++;
            }
        }
        static bool UpdateRandomGo(int[] _arrIndexX, int[,] _map, Random _rnd)
        {
            bool isFinish = false;

            for (int i = 0; i < _arrIndexX.Length; i++)
            {
                if (_arrIndexX[i] >= 19)
                {
                    isFinish = true;
                    break;
                }
            }
            int rndIndex = _rnd.Next(0, 5);  // 0 ~ 4

            int indexY = _arrIndexX[rndIndex];

            int temp = _map[rndIndex + 1, indexY];  //rndIndex가 0일 경우, map[1, ????], rndIndex가 1일 경우, map[2, ????]
            _map[rndIndex + 1, indexY + 1] = temp;
            _map[rndIndex + 1, indexY] = 0;

            _arrIndexX[rndIndex]++;

            return isFinish;
        }
        static void Main(string[] args)
        {
            Random rnd = new Random();
            //              0    1    2    3    4    5    6    7
            char[] tile = {' ', '-', '|', '1', '2', '3', '4', '5'};

            int[,] map = new int[MAP_X, MAP_Y]
            {
              // 0  1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  
                {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1}, //0
                {3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  2,  0}, //1
                {4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  2,  0}, //2
                {5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  2,  0}, //3
                {6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  2,  0}, //4
                {7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  2,  0}, //5
                {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1}, //6
            };
            int[] arrIndexX = {0, 0, 0, 0, 0};
            bool isFinish = false;

            while(true)
            {
                UpdateGO(arrIndexX, map);
                isFinish = UpdateRandomGo(arrIndexX, map,rnd);
                UpdateView(tile, map);

                if (isFinish)
                {
                    Console.WriteLine();

                    for (int i = 0; i < arrIndexX.Length; i++)
                    {
                        if (arrIndexX[i] >= 19)
                        {
                            Console.WriteLine("달리기 결과 : 1등 : {0}", (i + 1));
                            break;
                        }
                    }

                    Console.Write("\n다시 시작하려면 0을 입력");
                    string inputStr = Console.ReadLine();

                    if (inputStr == "0")
                    {
                        map[1, arrIndexX[0]] = 0;
                        map[2, arrIndexX[1]] = 0;
                        map[3, arrIndexX[2]] = 0;
                        map[4, arrIndexX[3]] = 0;
                        map[5, arrIndexX[4]] = 0;

                        arrIndexX[0] = 0;
                        arrIndexX[1] = 0;
                        arrIndexX[2] = 0;
                        arrIndexX[3] = 0;
                        arrIndexX[4] = 0;

                        map[1, 20] = 2;
                        map[2, 20] = 2;
                        map[3, 20] = 2;
                        map[4, 20] = 2;
                        map[5, 20] = 2;

                        map[1, 0] = 3;
                        map[2, 0] = 4;
                        map[3, 0] = 5;
                        map[4, 0] = 6;
                        map[5, 0] = 7;
                    }
                    else
                    {
                        Console.Write("\n나가기");
                        break;
                    }                        
                }

                ClearView();
            }
            Console.ReadKey();
        }
    }
}

728x90