diff options
Separated NumberGamble game into its own set of header and cpp files
7 files changed, 121 insertions, 90 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 bbc91b8..3e235be 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 @@ -27,7 +27,7 @@ void Game::free() { delete[] cases; } -TurnResult Game::turn(double& bet) { +TurnResult Game::turn(double& bet) const { std::cout << "Available commands: " << std::endl; for (int i = 0; i < this->caseCount; i++) cases[i].print(); @@ -84,7 +84,7 @@ Game::~Game() { this->free(); } -void Game::play(Player& player) { +void Game::play(Player& player) const { if (player.get_age() < 18) { std::cout << "You must be at least 18 years old to play." << std::endl; return; diff --git a/Year 1/Object-oriented Programming/07-Practice-05.04.2022/Game.h b/Year 1/Object-oriented Programming/07-Practice-05.04.2022/Game.h index 8915196..3b3bb0b 100644 --- a/Year 1/Object-oriented Programming/07-Practice-05.04.2022/Game.h +++ b/Year 1/Object-oriented Programming/07-Practice-05.04.2022/Game.h @@ -1,3 +1,6 @@ +#ifndef GAME_HEADER +#define GAME_HEADER + #include <iostream> #include <stdlib.h> // srand, time and rand #include "Player.h" @@ -19,7 +22,7 @@ class Game { void copyFrom(const Game&); void free(); - TurnResult turn(double&); + TurnResult turn(double&) const; public: Game(const GameCase*, unsigned); @@ -27,5 +30,7 @@ public: Game& operator=(const Game&); ~Game(); - void play(Player&); + void play(Player&) const; }; + +#endif 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 37a1b21..722b57a 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,3 +1,6 @@ +#ifndef GAMECASE_HEADER +#define GAMECASE_HEADER + #include <iostream> #include <cstring> #include "Player.h" @@ -22,3 +25,5 @@ public: bool exec(double&) const; }; + +#endif diff --git a/Year 1/Object-oriented Programming/07-Practice-05.04.2022/NumberGamble.cpp b/Year 1/Object-oriented Programming/07-Practice-05.04.2022/NumberGamble.cpp new file mode 100644 index 0000000..6ab799b --- /dev/null +++ b/Year 1/Object-oriented Programming/07-Practice-05.04.2022/NumberGamble.cpp @@ -0,0 +1,76 @@ +#include "NumberGamble.h" + +bool gameCase_evenOrOdd(double& bet) { + int rnd = getRandomBetween(1, 100); + std::cout << "Is the number [e]ven or [o]dd? "; + char buffer; + std::cin >> buffer; + + while (buffer != 'e' && buffer != 'o') { + std::cout << "Unknown option '" << buffer << "', choice must be either 'e' or 'o'." << std::endl << "Is the number [e]ven or [o]dd? "; + std::cin >> buffer; + } + + bool result = isEven(rnd) == (buffer == 'e'); + bet = (result) ? bet / 5 : 0; + std::cout << "The number was: " << rnd << std::endl; + return result; +} + +bool gameCase_isPrime(double& bet) { + int rnd = getRandomBetween(1, 100); + + bool result = isPrime(rnd); + bet = (result) ? bet / 2 : 0; + std::cout << "The number was: " << rnd << std::endl; + return result; +} + +bool gameCase_isPowerOfTwo(double& bet) { + int rnd = getRandomBetween(1, 100); + + bool result = isPowerOfTwo(rnd); + bet = (result) ? bet / 2 : 0; + std::cout << "The number was: " << rnd << std::endl; + return result; +} + +bool gameCase_higherOrLower(double& bet) { + int rnd = getRandomBetween(1, 100); + + std::cout << "Your number [20...60] "; + int num; + std::cin >> num; + + while (num < 20 || num > 60) { + std::cout << "Number not in range! Enter number [20...60] "; + std::cin >> num; + } + + std::cout << num << " will be [h]igher or [l]ower than random? "; + char guess; + std::cin >> guess; + + while (guess != 'h' && guess != 'l') { + std::cout << "Invalid option '" << guess << "', must be either 'h' or 'l'." << std::endl << num << " will be [h]igher or [l]ower than random? "; + std::cin >> guess; + } + + bool result = (rnd > num) == (guess == 'h'); + bet = (result) ? bet / 4 : 0; + std::cout << "The number was: " << rnd << std::endl; + return result; +} + +bool gameCase_guess(double& bet) { + int rnd = getRandomBetween(1, 100); + + std::cout << "Your guess: "; + int num; + std::cin >> num; + + bool result = num == rnd; + bet = (result) ? bet * 10 : 0; + std::cout << "The number was: " << rnd << std::endl; + return result; +} diff --git a/Year 1/Object-oriented Programming/07-Practice-05.04.2022/NumberGamble.h b/Year 1/Object-oriented Programming/07-Practice-05.04.2022/NumberGamble.h new file mode 100644 index 0000000..258fbe4 --- /dev/null +++ b/Year 1/Object-oriented Programming/07-Practice-05.04.2022/NumberGamble.h @@ -0,0 +1,24 @@ +#ifndef NUMBERGAMBLE_HEADER +#define NUMBERGAMBLE_HEADER + +#include "Game.h" +#include "GameCase.h" +#include "NumberHelpers.h" + +bool gameCase_evenOrOdd(double&); +bool gameCase_isPrime(double&); +bool gameCase_isPowerOfTwo(double&); +bool gameCase_higherOrLower(double&); +bool gameCase_guess(double&); + +const GameCase numberGambleCases[5] = { + 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) + }; + +const Game numberGamble(numberGambleCases, 5); + +#endif diff --git a/Year 1/Object-oriented Programming/07-Practice-05.04.2022/NumberHelpers.h b/Year 1/Object-oriented Programming/07-Practice-05.04.2022/NumberHelpers.h index 159f0f3..b8e57c9 100644 --- a/Year 1/Object-oriented Programming/07-Practice-05.04.2022/NumberHelpers.h +++ b/Year 1/Object-oriented Programming/07-Practice-05.04.2022/NumberHelpers.h @@ -1,5 +1,10 @@ +#ifndef NUMBERHELPER_HEADER +#define NUMBERHELPER_HEADER + #include <math.h> bool isEven(int); bool isPowerOfTwo(int); bool isPrime(int); + +#endif 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 16caf87..4b5a7b9 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 @@ -1,91 +1,7 @@ -#include "Game.h" -#include "NumberHelpers.h" - -bool gameCase_evenOrOdd(double& bet) { - int rnd = getRandomBetween(1, 100); - std::cout << "Is the number [e]ven or [o]dd? "; - char buffer; - std::cin >> buffer; - - while (buffer != 'e' && buffer != 'o') { - std::cout << "Unknown option '" << buffer << "', choice must be either 'e' or 'o'." << std::endl << "Is the number [e]ven or [o]dd? "; - std::cin >> buffer; - } - - bool result = isEven(rnd) == (buffer == 'e'); - bet = (result) ? bet / 5 : 0; - std::cout << "The number was: " << rnd << std::endl; - return result; -} - -bool gameCase_isPrime(double& bet) { - int rnd = getRandomBetween(1, 100); - - bool result = isPrime(rnd); - bet = (result) ? bet / 2 : 0; - std::cout << "The number was: " << rnd << std::endl; - return result; -} - -bool gameCase_isPowerOfTwo(double& bet) { - int rnd = getRandomBetween(1, 100); - - bool result = isPowerOfTwo(rnd); - bet = (result) ? bet / 2 : 0; - std::cout << "The number was: " << rnd << std::endl; - return result; -} - -bool gameCase_higherOrLower(double& bet) { - int rnd = getRandomBetween(1, 100); - - std::cout << "Your number [20...60] "; - int num; - std::cin >> num; - - while (num < 20 || num > 60) { - std::cout << "Number not in range! Enter number [20...60] "; - std::cin >> num; - } - - std::cout << num << " will be [h]igher or [l]ower than random? "; - char guess; - std::cin >> guess; - - while (guess != 'h' && guess != 'l') { - std::cout << "Invalid option '" << guess << "', must be either 'h' or 'l'." << std::endl << num << " will be [h]igher or [l]ower than random? "; - std::cin >> guess; - } - - bool result = (rnd > num) == (guess == 'h'); - bet = (result) ? bet / 4 : 0; - std::cout << "The number was: " << rnd << std::endl; - return result; -} - -bool gameCase_guess(double& bet) { - int rnd = getRandomBetween(1, 100); - - std::cout << "Your guess: "; - int num; - std::cin >> num; - - bool result = num == rnd; - bet = (result) ? bet * 10 : 0; - std::cout << "The number was: " << rnd << std::endl; - return result; -} +#include "Player.h" +#include "NumberGamble.h" int main() { - GameCase numberGambleCases[] = { - 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); - Player yorgus("Yorgus", "yorgus", 23, 57741.39); char buffer = '\0'; |
