1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
| #include <halloffame.hpp>
#include <vector>
#include <algorithm>
#include <iostream>
#include <fstream>
/* quelques fonctions qui seront nécessaires pour la facilité */
std::ostream & operator<<(std::ostream & ofs, Joueur const & j)
{
ofs<<j.name()<<"................... "<<j.score()<<std::endl;
return ofs;
}
std::ofstream & operator<<(std::ofstream & ofs, Joueur const & j)
{
ofs<<j.name()<<" "<<j.score()<<std::endl;
return ofs;
}
bool HallOfFame::charger()
{
std::cout<<"Indiquez le nom du fichier a charger :";
std::string filename;
cin>>filename;
std::ifstream ifs(filename);
if(!ifs)
{
std::cout<<"impossible de charger le fichier "<<filename<<std::endl;
return false;
}
int score;
std::string name;
while(ifs>>name>>score)
{
Joueur j(name, score);
joueurs_.insert(std::make_pair(name,j));
}
return true;
}
void HallOfFame::sauver() const
{
std::cout<<"Indiquez le nom du fichier pour l'enregistrement :";
std::string filename;
cin>>filename;
std::ofstream ofs(filename);
if(!ofs)
{
std::cout<<"impossible de charger le fichier "<<filename<<std::endl;
return false;
}
for(std::map<std::string, Joueur>::const_iterator it=joueurs_.begin();
it!=joueurs_.end(); ++it)
ofs<<(*it).second;
return true;
}
void HallOfFame::scores() const
{
std::vector<Joueurs> tab;
/* il y aurait surement d'autres solutions ;) */
for(std::map<std::string, Joueur>::const_iterator it=joueurs_.begin();
it!=joueurs_.end(); ++it)
tab.push_back((*it).second);
std::sort(tab.begin(), tab.end());
std::cout<<"Meilleurs scores"<<std::endl
<<"================"<<std::endl;
for(std::vector<Joueur>::const_iterator it=tab.begin();it!=tab.end();++it)
cout<<(*it);
}
int HallOfFame::menu() const
{
bool ok=false;
int choix;
whle(!ok)
{
std::cout<<"Que souhaitez vous faire :"<<std::endl
<<" 1- charger un fichier de joueurs"<<std::endl
<<" 2- sauvegarder les joueurs"<<std::endl
<<" 3- afficher les scores"<<std::endl
<<" 4- Lancer un duel"<<std::endl<<std::endl
<<" 0- quitter"<<std::endl
<<"Votre choix"
if(!(cin>>choix))
{
std::cout<<"Veuillez choisir entre 1, 2, 3, 4 et 0 uniquement"<<std::endl;
std::cin.clear();
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
}
if(choix==1 || choix==2 || choix==3 || choix==4 || choix==0)
ok=true;
}
return choix;
}
Duel HallOfFame::creerDuel()
{
Joueur j1=obtenirJoueur(1);
Joueur j2=obtenirJoueur(2);
return Duel(j1,j2);
}
Joueur HallOfFame::obtenirJoueur(int i)
{
std::cout<<"Veuillez introduire le nom du "
<<(i==1 ? "premier " :"deuxieme ")<<" dueliste :";
std::string nom;
cin>>nom;
std::map<std::string, Joueur>::iterator it = joueurs_.find(nom);
if(it != joueurs_.end())
return (*it).second;
Joueur j(nom);
joueurs_.insert(std::make_pair(nom,j));
return j;
void HallOfFame::mettreAJour(Joueur const & j)
{
std::map<std::string, Joueur>::iterator it = joueurs_.find(nom);
(*it).second=j;
} |
Partager