Skip to content

Instantly share code, notes, and snippets.

@bhugueney
Created January 2, 2018 21:19
Show Gist options
  • Save bhugueney/55bbd89b3092ce9860a3d28c118b9cb3 to your computer and use it in GitHub Desktop.
Save bhugueney/55bbd89b3092ce9860a3d28c118b9cb3 to your computer and use it in GitHub Desktop.
Pendu en C++ (traduction de python)
#include <iostream>
#include <string>
#include <vector>
std::string initialiser_mot_mystere(std::string const& mot){
std::string res;
for(std::size_t i=0; i != mot.size(); ++i){
res.push_back('_');
}
return res;
}
void affichage_mot_mystere(std::string const& mot_mystere){
for(char const& c : mot_mystere){
std::cout << c << ' ';
}
std::cout<<std::endl;
}
void affichage_coups_restants(int nb_coups_restants){
std::cout << "Il vous reste " << nb_coups_restants << " coups."<<std::endl;
}
char saisie_joueur2(){
std::string saisie;
while( saisie.size() == 0){
std::cout << "Lettre å tester :"<<std::endl;
std::cin >> saisie;
std::cout << std::endl;
}
return saisie[0];
}
std::string mise_a_jour_mot_mystere(char lettre, std::string const& mot, std::string const& mot_mystere){
std::string res;
for(std::size_t i=0; i != mot.size(); ++i){
res.push_back(mot[i] == lettre ? lettre : mot_mystere[i]);
}
return res;
}
std::string choix_joueur2(std::string const& mot, std::string const& mot_mystere
, int nb_coups_restants){
affichage_mot_mystere(mot_mystere);
affichage_coups_restants(nb_coups_restants);
char lettre_saisie= saisie_joueur2();
return mise_a_jour_mot_mystere(lettre_saisie, mot, mot_mystere);
}
bool test_jeu_fini(std::string const& mot_mystere, int nb_coups_restants){
if(mot_mystere.find('_') == std::string::npos){
std::cout<< "Félicitations! Vous avez trouvé le mot mystère."<< std::endl;
return true;
}else if(nb_coups_restants == 0){
std::cout<< "Félicitations! Vous avez trouvé le mot mystère."<< std::endl;
return true;
}
return false;
}
int main(int argc, char* argv[]){
while(true){
std::cout << "*************************************" << std::endl
<< "Bienvenu sur le jeu du Pendu" << std::endl
<< "1 - Commencer une nouvelle partie" << std::endl
<< "0 - Quitter" << std::endl
<< "*************************************"<< std::endl;
int choix;
std::cout<<"Faites votre choix"<<std::endl;
std::cin >> choix;
if( choix == 0){
break;
}
std::cerr<<" choix:" << choix <<std::endl;
if( choix != 1){
continue;
}
std::string mot;
std::getline(std::cin, mot);// flush line break
while( mot.size() ==0){
std::cout << "Joueur 1 - Saisissez le mot mystère:" << std::endl;
std::getline(std::cin, mot);
}
std::string mot_mystere= initialiser_mot_mystere(mot);
for(std::size_t i=0; i != 100; ++i){
std::cout << std::endl;
}
for(int nb_coups_restants= 10; !test_jeu_fini(mot_mystere, nb_coups_restants); --nb_coups_restants){
mot_mystere= choix_joueur2(mot, mot_mystere, nb_coups_restants);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment