Tortues_CPP/src/Jeu.cpp

152 lines
4.7 KiB
C++

#include <iostream>
#include <windows.h>
#include <string>
#include "Jeu.h"
#include "Joueur.h"
#include "PiocheDeCartes.h"
#include "PiocheDeTortues.h"
#include "PlateauDeJeu.h"
#include "../include/utils.h"
#include "../include/constantes.h"
using namespace std;
Jeu::Jeu()
{
// init joueurs
// pour chaque manche
// init pioche
// init plateau de jeu
// choix tortue par chaque joueur
// remplacement main joueur
// ... déroulement manche
// free pioche
// free plateau de jeu
//puis:
// free joueurs
int nbJoueurs = 0;
do
{
cout << "Combien y a-t-il de joueurs ?" << endl <<"(Entrez une valeur comprise entre 1 et " << NB_MAX_JOUEURS << ") ";
cin >> nbJoueurs;
}
while (nbJoueurs > NB_MAX_JOUEURS || nbJoueurs <= 0);
this->nombreJoueurs = nbJoueurs;
for (int indexJoueur = 0; indexJoueur < nbJoueurs; indexJoueur++)
{
Joueur* unJoueur;
unJoueur = new Joueur();
this->tabJoueurs.push_back(unJoueur);
}
}
void Jeu::jouer()
{
do
{
this->piocheDeCartes = new PiocheDeCartes();
this->listeTortues = new PiocheDeTortues();
this->tuiles = new PiocheDeTortues();
this->plateauDeJeu = new PlateauDeJeu(this->listeTortues);
for (int j = 0; j < this->nombreJoueurs; j++)
{
this->tabJoueurs[j]->debutDeManche(this->piocheDeCartes, this->tuiles);
}
this->manche();
for (int j = 0; j < this->nombreJoueurs; j++)
{
this->tabJoueurs[j]->finDeManche();
}
this->piocheDeCartes->~PiocheDeCartes();
this->listeTortues->~PiocheDeTortues();
this->tuiles->~PiocheDeTortues();
this->plateauDeJeu->~PlateauDeJeu();
}
while (!this->aTroisFeuillesDeSalade());
Joueur* joueurVainqueur;
for (int j = 0; j < this->nombreJoueurs; j++)
{
if (this->tabJoueurs[j]->getNbFeuillesDeSalade() == NB_MAX_FEUILLES_SALADE)
{
joueurVainqueur = this->tabJoueurs[j];
break;
}
}
cout << "Le/La Joueur(euse) " << joueurVainqueur->getNom() << "a gagné la partie ! Félicitations ! " << endl << "Merci d'avoir joué ! ";
}
bool Jeu::aTroisFeuillesDeSalade()
{
for (int i = 0; i < this->nombreJoueurs; i++ )
{
if (this->tabJoueurs[i]->getNbFeuillesDeSalade() == NB_MAX_FEUILLES_SALADE)
{
return true;
}
}
}
void Jeu::manche()
{
int joueurEnCours = this->indexJoueurLePlusJeune();
cout << endl;
do
{
this->plateauDeJeu->affichagePlateau();
Carte* carteJouee;
carteJouee = this->tabJoueurs[joueurEnCours]->choixCarteJouee();
this->plateauDeJeu->actionCarte(carteJouee);
this->plateauDeJeu->affichagePlateau();
this->tabJoueurs[joueurEnCours]->renouvelerCarte(this->piocheDeCartes, carteJouee);
carteJouee->~Carte();
//this->piocheDeCartes->afficherPaquet();
joueurEnCours = (joueurEnCours + 1)% this->nombreJoueurs;
}
while (! (this->plateauDeJeu->uneTortueEstArrivee()));
int* tabOrdreCouleursTortues = this->plateauDeJeu->getOrdreArriveeTortue();
for (int o = 0; o < NB_TORTUES; o++)
{
for (int i = 0; i < this->nombreJoueurs; i++)
{
if (this->tabJoueurs[i]->getTortueJoueur()->Getcouleur() == tabOrdreCouleursTortues[o])
{
this->tabJoueurs[i]->ajoutFeuilleDeSalade();
cout << "Le Joueur " << this->tabJoueurs[i]->getNom() << " (" << this->tabJoueurs[i]->getAge() << " ans) a gagné la manche et a remporté une feuille de salade." << endl << "Il a maintenant " << this->tabJoueurs[i]->getNbFeuillesDeSalade() << " feuille(s)." << endl << "Il avait la tortue ";
this->tabJoueurs[i]->getTortueJoueur()->affichageTortue();
cout << "." << endl;
goto finDeRecherche; //break n'aurait permis de ne sortir que d'un seul for
}
}
}
finDeRecherche:
if (!this->aTroisFeuillesDeSalade())
{
cout << "Appuyez sur une touche (sauf ENTREE) puis appuyez ensuite sur ENTREE pour passer à la manche suivante" << endl;
char reponse;
cin >> reponse;
}
}
int Jeu::indexJoueurLePlusJeune()
{
int plusJeuneAge = -1;
int indexPlusJeuneAge = -1;
int ageJoueurEnCours;
for (int i = 0; i < this->nombreJoueurs; i++)
{
ageJoueurEnCours = this->tabJoueurs[i]->getAge();
if (plusJeuneAge < 0 || ageJoueurEnCours < plusJeuneAge)
{
plusJeuneAge = ageJoueurEnCours;
indexPlusJeuneAge = i;
}
}
return indexPlusJeuneAge;
}
Jeu::~Jeu()
{
//dtor
}