2016-03-04 07:49:31 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2016-03-05 22:37:08 +00:00
|
|
|
#include <ctime>
|
2016-03-04 07:49:31 +00:00
|
|
|
|
2016-03-04 13:18:01 +00:00
|
|
|
#include "tp1/wordproc.cpp"
|
|
|
|
|
2016-03-05 22:37:08 +00:00
|
|
|
#define DICT_FILENAME "tp1/dict"
|
|
|
|
|
2016-03-04 07:49:31 +00:00
|
|
|
using namespace std;
|
|
|
|
|
2016-03-05 22:37:08 +00:00
|
|
|
string input_word();
|
|
|
|
string pickup_word(vector<string> dict);
|
|
|
|
void append_word();
|
|
|
|
void play(string word);
|
|
|
|
|
2016-03-04 07:49:31 +00:00
|
|
|
|
|
|
|
int main(){
|
|
|
|
|
|
|
|
/* [0] CONSTANTES
|
|
|
|
=========================================================*/
|
2016-03-05 22:37:08 +00:00
|
|
|
ifstream dict_r( DICT_FILENAME );
|
2016-03-04 07:49:31 +00:00
|
|
|
|
2016-03-05 22:37:08 +00:00
|
|
|
|
|
|
|
/* [0.1] On recupere les mots du dictionnaire
|
2016-03-04 07:49:31 +00:00
|
|
|
=========================================================*/
|
2016-03-05 22:37:08 +00:00
|
|
|
vector<string> dict(0);
|
|
|
|
string line;
|
|
|
|
|
|
|
|
// On lit toutes les lignes
|
|
|
|
while( getline( dict_r, line ) )
|
|
|
|
dict.push_back( line );
|
|
|
|
|
|
|
|
|
|
|
|
/* [1] MENU D'ACTION
|
|
|
|
=========================================================*/
|
|
|
|
int action(-1);
|
|
|
|
|
|
|
|
do{
|
|
|
|
cout << "+-----------------------------------+" << endl;
|
|
|
|
cout << "+ 1) Jouer (saisie manuelle) +" << endl;
|
|
|
|
cout << "+ 2) Jouer (mot aleatoire) +" << endl;
|
|
|
|
cout << "+ 3) Ajouter un mot +" << endl;
|
|
|
|
cout << "+-----------------------------------+" << endl;
|
|
|
|
cout << "+ ";
|
|
|
|
cin >> action;
|
|
|
|
cout << "+-----------------------------------+" << endl;
|
|
|
|
|
|
|
|
}while( action < 1 || action > 3 );
|
|
|
|
|
2016-03-04 07:49:31 +00:00
|
|
|
|
2016-03-05 22:37:08 +00:00
|
|
|
/* [2] TRAITEMENT DE L'ACTION
|
2016-03-04 07:49:31 +00:00
|
|
|
=========================================================*/
|
2016-03-05 22:37:08 +00:00
|
|
|
switch(action){
|
|
|
|
|
|
|
|
/* (1) Jeu avec saisie manuelle */
|
|
|
|
case 1:
|
|
|
|
play( input_word() );
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* (2) Jeu avec mot aleatoire */
|
|
|
|
case 2:
|
|
|
|
play( pickup_word(dict) );
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
/* (3) Saisie d'un nouveau mot dans le dictionnaire */
|
|
|
|
case 3:
|
|
|
|
append_word();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* RENVOIE LA SAISIE UTILISATEUR
|
|
|
|
*
|
|
|
|
* @return out<string> Mot saisi par l'utilisateur
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
string input_word(){
|
|
|
|
string out;
|
2016-03-04 13:18:01 +00:00
|
|
|
cout << "Mot secret: ";
|
2016-03-05 22:37:08 +00:00
|
|
|
cin >> out;
|
|
|
|
cin.ignore();
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* RENVOIE UN MOT ALEATOIRE DU DICTIONNAIRE
|
|
|
|
*
|
|
|
|
* @dict_r<ifstream> Lecteur sur le dictionnaire
|
|
|
|
*
|
|
|
|
* @return out<string> Mot choisi aleatoirement dans le dictionnaire
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
string pickup_word(vector<string> dict){
|
|
|
|
srand(time(0));
|
|
|
|
|
|
|
|
int index = rand() % dict.size();
|
|
|
|
|
|
|
|
return dict[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* AJOUTE UN MOT AU DICTIONNAIRE
|
|
|
|
*
|
|
|
|
* @nomParam<typeParam> Description du param
|
|
|
|
*
|
|
|
|
* @return nomRetour<typeRetour> Description du retour
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void append_word(){
|
|
|
|
// objet d'ecriture sur le dictionnaire
|
|
|
|
ofstream dict_w( DICT_FILENAME, ios::app );
|
|
|
|
|
|
|
|
string newword;
|
|
|
|
cout << "Nouveau mot: " << endl;
|
|
|
|
cin >> newword;
|
2016-03-04 13:18:01 +00:00
|
|
|
cout << endl;
|
|
|
|
|
2016-03-05 22:37:08 +00:00
|
|
|
// on ajoute le mot
|
|
|
|
dict_w << newword << endl;
|
|
|
|
|
|
|
|
cout << "Mot enregistre!" << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* JOUE AU JEU DU MOT_MYSTERE
|
|
|
|
*
|
|
|
|
* @word<string> Mot a deviner
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void play(string word){
|
|
|
|
/* [0] VARIABLES
|
|
|
|
=========================================================*/
|
|
|
|
string shuffled;
|
|
|
|
|
2016-03-04 13:18:01 +00:00
|
|
|
// On met le mot en majuscules
|
|
|
|
word = toUpperCase(word);
|
2016-03-04 07:49:31 +00:00
|
|
|
|
2016-03-04 13:18:01 +00:00
|
|
|
/* [2] On melange les lettres du mot
|
2016-03-04 07:49:31 +00:00
|
|
|
=========================================================*/
|
2016-03-04 13:18:01 +00:00
|
|
|
shuffled = shuffle(word);
|
2016-03-04 07:49:31 +00:00
|
|
|
|
2016-03-04 13:18:01 +00:00
|
|
|
/* [3] JEU > utilisateur
|
2016-03-04 07:49:31 +00:00
|
|
|
=========================================================*/
|
2016-03-04 13:18:01 +00:00
|
|
|
string guessed;
|
|
|
|
|
|
|
|
do{
|
|
|
|
cout << "What is this world ? " << shuffled << endl;
|
|
|
|
cin >> guessed;
|
|
|
|
cout << endl;
|
|
|
|
|
|
|
|
}while( toUpperCase(guessed) != word );
|
|
|
|
|
|
|
|
cout << "Vous avez gagne!!" << endl;
|
2016-03-04 07:49:31 +00:00
|
|
|
}
|