hangman game in C++

Asked

Viewed 37 times

-1

Hi guys. I have a problem regarding a specific part of the game, which is already complete. The only problem is the condition I put to check if the word was completely guessed. My code:

if (strchr(palavra,letra[0])!= NULL) {
      ptr =strchr(palavra, letra[0]);

      while(ptr != NULL){
        i = (ptr-palavra);
        ptr = strchr(ptr+1, letra[0]);
        tela[i] = letra[0];
        acert++;
      }

      for(i=0;i<strlen(palavra);i++){
        cout << "\t" << tela[i];
      }
      }
    else {
      cout << "\n\tletra errada.\n";
      max++;
    }

The acert++ counts how many letters were hit and if the number of letters is equal to the word size, it says that Voce won. The problem is that as he goes through this condition every time the letter is hit, if someone types the same letter repeatedly, he counts that same letter right more than once.

How can I say that:

If letter not contained in array that displays word count acert++

OR

If letter ja contained in array that displays word not count acert++

I think the first way is easier to increase, just do not know how. Any help is welcome. I thank you already.

  • thinking logically would no longer be valid a condition that checks whether the letter repeats itself 2 or 3 times followed, with this the acert++ would change to acert-- it removes 1 or more hits, causing the repeated letters not to be counted as certain.

  • That may be, but how do I write this condition? "condition that checks whether the letter repeats 2 or 3 times in a row". That is my central doubt.

  • Cara don’t know how to solve in C++, but what you can do is create a variable to count which letters have already been chosen and not let repeat letter

  • @user237530 friend gets a little difficult only with a part of the code, would have to make it available in full?

1 answer

0


I was able to solve!

I added this to the condition of the loop:


verif[0] = '_';

  while(max <= 5 || (strchr(tela, verif[0]) != NULL)){

And I added at the end:

    else if (strchr(tela, verif[0]) == NULL){
      limpar;
      cout << "\n\n\n\tVoce venceu! Palavra Adivinhada: " << palavra << endl;
      break;
    }

He checks if he still has _ in the word and if it has been fully guessed (i.e., has only letters and no _ ), it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.