ignore saisie clavier pendant décompte.
Bonjour à tous.
je fais un petit jeu où j'ai un décompte en seconde et je demande une réponse.
Le problème c'est pendant le décompte si l'utilisateur tape une ou des touches et valide avec la touche return, il le prend en compte avant la demande la réponse.
voici le code:
Code:
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
|
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>
#include <thread>
#include <chrono>
#include <unistd.h>
#include <iomanip>
#include <limits>
static bool finish = false;
/* void compteurTemps(int nombreCoups, std::string motMelange)
{
using namespace std::literals::chrono_literals;
for (int i(10); i >= 0; i--)
{
std::system("clear");
std::cout << "nombre restant d'essais : " << nombreCoups << "\r" << std::endl;
std::cout << "\nQuel est ce mot : " << motMelange << "\r" << std::endl;
std::cout << "temps restant : " << std::right << std::setw(2) << std::setfill('0') << i << "\r" << std::endl;
std::this_thread::sleep_for(1s);
}
std::system("clear");
std::cout << "\nRéponse : " << std::flush;
} */
std::string melangerlettres(std::string mot)
{
std::string melange;
int position(0);
while (mot.size() != 0)
{
position = rand() % mot.size();
melange += mot[position];
mot.erase(position, 1);
}
return melange;
}
int main()
{
std::string motMystere, motMelange, motUtilisateur, reponseRejouer;
srand(time(0));
using namespace std::literals::chrono_literals;
do
{
std::system("clear");
std::cout << "mot mystere: " << std::endl;
std::cin >> motMystere;
std::system("clear");
int nombreCoups = motMystere.size();
motMelange = melangerlettres(motMystere);
do
{
std::system("clear");
std::cout << "nombre restant d'essais : " << nombreCoups << std::endl;
std::cout << "\nQuel est ce mot : " << motMelange << std::endl;
//std::thread reponse(compteurTemps, nombreCoups, motMelange);
//reponse.join();
for (int i(3); i >= 0; i--)
{
std::system("clear");
std::cout << "nombre restant d'essais : " << nombreCoups << "\r" << std::endl;
std::cout << "\nQuel est ce mot : " << motMelange << "\r" << std::endl;
std::cout << "temps restant : " << std::right << std::setw(2) << std::setfill('0') << i << "\r" << std::endl;
std::this_thread::sleep_for(1s);
}
std::system("clear");
std::cout << "\nRéponse : " << std::flush;
std::cin >> motUtilisateur;
nombreCoups -= 1;
if (nombreCoups < 1)
{
std::cout << "COUPS DEPASSES." << std::endl;
motUtilisateur = motMystere;
}
else
{
if (motUtilisateur != motMystere)
{
std::cout << "NON RECOMMENCE." << std::endl;
}
else
{
std::cout << "BRAVO." << std::endl;
}
}
} while (motUtilisateur != motMystere);
std::cout << "\nVoulez-vous rejouer ? (o/n)" << std::endl;
std::cin >> reponseRejouer;
} while (reponseRejouer == "o" || reponseRejouer == "O");
return 0;
} |