본문 바로가기
[C++] Data Structure & Algorithm/Maze

[Maze] Chapter 01. 맵 만들기

by song.ift 2023. 8. 6.

GitHub : https://github.com/developeSHG/Data_Structure-Algorithm/commits/01.Maze

 

GitHub - developeSHG/Data_Structure-Algorithm: Data Structure & Algorithm

Data Structure & Algorithm. Contribute to developeSHG/Data_Structure-Algorithm development by creating an account on GitHub.

github.com

 

결과물

 

 


 

 

Board.cpp

 - Render : 좌표 0으로 매번 초기화하고, 각 좌표에 해당하는 타일을 갖고와 타입에 맞는 컬러로 렌더

- GenerateMap : 맵을 만들어주는 곳. (테두리와 랜덤으로 각 타일의 오른쪽과 아래 중 1곳을 벽으로 만든다.)

cpp
닫기
void Board::Render() { ‌ConsoleHelper::SetCursorPosition(0, 0); ‌ConsoleHelper::ShowConsoleCursor(false); for (int32 y = 0; y < 25; y++) ‌{ ‌‌for (int32 x = 0; x < 25; x++) ‌‌{ ‌‌‌ConsoleColor color = GetTileColor(Pos{ y, x }); ‌‌‌ConsoleHelper::SetCursorColor(color); ‌‌‌cout << TILE; ‌‌} ‌‌cout << endl; ‌} } // Binary Tree 미로 생성 알고리즘 // - Mazes For Programmers void Board::GenerateMap() { 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; ‌‌‌} ‌‌} ‌} } TileType Board::GetTileType(Pos pos) { if (pos.x < 0 || pos.x >= _size) ‌‌return TileType::NONE; if (pos.y < 0 || pos.y >= _size) ‌‌return TileType::NONE; return _tile[pos.y][pos.x]; }

 

ConsoleHelper.cpp 

Window API랑 관련된 부분으로, 커서의 색, 좌표, 정보를 그려주기위해 매번 갱신.

cpp
닫기
void ConsoleHelper::SetCursorPosition(int32 x, int32 y) { ‌HANDLE output = ::GetStdHandle(STD_OUTPUT_HANDLE); ‌COORD pos = { static_cast<SHORT>(x), static_cast<SHORT>(y) }; ‌::SetConsoleCursorPosition(output, pos); } void ConsoleHelper::SetCursorColor(ConsoleColor color) { ‌HANDLE output = ::GetStdHandle(STD_OUTPUT_HANDLE); ‌::SetConsoleTextAttribute(output, static_cast<int16>(color)); } void ConsoleHelper::ShowConsoleCursor(bool flag) { ‌HANDLE output = ::GetStdHandle(STD_OUTPUT_HANDLE); ‌CONSOLE_CURSOR_INFO cursorInfo; ‌::GetConsoleCursorInfo(output, &cursorInfo); ‌cursorInfo.bVisible = flag; ‌::SetConsoleCursorInfo(output, &cursorInfo); }

 

댓글