fixed fouras module revealing too many characters when asked for a clue

This commit is contained in:
Anselme 2017-04-21 17:21:53 +02:00
parent d78d63f0de
commit 5d5a7e862b

View File

@ -53,21 +53,27 @@ bool FourasModule::messageHandler(Message msg)
if(current != -1)
{
QString str = riddles->getAnswer(current);
std::srand(qHash(str));
// prepare the clue string
QString finalString = "_";
for(int i=0; i<str.size() - 1; ++i)
finalString.append(" _");
// initialize the rand function with the hash of the answer so the rand() function always reveal the characters in the same order
std::srand(qHash(str));
int nbRevealed = 0;
for(int i=0; i<nbClues; ++i)
{
// find a character to reveal
int id = std::rand()%str.size();
if(finalString[id*2] == QChar('_'))
{
++nbRevealed;
finalString[id*2] = str[id];
}
else
++nbClues;
while(finalString[id*2] != QChar('_'))
id = std::rand()%str.size();
// reveal it
++nbRevealed;
finalString[id*2] = str[id];
// if too many letters are revealed, the players have lost the game
if(nbRevealed == str.size())
{
answer = say(QString("%1 - Perdu ! La réponse était : %2").arg(current).arg(str));