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
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions data/scores.txt
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@


Mihir 1596 0 170 128 17.8156
8 changes: 4 additions & 4 deletions data/statistics.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
1596
2
0
0
0
0
0
323
43.6693
38 changes: 21 additions & 17 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ enum { CODE_HOTKEY_ACTION_SAVE = 'Z', CODE_HOTKEY_ALTERNATE_ACTION_SAVE = 'P' };

} // namespace Code
} // namespace Keypress

enum { CELL_BORDER_PADDING = 1, MINIMUM_CELL_WIDTH = 4 };

} // namespace

Color::Modifier Tile::tileColor(ull value) {
Expand Down Expand Up @@ -166,21 +169,10 @@ 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();

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++){
border.append("─");
}
for (int y = 0; y < gameBoardPlaySize; y++) {

std::cout << " ";
Expand All @@ -191,7 +183,7 @@ void Game::drawBoard() {
std::cout << "├";
}
for (int i = 0; i < gameBoardPlaySize; i++) {
std::cout << border;
std::cout << horizontalBorder;
if (i < gameBoardPlaySize - 1) {
if (y == 0) {
std::cout << "┬";
Expand All @@ -214,10 +206,10 @@ void Game::drawBoard() {

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

Expand All @@ -228,7 +220,7 @@ void Game::drawBoard() {
std::cout << " └";

for (int i = 0; i < gameBoardPlaySize; i++) {
std::cout << border;
std::cout << horizontalBorder;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Everytime you want to draw the [horizontalBorder string], you would just call the function.

if (i < gameBoardPlaySize - 1) {
std::cout << "┴";
} else {
Expand Down Expand Up @@ -508,8 +500,20 @@ void Game::move(int y, int x, int k, int l) {
score += targetTile.value;
targetTile.blocked = true;

largestTile =
largestTile < targetTile.value ? targetTile.value : largestTile;
if (largestTile < targetTile.value) {
largestTile = targetTile.value;

/**
* Default border length is 4. When largestTileLength exceeds 4
* then increase the border and cell width by 1
*/
int largestTileLength = std::to_string(largestTile).length();
if (cellWidth > MINIMUM_CELL_WIDTH) {
cellWidth = largestTileLength;
horizontalBorder.append("─");
Copy link
Collaborator

Choose a reason for hiding this comment

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

With an anonymous-namespace free function, you can take in a parameter [length] and the string returned would be a string of the [horizontalBorder character].

}
}

if (!win) {
if (targetTile.value == GAME_TILE_WINNING_SCORE) {
win = true;
Expand Down
6 changes: 4 additions & 2 deletions src/headers/game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ class Game {
ull score;
ull bestScore;
ull largestTile;
int cellWidth;
std::string horizontalBorder;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Better practice would be:

  • A free anonymous-namespace function that outputs a horizontalBorder string.

long long moveCount;
double duration;
ull gameBoardPlaySize;
Expand Down Expand Up @@ -89,8 +91,8 @@ class Game {
public:
Game()
: win(false), moved(true), boardFull(false), rexit(false), score(0),
bestScore(0), moveCount(-2), largestTile(2), stateSaved(false),
noSave(false) {}
bestScore(0), moveCount(-2), largestTile(2), cellWidth(4),
horizontalBorder("──────"), stateSaved(false), noSave(false) {}
Copy link
Collaborator

Choose a reason for hiding this comment

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

This means one would not need the [horizontalBorder] member-variable.

void startGame();
void continueGame();
};
Expand Down