Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change tile width dynamically to allow longer strings #56

Closed
wants to merge 8 commits into from
21 changes: 16 additions & 5 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,21 @@ void Game::collectFreeTiles(std::vector<std::vector<int>> &freeTiles) {

void Game::drawBoard() {

// The number of spaces between the border of a cell and a number
enum { CELL_BORDER = 1 };

clearScreen();
drawAscii();
drawScoreBoard(std::cout);

int numlen = std::to_string(largestTile).length();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


std::string border;
// Multiple cell border enum by 2 so it applies the same padding
// to both sides
for(int i = 0; i < numlen+( CELL_BORDER * 2); i++){
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 & for comment too.

border.append("─");
}
for (int y = 0; y < gameBoardPlaySize; y++) {

std::cout << " ";
Expand All @@ -180,7 +191,7 @@ void Game::drawBoard() {
std::cout << "├";
}
for (int i = 0; i < gameBoardPlaySize; i++) {
std::cout << "──────";
std::cout << border;
if (i < gameBoardPlaySize - 1) {
if (y == 0) {
std::cout << "┬";
Expand All @@ -197,17 +208,16 @@ void Game::drawBoard() {
}
endl();
std::cout << " ";

for (int x = 0; x < gameBoardPlaySize; x++) {

Tile currentTile = board[y][x];

std::cout << " │ ";
if (!currentTile.value) {
std::cout << " ";
std::cout << std::setw(numlen) << " ";
} else {
std::cout << currentTile.tileColor(currentTile.value) << bold_on
<< std::setw(4) << currentTile.value << bold_off << def;
<< std::setw(numlen) << currentTile.value << bold_off << def;
}
}

Expand All @@ -216,8 +226,9 @@ void Game::drawBoard() {
}

std::cout << " └";

for (int i = 0; i < gameBoardPlaySize; i++) {
std::cout << "──────";
std::cout << border;
if (i < gameBoardPlaySize - 1) {
std::cout << "┴";
} else {
Expand Down