Maze.cpp: 진입점(main), 프레임 루프, 전역 Board/Player 선언Board.h / Board.cpp: 맵 생성 및 렌더링, Binary Tree 알고리즘 기반Player.h / Player.cpp: 플레이어 위치 초기화 및 업데이트Pos.h: 좌표 구조체, 연산자 오버로딩 지원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() | 콘솔 맵 출력 |
Board 클래스 (맵 생성 및 출력 핵심)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(): 콘솔 화면에 맵 그리기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;
}
}
노란색파란색초록색, 벽: 빨간색Player 클래스 (플레이어 위치 및 이동 처리)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;
};
void Player::Init(Board* board)
{
_pos = board->GetEnterPos(); // 시작 위치 설정
_board = board;
}
void Player::Update(uint64 deltaTick)
{
// 추후 키 입력 및 이동 처리 구현 예정
}
_dir에 따른 위치 이동 처리 예정Pos 구조체 & 방향 정의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방향 이동을 위한 열거형참고 도서: "Mazes for Programmers"