Chapter 3-1. 맵 만들기

Date:     Updated:

카테고리:

태그:

인프런에 있는 Rookiss님의 [C#과 유니티로 만드는 MMORPG 게임 개발 시리즈] Part2: 자료구조와 알고리즘 강의를 듣고 정리한 필기입니다. 😀
🌜 강의 들으러 가기 Click

Chapter 3. 미로 준비

🚖 미로 준비

image

  • 미로는 한 칸 한 칸마다 채워져 있는 원 하나로 그린다.
  • 미로의 크기는 size X size 로 지정.
  • 미로 배열은 이차원 배열로 관리하고 원소를 타일이라고 하자.
  • 가장 자리의 타일들은 벽이며 빨간색.
  • 가장 자리가 아닌 타일들은 갈 수 있는 곳이며 초록색.

📜Board.cs

using System;
using System.Collections.Generic;
using System.Text;

namespace Algorithm
{
    class Board
    {
        const char CIRCLE = '\u25cf';  // 채워진 원 그리는 문자 코드

        public TileType[,] _tile;  // 맵 배열. 2차원 배열
        public int _size;  // 맵 크기. _size X _size 로 정해짐

        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);  // 동그라미 1개 그림
                }
                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;  
            }
        }
    }
}

image

  • 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);  // 동그라미 1개 그림
    }
    Console.WriteLine();  // 개행
}

Console.ForegroundColor = prevColor;  // 보존했던 색으로 다시 되돌림


📜Program.cs

using System;

namespace Algorithm
{
    class Program
    {
        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 프레임 관리
                int currentTick = System.Environment.TickCount;  // 밀리 세컨즈로 나타낸 현재시간.  1 밀리 세컨즈 = 1/1000 초
                int elapsedTick = currentTick - lastTick; // 경과 시간(현재시간 - 마지막으로 잰 시간)

                if (elapsedTick < 1000 / 30) // 경과한 시간이 1/30초보다 작다면 아래 과정을 처리 하지 않고 돌아감. 즉 30프레임 기준, 한 프레임 당 한번 처리되도록!
                    continue;
                lastTick = currentTick;  // 1/30초, 즉 한 프레임이 경과 되었다면 마지막 측정 시간을 현재 시간으로 업뎃
                #endregion

                // 1.입력
                // 2.로직
                // 3.렌더링
                Console.SetCursorPosition(0, 0);
                board.Render();  // 미로 렌더링
            }
        }
    }
}



🌜 개인 공부 기록용 블로그입니다. 오류나 틀린 부분이 있을 경우 
언제든지 댓글 혹은 메일로 지적해주시면 감사하겠습니다! 😄

맨 위로 이동하기

Algorithm Lesson 2 카테고리 내 다른 글 보러가기

댓글 남기기