Logic error in hangman game, I need it to only add to the counter if its wrong but it adds twice if its wrong...












0












$begingroup$


I don't know where its adding to the counter within the code but its screwing everything up because I have a method to add a specific body part according to the counter (1 = head, etc). If anyone could find the error or give me a different way of doing it I'd really appreciate it!



//create new Console 
c = new Console ();
f = new Console ();

//declare variables
String inputWord; //the word entered by player 1 that player 2 will guess
char inputLetter; //the letter entered by player 2 that is the guess
String category; //the category entered by player 1 which acts as a hint for player 2
String tempString = ""; //a temporary string variable that holds the guess, used to convert char to String so that we can use the replace method
int numGuesses = 0; //counter which keeps track of the incorrect guesses
StringBuffer blanks = new StringBuffer (""); //a string buffer object that prints out the "?"'s based on the length of the word entered by player 1
boolean isWinner; //used in main to check if player 2 has correctly guessed the word, then it will break

//print instructions then clear them (working)
instructions();
c.readLine();
c.clear();

//get word input from user (working)
c.println("Player 1, please enter the word you would like Player 2 to guess. Please do not enter any spaces or punctuation. It will be deleted.");
inputWord = c.readLine();
inputWord = inputWord.toUpperCase();
c.println("Please enter a category that your word falls under. Don't be too specific!");
category = c.readLine();

//print category in other console (working)
f.println("Category: " + category);

//draw underscores for each letter of the word (working)
for (int i = 0; i < inputWord.length(); i++)
{
blanks = blanks.append("?");
}//rof

//print the underscores out (working)
f.print(blanks.toString());

//draw template (working)
drawGallows();

//clear screen
c.clear();

//prompt for user 2 to enter input
c.println("Player 2, please enter your letter guess in uppercase.");
c.println("If you think you know the word, just type it in!");

//main game loop
whileLoop:
while (numGuesses < 7)
{
c.println (numGuesses);
//get letter guess from player 2 (working)
inputLetter = c.readChar();

//converts the char to a string so we can use the StringBuffer method replace
tempString = Character.toString(inputLetter);

//if the letter is somewhere in the word then replace
if (inputWord.indexOf(inputLetter) != -1)
{

//Loop to check every letter in word, outputs letter if letter is located in the word
for (int i = 0; i < inputWord.length(); i++)
{
if (inputWord.charAt(i) == inputLetter)
{
//boolean isLetterdetected is true if theres a match
//isLetterDetected = true;

//replaces the underscore with the correctly guessed letter
blanks.replace(i,i+1,tempString);

//clear the screen
f.clear();

//reprint the template and category

drawGallows();
f.println("Category: " + category);

//print the new stringbuffer
f.print(blanks.toString());

}//fi

}//rof

}//fi

//else the letter is not anywhere in the word so increase counter, and draw body part
else
{
numGuesses++;
c.println (numGuesses);
drawBodyPart(numGuesses);
}

//check if player 2 has correctly solved the word
forLoop:
for (int i = 0; i < blanks.length(); i++)
{
if (blanks.indexOf("?") == -1)
{
isWinner = true;
results (numGuesses, isWinner, inputWord);
break whileLoop;
}//fi
else
{
break forLoop;
}//esle
}//rof

}//elihw








share







New contributor




Georgia is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$

















    0












    $begingroup$


    I don't know where its adding to the counter within the code but its screwing everything up because I have a method to add a specific body part according to the counter (1 = head, etc). If anyone could find the error or give me a different way of doing it I'd really appreciate it!



    //create new Console 
    c = new Console ();
    f = new Console ();

    //declare variables
    String inputWord; //the word entered by player 1 that player 2 will guess
    char inputLetter; //the letter entered by player 2 that is the guess
    String category; //the category entered by player 1 which acts as a hint for player 2
    String tempString = ""; //a temporary string variable that holds the guess, used to convert char to String so that we can use the replace method
    int numGuesses = 0; //counter which keeps track of the incorrect guesses
    StringBuffer blanks = new StringBuffer (""); //a string buffer object that prints out the "?"'s based on the length of the word entered by player 1
    boolean isWinner; //used in main to check if player 2 has correctly guessed the word, then it will break

    //print instructions then clear them (working)
    instructions();
    c.readLine();
    c.clear();

    //get word input from user (working)
    c.println("Player 1, please enter the word you would like Player 2 to guess. Please do not enter any spaces or punctuation. It will be deleted.");
    inputWord = c.readLine();
    inputWord = inputWord.toUpperCase();
    c.println("Please enter a category that your word falls under. Don't be too specific!");
    category = c.readLine();

    //print category in other console (working)
    f.println("Category: " + category);

    //draw underscores for each letter of the word (working)
    for (int i = 0; i < inputWord.length(); i++)
    {
    blanks = blanks.append("?");
    }//rof

    //print the underscores out (working)
    f.print(blanks.toString());

    //draw template (working)
    drawGallows();

    //clear screen
    c.clear();

    //prompt for user 2 to enter input
    c.println("Player 2, please enter your letter guess in uppercase.");
    c.println("If you think you know the word, just type it in!");

    //main game loop
    whileLoop:
    while (numGuesses < 7)
    {
    c.println (numGuesses);
    //get letter guess from player 2 (working)
    inputLetter = c.readChar();

    //converts the char to a string so we can use the StringBuffer method replace
    tempString = Character.toString(inputLetter);

    //if the letter is somewhere in the word then replace
    if (inputWord.indexOf(inputLetter) != -1)
    {

    //Loop to check every letter in word, outputs letter if letter is located in the word
    for (int i = 0; i < inputWord.length(); i++)
    {
    if (inputWord.charAt(i) == inputLetter)
    {
    //boolean isLetterdetected is true if theres a match
    //isLetterDetected = true;

    //replaces the underscore with the correctly guessed letter
    blanks.replace(i,i+1,tempString);

    //clear the screen
    f.clear();

    //reprint the template and category

    drawGallows();
    f.println("Category: " + category);

    //print the new stringbuffer
    f.print(blanks.toString());

    }//fi

    }//rof

    }//fi

    //else the letter is not anywhere in the word so increase counter, and draw body part
    else
    {
    numGuesses++;
    c.println (numGuesses);
    drawBodyPart(numGuesses);
    }

    //check if player 2 has correctly solved the word
    forLoop:
    for (int i = 0; i < blanks.length(); i++)
    {
    if (blanks.indexOf("?") == -1)
    {
    isWinner = true;
    results (numGuesses, isWinner, inputWord);
    break whileLoop;
    }//fi
    else
    {
    break forLoop;
    }//esle
    }//rof

    }//elihw








    share







    New contributor




    Georgia is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.







    $endgroup$















      0












      0








      0





      $begingroup$


      I don't know where its adding to the counter within the code but its screwing everything up because I have a method to add a specific body part according to the counter (1 = head, etc). If anyone could find the error or give me a different way of doing it I'd really appreciate it!



      //create new Console 
      c = new Console ();
      f = new Console ();

      //declare variables
      String inputWord; //the word entered by player 1 that player 2 will guess
      char inputLetter; //the letter entered by player 2 that is the guess
      String category; //the category entered by player 1 which acts as a hint for player 2
      String tempString = ""; //a temporary string variable that holds the guess, used to convert char to String so that we can use the replace method
      int numGuesses = 0; //counter which keeps track of the incorrect guesses
      StringBuffer blanks = new StringBuffer (""); //a string buffer object that prints out the "?"'s based on the length of the word entered by player 1
      boolean isWinner; //used in main to check if player 2 has correctly guessed the word, then it will break

      //print instructions then clear them (working)
      instructions();
      c.readLine();
      c.clear();

      //get word input from user (working)
      c.println("Player 1, please enter the word you would like Player 2 to guess. Please do not enter any spaces or punctuation. It will be deleted.");
      inputWord = c.readLine();
      inputWord = inputWord.toUpperCase();
      c.println("Please enter a category that your word falls under. Don't be too specific!");
      category = c.readLine();

      //print category in other console (working)
      f.println("Category: " + category);

      //draw underscores for each letter of the word (working)
      for (int i = 0; i < inputWord.length(); i++)
      {
      blanks = blanks.append("?");
      }//rof

      //print the underscores out (working)
      f.print(blanks.toString());

      //draw template (working)
      drawGallows();

      //clear screen
      c.clear();

      //prompt for user 2 to enter input
      c.println("Player 2, please enter your letter guess in uppercase.");
      c.println("If you think you know the word, just type it in!");

      //main game loop
      whileLoop:
      while (numGuesses < 7)
      {
      c.println (numGuesses);
      //get letter guess from player 2 (working)
      inputLetter = c.readChar();

      //converts the char to a string so we can use the StringBuffer method replace
      tempString = Character.toString(inputLetter);

      //if the letter is somewhere in the word then replace
      if (inputWord.indexOf(inputLetter) != -1)
      {

      //Loop to check every letter in word, outputs letter if letter is located in the word
      for (int i = 0; i < inputWord.length(); i++)
      {
      if (inputWord.charAt(i) == inputLetter)
      {
      //boolean isLetterdetected is true if theres a match
      //isLetterDetected = true;

      //replaces the underscore with the correctly guessed letter
      blanks.replace(i,i+1,tempString);

      //clear the screen
      f.clear();

      //reprint the template and category

      drawGallows();
      f.println("Category: " + category);

      //print the new stringbuffer
      f.print(blanks.toString());

      }//fi

      }//rof

      }//fi

      //else the letter is not anywhere in the word so increase counter, and draw body part
      else
      {
      numGuesses++;
      c.println (numGuesses);
      drawBodyPart(numGuesses);
      }

      //check if player 2 has correctly solved the word
      forLoop:
      for (int i = 0; i < blanks.length(); i++)
      {
      if (blanks.indexOf("?") == -1)
      {
      isWinner = true;
      results (numGuesses, isWinner, inputWord);
      break whileLoop;
      }//fi
      else
      {
      break forLoop;
      }//esle
      }//rof

      }//elihw








      share







      New contributor




      Georgia is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.







      $endgroup$




      I don't know where its adding to the counter within the code but its screwing everything up because I have a method to add a specific body part according to the counter (1 = head, etc). If anyone could find the error or give me a different way of doing it I'd really appreciate it!



      //create new Console 
      c = new Console ();
      f = new Console ();

      //declare variables
      String inputWord; //the word entered by player 1 that player 2 will guess
      char inputLetter; //the letter entered by player 2 that is the guess
      String category; //the category entered by player 1 which acts as a hint for player 2
      String tempString = ""; //a temporary string variable that holds the guess, used to convert char to String so that we can use the replace method
      int numGuesses = 0; //counter which keeps track of the incorrect guesses
      StringBuffer blanks = new StringBuffer (""); //a string buffer object that prints out the "?"'s based on the length of the word entered by player 1
      boolean isWinner; //used in main to check if player 2 has correctly guessed the word, then it will break

      //print instructions then clear them (working)
      instructions();
      c.readLine();
      c.clear();

      //get word input from user (working)
      c.println("Player 1, please enter the word you would like Player 2 to guess. Please do not enter any spaces or punctuation. It will be deleted.");
      inputWord = c.readLine();
      inputWord = inputWord.toUpperCase();
      c.println("Please enter a category that your word falls under. Don't be too specific!");
      category = c.readLine();

      //print category in other console (working)
      f.println("Category: " + category);

      //draw underscores for each letter of the word (working)
      for (int i = 0; i < inputWord.length(); i++)
      {
      blanks = blanks.append("?");
      }//rof

      //print the underscores out (working)
      f.print(blanks.toString());

      //draw template (working)
      drawGallows();

      //clear screen
      c.clear();

      //prompt for user 2 to enter input
      c.println("Player 2, please enter your letter guess in uppercase.");
      c.println("If you think you know the word, just type it in!");

      //main game loop
      whileLoop:
      while (numGuesses < 7)
      {
      c.println (numGuesses);
      //get letter guess from player 2 (working)
      inputLetter = c.readChar();

      //converts the char to a string so we can use the StringBuffer method replace
      tempString = Character.toString(inputLetter);

      //if the letter is somewhere in the word then replace
      if (inputWord.indexOf(inputLetter) != -1)
      {

      //Loop to check every letter in word, outputs letter if letter is located in the word
      for (int i = 0; i < inputWord.length(); i++)
      {
      if (inputWord.charAt(i) == inputLetter)
      {
      //boolean isLetterdetected is true if theres a match
      //isLetterDetected = true;

      //replaces the underscore with the correctly guessed letter
      blanks.replace(i,i+1,tempString);

      //clear the screen
      f.clear();

      //reprint the template and category

      drawGallows();
      f.println("Category: " + category);

      //print the new stringbuffer
      f.print(blanks.toString());

      }//fi

      }//rof

      }//fi

      //else the letter is not anywhere in the word so increase counter, and draw body part
      else
      {
      numGuesses++;
      c.println (numGuesses);
      drawBodyPart(numGuesses);
      }

      //check if player 2 has correctly solved the word
      forLoop:
      for (int i = 0; i < blanks.length(); i++)
      {
      if (blanks.indexOf("?") == -1)
      {
      isWinner = true;
      results (numGuesses, isWinner, inputWord);
      break whileLoop;
      }//fi
      else
      {
      break forLoop;
      }//esle
      }//rof

      }//elihw






      java hangman





      share







      New contributor




      Georgia is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.










      share







      New contributor




      Georgia is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      share



      share






      New contributor




      Georgia is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 6 mins ago









      Georgia Georgia

      1




      1




      New contributor




      Georgia is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Georgia is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Georgia is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          0






          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          Georgia is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f211844%2flogic-error-in-hangman-game-i-need-it-to-only-add-to-the-counter-if-its-wrong-b%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          Georgia is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          Georgia is a new contributor. Be nice, and check out our Code of Conduct.













          Georgia is a new contributor. Be nice, and check out our Code of Conduct.












          Georgia is a new contributor. Be nice, and check out our Code of Conduct.
















          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f211844%2flogic-error-in-hangman-game-i-need-it-to-only-add-to-the-counter-if-its-wrong-b%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Сан-Квентин

          Алькесар

          Josef Freinademetz