This repository has been archived on 2024-01-18. You can view files and clone it, but cannot push or open issues or pull requests.
DamesEtCo/src/Plateau.cpp

79 lines
1.9 KiB
C++
Raw Normal View History

#include "../includes/Plateau.hpp"
2023-12-01 16:36:50 +01:00
#include "../includes/Ecran.hpp"
2023-11-26 17:53:37 +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++) {
plateau[i][j] = nullptr;
}
}
}
2023-11-24 20:34:22 +01:00
Plateau::~Plateau() {
for (int i = 0; i < taille; i++) {
delete[] plateau[i];
}
delete[] plateau;
}
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<float>(Ecran::largeur()) / taille;
2023-12-01 16:36:50 +01:00
2023-12-01 20:21:54 +01:00
// Adapte la vue pour le redimensionnement
const float tailleFenetre = taille * tailleCellule;
Ecran::window.setView(
2023-12-28 17:09:00 +01:00
sf::View(sf::FloatRect(0, 0, tailleFenetre, Ecran::window.getSize().y)));
2023-12-01 16:36:50 +01:00
2023-12-01 20:21:54 +01:00
// Cellule
sf::RectangleShape cell(sf::Vector2f(tailleCellule, tailleCellule));
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
// Position de la cellule
cell.setPosition(x, y);
if (d) {
out << "(" << x << ", " << y;
2023-12-01 16:36:50 +01:00
}
// Alternation des couleurs
if ((i + j) % 2 == 0) {
cell.setFillColor(sf::Color::White);
if (d) {
out << ", B), ";
2023-12-01 16:36:50 +01:00
}
} else {
cell.setFillColor(sf::Color::Black);
if (d) {
out << ", N), ";
2023-12-01 16:36:50 +01:00
}
}
// Dessine la cellule
Ecran::window.draw(cell);
}
if (d) {
out << "\n";
2023-12-01 16:36:50 +01:00
}
}
if (d) {
out << "---\n";
2023-12-01 16:36:50 +01:00
}
}
2023-11-24 20:14:05 +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) {
plateau[x][y] = piece;
} else {
throw std::invalid_argument("Coordonnées invalides");
}
}