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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
| #include <iostream>
#include<fstream>
#include<cstdlib>//fonctionnalité du nombre aléatoire
#include<ctime>//fonctionnalité du nombre aléatoire
#include<string>
using namespace std;
inline int nombreAleatoire(int nombreMax);
inline string ChoisirMot();
inline string MelangeMot(string motMystere);
int main()
{
int menu=0;
cout<<"*****Ce jeux devine le mot.****"<<endl;
cout<<"1.SOLO"<<endl;
cout<<"2.MULTIJOUEUR"<<endl;
cout<<"Veuillez choisir une option :"<<endl;
cin>>menu;
int score=0;
if(menu==1)
{
for(int j=0;j<5;j++)
{
string motmystere(""),NouveauMot("");//initialisation des variables motmystere && NouveauMot.
motmystere = ChoisirMot();//Choix d'un mot mystere dans le fichier par appelle de fonction
NouveauMot=MelangeMot(motmystere);//Melange du mot motmystere par Appele de fonction
cout << NouveauMot<<endl;//Affichage a l'écran du nouveau mot choisis.
string MotCache;//declaration de la var:MotCache mot Devineé entrée par l'user
cout<<"Quel est le mot cache ?"<<endl;
cin>>MotCache;
int i=2;
if(MotCache != motmystere )
{
do
{
cout<<"Ce n'est pas le mot recommencer!!!"<<endl;
cin>>MotCache;
i--;
}while(MotCache != motmystere && i>0);
if(MotCache != motmystere)
{
cout<<"Ce n'est pas le mot, vous avez perdu."<<endl;
}
else
{
cout<<"Felicitation vous avez gagne!!!"<<endl;
score++;
}
}
else
{
cout<<"Felicitation vous avez gagne!!!"<<endl;
score++;
}
}
cout<<"Votre score est de:"<<score<<"/5"<<endl;
if(score==0)
{
cout<<"Oh lalal vous vous avez perdu la partie"<<endl;
}
else if (score >= 1 || score <= 3)
{
cout<<"Peu satisfaisant"<<endl;
}
else
{
cout<<"Bravo! Vous etes parfait"<<endl;
}
}
/*--------------------------------------Fin du mode SOLO----------------------------------------------------------*/
else if(menu==2)
{
int score1=0;
string Nom1,Nom2,Mot1;//Déclaration des variables Nom des joeurs et mots entrée
int k;//Compteur de boucle
int a=2;
int b=2,h,score2=0;
string NouveauMot3,Mot2;
cout<<"Joueur1 entrez votre nom:"<<endl;
cin>>Nom1;
cout<<"Joueur2 entrez votre nom:"<<endl;
cin>>Nom2;
string NouveauMot2;//NouveauMot2 nouveau mot choisis.
for(k=0;k<3;k++)
{
cout<<"Veuillez entrer un mot:"<<Nom1<<endl;
cin>>Mot1;
NouveauMot2=MelangeMot(Mot1);//Melange du mot motmystere par Appele de fonction
cout << NouveauMot2<<endl;//Affichage a l'écran du nouveau mot choisis.
string MotCache2;//declaration de la var:MotCache mot Devineé entrée par l'user
cout<<"Quel est le mot cache? "<<Nom2<<endl;
cin>>MotCache2;
if(MotCache2!=Mot1)
{
do
{
cout<<"Ce n'est pas le mot recommencer!"<<endl;
cin>>MotCache2;
a --;
}while(MotCache2!=Mot1 && a>0);
if(MotCache2!=Mot1)
{
cout<<"Ce n'est pas le mot,le mot etait:"<<Mot1<<endl;
}
else
{
cout<<"Felicitation vous avez gagne!!!"<<endl;
score1 ++;
}
}
else
{
cout<<"Felicitation vous avez gagne!!!"<<endl;
score1++;
}
}
cout<<"Votre score Total est:"<<score1<<"/3"<<endl;
for(h=0;h<3;h++)
{
cout<<"Veuillez entrer un mot: "<<Nom2<<endl;
cin>>Mot2;
NouveauMot3=MelangeMot(Mot2);//Melange du mot motmystere par Appele de fonction
cout << NouveauMot3<<endl;//Affichage a l'écran du nouveau mot choisis.
string MotCache3;//declaration de la var:MotCache mot Devineé entrée par l'user
cout<<"Quel est le mot cache? "<<Nom1<<endl;
cin>>MotCache3;
if(MotCache3!=Mot2)
{
do
{
cout<<"Ce n'est pas le mot recommencer!"<<endl;
cin>>MotCache3;
b --;
}while(MotCache3!=Mot2 && b>0);
if(MotCache3!=Mot2)
{
cout<<"Ce n'est pas le mot,le mot etait:"<<Mot2<<endl;
}
else
{
cout<<"Felicitation vous avez gagne!!!"<<endl;
score2 ++;
}
}
else
{
cout<<"Felicitation vous avez gagne!!!"<<endl;
score2++;
}
}
cout<<"Votre score Total est: "<<score2<<"/3"<<endl;
if(score1>score2)
{
cout<<"Vous avez gagnez: "<<Nom2<<endl;
}
else if(score2>score1)
{
cout<<"Vous avez gagnez: "<<Nom1<<endl;
}
else
{
cout<<"Vous avez tous perdu :"<<Nom1<<"&&"<<Nom2<<endl;
}
}
else
{
cout<<"Erreur vous avez mal choisis!!!"<<endl;
}
return 0;
}
/*----------------------------------------Choix d'un numéro de MOT-----------------------------------*/
inline int nombreAleatoire(int nombreMax)
{
srand(time(0));
return (rand() % nombreMax);
}
/*--------------------------fonction pour choisir un mot dans le fichier------------------------------*/
inline string ChoisirMot()
{
ifstream fichier("Mot_Jeu.txt",ios::in);
string motLu;
// "ouverture reussie"
if (fichier)
{
int nombreMot=0, numMotChoisi=0;
//Détermination du nombre de mots
while(getline(fichier,motLu))
{
nombreMot++;
}
// On envoie le nombre de mot pour une séléction d'un numéro
numMotChoisi = nombreAleatoire(nombreMot);
//Une fois le numéro choisi on retourne au dédut du fichier
fichier.clear();
fichier.seekg(0,ios::beg);
//on choisis un mot au hazard qui est stocké dans motlu.
while(numMotChoisi > 0 && getline(fichier,motLu))
{
numMotChoisi--;
}
fichier.close(); //fermeture du fichier
}
else
cerr << "Erreur rencontree" << endl; //echec de l'ouverture du fichier.
return motLu;
}
/*---------------------------Fonction MelangeMot pour mélanger les letres du mot-----------------------------*/
inline string MelangeMot(string motMystere)
{
srand(time(0));//initialisation de la géneration de nombres aléatoire.
string melange;
int position(0);
while(motMystere.size()!=0)//Tant que la taille n'est pas encore egale a zero on pioche les lettres sans remise puis on les melange
{
position=rand()%motMystere.size();//Coix d'un mot à une pôsition
melange+=motMystere[position];//Positionnement d'une lettre dans Mot_mystere
motMystere.erase(position,1);//on retire la lettre du lettre mystere pour qu'elle ne soit plus retirer.
}
return melange;
} |
Partager