C#

[길찾기 알고리즘] 1. 미로 준비 (1) 기본 맵 생성

binary는 호남선 2024. 11. 13. 22:28

- 콘솔에 사방이 막힌 맵 출력

Program.cs

static void Main(string[] args)
{
    Board board = new Board();
    board.Initialize(25);

    Console.CursorVisible = false;
    
    const int WAIT_TICK = 1000 / 30;
    int lastTick = 0;


    while (true)
    {
        #region 프레임 관리
        // 만약 경과한 시간이 1/30초보다 작으면
        int currentTick = System.Environment.TickCount;
        if (currentTick - lastTick < WAIT_TICK)
            continue;
        lastTick = currentTick;
        #endregion

        Console.SetCursorPosition(0, 0);
        board.Render();
    }
}

Board.cs

class Board
{
    public TileType[,] _tile;   // 2차원 배열
    public int _size;
    const char CIRCLE = '\u25cf';

    public enum TileType
    {
        Empty,
        Wall,
    }

    public void Initialize(int size)
    {
        _tile = new TileType[size, size];
        _size = size;

        for (int y = 0; y < _size; y++)
        {
            for (int x = 0; x < _size; x++)
            {
                if (x == 0 || x == _size - 1 || y == 0 || y == _size - 1)
                    _tile[y, x] = TileType.Wall;
                else
                    _tile[y, x] = TileType.Empty;
            }
        }
    }
    public void Render()
    {
        ConsoleColor prevColor = Console.ForegroundColor;

        for (int y = 0; y < _size; y++)
        {
            for (int x = 0; x < _size; x++)
            {
                Console.ForegroundColor = GetTileColor(_tile[y, x]);
                Console.Write(CIRCLE);
            }
            Console.WriteLine();
        }

        Console.ForegroundColor = prevColor;
    }

    ConsoleColor GetTileColor(TileType type)
    {
        switch (type)
        {
            case TileType.Empty:
                return ConsoleColor.Green;
            case TileType.Wall:
                return ConsoleColor.Red;
            default:
                return ConsoleColor.Green;
        }
    }
}

콘솔 출력 결과 (예시)

●●●●●●●●●●●●●●●●●●●●●●●●●

●●●●●●●●●●●●●●●●●●●●●●● 

●●●●●●●●●●●●●●●●●●●●●●● 

●●●●●●●●●●●●●●●●●●●●●●● 

●●●●●●●●●●●●●●●●●●●●●●● 

●●●●●●●●●●●●●●●●●●●●●●● 

●●●●●●●●●●●●●●●●●●●●●●● 

●●●●●●●●●●●●●●●●●●●●●●● 

●●●●●●●●●●●●●●●●●●●●●●● 

●●●●●●●●●●●●●●●●●●●●●●● 

●●●●●●●●●●●●●●●●●●●●●●●●●

 

내용 출처 : Inflearn Rookiss님 강의 [C#과 유니티로 만드는 MMORPG 게임 개발 시리즈] Part2: 자료구조와 알고리즘

https://www.inflearn.com/course/%EC%9C%A0%EB%8B%88%ED%8B%B0-mmorpg-%EA%B0%9C%EB%B0%9C-part2