From baef0fce5cdaf449e03a0f2a0483a7c28a81d0dc Mon Sep 17 00:00:00 2001 From: Mylloon Date: Sat, 6 Jan 2024 15:26:03 +0100 Subject: [PATCH] butin now done: * calcul points butin * players CANT be const im crazy * plateau cant return list of pieces * joueurCourant is a pointer to a real player * always show score but final score take in account the malus --- includes/Butin/Butin.hpp | 12 ++++++-- includes/Jeu.hpp | 8 ++--- includes/Plateau.hpp | 4 +++ includes/Safari/Safari.hpp | 6 ++-- src/Butin/Butin.cpp | 62 +++++++++++++++++++++++++++++++------- src/Dames/Dames.cpp | 8 ++--- src/Jeu.cpp | 4 ++- src/Joueur.cpp | 7 +++++ src/Plateau.cpp | 13 ++++++++ src/Safari/Safari.cpp | 6 ++-- src/main.cpp | 4 +-- 11 files changed, 103 insertions(+), 31 deletions(-) diff --git a/includes/Butin/Butin.hpp b/includes/Butin/Butin.hpp index 070c106..7fe4e59 100644 --- a/includes/Butin/Butin.hpp +++ b/includes/Butin/Butin.hpp @@ -12,7 +12,7 @@ class Butin : private Jeu { PlateauButin plateau; // Joueurs - const Joueur &joueur2; + Joueur &joueur2; // Etape du jeu, pour savoir où on en est enum Etape { @@ -34,6 +34,9 @@ class Butin : private Jeu { // Message à chaque tour du joueur const std::string msgTonTour() const; + // Message affichant le nombre de points + const std::string msgPoints(const std::pair points) const; + // Position curseur const std::pair getPosition() const override; @@ -41,8 +44,8 @@ class Butin : private Jeu { void changerJoueurCourant(); public: - Butin(const Joueur &joueur1, const Joueur &joueur2); // constructor - virtual ~Butin(); // destructor + Butin(Joueur &joueur1, Joueur &joueur2); // constructor + virtual ~Butin(); // destructor // Fonction d'initialisation du jeu void init() override; @@ -52,4 +55,7 @@ public: // Fonction d'évènement void event(const int x, const int y) override; + + // Renvoie le nombre de points des joueurs 1 et 2 + std::pair calculpoints(const bool finPartie = false) const; }; diff --git a/includes/Jeu.hpp b/includes/Jeu.hpp index 8fb6113..17e30bb 100644 --- a/includes/Jeu.hpp +++ b/includes/Jeu.hpp @@ -4,8 +4,8 @@ struct Jeu { // Le joueur 1 est toujours celui qui commence - Jeu(const Joueur &j1); // constructor - virtual ~Jeu(); // destructor + Jeu(Joueur &j1); // constructor + virtual ~Jeu(); // destructor // Fonction d'initialisation d'un jeu virtual void init() = 0; @@ -18,8 +18,8 @@ struct Jeu { protected: // Joueurs, au moins un joueur - const Joueur &joueur1; - Joueur joueurCourant; + Joueur &joueur1; + Joueur *joueurCourant; // Position du dernier clic du curseur sur l'écran std::pair posCurseur; diff --git a/includes/Plateau.hpp b/includes/Plateau.hpp index 54d9a38..f21b722 100644 --- a/includes/Plateau.hpp +++ b/includes/Plateau.hpp @@ -1,6 +1,7 @@ #pragma once #include "../includes/Piece.hpp" +#include class Plateau { friend std::ostream &operator<<(std::ostream &, const Plateau &); @@ -46,4 +47,7 @@ public: // Change la pièce selectionnée void modifierSelection(const int x, const int y); + + // Renvoie la liste des pièces du plateau + std::vector getPieces() const; }; diff --git a/includes/Safari/Safari.hpp b/includes/Safari/Safari.hpp index 0ef9a41..d94c45e 100644 --- a/includes/Safari/Safari.hpp +++ b/includes/Safari/Safari.hpp @@ -18,9 +18,9 @@ class Safari : private Jeu { std::vector barrieres; public: - Safari(const Joueur &joueur1, const Joueur &joueur2, - const Joueur *joueur3 = nullptr); // constructor - virtual ~Safari(); // destructor + Safari(Joueur &joueur1, Joueur &joueur2, + Joueur *joueur3 = nullptr); // constructor + virtual ~Safari(); // destructor // Fonction d'initialisation du jeu void init() override; diff --git a/src/Butin/Butin.cpp b/src/Butin/Butin.cpp index f8e2c08..9a10dde 100644 --- a/src/Butin/Butin.cpp +++ b/src/Butin/Butin.cpp @@ -2,7 +2,7 @@ #include "../../includes/Butin/PieceButin.hpp" #include "../../includes/Ecran.hpp" -Butin::Butin(const Joueur &j1, const Joueur &j2) +Butin::Butin(Joueur &j1, Joueur &j2) : Jeu(j1), plateau(PlateauButin()), joueur2{j2} { init(); } @@ -20,7 +20,7 @@ const PieceButin *Butin::getPiece(const int x, const int y) const { } const std::string Butin::msgPieceJaune(const bool erreur) const { - std::string res = "Joueur " + std::to_string(joueurCourant.getNum()) + + std::string res = "Joueur " + std::to_string(joueurCourant->getNum()) + ", retirez une piece jaune en cliquant dessus."; if (erreur) { @@ -31,8 +31,13 @@ const std::string Butin::msgPieceJaune(const bool erreur) const { } const std::string Butin::msgTonTour() const { - return "Joueur " + std::to_string(joueurCourant.getNum()) + - ", c'est votre tour."; + return "Joueur " + std::to_string(joueurCourant->getNum()) + + ", c'est votre tour. " + msgPoints(calculpoints()); +} + +const std::string Butin::msgPoints(const std::pair points) const { + return "J1 : " + std::to_string(points.first) + " vs " + + std::to_string(points.second) + " : J2"; } void Butin::init() { @@ -131,15 +136,13 @@ void Butin::event(const int x, const int y) { plateau.modifierPlateau(pos.first, pos.second, nullptr); // Ajoute le point au joueur - joueurCourant.ajoutPiece(gain); + joueurCourant->ajoutPiece(gain); } // Vérification partie terminé = si plus aucun coup n'est possible if (!plateau.coupsPossible()) { etape = Fini; - Ecran::printMessage("Partie fini ! Joueur " + - std::to_string(joueurCourant.getNum()) + - " remporte la victoire !"); + Ecran::printMessage("Partie fini ! " + msgPoints(calculpoints(true))); } else { // On donne la main à l'adversaire changerJoueurCourant(); @@ -171,9 +174,46 @@ const std::pair Butin::getPosition() const { } void Butin::changerJoueurCourant() { - if (joueurCourant.getNum() == joueur1.getNum()) { - joueurCourant = joueur2; + if (joueurCourant->getNum() == joueur1.getNum()) { + joueurCourant = &joueur2; } else { - joueurCourant = joueur1; + joueurCourant = &joueur1; } } + +std::pair Butin::calculpoints(const bool end) const { + // Nombre de points des joueurs + int j1 = 0, j2 = 0; + for (const Piece *it : joueur1.getPieces()) { + j1 += dynamic_cast(it)->getPoints(); + } + for (const Piece *it : joueur2.getPieces()) { + j2 += dynamic_cast(it)->getPoints(); + } + + // Si c'est le calcul des points de la fin de partie + if (end) { + // Nombre de points sur le plateau restant + int malus = 0; + for (const Piece *it : plateau.getPieces()) { + malus += dynamic_cast(it)->getPoints(); + } + + // C'est le joueur qui a joué le dernier coup qui reçoit le malus + if (joueurCourant->getNum() == joueur1.getNum()) { + // Joueur 1 reçoit le malus + j1 -= malus; + if (j1 < 0) { + j1 = 0; + } + } else { + // Joueur 2 reçoit le malus + j2 -= malus; + if (j2 < 0) { + j2 = 0; + } + } + } + + return std::make_pair(j1, j2); +} diff --git a/src/Dames/Dames.cpp b/src/Dames/Dames.cpp index 8a899ef..bb217ce 100644 --- a/src/Dames/Dames.cpp +++ b/src/Dames/Dames.cpp @@ -6,11 +6,11 @@ Dames::Dames(Joueur &j1, Joueur &j2) std::srand(static_cast(time(0))); const int r = std::rand() % 2; if (r == 0) { - joueurCourant = j1; + joueurCourant = &j1; } else { - joueurCourant = j2; + joueurCourant = &j2; } - std::cout << "Joueur " << joueurCourant.getNum() + std::cout << "Joueur " << joueurCourant->getNum() << " jouera avec les pièces blanches et commencera la partie." << std::endl; @@ -60,7 +60,7 @@ bool Dames::prisePossible(Joueur &joueur) const { throw std::runtime_error("Cette pièce est.. étrange."); } if (!p->getDame()) { - //if(plateau.getPiece(pos.first-1, pos.second) == nullptr) + // if(plateau.getPiece(pos.first-1, pos.second) == nullptr) } } return false; diff --git a/src/Jeu.cpp b/src/Jeu.cpp index 12858b5..f14b7a8 100644 --- a/src/Jeu.cpp +++ b/src/Jeu.cpp @@ -1,5 +1,7 @@ #include "../includes/Jeu.hpp" -Jeu::Jeu(const Joueur &j1) : joueur1(j1), joueurCourant(Joueur(j1)) {} +Jeu::Jeu(Joueur &j1) : joueur1(j1) { + joueurCourant = &j1; +} Jeu::~Jeu() {} diff --git a/src/Joueur.cpp b/src/Joueur.cpp index 325faf8..302f207 100644 --- a/src/Joueur.cpp +++ b/src/Joueur.cpp @@ -15,6 +15,13 @@ const Joueur &Joueur::operator=(const Joueur &src) { return *this; } + std::cout << "internally :\n"; + std::cout << "id : " << id << "\n"; + std::cout << "pieces(len) : " << pieces.size() << "\n"; + std::cout << "\nwill be replaced by :\n"; + std::cout << "id : " << src.id << "\n"; + std::cout << "pieces(len) : " << src.pieces.size() << "\n"; + id = src.id; pieces = src.pieces; diff --git a/src/Plateau.cpp b/src/Plateau.cpp index 7cb1bcf..414fede 100644 --- a/src/Plateau.cpp +++ b/src/Plateau.cpp @@ -176,3 +176,16 @@ const std::pair Plateau::moveSelection(const int x, return ancienneCoordonnees; } + +std::vector Plateau::getPieces() const { + std::vector pieces; + for (int i = 0; i < taille; i++) { + for (int j = 0; j < taille; j++) { + if (plateau[i][j]) { + pieces.push_back(plateau[i][j]); + } + } + } + + return pieces; +} diff --git a/src/Safari/Safari.cpp b/src/Safari/Safari.cpp index 3972c94..2e96c6b 100644 --- a/src/Safari/Safari.cpp +++ b/src/Safari/Safari.cpp @@ -3,7 +3,7 @@ /* Contrairement aux autres jeux ici on donne des pointeurs et pas des * références vu que j3 peut ne pas exister (default to nullptr) * Je sais pas trop si c'est une bonne idée, à méditer */ -Safari::Safari(const Joueur &j1, const Joueur &j2, const Joueur *j3) +Safari::Safari(Joueur &j1, Joueur &j2, Joueur *j3) : Jeu(j1), plateau(PlateauSafari()), joueur2{j2}, joueur3{j3} { for (int i = 0; i < 50; i++) { // TODO: Les pièces ont des positions invalides, il faut penser à les @@ -49,10 +49,10 @@ void Safari::choixAnimal(const PieceSafari::Categorie animal) { throw std::invalid_argument("Animal non valide"); } - if (joueurCourant.getPieces().empty()) { + if (joueurCourant->getPieces().empty()) { for (int i = 0; i < 3; i++) { // Position invalide en attendant d'être ajouté au plateau de jeu - joueurCourant.ajoutPiece(new PieceSafari(animal, -1, -1)); + joueurCourant->ajoutPiece(new PieceSafari(animal, -1, -1)); } } } diff --git a/src/main.cpp b/src/main.cpp index b458c81..ebad96f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -35,7 +35,7 @@ int main(int argc, char const *argv[]) { } else if (arg.compare("butin") == 0) { - const Joueur j2(2); + Joueur j2(2); Butin b(j1, j2); b.init(); @@ -50,7 +50,7 @@ int main(int argc, char const *argv[]) { } else if (arg.compare("safari") == 0) { - const Joueur j2(2); + Joueur j2(2); Joueur *j3 = nullptr; // TODO: demander à l'utilisateur un 3e joueur {}