Skip to content
Open
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
8 changes: 5 additions & 3 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ void Game::drawBoard() {
std::cout << " ";
} else {
std::cout << currentTile.tileColor(currentTile.value) << bold_on
<< std::setw(4) << currentTile.value << bold_off << def;
<< std::setw(4) << themes.themedOutput(currentTile.value)
<< bold_off << def;
}
}

Expand Down Expand Up @@ -544,7 +545,7 @@ void Game::statistics() {
endl();
std::cout << " Final score: " << bold_on << score << bold_off;
endl();
std::cout << " Largest Tile: " << bold_on << largestTile << bold_off;
std::cout << " Largest Tile: " << bold_on << themes.themedOutput(largestTile) << bold_off;
endl();
std::cout << " Number of moves: " << bold_on << moveCount << bold_off;
endl();
Expand Down Expand Up @@ -577,7 +578,7 @@ void Game::saveScore() {
s.score = score;
s.win = win;
s.moveCount = moveCount;
s.largestTile = largestTile;
s.largestTile = themes.themedOutput(largestTile);
s.duration = duration;
s.save();
}
Expand Down Expand Up @@ -688,6 +689,7 @@ void Game::startGame() {
}

setBoardSize();
themes.chooseTheme();

initialiseBoardArray();
addTile();
Expand Down
2 changes: 2 additions & 0 deletions src/headers/game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "global.hpp"
#include "scores.hpp"
#include "statistics.hpp"
#include "themes.hpp"
#include <chrono>
#include <cmath>
#include <fstream>
Expand Down Expand Up @@ -62,6 +63,7 @@ class Game {
RandInt randInt;
bool stateSaved;
bool noSave;
Themes themes;

enum ContinueStatus { STATUS_END_GAME = 0, STATUS_CONTINUE = 1 };
enum KeyInputErrorStatus { STATUS_INPUT_VALID = 0, STATUS_INPUT_ERROR = 1 };
Expand Down
4 changes: 2 additions & 2 deletions src/headers/scores.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ struct Score {
std::string name;
ull score;
bool win;
ull largestTile;
std::string largestTile;
long long moveCount;
double duration;
};
Expand All @@ -31,7 +31,7 @@ class Scoreboard {
public:
ull score = 0;
bool win;
ull largestTile;
std::string largestTile;
long long moveCount;
double duration;
void printScore();
Expand Down
29 changes: 29 additions & 0 deletions src/headers/themes.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef THEMES_H
#define THEMES_H

#include "color.hpp"
#include "global.hpp"
#include <cmath>
#include <limits>
#include <vector>

class Themes {
public:
Themes()
: fibonacci_numbers{1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 377, 610},
elements{"H", "He", "Li", "Be", "B", "C", "N",
"O", "F", "Ne", "Na", "Ca", "Sc"},
programming_languages{"C", "C++", "PHP", "C#", "Py", "Bash", "Java",
"SQL", "CSS", "HTML", "JS", "Go", "Rust"} {}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Ask the question: "If I had 400 themes, is the [Theme's class constructor] the only way a theme can be added to this game?"

  • It is possible that if it is not sustainable to do so, you may have to make some API to accept:
    [some "x theme" datatype] and add it to [a list of some "x theme" datatypes]

Copy link
Author

Choose a reason for hiding this comment

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

Changed: Theme defines just one theme datatype, and ThemeController handles the list of Theme objects

std::string themedOutput(ull value);
void chooseTheme();

private:
int themeCode = 0;
const int themeCount = 4;
std::vector<std::string> elements;
std::vector<std::string> programming_languages;
std::vector<int> fibonacci_numbers;
};

#endif
4 changes: 2 additions & 2 deletions src/scores.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void Scoreboard::printScore() {
ull playerScore = scoreList[i].score;
std::string won = scoreList[i].win ? "Yes" : "No";
long long moveCount = scoreList[i].moveCount;
ull largestTile = scoreList[i].largestTile;
std::string largestTile = scoreList[i].largestTile;
double duration = scoreList[i].duration;

if (i == size - 1) {
Expand Down Expand Up @@ -143,7 +143,7 @@ void Scoreboard::readFile() {
std::string playerName;
ull playerScore;
bool win;
ull largestTile;
std::string largestTile;
long long moveCount;
double duration;

Expand Down
43 changes: 43 additions & 0 deletions src/themes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include "themes.hpp"
#include "menu.hpp"

std::string Themes::themedOutput(ull value) {
switch (themeCode) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Ask the question: "If I had 400 themes, will this switch(...) statement be the only method/way of adding a [theme] to [a theme's registry]?".
Is there a more sustainable "generic" method of doing the action of:

  1. [adding an item (or many items) to a list]
  2. [checking if an item exists in the list], and then,
  3. [acting (possibly x command) when an item is found in the list]

Copy link
Author

Choose a reason for hiding this comment

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

case 1:
return std::to_string(value);
case 2:
return elements[log2(value) - 1];
case 3:
return std::to_string(fibonacci_numbers[log2(value) - 1]);
case 4:
return programming_languages[log2(value) - 1];
default:
return "ERR";
}
}

void Themes::chooseTheme() {
bool err = false;
while ((themeCode > themeCount || themeCode < 1)) {
clearScreen();
drawAscii();

if (err) {
std::cout << red
<< " Invalid input. Theme number should range from 1 to "
<< themeCount << "." << def;
endl(2);
}

std::cout << bold_on << " 1. Standard: 2, 4, 8, ..." << std::endl
<< " 2. Elements: H, He, Li, ..." << std::endl
<< " 3. Fibonacci Numbers: 1, 2, 3, ..." << std::endl
<< " 4. Programming Languages: C, C++, PHP, ..." << std::endl
<< " Enter theme number: " << bold_off;

std::cin >> themeCode;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::int32_t>::max(), '\n');
err = true;
}
}