Revision.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include "Revision.h"
  2. using namespace std;
  3. /// Verb >
  4. Verb::Verb( string mot[4] )
  5. {
  6. for ( int i(0); i<4; i++ )
  7. m_mot[i] = mot[i];
  8. }
  9. void Verb::afficher( unsigned int hide )
  10. {
  11. for ( unsigned int i(0); i<4; i++ )
  12. {
  13. if ( i != hide )
  14. cout << "???";
  15. else
  16. cout << m_mot[i];
  17. cout << " - ";
  18. }
  19. cout << endl;
  20. }
  21. string Verb::get( unsigned int select )
  22. {
  23. if ( select < 4 )
  24. return m_mot[ select ];
  25. return "FAILURE:HORS BORNES";
  26. }
  27. /// Revision >
  28. Revision::Revision():m_total(0)
  29. {
  30. srand(time(0));
  31. }
  32. Revision::~Revision()
  33. {
  34. while ( !m_file.empty() )
  35. {
  36. delete m_file.front();
  37. m_file.pop();
  38. }
  39. }
  40. bool Revision::load( string file )
  41. {
  42. /// Démarrage
  43. ifstream flux( file.c_str() );
  44. string mot[4];
  45. unsigned int n(0);
  46. Verb* verbTempo;
  47. /// Lecture
  48. while ( flux >> mot[n] )
  49. {
  50. n++;
  51. if ( n == 4 )
  52. {
  53. verbTempo = new Verb( mot );
  54. m_file.push( verbTempo );
  55. n = 0;
  56. m_total++;
  57. }
  58. }
  59. /// Fin
  60. return 0;
  61. }
  62. void Revision::afficher()
  63. {
  64. m_select = rand() % 4;
  65. m_file.front()->afficher( m_select );
  66. }
  67. bool Revision::corriger( string saisie[3] )
  68. {
  69. /// Vérification
  70. bool exact(true);
  71. unsigned int i(0), n(0);
  72. for ( i = 0; i<4; i++)
  73. {
  74. if ( i != m_select ) {
  75. if ( m_file.front()->get( i ) != saisie[n] )
  76. exact = false;
  77. n++;
  78. }
  79. }
  80. /// Réussite
  81. if ( exact ) {
  82. cout << "\033[1;32m" << "Well done ! Progression : ";
  83. cout << 100 - ( m_file.size() * 100 / m_total ) << "%" << "\033[0m" << endl;
  84. /// Retirer de la liste
  85. delete m_file.front();
  86. m_file.pop();
  87. if ( m_file.empty() ) return false;
  88. }
  89. /// Echec
  90. else {
  91. cout << "\033[1;31m" << "Oups ! You will try again later ..." << "\033[0m" << endl;
  92. cout << "The good answer were";
  93. for ( int i(0); i<4; i++ )
  94. cout << ' ' << '"' << m_file.front()->get( i ) << '"';
  95. cout << endl;
  96. /// Déplacer à la fin
  97. Verb* verbTempo( m_file.front() );
  98. m_file.pop();
  99. m_file.push( verbTempo );
  100. }
  101. cout << endl << "Next :" << endl;
  102. return true;
  103. }