diff --git a/CMakeLists.txt b/CMakeLists.txt index acee1a1..0a588c5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,3 +17,16 @@ add_executable( src/games/truc/print.cpp src/games/truc/statistics.cpp ) + +add_executable( + escambrit + src/games/escambrit/escambrit.cpp + src/games/escambrit/card.cpp + src/games/escambrit/banca.cpp + src/games/escambrit/deck.cpp + src/games/escambrit/game.cpp + src/games/escambrit/human.cpp + src/games/escambrit/player.cpp + src/games/escambrit/print.cpp + src/games/escambrit/statistics.cpp +) diff --git a/src/games/escambrit/banca.cpp b/src/games/escambrit/banca.cpp new file mode 100644 index 0000000..cf08acf --- /dev/null +++ b/src/games/escambrit/banca.cpp @@ -0,0 +1,14 @@ +#include "headers/banca.h" +#include + + +// Prints first card revealed and second card hidden +void Banca::printFirstCard(){ + std::cout<<"\n"; + std::cout<<".------..------."<<"\n"; + std::cout<<"|"< + +Card::Card(){ + number = 0; + suit = '\0'; + block = false; +} + +Card::Card(int no, char s){ + number = no; + suit = s; + block = false; +} + +int Card::getNumber(){ + return number; +} + +char Card::getSuit(){ + return suit; +} +bool Card::getBlock(){ + return block; +} + +void Card::setNumber(int no){ + number = no; +} +void Card::setSuit(char c){ + suit = c; +} +void Card::setBlock(bool b){ + block = b; +} + +char Card::getPrintNumber(){ + switch(number){ + case 1: return 'A'; + case 10: return 'X'; + case 11: return 'J'; + case 12: return 'Q'; + case 13: return 'K'; + default: char digits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; + return digits[number]; + } +} +void Card::printCardL1(){ + switch(suit){ + case 'C': std::cout<<"| :(): |"; break; + case 'H': std::cout<<"| (\\/) |"; break; + case 'D': + case 'S': std::cout<<"| :/\\: |"; break; + default : std::cout<<"| // |"; + } +} + +void Card::printCardL2(){ + switch(suit){ + case 'C': std::cout<<"| ()() |"; break; + case 'H': + case 'D': std::cout<<"| :\\/: |"; break; + case 'S': std::cout<<"| (__) |"; break; + default : std::cout<<"| // |"; + } +} \ No newline at end of file diff --git a/src/games/escambrit/deck.cpp b/src/games/escambrit/deck.cpp new file mode 100644 index 0000000..48d017d --- /dev/null +++ b/src/games/escambrit/deck.cpp @@ -0,0 +1,56 @@ +#include "headers/deck.h" +#include +#include +#include + +// Shuffles a deck +void Deck::shuffleDeck() { + std::random_shuffle(deck.begin(), deck.end()); +} + +// Constructs a Deck +/* + * O: Oros; + * E: Espases; + * B: Bastos; + * C: Copes; + */ +void Deck::initializeDeck(){ + deck.clear(); + char suits[4] = {'O','E','B','C'}; + for(int i=0;i<4;i++){ + for(int j=0;j<12;j++){ + Card c(j+1,suits[i]); + deck.push_back(c); + } + } + shuffleDeck(); +} + +// Getter Function for size of deck +int Deck::getSize(){ + return deck.size(); +} + +// Deals by returning one card from the deck +Card Deck::deal(){ + int val = (rand()%(deck.size())); // TODO: Why shuffle again if the deck is initialized and shuffled? Can you choose any card at your will? + Card t = deck[val]; + deck.erase(deck.begin()+val); + return t; +} + +// Pick a number of cards (default is 1) from the deck (the first one) +Card* Deck::pick(short number=1){ + short val = number; + Card cards[number-1]; + + // Test that the number is 1 or more + assert(number>=1); + for(short i=0;i +#include + +int main(){ + + srand(time(NULL)); // To seed rand() function across all files + + Game game; // Constructs object GAME + game.beginMenu(false, ""); // Begins with the interface + + return 0; // Return integer value at end of main() + +} diff --git a/src/games/escambrit/game.cpp b/src/games/escambrit/game.cpp new file mode 100644 index 0000000..391e0a2 --- /dev/null +++ b/src/games/escambrit/game.cpp @@ -0,0 +1,304 @@ +#include "headers/game.h" +#include "headers/compatible.h" +#include +#include +#include +#include +#include + +//////////////* Default Constructor *//// + +Game::Game(){ + deck.initializeDeck(); +} + +//////////////* Deals dealer towards the end *//// + +bool Game::dealDealer(){ + if(dealer.getSum()dealer.getSum()){ + printTop(); + std::cout<player.getSum()){ + printTop(); + std::cout<0){ + while(true){ + printTop(); + std::cout<<"Place your bet!\t\t $"<=5){ + player.setBet(5); + } + break; + case 83: if(player.getBet()>=5){ + player.setBet(-5); + } + break; + } + if(c==82) break; + } + return true; + } + else{ + return false; + } +} + +bool Game::startGame(){ + player.addCard(deck.deal()); + dealer.addCard(deck.deal()); + player.addCard(deck.deal()); + dealer.addCard(deck.deal()); + printBody(); + if(checkWins()){ + return false; + } + while(true){ + std::cout << lightYellow << "\n\nH : Hit | S : Stand\n"<>cont; + } while (cont != 'N' && cont != 'n'); + char saveChoice; + std::cout<<"\nSave game? [Y/N]: "; + std::cin>>saveChoice; + if(saveChoice == 'Y' || saveChoice == 'y'){ + saveGame(); + } +} + +//////////////* Main Method to be Called *//// + +void Game::beginMenu(bool rep, std::string message){ + clearscr(); + std::cout<>c; + switch(c){ + case '1': char nm[100]; + std::cout<<"Enter player name: "; + std::cin>>nm; + player.setName(nm); + beginGame(); + break; + case '2': loadGame(); + beginGame(); + break; + case '3': printStatistics(); + beginMenu(false, ""); + break; + case '4': printInstructions(); + beginMenu(false, ""); + break; + case '5': exit(0); + break; + default: beginMenu(true, "Invalid input."); + } +} + +//////////////* Data File Handling *//// + +void Game::saveGame(){ + std::fstream f1,f2; + std::string filename; + std::string path = "data/"; + do{ + std::cout<<"Enter filename: "; + std::cin>>filename; + std::transform(filename.begin(), filename.end(), filename.begin(), ::tolower); + }while(filename.compare("statistics")==0); + path+=filename+".bin"; + std::string sName = player.getName(); + int sCash = player.getCash(); + int sWins = player.getWins(); + int sLoses = player.getLoses(); + int nameSize = sName.size(); + f2.open(path, std::ios::in | std::ios::binary); + if(!f2.fail()){ + char choice; + std::cout<>choice; + if(choice == 'N' || choice == 'n'){ + saveGame(); + } + } + f2.close(); + f1.open(path, std::ios::out | std::ios::binary); + f1.write((char*)&nameSize, sizeof(nameSize)); + f1.write(sName.c_str(), sName.size()); + f1.write((char*)&sCash, sizeof(sCash)); + f1.write((char*)&sWins, sizeof(sWins)); + f1.write((char*)&sLoses, sizeof(sLoses)); + f1.close(); +} + +void Game::loadGame(){ + std::fstream f1; + std::string filename; + std::string path = "data/"; + do{ + std::cout<<"Enter filename: "; + std::cin>>filename; + std::transform(filename.begin(), filename.end(), filename.begin(), ::tolower); + }while(filename.compare("statistics")==0); + path+=filename+".bin"; + f1.open(path, std::ios::in | std::ios::binary); + if(!f1.fail()){ + std::string sName; + int sCash; + int sWins; + int sLoses; + int nameSize; + f1.read((char*)&nameSize, sizeof(nameSize)); + sName.resize(nameSize); + f1.read(&sName[0], sName.size()); + f1.read((char*)&sCash, sizeof(sCash)); + f1.read((char*)&sWins, sizeof(sWins)); + f1.read((char*)&sLoses, sizeof(sLoses)); + f1.close(); + player.setName(sName); + player.addCash(sCash - player.getCash()); + while(player.getWins()!=sWins){ + player.incrementWins(); + } + while(player.getLoses()!=sLoses){ + player.incrementLoses(); + } + } + else{ + beginMenu(true, "File does not exist."); + } +} + +//////////////* Printing Stuff *//// + +void Game::printStatistics(){ + clearscr(); + std::cout< + + +namespace Color{ +enum class Code{ + BOLD = 1, + RESET = 0, + BG_BLUE = 44, + BG_DEFAULT = 49, + BG_GREEN = 42, + BG_RED = 41, + FG_BLACK = 30, + FG_BLUE = 34, + FG_CYAN = 36, + FG_DARK_GRAY = 90, + FG_DEFAULT = 39, + FG_GREEN = 32, + FG_LIGHT_BLUE = 94, + FG_LIGHT_CYAN = 96, + FG_LIGHT_GRAY = 37, + FG_LIGHT_GREEN = 92, + FG_LIGHT_MAGENTA = 95, + FG_LIGHT_RED = 91, + FG_LIGHT_YELLOW = 93, + FG_MAGENTA = 35, + FG_RED = 31, + FG_WHITE = 97, + FG_YELLOW = 33, +}; +class Modifier{ + Code code; +public: + Modifier(Code pCode) : code(pCode) {} + friend std::ostream &operator<<(std::ostream &os, const Modifier &mod){ + return os << "\033[" << static_cast(mod.code) << "m"; + } +}; +} + +static Color::Modifier bold_off(Color::Code::RESET); +static Color::Modifier bold_on(Color::Code::BOLD); +static Color::Modifier def(Color::Code::FG_DEFAULT); +static Color::Modifier red(Color::Code::FG_RED); +static Color::Modifier green(Color::Code::FG_GREEN); +static Color::Modifier yellow(Color::Code::FG_YELLOW); +static Color::Modifier blue(Color::Code::FG_BLUE); +static Color::Modifier magenta(Color::Code::FG_MAGENTA); +static Color::Modifier cyan(Color::Code::FG_CYAN); +static Color::Modifier lightGray(Color::Code::FG_LIGHT_GRAY); +static Color::Modifier darkGray(Color::Code::FG_DARK_GRAY); +static Color::Modifier lightRed(Color::Code::FG_LIGHT_RED); +static Color::Modifier lightGreen(Color::Code::FG_LIGHT_GREEN); +static Color::Modifier lightYellow(Color::Code::FG_LIGHT_YELLOW); +static Color::Modifier lightBlue(Color::Code::FG_LIGHT_BLUE); +static Color::Modifier lightMagenta(Color::Code::FG_LIGHT_MAGENTA); +static Color::Modifier lightCyan(Color::Code::FG_LIGHT_CYAN); + +#endif + +// Extracted from https://github.com/plibither8/2048.cpp \ No newline at end of file diff --git a/src/games/escambrit/headers/compatible.h b/src/games/escambrit/headers/compatible.h new file mode 100644 index 0000000..82c66c0 --- /dev/null +++ b/src/games/escambrit/headers/compatible.h @@ -0,0 +1,37 @@ +#ifdef _WIN32 +#include + +void clearscr(){ + system("cls"); +} + +#else +#include +#include + +void clearscr(){ + system("clear"); +} + +char getch() +{ + char buf = 0; + struct termios old = {0}; + if (tcgetattr(0, &old) < 0) + perror("tcsetattr()"); + old.c_lflag &= ~ICANON; + old.c_lflag &= ~ECHO; + old.c_cc[VMIN] = 1; + old.c_cc[VTIME] = 0; + if (tcsetattr(0, TCSANOW, &old) < 0) + perror("tcsetattr ICANON"); + if (read(0, &buf, 1) < 0) + perror("read()"); + old.c_lflag |= ICANON; + old.c_lflag |= ECHO; + if (tcsetattr(0, TCSADRAIN, &old) < 0) + perror("tcsetattr ~ICANON"); + return (buf); +} + +#endif \ No newline at end of file diff --git a/src/games/escambrit/headers/deck.h b/src/games/escambrit/headers/deck.h new file mode 100644 index 0000000..92a7fea --- /dev/null +++ b/src/games/escambrit/headers/deck.h @@ -0,0 +1,20 @@ +#ifndef DECK_HPP +#define DECK_HPP + +#include "card.h" +#include + +class Deck{ + + private: + std::vector deck; // Deck (Vector) of Cards + + public: + void initializeDeck(); + void shuffleDeck(); + int getSize(); + Card deal(); + Card* pick(short number); +}; + +#endif diff --git a/src/games/escambrit/headers/escambrit.h b/src/games/escambrit/headers/escambrit.h new file mode 100644 index 0000000..679fedf --- /dev/null +++ b/src/games/escambrit/headers/escambrit.h @@ -0,0 +1 @@ +#include "game.h" \ No newline at end of file diff --git a/src/games/escambrit/headers/game.h b/src/games/escambrit/headers/game.h new file mode 100644 index 0000000..00db2e4 --- /dev/null +++ b/src/games/escambrit/headers/game.h @@ -0,0 +1,37 @@ +#ifndef GAME_HPP +#define GAME_HPP + +#include "deck.h" +#include "banca.h" +#include "player.h" +#include "print.h" +#include "statistics.h" +#include + +class Game{ + + private: + Player player; // Player in the game (user) + Banca dealer; // Dealer in the game + Deck deck; // Deck of cards in the game + Statistics s; // Leaderboard + + public: + Game(); + bool dealDealer(); + char compareSum(); + bool checkWins(); + bool checkEnd(); + bool startBet(); + bool startGame(); + void beginGame(); + void beginMenu(bool rep, std::string message); + void saveGame(); + void loadGame(); + void printStatistics(); + void printInstructions(); + void printTop(); + void printBody(); +}; + +#endif diff --git a/src/games/escambrit/headers/human.h b/src/games/escambrit/headers/human.h new file mode 100644 index 0000000..ae70464 --- /dev/null +++ b/src/games/escambrit/headers/human.h @@ -0,0 +1,22 @@ +#ifndef HUMAN_HPP +#define HUMAN_HPP + +#include "card.h" +#include + +class Human{ + + protected: + std::vector hand; + int sum; + + public: + Human(); + int getSum(); + void switchAce(); + void addCard(Card c); + void clearCards(); + void printCards(); +}; + +#endif \ No newline at end of file diff --git a/src/games/escambrit/headers/player.h b/src/games/escambrit/headers/player.h new file mode 100644 index 0000000..83da3ad --- /dev/null +++ b/src/games/escambrit/headers/player.h @@ -0,0 +1,29 @@ +#ifndef PLAYER_HPP +#define PLAYER_HPP + +#include "human.h" +#include + +class Player: public Human{ + +private: + std::string name; // Name of Player + int cash, bet; // Player's Cash, Player's Bet + int wins, loses; // Player's Stats (number of wins and loses) + +public: + Player(); + std::string getName(); + int getBet(); + int getCash(); + int getWins(); + int getLoses(); + void setName(std::string nm); + void setBet(int b); + void addCash(int c); + void incrementWins(); + void incrementLoses(); + void clearCards(); +}; + +#endif diff --git a/src/games/escambrit/headers/print.h b/src/games/escambrit/headers/print.h new file mode 100644 index 0000000..c09ddc7 --- /dev/null +++ b/src/games/escambrit/headers/print.h @@ -0,0 +1,22 @@ +#ifndef PRINT_HPP +#define PRINT_HPP + +#include + +struct Print{ + + static std::string title_escambrit(); + static std::string begin_menu(); + static std::string statistics(); + static std::string instructions(); + static std::string bust(); + static std::string blackjack(); + static std::string dealer_wins(); + static std::string you_win(); + static std::string draw(); + static std::string dealer_border(); + static std::string player_border(); + +}; + +#endif diff --git a/src/games/escambrit/headers/statistics.h b/src/games/escambrit/headers/statistics.h new file mode 100644 index 0000000..356cb89 --- /dev/null +++ b/src/games/escambrit/headers/statistics.h @@ -0,0 +1,39 @@ +#ifndef STATISTICS_HPP +#define STATISTICS_HPP + +#include "player.h" +#include "color.h" +#include + +class PlayerSet{ + + private: + std::string name; // Name of Player + int cash, wins, loses; // Stat Data + // This class is almost similar to Player, but does not need vectors and betting values. + + public: + PlayerSet(); + std::string getName(); + int getCash(); + int getWins(); + int getLoses(); + void setValues(std::string nm, int c, int w, int l); + +}; + +class Statistics{ + + private: + PlayerSet p[3]; // 3 Players + + public: + Statistics(); + bool check(Player pl); + void print(); + void saveStats(); + void loadStats(); + +}; + +#endif \ No newline at end of file diff --git a/src/games/escambrit/human.cpp b/src/games/escambrit/human.cpp new file mode 100644 index 0000000..20e55c0 --- /dev/null +++ b/src/games/escambrit/human.cpp @@ -0,0 +1,62 @@ +#include "headers/human.h" +#include + +// Default Constructor +Human::Human(){ + sum = 0; +} + +// Getter Function for sum to check end of game +int Human::getSum(){ + switchAce(); + return sum; +} + +// Switches Ace between 1 and 11 +void Human::switchAce(){ + if(sum>21){ + for(int i=0;i10){ + c.setNumber(10); + } + else if (c.getNumber()==1){ + c.setNumber(11); + } + sum+= c.getNumber(); +} + +// Clears Human's hand +void Human::clearCards(){ + hand.clear(); + sum = 0; +} + +// Prints Human's cards +void Human::printCards(){ + std::cout<<"\n"; + for(int i=0;i<6;i++){ + for(int j=0;j + +//////////////* Default Constructor *//// + +Player::Player(){ + name = "Unknown"; + bet = 0; + cash = 1000; + wins = 0; + loses = 0; +} + +//////////////* Getter Functions *//// + +// Returns name of Player +std::string Player::getName(){ + return name; +} + +// Returns amount of bet +int Player::getBet(){ + return bet; +} + +// Returns Player's cash amount +int Player::getCash(){ + return cash; +} + +// Returns Player's statistic (number of wins) +int Player::getWins(){ + return wins; +} + +// Returns Player's statistic (number of loses) +int Player::getLoses(){ + return loses; +} + +//////////////* Setter Functions *//// + +// Sets name of Player +void Player::setName(std::string nm){ + name = nm; +} + +// Sets bet for game +void Player::setBet(int b){ + cash-=b; + bet+=b; +} + +// Adds cash to Player's cash amount +void Player::addCash(int c){ + cash+=c; +} + +// Increments Player's number of wins by one +void Player::incrementWins(){ + wins+=1; +} + +// Increments Player's number of loses by one +void Player::incrementLoses(){ + loses+=1; +} + +//////////////* Game Functions *//// + +// Clears player's hand +void Player::clearCards(){ + Human::clearCards(); + bet=0; +} \ No newline at end of file diff --git a/src/games/escambrit/print.cpp b/src/games/escambrit/print.cpp new file mode 100644 index 0000000..75bffb1 --- /dev/null +++ b/src/games/escambrit/print.cpp @@ -0,0 +1,150 @@ +#include "headers/print.h" +#include +#include + +std::string Print::title_escambrit(){ + // https://patorjk.com/software/taag/#p=display&f=Blocks&t=ESCAMBRIT + constexpr auto title_escambrit = R"( + /$$$$$$$$ /$$$$$$ /$$$$$$ /$$$$$$ /$$ /$$ /$$$$$$$ /$$$$$$$ /$$$$$$ /$$$$$$$$ +| $$_____/ /$$__ $$ /$$__ $$ /$$__ $$| $$$ /$$$| $$__ $$| $$__ $$|_ $$_/|__ $$__/ +| $$ | $$ \__/| $$ \__/| $$ \ $$| $$$$ /$$$$| $$ \ $$| $$ \ $$ | $$ | $$ +| $$$$$ | $$$$$$ | $$ | $$$$$$$$| $$ $$/$$ $$| $$$$$$$ | $$$$$$$/ | $$ | $$ +| $$__/ \____ $$| $$ | $$__ $$| $$ $$$| $$| $$__ $$| $$__ $$ | $$ | $$ +| $$ /$$ \ $$| $$ $$| $$ | $$| $$\ $ | $$| $$ \ $$| $$ \ $$ | $$ | $$ +| $$$$$$$$| $$$$$$/| $$$$$$/| $$ | $$| $$ \/ | $$| $$$$$$$/| $$ | $$ /$$$$$$ | $$ +|________/ \______/ \______/ |__/ |__/|__/ |__/|_______/ |__/ |__/|______/ |__/ + )"; + + std::ostringstream toReturn; + toReturn << title_escambrit; + return toReturn.str(); + +} + +std::string Print::begin_menu(){ + constexpr auto begin_menu = R"( + 1 - Start a New Game + 2 - Load from Game + 3 - Statistics + 4 - How to Play + 5 - Exit + )"; + + std::ostringstream toReturn; + toReturn << begin_menu; + return toReturn.str(); +} + +std::string Print::statistics(){ + constexpr auto statistics = R"( + ____ ____ __ ____ __ ____ ____ __ ___ ____ + / ___)(_ _)/ _\(_ _)( )/ ___)(_ _)( )/ __)/ ___) + \___ \ )( / \ )( )( \___ \ )( )(( (__ \___ \ + (____/ (__)\_/\_/(__) (__)(____/ (__) (__)\___)(____/ + )"; + + std::ostringstream toReturn; + toReturn << statistics << "\n\n"; + return toReturn.str(); +} + +std::string Print::instructions(){ + // TODO: Escriure Instruccions + constexpr auto instructions = R"( + FALTEN INSTRUCCIONS! + )"; + + std::ostringstream toReturn; + toReturn << instructions; + return toReturn.str(); +} + +std::string Print::bust(){ + constexpr auto bust = R"( + ___ _ _ + | _ ) _ _ ___| |_ | | + | _ \| || |(_-<| _||_| + |___/ \_,_|/__/ \__|(_) + )"; + + std::ostringstream toReturn; + toReturn << bust; + return toReturn.str(); +} + +std::string Print::blackjack(){ + constexpr auto blackjack = R"( + ___ _ _ _ _ _ + | _ )| | __ _ __ | |__ (_) __ _ __ | |__| | + | _ \| |/ _` |/ _|| / / | |/ _` |/ _|| / /|_| + |___/|_|\__,_|\__||_\_\_/ |\__,_|\__||_\_\(_) + |__/ + )"; + + std::ostringstream toReturn; + toReturn << blackjack; + return toReturn.str(); +} + +std::string Print::dealer_wins(){ + constexpr auto dealer_wins = R"( + ___ _ _ + | \ ___ __ _| |___ _ _ __ __ _(_)_ _ ___ + | |) / -_/ _` | / -_| '_| \ V V | | ' \(_-<_ + |___/\___\__,_|_\___|_| \_/\_/|_|_||_/__(_) + )"; + + std::ostringstream toReturn; + toReturn << dealer_wins; + return toReturn.str(); +} + +std::string Print::you_win(){ + constexpr auto you_win = R"( + __ __ _ _ + \ \ / /___ _ _ __ __ __(_) _ _ | | + \ V // _ \| || | \ V V /| || ' \ |_| + |_| \___/ \_,_| \_/\_/ |_||_||_|(_) + )"; + + std::ostringstream toReturn; + toReturn << you_win; + return toReturn.str(); +} + +std::string Print::draw(){ + constexpr auto draw = R"( + ___ _ _ + | _ \ _ _ ___| |_ | | + | _/| || |(_-<| ' \ |_| + |_| \_,_|/__/|_||_|(_) + )"; + + std::ostringstream toReturn; + toReturn << draw; + return toReturn.str(); +} + +std::string Print::dealer_border(){ + constexpr auto dealer_border = R"( + _ __ _ __ _ +/)/)/)/)/)/)/)/)/) | \|_ |_|| |_ |_) /)/)/)/)/)/)/)/)/) +(/(/(/(/(/(/(/(/(/ |_/|__| ||__|__| \ (/(/(/(/(/(/(/(/(/ + )"; + + std::ostringstream toReturn; + toReturn << dealer_border; + return toReturn.str(); +} + +std::string Print::player_border(){ + constexpr auto player_border = R"( + _ _ __ _ +/)/)/)/)/)/)/)/)/) |_)| |_|\/ |_ |_) /)/)/)/)/)/)/)/)/) +(/(/(/(/(/(/(/(/(/ | |__| | | |__| \ (/(/(/(/(/(/(/(/(/ + )"; + + std::ostringstream toReturn; + toReturn << player_border; + return toReturn.str(); +} diff --git a/src/games/escambrit/statistics.cpp b/src/games/escambrit/statistics.cpp new file mode 100644 index 0000000..b9621db --- /dev/null +++ b/src/games/escambrit/statistics.cpp @@ -0,0 +1,137 @@ +#include "headers/statistics.h" +#include +#include +#include + +//////////////* Default Constructor *//// + +PlayerSet::PlayerSet(){ + name = "N/A"; + cash=1000; + wins=0; + loses=0; +} + +//////////////* Getter Functions *//// + +// Returns name of Player +std::string PlayerSet::getName(){ + return name; +} + +// Returns cash of Player +int PlayerSet::getCash(){ + return cash; +} + +// Returns wins of Player +int PlayerSet::getWins(){ + return wins; +} + +// Returns loses of Player +int PlayerSet::getLoses(){ + return loses; +} + +//////////////* Setter Function *//// + +void PlayerSet::setValues(std::string nm, int c, int w, int l){ + name = nm; + cash = c; + wins = w; + loses = l; +} + +////////////////////////////////////////////////////////////////// + + +//////////////* Default Constructor *//// + +Statistics::Statistics(){ + std::fstream temp; + temp.open("data/statistics.bin", std::ios::in | std::ios::binary); + if(temp.fail()){ + saveStats(); + } + else{ + temp.close(); + loadStats(); + } +} + +//////////////* Checks for High Score & Saves *//// + +bool Statistics::check(Player pl){ + bool rewrite = false; + if(pl.getCash()>p[0].getCash()){ + p[0].setValues(pl.getName(), pl.getCash(), pl.getWins(), pl.getLoses()); + rewrite = true; + } + if(pl.getWins()>p[1].getWins()){ + p[1].setValues(pl.getName(), pl.getCash(), pl.getWins(), pl.getLoses()); + rewrite = true; + } + if(pl.getLoses()>p[2].getLoses()){ + p[2].setValues(pl.getName(), pl.getCash(), pl.getWins(), pl.getLoses()); + rewrite = true; + } + if(rewrite){ + saveStats(); + } + return rewrite; +} + +//////////////* Printing *//// + +void Statistics::print(){ + int maxlength = std::max(std::max(p[0].getName().length(), p[1].getName().length()),p[2].getName().length()); + for(int i=0;i<3;i++){ + switch(i){ + case 0: std::cout<<"MAX CASH ||||||||| "; break; + case 1: std::cout<<"MAX WINS ||||||||| "; break; + case 2: std::cout<<"MAX LOSES ||||||||| "; + } + std::cout<