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

Draft: Afegir joc escambrit #8

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
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
Afegir joc escambrit
antonialoytorrens committed Nov 17, 2021
commit d482d2be71ddd3dc77b7efec18477d6949058f25
13 changes: 13 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
)
14 changes: 14 additions & 0 deletions src/games/escambrit/banca.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "headers/banca.h"
#include <iostream>


// Prints first card revealed and second card hidden
void Banca::printFirstCard(){
std::cout<<"\n";
std::cout<<".------..------."<<"\n";
std::cout<<"|"<<hand[0].getPrintNumber()<<".--. || .--. |"<<"\n";
hand[0].printCardL1(); std::cout<<"| // |"<<"\n";
hand[0].printCardL2(); std::cout<<"| // |"<<"\n";
std::cout<<"| '--'"<<hand[0].getPrintNumber()<<"|| '--' |"<<"\n";
std::cout<<"`------'`------'"<<"\n";
}
66 changes: 66 additions & 0 deletions src/games/escambrit/card.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include "headers/card.h"
#include <iostream>

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<<"| // |";
}
}
56 changes: 56 additions & 0 deletions src/games/escambrit/deck.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include "headers/deck.h"
#include <iostream>
#include <algorithm>
#include <assert.h>

// 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<number;i++){
Card card = deck[0];
cards[i] = card;
deck.erase(deck.begin()+val);
}
return cards;
}
14 changes: 14 additions & 0 deletions src/games/escambrit/escambrit.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "headers/escambrit.h"
#include <iostream>
#include <time.h>

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()

}
304 changes: 304 additions & 0 deletions src/games/escambrit/game.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,304 @@
#include "headers/game.h"
#include "headers/compatible.h"
#include <vector>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cassert>

//////////////* Default Constructor *////

Game::Game(){
deck.initializeDeck();
}

//////////////* Deals dealer towards the end *////

bool Game::dealDealer(){
if(dealer.getSum()<player.getSum()){
while (dealer.getSum() < 17){
dealer.addCard(deck.deal());
if (checkWins()){
return false;
}
}
return true;
}
else{
if(checkWins()){
return false;
}
return true;
}
}

//////////////* Checkers *////

char Game::compareSum(){
if(player.getSum()>dealer.getSum()){
printTop();
std::cout<<lightYellow<<Print::you_win()<<def<<"\n (Dealer has "<<dealer.getSum()<<")\n";
return 'p';
}
else if(dealer.getSum()>player.getSum()){
printTop();
std::cout<<lightRed<<Print::dealer_wins()<<def<<"\n ("<<dealer.getSum()<<")\n";
return 'd';
}
else{
printTop();
std::cout<<lightMagenta<<Print::draw()<<def;
return 'n';
}
}

bool Game::checkWins(){
switch(checkEnd()){
case 'f': return false;
case 'd': player.incrementLoses(); return true;
case 'p': player.incrementWins();
player.addCash((player.getBet()*2));
return true;
}
return false;
}

bool Game::checkEnd(){
bool end = false;
if(deck.getSize() == 0) {

}
return end;
}

//////////////* Game Starters *////

bool Game::startBet(){
if(player.getCash()>0){
while(true){
printTop();
std::cout<<"Place your bet!\t\t $"<<green<<player.getBet()<<def<<"\n[W = Raise Bet | S = Decrease Bet | R = Done]\n";
int c = toupper(getch());
switch(c){
case 87: if(player.getCash()>=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"<<def;
int c = toupper(getch());
if(c==72){
player.addCard(deck.deal());
printBody();
if(checkWins()) return false;
}
else if(c==83){
break;
}
}
return true;
}

void Game::beginGame(){
char cont;
do{
int expectedCardNumber=48;
// Check if the deck has all the cards (48)
assert(deck.getSize()==expectedCardNumber);
deck.initializeDeck();
player.clearCards();
dealer.clearCards();
if (startGame()){
if (dealDealer()){
switch (compareSum()){
case 'p': player.incrementWins();
player.addCash((player.getBet()*2));
break;
case 'd': player.incrementLoses(); break;
case 'n': player.addCash(player.getBet()); break;
}
}
}
std::cout<<lightRed<<Print::dealer_border()<<def;
dealer.printCards();
std::cout<<lightCyan<<Print::player_border()<<def;
player.printCards();
std::cout << yellow << "\nYour wins: " << player.getWins()<< lightRed <<"\nYour loses: "<<player.getLoses()<<def<<"\n";
if(s.check(player)){
std::cout<< lightYellow << "High Score!\n"<<def;
}
std::cout<<"\nContinue playing? [Y/N]: ";
std::cin>>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<<yellow<<Print::title_escambrit()<<def<<"\n";
std::cout<<Print::begin_menu()<<"\n";
if(rep){
std::cout<<red<<message<<def<<"\n";
}
char c;
std::cout<<"Input : ";
std::cin>>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<<red<<"File already exists."<<def<<" Do you want to overwrite it? [Y/N]: ";
std::cin>>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<<yellow<<Print::title_escambrit()<<def<<"\n";
std::cout<<"\n"<<lightGreen<<Print::statistics()<<def<<"\n";
s.print();
std::cout<<"\n\n\t(Press any key to continue)\n";
getch();
}

void Game::printInstructions(){
clearscr();
std::cout<<yellow<<Print::title_escambrit()<<def<<"\n";
std::cout<<"\n"<<lightGreen<<Print::instructions()<<def<<"\n";
getch();
}

void Game::printTop(){
clearscr();
std::cout<<yellow<<Print::title_escambrit()<<def<<"\n";
std::cout<<lightRed<<"\t\tCards: "<<deck.getSize()<<lightGreen<<" \tCash: "<<player.getCash()<<lightMagenta
<<" \tBet: "<<player.getBet()<<lightBlue<<" \tName: "<<player.getName()<<def<<"\n\n\n";
}

void Game::printBody(){
printTop();
std::cout<<lightRed<<Print::dealer_border()<<def;
dealer.printFirstCard();
std::cout<<lightCyan<<Print::player_border()<<def;
player.printCards();
std::cout << lightGreen<< "\nSum: "<<lightRed<< player.getSum()<<def<<"\n";
}
12 changes: 12 additions & 0 deletions src/games/escambrit/headers/banca.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef BANCA_HPP
#define BANCA_HPP

#include "human.h"

class Banca: public Human{

public:
void printFirstCard();
};

#endif
30 changes: 30 additions & 0 deletions src/games/escambrit/headers/card.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef CARD_H
#define CARD_H

class Card{

private:
int number; // Card Number
char suit; // Card Suit
bool block; // Boolean value for Ace Switching

public:
// Default Constructor
Card();
// Parameterised Constructor (for initializing deck)
Card(int no, char s);
// Getter Functions
int getNumber();
char getSuit();
bool getBlock();
// Setter Functions
void setNumber(int no);
void setSuit(char c);
void setBlock(bool b);
// Printing Card Details
char getPrintNumber();
void printCardL1();
void printCardL2();
};

#endif
63 changes: 63 additions & 0 deletions src/games/escambrit/headers/color.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#ifndef COLOR_HPP
#define COLOR_HPP

#include <ostream>


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<int>(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
37 changes: 37 additions & 0 deletions src/games/escambrit/headers/compatible.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#ifdef _WIN32
#include <conio.h>

void clearscr(){
system("cls");
}

#else
#include <termios.h>
#include <unistd.h>

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
20 changes: 20 additions & 0 deletions src/games/escambrit/headers/deck.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef DECK_HPP
#define DECK_HPP

#include "card.h"
#include <vector>

class Deck{

private:
std::vector<Card> deck; // Deck (Vector) of Cards

public:
void initializeDeck();
void shuffleDeck();
int getSize();
Card deal();
Card* pick(short number);
};

#endif
1 change: 1 addition & 0 deletions src/games/escambrit/headers/escambrit.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "game.h"
37 changes: 37 additions & 0 deletions src/games/escambrit/headers/game.h
Original file line number Diff line number Diff line change
@@ -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 <string>

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
22 changes: 22 additions & 0 deletions src/games/escambrit/headers/human.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef HUMAN_HPP
#define HUMAN_HPP

#include "card.h"
#include <vector>

class Human{

protected:
std::vector<Card> hand;
int sum;

public:
Human();
int getSum();
void switchAce();
void addCard(Card c);
void clearCards();
void printCards();
};

#endif
29 changes: 29 additions & 0 deletions src/games/escambrit/headers/player.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef PLAYER_HPP
#define PLAYER_HPP

#include "human.h"
#include <string>

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
22 changes: 22 additions & 0 deletions src/games/escambrit/headers/print.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef PRINT_HPP
#define PRINT_HPP

#include <string>

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
39 changes: 39 additions & 0 deletions src/games/escambrit/headers/statistics.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#ifndef STATISTICS_HPP
#define STATISTICS_HPP

#include "player.h"
#include "color.h"
#include <string>

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
62 changes: 62 additions & 0 deletions src/games/escambrit/human.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include "headers/human.h"
#include <iostream>

// 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;i<hand.size();i++){
if(hand[i].getNumber()==1 && !(hand[i].getBlock())){
hand[i].setBlock(true);
sum-=10;
return;
}
}
}
}

// Adds card to Human's hand
void Human::addCard(Card c){
hand.push_back(c);
if(c.getNumber()>10){
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<hand.size();j++){
switch(i){
case 0: std::cout<<".------."; break;
case 1: std::cout<<"|"<<hand[j].getPrintNumber()<<".--. |"; break;
case 2: hand[j].printCardL1(); break;
case 3: hand[j].printCardL2(); break;
case 4: std::cout<<"| '--'"<<hand[j].getPrintNumber()<<"|"; break;
case 5: std::cout<<"`------'";
}
}
std::cout<<"\n";
}
}
75 changes: 75 additions & 0 deletions src/games/escambrit/player.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include "headers/player.h"
#include <iostream>

//////////////* 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;
}
150 changes: 150 additions & 0 deletions src/games/escambrit/print.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
#include "headers/print.h"
#include <iostream>
#include <sstream>

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();
}
137 changes: 137 additions & 0 deletions src/games/escambrit/statistics.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
#include "headers/statistics.h"
#include <iostream>
#include <iomanip>
#include <fstream>

//////////////* 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<<std::setw(maxlength+1)<<p[i].getName()<<"\t | \t"<<lightGreen<<"Cash: "<<std::setw(7)<<p[i].getCash()<<"\t | \t"<<yellow<<"Wins: "<<std::setw(5)<<p[i].getWins()<<"\t | \t"<<lightRed<<"Loses: "<<std::setw(5)<<p[i].getLoses()<<def<<"\n";
}
}

//////////////* File Handling *////

void Statistics::saveStats(){
std::fstream f1;
f1.open("data/statistics.bin", std::ios::out | std::ios::binary);
for(int i=0;i<3;i++){
std::string sName = p[i].getName();
int nameSize = sName.size();
int sCash = p[i].getCash();
int sWins = p[i].getWins();
int sLoses = p[i].getLoses();
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 Statistics::loadStats(){
std::fstream f1;
f1.open("data/statistics.bin", std::ios::in | std::ios::binary);
for(int i=0;i<3;i++){
std::string sName;
int nameSize;
int sCash;
int sWins;
int sLoses;
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));
p[i].setValues(sName, sCash, sWins, sLoses);
}
f1.close();
}