* add const identifiers
* expose category for children
* documentation
* helper for ignoring arguments
* reduce useless imports
* use enum for categories
* refactor Dames instanciation
This commit is contained in:
Mylloon 2023-12-14 17:09:27 +01:00
parent 1beaf6bedd
commit bf74632047
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
22 changed files with 155 additions and 72 deletions

View file

@ -1,8 +1,5 @@
#pragma once #pragma once
#include <iostream>
#include "../Joueur.hpp"
#include "PlateauButin.hpp" #include "PlateauButin.hpp"
class Butin { class Butin {
@ -22,6 +19,7 @@ public:
Butin(const Butin &); // copy constructor Butin(const Butin &); // copy constructor
const Butin &operator=(const Butin &); // copy assignement const Butin &operator=(const Butin &); // copy assignement
};
// Fonction d'initialisation du jeu // Fonction d'initialisation du jeu
void init();
};

View file

@ -2,10 +2,18 @@
#include "../Piece.hpp" #include "../Piece.hpp"
class PieceButin : public Piece { struct PieceButin : public Piece {
enum Categorie {
Jaune = 1,
Rouge = 2,
Noire = 3,
};
PieceButin(const enum Categorie);
virtual ~PieceButin();
private:
int points; int points;
public: std::string to_string(const enum Categorie) const;
PieceButin(std::string categorie);
virtual ~PieceButin();
}; };

View file

@ -23,10 +23,11 @@ public:
Dames(const Dames &); // copy constructor Dames(const Dames &); // copy constructor
const Dames &operator=(const Dames &); // copy assignement const Dames &operator=(const Dames &); // copy assignement
// Vérifie si une prise est possible pour une pièce donnée
// J'ai des erreurs depuis que j'ai voulu faire cette fonction et je comprends
// pas pourquoi
bool prisePossible(Piece *piece);
// Fonction d'initialisation du jeu // Fonction d'initialisation du jeu
void init();
// Vérifie si une prise est possible pour une pièce donnée
// TODO: J'ai des erreurs depuis que j'ai voulu faire cette fonction
// et je comprends pas pourquoi
bool prisePossible(const Piece *piece) const;
}; };

View file

@ -2,11 +2,18 @@
#include "../Piece.hpp" #include "../Piece.hpp"
class PieceDames : public Piece { struct PieceDames : public Piece {
enum Categorie {
Blanche,
Noire,
};
PieceDames(const enum Categorie);
virtual ~PieceDames();
private:
// True si la piece est une dame // True si la piece est une dame
bool dame; bool dame;
public: std::string to_string(const enum Categorie) const;
PieceDames(std::string categorie);
virtual ~PieceDames();
}; };

View file

@ -11,5 +11,5 @@ struct Ecran {
const std::string name = "Projet"); // constructor const std::string name = "Projet"); // constructor
~Ecran(); // destructor ~Ecran(); // destructor
void afficher(std::function<void()> f = {[]() {}}); void afficher(const std::function<void()> f = {[]() {}}) const;
}; };

View file

@ -2,6 +2,10 @@
#include <iostream> #include <iostream>
/* Est ce que cette classe c'est pas mieux si on la déplace à l'intérieur de la
* classe Piece qui aurait une méthode "moveTo" par exemple et qui se
* déplacerait vers une case ? */
class Mouvement { class Mouvement {
friend std::ostream &operator<<(std::ostream &, const Mouvement &); friend std::ostream &operator<<(std::ostream &, const Mouvement &);

View file

@ -5,6 +5,7 @@
class Piece { class Piece {
friend std::ostream &operator<<(std::ostream &, const Piece &); friend std::ostream &operator<<(std::ostream &, const Piece &);
protected:
// Couleur de la pièce ou type d'animal pour Safari par ex // Couleur de la pièce ou type d'animal pour Safari par ex
std::string categorie; std::string categorie;

View file

@ -13,8 +13,8 @@ protected:
int taille; int taille;
public: public:
Plateau(int taille); // constructor Plateau(const int taille); // constructor
virtual ~Plateau(); // destructor virtual ~Plateau(); // destructor
// Fonction pour initialiser le plateau (selon le jeu) // Fonction pour initialiser le plateau (selon le jeu)
// Seulement deux joueurs pour le jeu de dame uniquement, je suis pas sûre de // Seulement deux joueurs pour le jeu de dame uniquement, je suis pas sûre de
@ -22,8 +22,8 @@ public:
virtual void initialiserPlateau(Joueur &j1, Joueur &j2) = 0; virtual void initialiserPlateau(Joueur &j1, Joueur &j2) = 0;
// Fonction pour afficher le plateau (selon le jeu) // Fonction pour afficher le plateau (selon le jeu)
void afficherPlateau(const bool debug = false); void afficherPlateau(const bool debug = false) const;
// Fonction pour modifier le plateau // Fonction pour modifier le plateau
void modifierPlateau(int x, int y, Piece *piece); void modifierPlateau(const int x, const int y, Piece *piece) const;
}; };

View file

@ -2,8 +2,17 @@
#include "../Piece.hpp" #include "../Piece.hpp"
class PieceSafari : public Piece { struct PieceSafari : public Piece {
public: enum Categorie {
PieceSafari(std::string categorie); Barriere,
Elephant,
Rhinoceros,
Lion,
};
PieceSafari(const enum Categorie);
virtual ~PieceSafari(); virtual ~PieceSafari();
private:
std::string to_string(const enum Categorie) const;
}; };

View file

@ -12,15 +12,15 @@ class Safari {
// Plateau de jeu // Plateau de jeu
PlateauSafari plateau; PlateauSafari plateau;
// Barrières
std::vector<PieceSafari *> barrieres;
// Joueurs // Joueurs
Joueur &joueur1; Joueur &joueur1;
Joueur &joueur2; Joueur &joueur2;
Joueur &joueur3; Joueur &joueur3;
Joueur &joueurCourant; Joueur &joueurCourant;
// Barrières
std::vector<PieceSafari *> barrieres;
public: public:
Safari(Joueur &joueur1, Joueur &joueur2, Joueur &joueur3); // constructor Safari(Joueur &joueur1, Joueur &joueur2, Joueur &joueur3); // constructor
virtual ~Safari(); // destructor virtual ~Safari(); // destructor
@ -29,5 +29,8 @@ public:
const Safari &operator=(const Safari &); // copy assignement const Safari &operator=(const Safari &); // copy assignement
// Fonction d'initialisation du jeu // Fonction d'initialisation du jeu
void choixAnimal(std::string animal); void init();
// Fonction d'initialisation du jeu
void choixAnimal(const PieceSafari::Categorie animal) const;
}; };

4
includes/utils.hpp Normal file
View file

@ -0,0 +1,4 @@
#pragma once
template <typename T>
void ignore(T &&) {}

View file

@ -2,7 +2,7 @@
Butin::Butin(Joueur &j1, Joueur &j2) Butin::Butin(Joueur &j1, Joueur &j2)
: plateau(PlateauButin()), joueur1{j1}, joueur2{j2}, joueurCourant{j1} { : plateau(PlateauButin()), joueur1{j1}, joueur2{j2}, joueurCourant{j1} {
plateau.initialiserPlateau(j1, j2); init();
} }
Butin::~Butin() {} Butin::~Butin() {}
@ -10,7 +10,7 @@ Butin::~Butin() {}
Butin::Butin(const Butin &src) Butin::Butin(const Butin &src)
: plateau(PlateauButin()), joueur1{src.joueur1}, joueur2{src.joueur2}, : plateau(PlateauButin()), joueur1{src.joueur1}, joueur2{src.joueur2},
joueurCourant{src.joueurCourant} { joueurCourant{src.joueurCourant} {
plateau.initialiserPlateau(joueur1, joueur2); init();
} }
const Butin &Butin::operator=(const Butin &src) { const Butin &Butin::operator=(const Butin &src) {
@ -20,3 +20,5 @@ const Butin &Butin::operator=(const Butin &src) {
return *this; return *this;
} }
void Butin::init() { plateau.initialiserPlateau(joueur1, joueur2); }

View file

@ -1,16 +1,23 @@
#include "../../includes/Butin/PieceButin.hpp" #include "../../includes/Butin/PieceButin.hpp"
PieceButin::PieceButin(const std::string cat) : Piece(cat) { PieceButin::PieceButin(const enum Categorie cat)
: Piece(to_string(cat)), points(cat) {
std::cout << "pièce - " << cat << "\n"; std::cout << "pièce - " << cat << "\n";
if (cat == "jaune") { this->points = cat;
this->points = 1;
}
if (cat == "rouge") {
this->points = 2;
}
if (cat == "noire") {
this->points = 3;
}
} }
PieceButin::~PieceButin() {} PieceButin::~PieceButin() {}
std::string PieceButin::to_string(const enum Categorie cat) const {
switch (cat) {
case Jaune:
return "Jaune";
case Rouge:
return "Rouge";
case Noire:
return "Noire";
default:
exit(EXIT_FAILURE);
}
}

View file

@ -1,5 +1,6 @@
#include "../../includes/Butin/PlateauButin.hpp" #include "../../includes/Butin/PlateauButin.hpp"
#include "../../includes/Butin/PieceButin.hpp" #include "../../includes/Butin/PieceButin.hpp"
#include "../../includes/utils.hpp"
#include <algorithm> #include <algorithm>
#include <random> #include <random>
@ -8,19 +9,20 @@ PlateauButin::PlateauButin() : Plateau(8) {}
PlateauButin::~PlateauButin() {} PlateauButin::~PlateauButin() {}
// On utilise pas les arguments ici, à voir si il faut vraiment faire un
// override
void PlateauButin::initialiserPlateau(Joueur &j1, Joueur &j2) { void PlateauButin::initialiserPlateau(Joueur &j1, Joueur &j2) {
ignore(j1);
ignore(j2);
// Vecteur de toutes les pièeces du jeu // Vecteur de toutes les pièeces du jeu
std::vector<PieceButin> pieces; std::vector<PieceButin> pieces;
for (int i = 0; i < 34; i++) { for (int i = 0; i < 34; i++) {
pieces.push_back(PieceButin("jaune")); pieces.push_back(PieceButin(PieceButin::Jaune));
} }
for (int i = 0; i < 20; i++) { for (int i = 0; i < 20; i++) {
pieces.push_back(PieceButin("rouge")); pieces.push_back(PieceButin(PieceButin::Rouge));
} }
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
pieces.push_back(PieceButin("noire")); pieces.push_back(PieceButin(PieceButin::Noire));
} }
// Mélange le vecteur de pièces (j'ai jamais utilisé ça pour randomiser faudra // Mélange le vecteur de pièces (j'ai jamais utilisé ça pour randomiser faudra

View file

@ -6,16 +6,14 @@ Dames::Dames(Joueur &j1, Joueur &j2)
int r = std::rand() % 2; int r = std::rand() % 2;
if (r == 0) { if (r == 0) {
joueurCourant = j1; joueurCourant = j1;
std::cout << j1.getNom()
<< " jouera avec les pièces blanches et commencera la partie."
<< std::endl;
} else { } else {
joueurCourant = j2; joueurCourant = j2;
std::cout << j2.getNom()
<< " jouera avec les pièces blanches et commencera la partie."
<< std::endl;
} }
plateau.initialiserPlateau(j1, j2); std::cout << joueurCourant.getNom()
<< " jouera avec les pièces blanches et commencera la partie."
<< std::endl;
init();
} }
Dames::~Dames() {} Dames::~Dames() {}
@ -23,7 +21,7 @@ Dames::~Dames() {}
Dames::Dames(const Dames &src) Dames::Dames(const Dames &src)
: plateau(PlateauDames()), joueur1{src.joueur1}, joueur2{src.joueur2}, : plateau(PlateauDames()), joueur1{src.joueur1}, joueur2{src.joueur2},
joueurCourant{src.joueurCourant} { joueurCourant{src.joueurCourant} {
plateau.initialiserPlateau(joueur1, joueur2); init();
} }
const Dames &Dames::operator=(const Dames &src) { const Dames &Dames::operator=(const Dames &src) {
@ -34,8 +32,10 @@ const Dames &Dames::operator=(const Dames &src) {
return *this; return *this;
} }
// A continuer void Dames::init() { plateau.initialiserPlateau(joueur1, joueur2); }
bool Dames::prisePossible(Piece *piece) {
// TODO: A continuer
bool Dames::prisePossible(const Piece *piece) const {
/* if (!piece->dame) /* if (!piece->dame)
if () */ if () */

View file

@ -1,7 +1,20 @@
#include "../../includes/Dames/PieceDames.hpp" #include "../../includes/Dames/PieceDames.hpp"
PieceDames::PieceDames(const std::string cat) : Piece(cat) { PieceDames::PieceDames(const enum Categorie cat)
std::cout << "pièce - " << cat << "\n"; : Piece(to_string(cat)), dame(false) {
std::cout << "pièce - " << categorie << "\n";
} }
PieceDames::~PieceDames() {} PieceDames::~PieceDames() {}
std::string PieceDames::to_string(const enum Categorie cat) const {
switch (cat) {
case Blanche:
return "Blanche";
case Noire:
return "Noire";
default:
exit(EXIT_FAILURE);
}
}

View file

@ -9,7 +9,7 @@ void PlateauDames::initialiserPlateau(Joueur &j1, Joueur &j2) {
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
for (int j = 0; j < 10; j++) { for (int j = 0; j < 10; j++) {
if ((i % 2 == 0 && j % 2 == 1) || (i % 2 == 1 && j % 2 == 0)) { if ((i % 2 == 0 && j % 2 == 1) || (i % 2 == 1 && j % 2 == 0)) {
plateau[i][j] = new PieceDames("noire"); plateau[i][j] = new PieceDames(PieceDames::Noire);
j1.ajoutPiece(plateau[i][j]); j1.ajoutPiece(plateau[i][j]);
} }
} }
@ -18,7 +18,7 @@ void PlateauDames::initialiserPlateau(Joueur &j1, Joueur &j2) {
for (int i = 6; i < 10; i++) { for (int i = 6; i < 10; i++) {
for (int j = 0; j < 10; j++) { for (int j = 0; j < 10; j++) {
if ((i % 2 == 0 && j % 2 == 1) || (i % 2 == 1 && j % 2 == 0)) { if ((i % 2 == 0 && j % 2 == 1) || (i % 2 == 1 && j % 2 == 0)) {
plateau[i][j] = new PieceDames("blanche"); plateau[i][j] = new PieceDames(PieceDames::Blanche);
j2.ajoutPiece(plateau[i][j]); j2.ajoutPiece(plateau[i][j]);
} }
} }

View file

@ -9,7 +9,7 @@ Ecran::Ecran(const uint w, const uint h, const std::string n) {
Ecran::~Ecran() {} Ecran::~Ecran() {}
void Ecran::afficher(std::function<void()> dessin) { void Ecran::afficher(const std::function<void()> dessin) const {
while (window.isOpen()) { while (window.isOpen()) {
sf::Event event; sf::Event event;
while (window.pollEvent(event)) { while (window.pollEvent(event)) {

View file

@ -1,8 +1,7 @@
#include "../includes/Plateau.hpp" #include "../includes/Plateau.hpp"
#include "../includes/Dames/PieceDames.hpp"
#include "../includes/Ecran.hpp" #include "../includes/Ecran.hpp"
Plateau::Plateau(int t) : plateau(new Piece **[t]), taille(t) { Plateau::Plateau(const int t) : plateau(new Piece **[t]), taille(t) {
// Création du plateau vide // Création du plateau vide
for (int i = 0; i < t; i++) { for (int i = 0; i < t; i++) {
plateau[i] = new Piece *[t]; plateau[i] = new Piece *[t];
@ -19,7 +18,7 @@ Plateau::~Plateau() {
delete[] plateau; delete[] plateau;
} }
void Plateau::afficherPlateau(const bool d) { void Plateau::afficherPlateau(const bool d) const {
const float tailleCellule = const float tailleCellule =
static_cast<float>(Ecran::window.getSize().x) / taille; static_cast<float>(Ecran::window.getSize().x) / taille;
@ -66,7 +65,7 @@ void Plateau::afficherPlateau(const bool d) {
} }
} }
void Plateau::modifierPlateau(int x, int y, Piece *piece) { void Plateau::modifierPlateau(const int x, const int y, Piece *piece) const {
if (x >= 0 && x < taille && y >= 0 && y < taille) { if (x >= 0 && x < taille && y >= 0 && y < taille) {
plateau[x][y] = piece; plateau[x][y] = piece;
} else { } else {

View file

@ -1,7 +1,23 @@
#include "../../includes/Safari/PieceSafari.hpp" #include "../../includes/Safari/PieceSafari.hpp"
PieceSafari::PieceSafari(const std::string cat) : Piece(cat) { PieceSafari::PieceSafari(const enum Categorie cat) : Piece(to_string(cat)) {
std::cout << "pièce - " << cat << "\n"; std::cout << "pièce - " << categorie << "\n";
} }
PieceSafari::~PieceSafari() {} PieceSafari::~PieceSafari() {}
std::string PieceSafari::to_string(const enum Categorie cat) const {
switch (cat) {
case Barriere:
return "Barrière";
case Elephant:
return "Éléphant";
case Rhinoceros:
return "Rhinocéros";
case Lion:
return "Lion";
default:
exit(EXIT_FAILURE);
}
}

View file

@ -1,9 +1,12 @@
#include "../../includes/Safari/PlateauSafari.hpp" #include "../../includes/Safari/PlateauSafari.hpp"
#include "../../includes/utils.hpp"
PlateauSafari::PlateauSafari() : Plateau(8) {} PlateauSafari::PlateauSafari() : Plateau(8) {}
PlateauSafari::~PlateauSafari() {} PlateauSafari::~PlateauSafari() {}
void PlateauSafari::initialiserPlateau(Joueur &j1, Joueur &j2) { void PlateauSafari::initialiserPlateau(Joueur &j1, Joueur &j2) {
ignore(j1);
ignore(j2);
// TODO // TODO
} }

View file

@ -4,7 +4,7 @@ Safari::Safari(Joueur &j1, Joueur &j2, Joueur &j3)
: plateau(PlateauSafari()), joueur1{j1}, joueur2{j2}, joueur3{j3}, : plateau(PlateauSafari()), joueur1{j1}, joueur2{j2}, joueur3{j3},
joueurCourant{j1} { joueurCourant{j1} {
for (int i = 0; i < 50; i++) { for (int i = 0; i < 50; i++) {
barrieres.push_back(new PieceSafari("barriere")); barrieres.push_back(new PieceSafari(PieceSafari::Barriere));
} }
} }
@ -14,7 +14,7 @@ Safari::Safari(const Safari &src)
: plateau(PlateauSafari()), joueur1{src.joueur1}, joueur2{src.joueur2}, : plateau(PlateauSafari()), joueur1{src.joueur1}, joueur2{src.joueur2},
joueur3{src.joueur3}, joueurCourant{src.joueurCourant} { joueur3{src.joueur3}, joueurCourant{src.joueurCourant} {
for (int i = 0; i < 50; i++) { for (int i = 0; i < 50; i++) {
barrieres.push_back(new PieceSafari("barriere")); barrieres.push_back(new PieceSafari(PieceSafari::Barriere));
} }
} }
@ -26,10 +26,16 @@ const Safari &Safari::operator=(const Safari &src) {
return *this; return *this;
} }
void Safari::choixAnimal(std::string animal) { void Safari::init() {
if (animal != "éléphant" || animal != "rhinocéros" || animal != "lion") { // On peut donner que 2 joueurs ?
plateau.initialiserPlateau(joueur1, joueur2);
}
void Safari::choixAnimal(const PieceSafari::Categorie animal) const {
if (animal == PieceSafari::Barriere) {
throw std::invalid_argument("Animal non valide"); throw std::invalid_argument("Animal non valide");
} }
if (joueurCourant.getPieces().empty()) { if (joueurCourant.getPieces().empty()) {
for (int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++) {
joueurCourant.ajoutPiece(new PieceSafari(animal)); joueurCourant.ajoutPiece(new PieceSafari(animal));