2023-11-12 16:05:42 +01:00
|
|
|
#include "../includes/Plateau.hpp"
|
2023-12-01 16:36:50 +01:00
|
|
|
#include "../includes/Ecran.hpp"
|
2023-11-26 17:53:37 +01:00
|
|
|
|
2023-12-14 17:09:27 +01:00
|
|
|
Plateau::Plateau(const int t) : plateau(new Piece **[t]), taille(t) {
|
2023-11-24 20:34:22 +01:00
|
|
|
// Création du plateau vide
|
2023-11-21 18:07:13 +01:00
|
|
|
for (int i = 0; i < t; i++) {
|
|
|
|
plateau[i] = new Piece *[t];
|
|
|
|
for (int j = 0; j < t; j++) {
|
2023-11-21 12:33:04 +01:00
|
|
|
plateau[i][j] = nullptr;
|
|
|
|
}
|
|
|
|
}
|
2023-12-29 01:33:35 +01:00
|
|
|
selection = nullptr;
|
2023-11-21 12:33:04 +01:00
|
|
|
}
|
2023-11-12 16:05:42 +01:00
|
|
|
|
2023-11-24 20:34:22 +01:00
|
|
|
Plateau::~Plateau() {
|
|
|
|
for (int i = 0; i < taille; i++) {
|
|
|
|
delete[] plateau[i];
|
|
|
|
}
|
|
|
|
delete[] plateau;
|
|
|
|
}
|
2023-11-12 16:05:42 +01:00
|
|
|
|
2023-12-14 17:51:27 +01:00
|
|
|
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 {
|
2023-12-28 16:12:41 +01:00
|
|
|
const float tailleCellule = static_cast<float>(Ecran::largeur()) / taille;
|
2023-12-01 16:36:50 +01:00
|
|
|
|
2023-12-28 22:05:00 +01:00
|
|
|
const float decalagePiece = tailleCellule / 6;
|
|
|
|
|
2023-12-01 20:21:54 +01:00
|
|
|
// Cellule
|
|
|
|
sf::RectangleShape cell(sf::Vector2f(tailleCellule, tailleCellule));
|
2023-12-28 22:05:00 +01:00
|
|
|
|
|
|
|
// Pièce
|
|
|
|
sf::CircleShape piece(tailleCellule / 3);
|
|
|
|
piece.setOutlineThickness(2.);
|
|
|
|
|
2023-12-01 16:36:50 +01:00
|
|
|
for (int i = 0; i < taille; i++) {
|
|
|
|
for (int j = 0; j < taille; j++) {
|
2023-12-01 20:22:56 +01:00
|
|
|
const float x = i * tailleCellule;
|
|
|
|
const float y = j * tailleCellule;
|
2023-12-01 16:36:50 +01:00
|
|
|
|
2023-12-28 22:05:00 +01:00
|
|
|
// Position de la cellule et de la pièce
|
2023-12-01 16:36:50 +01:00
|
|
|
cell.setPosition(x, y);
|
2023-12-28 22:05:00 +01:00
|
|
|
piece.setPosition(x + decalagePiece, y + decalagePiece);
|
2023-12-01 16:36:50 +01:00
|
|
|
if (d) {
|
2023-12-14 17:51:27 +01:00
|
|
|
out << "(" << x << ", " << y;
|
2023-12-01 16:36:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Alternation des couleurs
|
|
|
|
if ((i + j) % 2 == 0) {
|
|
|
|
cell.setFillColor(sf::Color::White);
|
2023-12-28 22:05:00 +01:00
|
|
|
piece.setOutlineColor(sf::Color::Black);
|
2023-12-01 16:36:50 +01:00
|
|
|
if (d) {
|
2023-12-14 17:51:27 +01:00
|
|
|
out << ", B), ";
|
2023-12-01 16:36:50 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cell.setFillColor(sf::Color::Black);
|
2023-12-28 22:05:00 +01:00
|
|
|
piece.setOutlineColor(sf::Color::White);
|
2023-12-01 16:36:50 +01:00
|
|
|
if (d) {
|
2023-12-14 17:51:27 +01:00
|
|
|
out << ", N), ";
|
2023-12-01 16:36:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dessine la cellule
|
|
|
|
Ecran::window.draw(cell);
|
2023-12-28 22:05:00 +01:00
|
|
|
|
|
|
|
// Dessine la pièce
|
|
|
|
Piece *p = plateau[i][j];
|
|
|
|
if (p != nullptr) {
|
2023-12-29 01:33:35 +01:00
|
|
|
if (p->isSelectionnee()) {
|
|
|
|
piece.setOutlineColor(sf::Color::Green);
|
|
|
|
}
|
2023-12-28 22:05:00 +01:00
|
|
|
piece.setFillColor(p->getScreenColor());
|
|
|
|
Ecran::window.draw(piece);
|
|
|
|
}
|
2023-12-01 16:36:50 +01:00
|
|
|
}
|
|
|
|
if (d) {
|
2023-12-14 17:51:27 +01:00
|
|
|
out << "\n";
|
2023-12-01 16:36:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (d) {
|
2023-12-14 17:51:27 +01:00
|
|
|
out << "---\n";
|
2023-12-01 16:36:50 +01:00
|
|
|
}
|
|
|
|
}
|
2023-11-24 20:14:05 +01:00
|
|
|
|
2023-12-14 17:09:27 +01:00
|
|
|
void Plateau::modifierPlateau(const int x, const int y, Piece *piece) const {
|
2023-11-24 20:14:05 +01:00
|
|
|
if (x >= 0 && x < taille && y >= 0 && y < taille) {
|
2023-12-28 19:40:06 +01:00
|
|
|
if (piece == nullptr) {
|
|
|
|
delete plateau[x][y];
|
|
|
|
}
|
2023-11-24 20:14:05 +01:00
|
|
|
plateau[x][y] = piece;
|
|
|
|
} else {
|
|
|
|
throw std::invalid_argument("Coordonnées invalides");
|
|
|
|
}
|
|
|
|
}
|
2023-12-28 18:01:50 +01:00
|
|
|
|
2023-12-28 19:40:06 +01:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-28 18:01:50 +01:00
|
|
|
std::pair<int, int> Plateau::trouveCoordonnees(const int x, const int y) const {
|
|
|
|
const float tailleCellule = static_cast<float>(Ecran::largeur()) / taille;
|
|
|
|
|
|
|
|
int xPlateau = static_cast<int>(x / tailleCellule);
|
|
|
|
int yPlateau = static_cast<int>(y / tailleCellule);
|
|
|
|
return std::make_pair(xPlateau, yPlateau);
|
|
|
|
}
|
2023-12-28 18:26:52 +01:00
|
|
|
|
|
|
|
int Plateau::getTaille() const {
|
|
|
|
return taille;
|
|
|
|
}
|
2023-12-29 01:33:35 +01:00
|
|
|
|
|
|
|
void Plateau::modifierSelection(const int x, const int y) {
|
2023-12-29 01:39:08 +01:00
|
|
|
Piece *p = getPiece(x, y);
|
|
|
|
if (p == nullptr) {
|
|
|
|
// Si rien est selectionné on ne fait rien
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-12-29 01:33:35 +01:00
|
|
|
if (selection) {
|
|
|
|
// Déselectionne l'ancienne sélection
|
|
|
|
selection->changeSelection();
|
|
|
|
}
|
|
|
|
|
2023-12-29 01:39:08 +01:00
|
|
|
if (p != selection) {
|
2023-12-29 01:33:35 +01:00
|
|
|
// Si la sélection à changer alors changer l'état de la nouvelle pièce
|
|
|
|
p->changeSelection();
|
|
|
|
selection = p;
|
2023-12-29 02:09:01 +01:00
|
|
|
} else {
|
|
|
|
// Si c'était la même alors on ne sélectionne plus rien
|
|
|
|
selection = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-29 02:19:06 +01:00
|
|
|
std::pair<int, int> Plateau::moveSelection(const int x, const int y) {
|
2023-12-29 02:09:01 +01:00
|
|
|
if (selection == nullptr) {
|
|
|
|
// Ne fais rien si on a rien a bouger
|
2023-12-29 02:19:06 +01:00
|
|
|
return std::make_pair(-1, -1);
|
2023-12-29 01:33:35 +01:00
|
|
|
}
|
2023-12-29 02:09:01 +01:00
|
|
|
|
2023-12-29 02:19:06 +01:00
|
|
|
// Récupère les coordonnées
|
|
|
|
std::pair<int, int> ancienneCoordonnees =
|
|
|
|
std::make_pair(selection->x, selection->y);
|
|
|
|
|
2023-12-29 02:09:01 +01:00
|
|
|
// Retire la pièce de là où elle est pour le plateau
|
2023-12-29 02:19:06 +01:00
|
|
|
plateau[ancienneCoordonnees.first][ancienneCoordonnees.second] = nullptr;
|
2023-12-29 02:09:01 +01:00
|
|
|
|
|
|
|
// Déplace la pièce sur le plateau
|
|
|
|
modifierPlateau(x, y, selection);
|
|
|
|
|
|
|
|
// Met à jour les coordonnées de la pièce
|
|
|
|
selection->moveTo(x, y);
|
|
|
|
|
|
|
|
// Déselectionne la pièce
|
|
|
|
modifierSelection(x, y);
|
2023-12-29 02:19:06 +01:00
|
|
|
|
|
|
|
return ancienneCoordonnees;
|
2023-12-29 01:33:35 +01:00
|
|
|
}
|