Projet

Général

Profil

Publication de fichiers » tournoi.cpp

Anonyme, 11/01/2022 10:42

 
#include "tournoi.hpp"
#include <sstream>
#include <vector>
#include <fstream>

using namespace std;

//constructeur
Tournoi::Tournoi(int nbjoueurs){
this->nbjoueurs=nbjoueurs;
}

//constructeur de recopie
Tournoi::Tournoi(const Tournoi& T){
listejoueurs=T.listejoueurs;
matchs=T.matchs;
classement=T.classement;
}

//destructeur
Tournoi::~Tournoi(){
listejoueurs.clear();
matchs.clear();
classement.clear();
}

//operations
Tournoi& Tournoi::operator=(const Tournoi& T){
if (listejoueurs!= T.listejoueurs){
listejoueurs.clear();

for (int i=0;i<=int (listejoueurs.size())-1;i++){
listejoueurs[i]=T.listejoueurs[i];
}
}
if (matchs!= T.matchs){
matchs.clear();

for (int i=0;i<=int(matchs.size())-1;i++){
matchs[i]=T.matchs[i];
}
}
if (classement!= T.classement){
classement.clear();

for (int i=0;i<=int(classement.size())-1;i++){
classement[i]=T.classement[i];
}
}


return *this;


}



void Tournoi:: creerFichier(string nomFichier){
// Ouverture en écriture avec effacement
// du fichier ouvert
ofstream fichier(nomFichier.c_str(),ios::out|ios::trunc);
if(!fichier.fail())
{
cout << "Fichier" << nomFichier<< "cree" <<endl;
fichier.close();
}
else
cerr << "Impossible d'ouvrir le fichier !" << endl;
}


void Tournoi:: fichierJoueurs(string nomFichier){

fstream fin;
// Ouverture fichier
fin.open(nomFichier , ios::in);
// lecture du fichier
vector<string> row;
string line, word, temp;
while (fin >> temp) {
row.clear();
// read an entire row and
// store it in a string variable 'line'
getline(fin, line);
// used for breaking words
stringstream s(line);
// read every column data of a row and
// store it in a string variable, 'word'
while (getline(s, word,' ')) {
// add all the column data
// of a row to a vector
row.push_back(word);
}

string licence,nom,prenom;
licence=row[0];
nom=row[1];
prenom=row[2];
Joueur *j=new Joueur(licence,nom,prenom);
listejoueurs.push_back(j);
break;
}
}




void Tournoi :: Poules(string nomFichier){ //nom du fichier que l'utilisateur � d�j� avec le numLicence/nom/prenom de tout les joueurs
Joueur j;

ifstream fichier(nomFichier);
creerFichier("Poule");
ifstream fichier_poule("/Polytech/linapieral/Documents/Projet_Ping_Pong/Poule.txt");

if(fichier){
int i = 0;
string ligne;
while (getline(fichier, ligne)){
if ( i%4 == 0 and i < (nbjoueurs/4 - nbjoueurs%4) *4 ) { // normalement ?a nous permet de savoir ? partir de quand on fait des poules de 5 au lieu de 4 selon le nombre de joueurs
fichier_poule << "Groupe" << char(65 + i/4); // A = 65
}
if ( i%5 == 0 and i >= (nbjoueurs/4 - nbjoueurs%4) *4) { // pour faire les poules de 5
fichier_poule << "Groupe" << char(65 + i/4); // A = 65
}
fichier_poule << ligne << endl;
i = i+1;
}
fichier.close();
fichier_poule.close();
}
else
cerr << "Impossible d'ouvrir le fichier !" << endl;
}

void Tournoi::entrerScores(int num){
int sA,sB;
cout<<"entrez la score du joueur " << matchs[num]->getJoueurA().getnumLicence()<<" : ";
cin>> sA;
matchs[num]->setScoreA(sA);
cout<<"entrez la score du joueur " << matchs[num]->getJoueurB().getnumLicence()<<" : ";
cin>> sB;
matchs[num]->setScoreA(sB);
matchs[num]->setEcart(sA,sB);
int rep=2;
while (rep!=1 || rep!=0){
cout<<"voulez vous valider vos resultats ? (1 : oui, 0: annuler) : ";
cin>>rep;
}
if (rep==1){
matchs[num]->setJoue(1);
}

}

void Tournoi::match(string nomFichier){
fstream f;
f.open(nomFichier, ios::out | ios::app);
for (int i=0;i<=int(matchs.size());i++){
f<<"______________________________"<< "\n"
<<"\n"
<<"match numero: "<<matchs[i]->getNumMatch()<<"\n"
<<"\n"
<<"Joueur A : "<<matchs[i]->getJoueurA().getnumLicence()<<" "<<matchs[i]->getJoueurA().getm_nom()<<"\n"
<<"score A : "<<"\n"
<<"\n"
<<"Joueur B : "<<matchs[i]->getJoueurB().getnumLicence()<<" "<<matchs[i]->getJoueurB().getm_nom()<<"\n"
<<"score B : "<<"\n"
<<"\n"
<<"\n";
}

}

bool Tournoi::finMatchs(){
for(int i=0;i<=int(matchs.size());i++){
if (matchs[i]->getJoue()==false){
return false;
}

}
return true;
}


void Tournoi::classer(){
for(int i=0;i<=int(classement.size());i++){
}
}
(6-6/7)