🎮 전체 구조 개요

  • Maze.cpp: 진입점(main), 프레임 루프, 전역 Board/Player 선언
  • Board.h / Board.cpp: 맵 생성 및 렌더링, Binary Tree 알고리즘 기반
  • Player.h / Player.cpp: 플레이어 위치 초기화 및 업데이트
  • Pos.h: 좌표 구조체, 연산자 오버로딩 지원

1️⃣ main 함수 (게임 루프 구성)

int main()
{
    ::srand(static_cast<unsigned>(time(nullptr))); // 랜덤 시드 설정

    board.Init(25, &player);  // 보드 초기화
    player.Init(&board);      // 플레이어 초기화

    uint64 lastTick = 0;

    while (true)
    {
        const uint64 currentTick = ::GetTickCount64();
        const uint64 deltaTick = currentTick - lastTick;
        lastTick = currentTick;

        // 입력 처리 (추후 구현 가능)
        player.Update(deltaTick);

        // 맵 출력
        board.Render();
    }
}

💡 설명 요약

코드설명
srand(time(nullptr))매 실행 시 다른 난수 생성
Init(25, &player)25x25 크기 보드 생성 및 플레이어 포인터 전달
Update(deltaTick)프레임 기반 상태 업데이트
Render()콘솔 맵 출력

2️⃣ Board 클래스 (맵 생성 및 출력 핵심)

📁 Board.h

enum class TileType { NONE = 0, EMPTY, WALL };

class Board
{
public:
    void Init(int32 size, Player* player);
    void Render();
    void GenerateMap();

    TileType GetTileType(Pos pos);
    ConsoleColor GetTileColor(Pos pos);

    Pos GetEnterPos() { return Pos{1, 1}; }
    Pos GetExitPos() { return Pos{_size - 2, _size - 2}; }

private:
    TileType _tile[BOARD_MAX_SIZE][BOARD_MAX_SIZE] = {};
    int32 _size = 0;
    Player* _player = nullptr;
};

🛠️ 핵심 기능 설명

  • GenerateMap(): Binary Tree 알고리즘으로 미로 생성
  • GetTileColor(pos): 플레이어, 출구, 일반 타일에 따른 색상 반환
  • Render(): 콘솔 화면에 맵 그리기

📄 Board.cpp

🔨 맵 생성 알고리즘 (Binary Tree 방식)

for (int32 y = 0; y < _size; y++)
{
    for (int32 x = 0; x < _size; x++)
    {
        if (x % 2 == 0 || y % 2 == 0)
            _tile[y][x] = TileType::WALL;  // 격자 벽
        else
            _tile[y][x] = TileType::EMPTY; // 경로 후보
    }
}

for (int32 y = 0; y < _size; y++)
{
    for (int32 x = 0; x < _size; x++)
    {
        if (x % 2 == 0 || y % 2 == 0)
            continue;

        if (y == _size - 2 && x == _size - 2)
            continue;

        if (y == _size - 2)
        {
            _tile[y][x + 1] = TileType::EMPTY;
            continue;
        }

        if (x == _size - 2)
        {
            _tile[y + 1][x] = TileType::EMPTY;
            continue;
        }

        const int32 randValue = rand() % 2;
        if (randValue == 0)
            _tile[y][x + 1] = TileType::EMPTY;
        else
            _tile[y + 1][x] = TileType::EMPTY;
    }
}
  • 짝수 위치에 벽 생성 (격자 형식)
  • 홀수 위치에서 우측 또는 아래로 통로 랜덤 생성

🎨 타일 색상 반환

ConsoleColor Board::GetTileColor(Pos pos)
{
    if (_player && _player->GetPos() == pos)
        return ConsoleColor::YELLOW;

    if (GetExitPos() == pos)
        return ConsoleColor::BLUE;

    TileType tileType = GetTileType(pos);

    switch (tileType)
    {
        case TileType::EMPTY: return ConsoleColor::GREEN;
        case TileType::WALL: return ConsoleColor::RED;
        default: return ConsoleColor::WHITE;
    }
}
  • 플레이어 위치: 노란색
  • 출구: 파란색
  • 경로: 초록색, 벽: 빨간색

3️⃣ Player 클래스 (플레이어 위치 및 이동 처리)

📁 Player.h

class Player
{
public:
    void Init(Board* board);
    void Update(uint64 deltaTick);

    void SetPos(Pos pos) { _pos = pos; }
    Pos GetPos() { return _pos; }

private:
    Pos _pos = {};
    int32 _dir = DIR_UP;
    Board* _board = nullptr;
};

📄 Player.cpp

void Player::Init(Board* board)
{
    _pos = board->GetEnterPos();  // 시작 위치 설정
    _board = board;
}

void Player::Update(uint64 deltaTick)
{
    // 추후 키 입력 및 이동 처리 구현 예정
}
  • 현재는 이동 로직 미구현
  • 향후 키보드 입력에 따라 _dir에 따른 위치 이동 처리 예정

4️⃣ Pos 구조체 & 방향 정의

📁 Pos.h

struct Pos
{
    int32 y = 0;
    int32 x = 0;

    bool operator==(Pos& other) { return y == other.y && x == other.x; }
    bool operator!=(Pos& other) { return !(*this == other); }

    Pos operator+(Pos& other)
    {
        return Pos{ y + other.y, x + other.x };
    }

    Pos& operator+=(Pos& other)
    {
        y += other.y;
        x += other.x;
        return *this;
    }
};

enum Dir
{
    DIR_UP = 0, DIR_LEFT = 1, DIR_DOWN = 2, DIR_RIGHT = 3, DIR_COUNT = 4
};
  • 연산자 오버로딩을 통해 좌표 연산을 간결하게 구현
  • Dir은 4방향 이동을 위한 열거형

🧩 맵 생성 알고리즘: Binary Tree 소개

참고 도서: "Mazes for Programmers"

  • Binary Tree 알고리즘은 각 셀에서 우측 또는 하단으로만 길을 확장해가는 간단하면서도 빠른 미로 생성 방법입니다.
  • 각 홀수 좌표에서 랜덤으로 한 방향만 열어 길을 형성합니다.
  • 장점: 구현이 간단하고 빠름
  • 단점: 복잡한 구조의 미로 생성에는 적합하지 않음 (해결책은 DFS, BFS 등 추후 적용 가능)

profile
李家네_공부방

0개의 댓글