diff --git a/Algo/tp/Cpp/2_tp/.vscode/settings.json b/Algo/tp/Cpp/2_tp/.vscode/settings.json index 0cba2e6..952e185 100644 --- a/Algo/tp/Cpp/2_tp/.vscode/settings.json +++ b/Algo/tp/Cpp/2_tp/.vscode/settings.json @@ -1,5 +1,6 @@ { "files.associations": { - "iostream": "cpp" + "iostream": "cpp", + "iosfwd": "cpp" } } \ No newline at end of file diff --git a/Algo/tp/Cpp/2_tp/bin/exe b/Algo/tp/Cpp/2_tp/bin/exe new file mode 100755 index 0000000..798aa62 Binary files /dev/null and b/Algo/tp/Cpp/2_tp/bin/exe differ diff --git a/Algo/tp/Cpp/2_tp/obj/joueur.o b/Algo/tp/Cpp/2_tp/obj/joueur.o index 96381fe..b342a93 100644 Binary files a/Algo/tp/Cpp/2_tp/obj/joueur.o and b/Algo/tp/Cpp/2_tp/obj/joueur.o differ diff --git a/Algo/tp/Cpp/2_tp/obj/partie.o b/Algo/tp/Cpp/2_tp/obj/partie.o index 10f2d71..139d0b9 100644 Binary files a/Algo/tp/Cpp/2_tp/obj/partie.o and b/Algo/tp/Cpp/2_tp/obj/partie.o differ diff --git a/Algo/tp/Cpp/2_tp/obj/testde.o b/Algo/tp/Cpp/2_tp/obj/testde.o index 3fceedb..af1b402 100644 Binary files a/Algo/tp/Cpp/2_tp/obj/testde.o and b/Algo/tp/Cpp/2_tp/obj/testde.o differ diff --git a/Algo/tp/Cpp/2_tp/src/de.cpp b/Algo/tp/Cpp/2_tp/src/de.cpp index 43ab4ce..aec6adc 100644 --- a/Algo/tp/Cpp/2_tp/src/de.cpp +++ b/Algo/tp/Cpp/2_tp/src/de.cpp @@ -2,6 +2,7 @@ #include using namespace std; + namespace jeu { De::De(int nbFaces) @@ -16,7 +17,7 @@ namespace jeu { srand(time(nullptr)); }; - int De::getValeur() { + int De::getValeur() const { if(valeur == 0){ return 0; } diff --git a/Algo/tp/Cpp/2_tp/src/de.hpp b/Algo/tp/Cpp/2_tp/src/de.hpp index c7b6a75..1d82832 100644 --- a/Algo/tp/Cpp/2_tp/src/de.hpp +++ b/Algo/tp/Cpp/2_tp/src/de.hpp @@ -12,7 +12,7 @@ private: public: De(int nbFaces); De(); - int getValeur(); + int getValeur() const; int lancer(); void effacer(); }; diff --git a/Algo/tp/Cpp/2_tp/src/joueur.cpp b/Algo/tp/Cpp/2_tp/src/joueur.cpp index 75fe21c..d826fcd 100644 --- a/Algo/tp/Cpp/2_tp/src/joueur.cpp +++ b/Algo/tp/Cpp/2_tp/src/joueur.cpp @@ -2,7 +2,6 @@ #include using namespace std; -using namespace jeu; namespace jeu { int Joueur::numJoueur = 0; @@ -12,7 +11,7 @@ namespace jeu { {} Joueur::Joueur() - :Joueur{"Joueur" + numJoueur} + :Joueur{"Joueur" + to_string(numJoueur+1)} {} string Joueur::getNom() const { diff --git a/Algo/tp/Cpp/2_tp/src/partie.cpp b/Algo/tp/Cpp/2_tp/src/partie.cpp index 3751c37..5387e80 100644 --- a/Algo/tp/Cpp/2_tp/src/partie.cpp +++ b/Algo/tp/Cpp/2_tp/src/partie.cpp @@ -1,14 +1,77 @@ #include "de.hpp" +#include "joueur.hpp" +#include "partie.hpp" + #include using namespace std; + namespace jeu { - + Partie::Partie(string nomJoueur1, string nomJoueur2) + :gameOver{false}, joueur1{nomJoueur1}, joueur2{nomJoueur2} + { + joueurCourant = &joueur1; + } + Joueur Partie::getJoueurCourant() const { + return *joueurCourant; + + } -} // fin du namespace jeu + void Partie::lancerDe(){ + if(gameOver) { + cout << "La partie est términé Jami !!!" << endl; + return; + } + de.lancer(); + if(getValeurDe() == 1) { + cout << "Vous avez perdu tt vos points !!!" << endl; + joueurCourant->effacerScoreCourant(); + passerMainJoueurSuivant(); + cout << joueurCourant->getNom() << " C'est à vous de jouer !!" << endl; + return; + } + joueurCourant->ajouterAuScoreCourant(getValeurDe()); + } + + int Partie::getValeurDe() const { + return de.getValeur(); + } + bool Partie::isGameOver() const { + return gameOver; + } + void Partie::passerMainJoueurSuivant() { + joueurCourant->ajouterAuScoreTotal(joueurCourant->getScoreCourant()); + if(joueurCourant->getScoreTotal() >= 100) { + gameOver = true; + cout << "Félicitation " << joueurCourant->getNom() << " vous avez gagné " << joueurCourant->getScoreTotal() << " points !" << endl; + return; + } + + if(joueurCourant->getNom() == joueur1.getNom()) + joueurCourant = &joueur2; + else + joueurCourant = &joueur1; + } + + void Partie::rejouer() { + joueur1.effacerScores(); + joueur2.effacerScores(); + de.effacer(); + gameOver = false; + if(joueur1.getScoreTotal() < joueur2.getScoreTotal()) + joueurCourant = &joueur1; + else + joueurCourant = &joueur2; + + } +} // fin du namespace jeu +// ostream &operator<<(ostream &s, const jeu::Joueur &j) { +// s << "Le joueur " << j.getNom() << " possede un score courant de " << j.getScoreCourant() << " et un score de " << j.getScoreTotal() << endl; +// return s; +// } \ No newline at end of file diff --git a/Algo/tp/Cpp/2_tp/src/partie.hpp b/Algo/tp/Cpp/2_tp/src/partie.hpp index 54fa03e..a74f548 100644 --- a/Algo/tp/Cpp/2_tp/src/partie.hpp +++ b/Algo/tp/Cpp/2_tp/src/partie.hpp @@ -1,17 +1,33 @@ #ifndef PARTIE_HPP #define PARTIE_HPP +#include "joueur.hpp" #include namespace jeu { + constexpr int valeurPerte{1}; constexpr int scoreVictoire{100}; class Partie { -private: - -public: - + +private : + bool gameOver; + Joueur joueur1; + Joueur joueur2; + Joueur *joueurCourant; + De de; + +public : + Partie(std::string nomJoueur1, std::string nomJoueur2); + + Joueur getJoueurCourant() const; + + void lancerDe(); + int getValeurDe() const; + bool isGameOver() const; + void passerMainJoueurSuivant(); + void rejouer(); }; } //namespace jeu diff --git a/Algo/tp/Cpp/2_tp/src/testde.cpp b/Algo/tp/Cpp/2_tp/src/testde.cpp index ea874ff..3a8054d 100644 --- a/Algo/tp/Cpp/2_tp/src/testde.cpp +++ b/Algo/tp/Cpp/2_tp/src/testde.cpp @@ -52,8 +52,35 @@ void testJoueur(void){ cout << marc << endl; } +void testPartie(void) { + Partie partie{"Antoine", "Vivien"}; + int choix, rej; + while(1) { + while(! partie.isGameOver()) { + partie.lancerDe(); + cout << partie.getJoueurCourant().getNom() << endl; + cout << "voulez passer la main (0) ou bien rejouer (1) (possibilité de perdre votre score)" << endl; + cin >> choix; + if(choix == 0) { + partie.passerMainJoueurSuivant(); + } + else + cout << partie.getJoueurCourant().getNom() << " à vous de rejouer !" << endl; + } + cout << "voulez vous rejouer ??? (0,non/1,oui)" << endl; + cin >> rej; + if(rej == 1) + partie.rejouer(); + else { + cout << "Merci d'avoir joué à notre jeu !\nBonne journée !!!" << endl; + exit(0); + } + } +} + int main(void){ // testDe(); - testJoueur(); + // testJoueur(); + testPartie(); return 0; } \ No newline at end of file