ajout du tp 2 de Cpp

master
antoine.perederii 2 years ago
parent b73961ad39
commit 026077f884

@ -1,5 +1,6 @@
{ {
"files.associations": { "files.associations": {
"iostream": "cpp" "iostream": "cpp",
"iosfwd": "cpp"
} }
} }

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

@ -2,6 +2,7 @@
#include <iostream> #include <iostream>
using namespace std; using namespace std;
namespace jeu { namespace jeu {
De::De(int nbFaces) De::De(int nbFaces)
@ -16,7 +17,7 @@ namespace jeu {
srand(time(nullptr)); srand(time(nullptr));
}; };
int De::getValeur() { int De::getValeur() const {
if(valeur == 0){ if(valeur == 0){
return 0; return 0;
} }

@ -12,7 +12,7 @@ private:
public: public:
De(int nbFaces); De(int nbFaces);
De(); De();
int getValeur(); int getValeur() const;
int lancer(); int lancer();
void effacer(); void effacer();
}; };

@ -2,7 +2,6 @@
#include <iostream> #include <iostream>
using namespace std; using namespace std;
using namespace jeu;
namespace jeu { namespace jeu {
int Joueur::numJoueur = 0; int Joueur::numJoueur = 0;
@ -12,7 +11,7 @@ namespace jeu {
{} {}
Joueur::Joueur() Joueur::Joueur()
:Joueur{"Joueur" + numJoueur} :Joueur{"Joueur" + to_string(numJoueur+1)}
{} {}
string Joueur::getNom() const { string Joueur::getNom() const {

@ -1,14 +1,77 @@
#include "de.hpp" #include "de.hpp"
#include "joueur.hpp"
#include "partie.hpp"
#include <iostream> #include <iostream>
using namespace std; using namespace std;
namespace jeu { 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;
// }

@ -1,17 +1,33 @@
#ifndef PARTIE_HPP #ifndef PARTIE_HPP
#define PARTIE_HPP #define PARTIE_HPP
#include "joueur.hpp"
#include <string> #include <string>
namespace jeu { namespace jeu {
constexpr int valeurPerte{1}; constexpr int valeurPerte{1};
constexpr int scoreVictoire{100}; constexpr int scoreVictoire{100};
class Partie { class Partie {
private:
private :
public: 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 } //namespace jeu

@ -52,8 +52,35 @@ void testJoueur(void){
cout << marc << endl; 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){ int main(void){
// testDe(); // testDe();
testJoueur(); // testJoueur();
testPartie();
return 0; return 0;
} }
Loading…
Cancel
Save