Logic error in hangman game, I need it to only add to the counter if its wrong but it adds twice if its wrong...
$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
java hangman
New contributor
$endgroup$
put on hold as off-topic by Jamal♦ 3 mins ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Jamal
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
$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
java hangman
New contributor
$endgroup$
put on hold as off-topic by Jamal♦ 3 mins ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Jamal
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
$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
java hangman
New contributor
$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
java hangman
New contributor
New contributor
New contributor
asked 19 mins ago
Georgia Georgia
1
1
New contributor
New contributor
put on hold as off-topic by Jamal♦ 3 mins ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Jamal
If this question can be reworded to fit the rules in the help center, please edit the question.
put on hold as off-topic by Jamal♦ 3 mins ago
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Jamal
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
add a comment |
0
active
oldest
votes
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes