diff --git a/includes/Dames/PlateauDames.hpp b/includes/Dames/PlateauDames.hpp index aa59c07..8239b6d 100644 --- a/includes/Dames/PlateauDames.hpp +++ b/includes/Dames/PlateauDames.hpp @@ -3,16 +3,22 @@ #include "../Joueur.hpp" #include "../Plateau.hpp" -struct PlateauDames final : public Plateau { +struct PlateauDames : public Plateau { PlateauDames(Joueur &joueur1, Joueur &joueur2); virtual ~PlateauDames(); Joueur *j1, *j2; + // Renvoie la pièce entre la pièce sélectionné et une position + Piece *piecePrise(const int destX, const int destY) const; + // Pareil que deplacementValide mais est utilisable avec des coordonnées bool deplacementValideCoor(const int x1, const int y1, const int x2, const int y2) const; // Vérifie que le déplacement est valide bool deplacementValide(const int destX, const int destY) const; + + // Vérifie que la prise est valide + bool priseValide(const int destX, const int destY) const; }; diff --git a/src/Dames/Dames.cpp b/src/Dames/Dames.cpp index a0fcd70..b6520c8 100644 --- a/src/Dames/Dames.cpp +++ b/src/Dames/Dames.cpp @@ -67,6 +67,19 @@ void Dames::event(const int x, const int y) { // Si une prise est possible, le joueur est obligé d'en faire une if (prisePossible(*joueurCourant)) { Ecran::printMessage("Vous devez forcément faire une prise."); + if (plateau.priseValide(posCurseur.first, posCurseur.second)) { + if (p == nullptr) { + plateau.moveSelection(posCurseur.first, posCurseur.second); + // On efface la piece prise du plateau + Piece *pieceEffacee = + plateau.piecePrise(posCurseur.first, posCurseur.second); + plateau.modifierPlateau(pieceEffacee->getPos().first, + pieceEffacee->getPos().second, nullptr); + // Donne la main au joueur suivant + changerJoueurCourant(); + Ecran::printMessage(msgTonTour()); + } + } break; // Quand il ne peux pas faire de prise, il déplace une de ses pieces } @@ -75,7 +88,7 @@ void Dames::event(const int x, const int y) { // Test si le déplacement est possible pour un pions : en diagonal en // direction de l'autre joueur // Crash ici parce que p = nullptr donc p->getPos() provoque une segfault - if (p==nullptr) { + if (p == nullptr) { plateau.moveSelection(posCurseur.first, posCurseur.second); // Donne la main au joueur suivant changerJoueurCourant(); @@ -126,56 +139,68 @@ bool Dames::prisePossible(Joueur &joueur) const { if (pos.first > 1) { pieceAPrendre = getPiece(plateau.getPiece(pos.first - 1, pos.second)); if (pieceAPrendre != nullptr && - pieceAPrendre->getCategory() != p->getCategory() && getPiece(plateau.getPiece(pos.first-2, pos.second))==nullptr) { + pieceAPrendre->getCategory() != p->getCategory() && + getPiece(plateau.getPiece(pos.first - 2, pos.second)) == nullptr) { return true; } } if (pos.first > 1 && pos.second > 1) { pieceAPrendre = getPiece(plateau.getPiece(pos.first - 1, pos.second - 1)); if (pieceAPrendre != nullptr && - pieceAPrendre->getCategory() != p->getCategory() && getPiece(plateau.getPiece(pos.first-2, pos.second-2))==nullptr) { + pieceAPrendre->getCategory() != p->getCategory() && + getPiece(plateau.getPiece(pos.first - 2, pos.second - 2)) == + nullptr) { return true; } } if (pos.first > 1 && pos.second < 8) { - pieceAPrendre = getPiece(plateau.getPiece(pos.first - 1, pos.second + 1) ); + pieceAPrendre = getPiece(plateau.getPiece(pos.first - 1, pos.second + 1)); if (pieceAPrendre != nullptr && - pieceAPrendre->getCategory() != p->getCategory() && getPiece(plateau.getPiece(pos.first-2, pos.second+2))==nullptr) { + pieceAPrendre->getCategory() != p->getCategory() && + getPiece(plateau.getPiece(pos.first - 2, pos.second + 2)) == + nullptr) { return true; } } if (pos.second > 1) { pieceAPrendre = getPiece(plateau.getPiece(pos.first, pos.second - 1)); if (pieceAPrendre != nullptr && - pieceAPrendre->getCategory() != p->getCategory() && getPiece(plateau.getPiece(pos.first, pos.second-2))==nullptr) { + pieceAPrendre->getCategory() != p->getCategory() && + getPiece(plateau.getPiece(pos.first, pos.second - 2)) == nullptr) { return true; } } if (pos.second < 8) { pieceAPrendre = getPiece(plateau.getPiece(pos.first, pos.second + 1)); if (pieceAPrendre != nullptr && - pieceAPrendre->getCategory() != p->getCategory() && getPiece(plateau.getPiece(pos.first, pos.second+2))==nullptr) { + pieceAPrendre->getCategory() != p->getCategory() && + getPiece(plateau.getPiece(pos.first, pos.second + 2)) == nullptr) { return true; } } if (pos.first < 8 && pos.second > 1) { pieceAPrendre = getPiece(plateau.getPiece(pos.first + 1, pos.second - 1)); if (pieceAPrendre != nullptr && - pieceAPrendre->getCategory() != p->getCategory() && getPiece(plateau.getPiece(pos.first+2, pos.second-2))==nullptr) { + pieceAPrendre->getCategory() != p->getCategory() && + getPiece(plateau.getPiece(pos.first + 2, pos.second - 2)) == + nullptr) { return true; } } if (pos.first < 8 && pos.second < 8) { - pieceAPrendre = getPiece(plateau.getPiece(pos.first + 1, pos.second + 1 )); + pieceAPrendre = getPiece(plateau.getPiece(pos.first + 1, pos.second + 1)); if (pieceAPrendre != nullptr && - pieceAPrendre->getCategory() != p->getCategory() && getPiece(plateau.getPiece(pos.first+2, pos.second+2))==nullptr) { + pieceAPrendre->getCategory() != p->getCategory() && + getPiece(plateau.getPiece(pos.first + 2, pos.second + 2)) == + nullptr) { return true; } } if (pos.first < 8) { pieceAPrendre = getPiece(plateau.getPiece(pos.first + 1, pos.second)); if (pieceAPrendre != nullptr && - pieceAPrendre->getCategory() != p->getCategory() && getPiece(plateau.getPiece(pos.first+2, pos.second))==nullptr) { + pieceAPrendre->getCategory() != p->getCategory() && + getPiece(plateau.getPiece(pos.first + 2, pos.second)) == nullptr) { return true; } } diff --git a/src/Dames/PlateauDames.cpp b/src/Dames/PlateauDames.cpp index 8171c12..b7ca016 100644 --- a/src/Dames/PlateauDames.cpp +++ b/src/Dames/PlateauDames.cpp @@ -25,20 +25,53 @@ PlateauDames::PlateauDames(Joueur &joueur1, Joueur &joueur2) PlateauDames::~PlateauDames() {} +Piece *PlateauDames::piecePrise(const int x, const int y) const { + const Position posInitiale = selection->getPos(); + if (x == posInitiale.first + 2 && y == posInitiale.second + 2) { + return plateau[x + 1][y + 1]; + } + if (x == posInitiale.first - 2 && y == posInitiale.second - 2) { + return plateau[x - 1][y - 1]; + } + if (x == posInitiale.first + 2 && y == posInitiale.second - 2) { + return plateau[x + 1][y - 1]; + } + if (x == posInitiale.first - 2 && y == posInitiale.second + 2) { + return plateau[x - 1][y + 1]; + } + return nullptr; +} + bool PlateauDames::deplacementValideCoor(const int x1, const int y1, const int x2, const int y2) const { - if((x2==x1+1 && y2==y1+1) || (x2==x1-1 && y2==y1-1) || (x2==x1+1 && y2==y1-1) || (x2==x1-1 && y2==y1+1)) - return true; + if ((x2 == x1 + 1 && y2 == y1 + 1) || (x2 == x1 - 1 && y2 == y1 - 1) || + (x2 == x1 + 1 && y2 == y1 - 1) || (x2 == x1 - 1 && y2 == y1 + 1)) { + return true; + } return false; } -// Peut-etre utiliser cette fonction pour prisePossible ? bool PlateauDames::deplacementValide(const int x, const int y) const { if (!selection) { return false; } - // TODO Position posInitiale = selection->getPos(); return deplacementValideCoor(posInitiale.first, posInitiale.second, x, y); } + +bool PlateauDames::priseValide(const int x, const int y) const { + if (!selection) { + return false; + } + + Position posInitiale = selection->getPos(); + if ((x == posInitiale.first + 2 && y == posInitiale.second + 2) || + (x == posInitiale.first - 2 && y == posInitiale.second - 2) || + (x == posInitiale.first + 2 && y == posInitiale.second - 2) || + (x == posInitiale.first - 2 && y == posInitiale.second + 2)) { + + return true; + } + return false; +}