From 8d3b8a7668038981375770455e8f5ab08b3316b1 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Thu, 28 Dec 2023 19:40:06 +0100 Subject: [PATCH] fix init + fonction qui recupere une piece --- includes/Plateau.hpp | 3 +++ src/Butin/PlateauButin.cpp | 18 ++++++++++++------ src/Plateau.cpp | 11 +++++++++++ 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/includes/Plateau.hpp b/includes/Plateau.hpp index b2c7bfb..36dd251 100644 --- a/includes/Plateau.hpp +++ b/includes/Plateau.hpp @@ -27,6 +27,9 @@ public: // Fonction pour modifier le plateau void modifierPlateau(const int x, const int y, Piece *piece) const; + // Renvoie une pièce à une position + Piece *getPiece(const int x, const int y) const; + // Prend des coordonnées écran et renvoie des coordonnées jeu std::pair trouveCoordonnees(const int x, const int y) const; diff --git a/src/Butin/PlateauButin.cpp b/src/Butin/PlateauButin.cpp index 4b6e9af..d0d2245 100644 --- a/src/Butin/PlateauButin.cpp +++ b/src/Butin/PlateauButin.cpp @@ -6,20 +6,26 @@ PlateauButin::PlateauButin() : Plateau(8) {} -PlateauButin::~PlateauButin() {} +PlateauButin::~PlateauButin() { + for (int i = 0; i < taille; i++) { + for (int j = 0; j < taille; j++) { + delete plateau[i][j]; + } + } +} void PlateauButin::initialiserPlateau() { // Vecteur de toutes les pièeces du jeu - std::vector pieces; + std::vector pieces; for (int i = 0; i < 34; i++) { - pieces.push_back(PieceButin(PieceButin::Jaune)); + pieces.push_back(new PieceButin(PieceButin::Jaune)); } for (int i = 0; i < 20; i++) { - pieces.push_back(PieceButin(PieceButin::Rouge)); + pieces.push_back(new PieceButin(PieceButin::Rouge)); } for (int i = 0; i < 10; i++) { - pieces.push_back(PieceButin(PieceButin::Noire)); + pieces.push_back(new PieceButin(PieceButin::Noire)); } // Mélange le vecteur de pièces (j'ai jamais utilisé ça pour randomiser faudra @@ -32,7 +38,7 @@ void PlateauButin::initialiserPlateau() { int index = 0; for (int i = 0; i < taille; i++) { for (int j = 0; j < taille; j++) { - plateau[i][j] = &pieces[static_cast(index++)]; + plateau[i][j] = pieces[static_cast(index++)]; } } } diff --git a/src/Plateau.cpp b/src/Plateau.cpp index f201c7e..5b38113 100644 --- a/src/Plateau.cpp +++ b/src/Plateau.cpp @@ -66,12 +66,23 @@ void Plateau::afficherPlateau(std::ostream &out, const bool d) const { void Plateau::modifierPlateau(const int x, const int y, Piece *piece) const { if (x >= 0 && x < taille && y >= 0 && y < taille) { + if (piece == nullptr) { + delete plateau[x][y]; + } plateau[x][y] = piece; } else { throw std::invalid_argument("Coordonnées invalides"); } } +Piece *Plateau::getPiece(const int x, const int y) const { + if (x >= 0 && x < taille && y >= 0 && y < taille) { + return plateau[x][y]; + } else { + throw std::invalid_argument("Coordonnées invalides"); + } +} + std::pair Plateau::trouveCoordonnees(const int x, const int y) const { const float tailleCellule = static_cast(Ecran::largeur()) / taille;