From adf9f747edecdbce404f18a71216e180b61d997d Mon Sep 17 00:00:00 2001 From: Mylloon Date: Thu, 14 Dec 2023 17:51:27 +0100 Subject: [PATCH] implement missing declaration (operator<<) --- includes/Plateau.hpp | 4 ++-- src/Butin/Butin.cpp | 6 ++++++ src/Dames/Dames.cpp | 6 ++++++ src/Joueur.cpp | 16 ++++++++++++++++ src/Mouvement.cpp | 6 ++++++ src/Piece.cpp | 5 +++++ src/Plateau.cpp | 17 +++++++++++------ src/Safari/Safari.cpp | 7 +++++++ src/main.cpp | 4 +++- 9 files changed, 62 insertions(+), 9 deletions(-) diff --git a/includes/Plateau.hpp b/includes/Plateau.hpp index afa142f..ebacd75 100644 --- a/includes/Plateau.hpp +++ b/includes/Plateau.hpp @@ -21,8 +21,8 @@ public: // comment initialiser la fonction autrement virtual void initialiserPlateau() = 0; - // Fonction pour afficher le plateau (selon le jeu) - void afficherPlateau(const bool debug = false) const; + // Fonction pour afficher le plateau (selon le jeu) vers une sortie + void afficherPlateau(std::ostream &, const bool debug = false) const; // Fonction pour modifier le plateau void modifierPlateau(const int x, const int y, Piece *piece) const; diff --git a/src/Butin/Butin.cpp b/src/Butin/Butin.cpp index d1752c7..7051644 100644 --- a/src/Butin/Butin.cpp +++ b/src/Butin/Butin.cpp @@ -21,4 +21,10 @@ const Butin &Butin::operator=(const Butin &src) { return *this; } +std::ostream &operator<<(std::ostream &out, const Butin &data) { + out << "j1: " << data.joueur1 << "\nj2: " << data.joueur2 << "\nPlateau:\n" + << data.plateau; + return out; +} + void Butin::init() { plateau.initialiserPlateau(); } diff --git a/src/Dames/Dames.cpp b/src/Dames/Dames.cpp index ca2de26..fc24da4 100644 --- a/src/Dames/Dames.cpp +++ b/src/Dames/Dames.cpp @@ -33,6 +33,12 @@ const Dames &Dames::operator=(const Dames &src) { return *this; } +std::ostream &operator<<(std::ostream &out, const Dames &data) { + out << "j1: " << data.joueur1 << "\nj2: " << data.joueur2 << "\nPlateau:\n" + << data.plateau; + return out; +} + void Dames::init() { plateau.initialiserPlateau(); } // TODO: A continuer diff --git a/src/Joueur.cpp b/src/Joueur.cpp index 4b42d9d..83d3e12 100644 --- a/src/Joueur.cpp +++ b/src/Joueur.cpp @@ -13,3 +13,19 @@ const Joueur &Joueur::operator=(const Joueur &src) { return *this; } + +std::ostream &operator<<(std::ostream &out, const Joueur &data) { + out << "nom: " << data.nom << "\npieces: "; + + if (data.pieces.empty()) { + out << "[]"; + } else { + out << "[\n"; + for (Piece *p : data.pieces) { + out << " " << *p << ",\n"; + } + out << ']'; + } + + return out; +} diff --git a/src/Mouvement.cpp b/src/Mouvement.cpp index 439eb74..b57e91e 100644 --- a/src/Mouvement.cpp +++ b/src/Mouvement.cpp @@ -13,3 +13,9 @@ const Mouvement &Mouvement::operator=(const Mouvement &src) { return *this; } + +std::ostream &operator<<(std::ostream &out, const Mouvement &data) { + out << "source: (" << data.sourceX << ", " << data.sourceY + << "), destination: (" << data.destX << ", " << data.destY << ")"; + return out; +} diff --git a/src/Piece.cpp b/src/Piece.cpp index 2b35759..983da7f 100644 --- a/src/Piece.cpp +++ b/src/Piece.cpp @@ -13,3 +13,8 @@ const Piece &Piece::operator=(const Piece &src) { return *this; } + +std::ostream &operator<<(std::ostream &out, const Piece &data) { + out << '"' << data.categorie << '"'; + return out; +} diff --git a/src/Plateau.cpp b/src/Plateau.cpp index 4b55f26..775f84a 100644 --- a/src/Plateau.cpp +++ b/src/Plateau.cpp @@ -18,7 +18,12 @@ Plateau::~Plateau() { delete[] plateau; } -void Plateau::afficherPlateau(const bool d) const { +std::ostream &operator<<(std::ostream &out, const Plateau &data) { + data.afficherPlateau(out, false); + return out; +} + +void Plateau::afficherPlateau(std::ostream &out, const bool d) const { const float tailleCellule = static_cast(Ecran::window.getSize().x) / taille; @@ -37,19 +42,19 @@ void Plateau::afficherPlateau(const bool d) const { // Position de la cellule cell.setPosition(x, y); if (d) { - std::cout << "(" << x << ", " << y; + out << "(" << x << ", " << y; } // Alternation des couleurs if ((i + j) % 2 == 0) { cell.setFillColor(sf::Color::White); if (d) { - std::cout << ", B), "; + out << ", B), "; } } else { cell.setFillColor(sf::Color::Black); if (d) { - std::cout << ", N), "; + out << ", N), "; } } @@ -57,11 +62,11 @@ void Plateau::afficherPlateau(const bool d) const { Ecran::window.draw(cell); } if (d) { - std::cout << "\n"; + out << "\n"; } } if (d) { - std::cout << "---\n"; + out << "---\n"; } } diff --git a/src/Safari/Safari.cpp b/src/Safari/Safari.cpp index 533fac6..ea4eb16 100644 --- a/src/Safari/Safari.cpp +++ b/src/Safari/Safari.cpp @@ -26,6 +26,13 @@ const Safari &Safari::operator=(const Safari &src) { return *this; } +std::ostream &operator<<(std::ostream &out, const Safari &data) { + out << "j1: " << data.joueur1 << "\nj2: " << data.joueur2 + << "\nj3: " << data.joueur3 << "\nPlateau:\n" + << data.plateau; + return out; +} + void Safari::init() { // On peut donner que 2 joueurs ? plateau.initialiserPlateau(); diff --git a/src/main.cpp b/src/main.cpp index d610979..9654d56 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,12 +1,14 @@ #include "../includes/Dames/PlateauDames.hpp" #include "../includes/Ecran.hpp" +void draw_debug(PlateauDames &p) { p.afficherPlateau(std::cout, true); } + int main() { Joueur j1; Joueur j2; PlateauDames p(j1, j2); Ecran e; - // e.afficher(std::bind(&Plateau::afficherPlateau, &p, true)); + // e.afficher(std::bind(&draw_debug, p)); return 0; }