Ok so i'm super struggling in writing a hangman game in blueJ. So far i have a class for canvas, a class to control the canvas, a class to generate random words, and the main class. Now the generating works fine, but i'm just so burned out where im at right now.. Please bare with me as i am super NEWB. Right now i am at the point where the person types a letter and then i search thru the word. I'm not sure how to handle when the word has the same letter in it twice, and im also not sure how to handle it when it is incorrect because when i compile and i type a letter if it is incorrect incorrect shows up the screen like 8 times, dont get why. i need some help someone!!! lol
Code:import java.util.*;
/**
* hangman
*
* @author petey
* @version (a version number or a date)
*/
public class Hangman
{
// instance variables - replace the example below with your own
private Scanner InputS;
private int MissedTimes;
private Generator generator;
/**
* Constructor for objects of class Hangman
*/
public Hangman()
{
// initialise instance variables
InputS = new Scanner(System.in);
generator = new Generator();
MissedTimes = 0;
}
public String word()
{
String
word = generator.generate();
return word;
}
public void startGame()
{
boolean yesno=false;
System.out.print("Play HangMan?(Yes/No)");
String toDo = InputS.nextLine();
toDo = toDo.toLowerCase();
if (toDo.equalsIgnoreCase("yes"))
{
yesno=true;
}
else
{
yesno=false;
}
while(yesno)
{
String secretWord = generator.generate();
while (MissedTimes < 8 ) {
System.out.print("Please type a letter");
System.out.print(" ");
String Letter = InputS.nextLine();
char letterChar = Letter.charAt(0);
for (int i = 0; i < secretWord.length (); i++)
{
if(secretWord.charAt(i) == letterChar) {
// found
System.out.print(" ");
System.out.print("Correct! "+ letterChar + "");
System.out.print(" ");
// draw the letter on the canvas
}
else
{
System.out.print("Incorrect");
}
}
}
System.out.print("Play HangMan?(Yes/No)");
InputS = new Scanner(System.in);
toDo = InputS.nextLine();
toDo = toDo.toLowerCase();
if (toDo.equalsIgnoreCase("yes"))
{
yesno=true;
}
else
{
yesno=false;
System.out.print("Goodbye.");
}
}
}
}

