Advertisement
Guest User

Untitled

a guest
Nov 27th, 2014
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.16 KB | None | 0 0
  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18. main.cpp
  19.  
  20. #include <iostream>
  21. #include "dictionnaire.h"
  22. #include <string>
  23. #include <cstdlib>
  24. #include <time.h>
  25.  
  26. using namespace std;
  27.  
  28. int nombreLettre(string motUser, string motInitial)
  29. {
  30.     int nombreLettreJuste(0), position(0);
  31.  
  32.     while (position < motInitial.size())
  33.     {
  34.         if (motUser[position] == motInitial[position])
  35.         {
  36.             nombreLettreJuste++;
  37.         }
  38.         position++;
  39.     }
  40.     return nombreLettreJuste;
  41. }
  42.  
  43. string melange(string motAleatoire)
  44. {
  45.     int position(0);
  46.     string motMelanger;
  47.     srand(time(NULL));
  48.     while (motAleatoire.size() != 0)
  49.     {
  50.         position = rand() % motAleatoire.size();
  51.         motMelanger += motAleatoire[position];
  52.         motAleatoire.erase(position, 1);
  53.     }
  54.     return motMelanger;
  55. }
  56. int verification(string motInitial, string motMelanger)
  57. {
  58.     string motUser;
  59.     int nombreCoup(0), nombreLettreJuste(0);
  60.     while (motUser != motInitial)
  61.     {
  62.         cout << "Mot a deviner: " << motMelanger << endl << "Votre proposition: ";
  63.         cin >> motUser;
  64.         nombreCoup++;
  65.         system("cls");
  66.         cout << "Derniere propositioon: " << motUser << endl << "Lettres correctes: " << nombreLettre(motUser, motInitial) << endl;
  67.         if (motUser == "spoil")
  68.         {
  69.             system("cls");
  70.             cout << "SPOIL: Mot initial: " << motInitial << endl;
  71.         }
  72.     }
  73.     return nombreCoup;
  74. }
  75. void partie(string dir)
  76. {
  77.     string motRandom, motMelanger;
  78.     int nombreCoup(0);
  79.     motRandom = motHasard(dir);
  80.     motMelanger = melange(motRandom);
  81.     nombreCoup = verification(motRandom, motMelanger);
  82.  
  83.  
  84. }
  85. void affichage()
  86. {
  87.     cout << "Mot Mystere: v0.01" << endl << "";
  88. }
  89. int main()
  90. {
  91.     string directory("dico.txt");
  92.     partie(directory);
  93.  
  94. }
  95.  
  96.  
  97.  
  98. dictionnaire.cpp
  99. /////////
  100.  
  101. #include "dictionnaire.h"
  102. #include <fstream>
  103. #include <string>
  104. #include <iostream>
  105. #include <cstdlib>
  106. #include <time.h>
  107.  
  108. std::string motHasard(std::string dir)
  109. {
  110.     int nombreLigne, ligneHasard;
  111.  
  112.     std::string motRandom;
  113.     nombreLigne = getLigne(dir, nombreLigne);
  114.     ligneHasard = nombreHasard(nombreLigne);
  115.     motRandom = getRandomMot(ligneHasard, dir);
  116.     return motRandom;
  117. }
  118. std::string getRandomMot(int random, std::string dir)
  119. {
  120.     std::ifstream getMotHasard(dir.c_str());
  121.     std::string resultat("none"), ligne("none");
  122.     if (getMotHasard)
  123.     {
  124.         for (int i(0) ; i < random - 1 ; i++)
  125.         {
  126.             getline(getMotHasard, ligne);
  127.         }
  128.         resultat = ligne;
  129.     }
  130.     else
  131.     {
  132.         std::cout << "Erreur ouverture flux getMotHasard";
  133.     }
  134.     return resultat;
  135. }
  136.  
  137.  
  138. int nombreHasard(int nombreLigne)
  139. {
  140.     int nombreAleatoire(1);
  141.     srand(time(NULL));
  142.     nombreAleatoire = rand() % nombreLigne;
  143.     return nombreAleatoire;
  144. }
  145.  
  146. ////////
  147. dictionnaire.h
  148.  
  149. #ifndef DICTIONNAIRE_H_INCLUDED
  150. #define DICTIONNAIRE_H_INCLUDED
  151.  
  152. #include <string>
  153.  
  154. std::string motHasard(std::string dir);
  155. int getLigne(std::string dir, int nombre);
  156. int nombreHasard(int nombreLigne);
  157. std::string getRandomMot(int random, std::string dir);
  158.  
  159. #endif // DICTIONNAIRE_H_INCLUDED
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement