diff options
Added GameCase descriptions and a printing function for them
4 files changed, 66 insertions, 8 deletions
diff --git a/Year 1/Object-oriented Programming/07-Practice-05.04.2022/Game.cpp b/Year 1/Object-oriented Programming/07-Practice-05.04.2022/Game.cpp index 14274f9..bbc91b8 100644 --- a/Year 1/Object-oriented Programming/07-Practice-05.04.2022/Game.cpp +++ b/Year 1/Object-oriented Programming/07-Practice-05.04.2022/Game.cpp @@ -28,6 +28,10 @@ void Game::free() { } TurnResult Game::turn(double& bet) { + std::cout << "Available commands: " << std::endl; + for (int i = 0; i < this->caseCount; i++) + cases[i].print(); + std::cout << "Enter command: "; char buffer; std::cin >> buffer; @@ -61,6 +65,7 @@ Game::Game(const GameCase* cases, unsigned casesCount) { this->cases = new GameCase[casesCount]; for (int i = 0; i < casesCount; i++) this->cases[i] = cases[i]; + this->caseCount = casesCount; } Game::Game(const Game& other) { diff --git a/Year 1/Object-oriented Programming/07-Practice-05.04.2022/GameCase.cpp b/Year 1/Object-oriented Programming/07-Practice-05.04.2022/GameCase.cpp index f16e72b..e0337a9 100644 --- a/Year 1/Object-oriented Programming/07-Practice-05.04.2022/GameCase.cpp +++ b/Year 1/Object-oriented Programming/07-Practice-05.04.2022/GameCase.cpp @@ -1,17 +1,59 @@ #include "GameCase.h" -GameCase::GameCase() : GameCase('\0', nullptr) +/* Private */ + +void GameCase::copyFrom(const GameCase& other) { + this->command = other.command; + this->description = new char[strlen(other.description) + 1]; + strcpy(this->description, other.description); + this->func = other.func; +} + +void GameCase::free() { + delete[] description; +} + +/* Public */ + +GameCase::GameCase() : GameCase('\0', nullptr, nullptr) {} -GameCase::GameCase(char command, bool func(double&)) { +GameCase::GameCase(char command, const char* description, bool func(double&)) { this->command = command; this->func = func; + + if (description == nullptr) + return; + + int descLen = strlen(description); + this->description = new char[descLen + 1]; + strcpy(this->description, description); +} + +GameCase::GameCase(const GameCase& other) { + this->copyFrom(other); +} + +GameCase& GameCase::operator=(const GameCase& other) { + if (this != &other) { + this->free(); + this->copyFrom(other); + } + return *this; +} + +GameCase::~GameCase() { + this->free(); } char GameCase::get_comm() const { return this->command; } +void GameCase::print() const { + std::cout << this->command << " - " << this->description << std::endl; +} + bool GameCase::exec(double& bet) const { return this->func(bet); } diff --git a/Year 1/Object-oriented Programming/07-Practice-05.04.2022/GameCase.h b/Year 1/Object-oriented Programming/07-Practice-05.04.2022/GameCase.h index 8efd039..37a1b21 100644 --- a/Year 1/Object-oriented Programming/07-Practice-05.04.2022/GameCase.h +++ b/Year 1/Object-oriented Programming/07-Practice-05.04.2022/GameCase.h @@ -1,13 +1,24 @@ +#include <iostream> +#include <cstring> #include "Player.h" class GameCase { char command; + char* description; bool (*func)(double&); + void copyFrom(const GameCase&); + void free(); public: GameCase(); - GameCase(char, bool f(double&)); + GameCase(char, const char*, bool f(double&)); + + GameCase(const GameCase&); + GameCase& operator=(const GameCase&); + ~GameCase(); char get_comm() const; + void print() const; + bool exec(double&) const; }; diff --git a/Year 1/Object-oriented Programming/07-Practice-05.04.2022/source.cpp b/Year 1/Object-oriented Programming/07-Practice-05.04.2022/source.cpp index 1a3f4ba..16caf87 100644 --- a/Year 1/Object-oriented Programming/07-Practice-05.04.2022/source.cpp +++ b/Year 1/Object-oriented Programming/07-Practice-05.04.2022/source.cpp @@ -78,11 +78,11 @@ bool gameCase_guess(double& bet) { int main() { GameCase numberGambleCases[] = { - GameCase('e', gameCase_evenOrOdd), - GameCase('p', gameCase_isPrime), - GameCase('t', gameCase_isPowerOfTwo), - GameCase('h', gameCase_higherOrLower), - GameCase('g', gameCase_guess) + GameCase('e', "Guess if random is an even or odd number. [WIN: bet/4]", gameCase_evenOrOdd), + GameCase('p', "Guess if random is a prime number. [WIN: bet/2]", gameCase_isPrime), + GameCase('t', "Guess if random is a power of two. [WIN: bet*2]", gameCase_isPowerOfTwo), + GameCase('h', "Guess if random is higher or lower than a number. [WIN: bet/4]", gameCase_higherOrLower), + GameCase('g', "Guess random. [WIN: bet*10]", gameCase_guess) }; Game numberGamble(numberGambleCases, 5); |
