ajout fonction chemin des pièces
This commit is contained in:
parent
19e27ce560
commit
35e5c5908e
2 changed files with 50 additions and 0 deletions
|
@ -1,6 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include "../Plateau.hpp"
|
||||
#include <vector>
|
||||
|
||||
class PlateauButin : public Plateau {
|
||||
public:
|
||||
|
@ -9,4 +10,7 @@ public:
|
|||
|
||||
// Initialise le plateau du Butin
|
||||
void initialiserPlateau() override;
|
||||
|
||||
// Renvoie la liste des pièces entre la pièce sélectionné et une position
|
||||
std::vector<Piece *> cheminPieces(const int destX, const int destY) const;
|
||||
};
|
||||
|
|
|
@ -45,3 +45,49 @@ void PlateauButin::initialiserPlateau() {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<Piece *> PlateauButin::cheminPieces(const int destX,
|
||||
const int destY) const {
|
||||
// Position départ
|
||||
std::pair<int, int> posSelection = selection->getPos();
|
||||
const int depX = posSelection.first;
|
||||
const int depY = posSelection.second;
|
||||
|
||||
// Distances
|
||||
const int deltaX = destX - depX;
|
||||
const int deltaY = destY - depY;
|
||||
|
||||
// Direction à prendre
|
||||
const int stepX = (deltaX > 0) ? 1 : -1;
|
||||
const int stepY = (deltaY > 0) ? 1 : -1;
|
||||
|
||||
std::vector<Piece *> chemin;
|
||||
|
||||
// Déplacement vertical
|
||||
if (deltaX == 0) {
|
||||
for (int dy = depY + stepY; dy != destY; dy += stepY) {
|
||||
chemin.push_back(plateau[depX][dy]);
|
||||
}
|
||||
}
|
||||
|
||||
// Déplacement horizontal
|
||||
else if (deltaY == 0) {
|
||||
for (int dx = depX + stepX; dx != destX; dx += stepX) {
|
||||
chemin.push_back(plateau[dx][depY]);
|
||||
}
|
||||
}
|
||||
|
||||
// Déplacement en diagonale
|
||||
else if (abs(deltaX) == abs(deltaY)) {
|
||||
for (int dx = depX + stepX, dy = depY + stepY; dx != destX;
|
||||
dx += stepX, dy += stepY) {
|
||||
chemin.push_back(plateau[dx][dy]);
|
||||
}
|
||||
}
|
||||
|
||||
// Retire les cases vides
|
||||
chemin.erase(std::remove(chemin.begin(), chemin.end(), nullptr),
|
||||
chemin.end());
|
||||
|
||||
return chemin;
|
||||
}
|
||||
|
|
Reference in a new issue