I am new to java. All suggestions as far as this program goes is appreciated however please do not change the entire code because I am aware that there are bugs/issues with it. The questions I have is when I run this program(eclipse) I have a Dictionary.txt and that works fine. However when I guess the word correctly the while loop of the game does not end, how do I fix this? Can I not compare a StringBuilder to a String? When I guess the word wrong(run out of attempts) the program ends the while loop but jumps back into it after the endGame() method. I assume because it picks up from where the method was called. How can I change this a little to allow the game to function as I intended? I am not familiar with the structure of a game in java. Also how do I attach programs to the forum as other members have? Tried to attach a .java and it would not let me. Apologize for the .txt format
PHP Code:
public class Hangman1 {
public static void main(String[] args){
initGame();
}//end main
private static void initGame() { // needs io exception
System.out.println("Chris' Hangman");
System.out.println("Enter Number For Difficulty You Would Like To Play");
System.out.print("Easy: 1, Medium: 2, Hard: 3 -- ");
Scanner scan = new Scanner(System.in);
int diff = scan.nextInt();
while(true){
if(diff == 1 || diff == 2 || diff == 3 ){
getDifficulty(diff);
//break;
}
else{
System.out.print("Please Enter The Number 1, 2, Or 3 -- ");
diff = scan.nextInt();
//System.out.println("");
}
}//end while
}//end initGame()
private static void getDifficulty(int diff) {
Scanner scan = null;
try {
scan = new Scanner(new File("Dictionary"));
} catch (FileNotFoundException e) {
System.out.println("File Not Found");
System.exit(0);
}
ArrayList<String> easy = new ArrayList<String>();
ArrayList<String> medium = new ArrayList<String>();
ArrayList<String> hard = new ArrayList<String>();
while (scan.hasNextLine()){ // sorts file into 3 arrayLists based on length of word.
//while(true){
String s = new String(scan.nextLine());
if(s.length() > 7 && s.length() <= 9){ //hard 8-9 letters
hard.add(s);
//break;
}
if(s.length() > 5 && s.length() <= 7 ){ //medium 6-7 letters
medium.add(s);
//break;
}
if(s.length() > 3 && s.length() <= 5){ //easy 4-5 letters
easy.add(s);
//break;
}
}//end while
switch(diff){ //after method call within switch program continues to run through other cases or start over in an infinite loop
case 1:
Random rand1 = new Random();
int i1 = rand1.nextInt(easy.size());
startGame(easy.get(i1));// Get word from random index of ArrayList easy
case 2:
Random rand2 = new Random();
int i2 = rand2.nextInt(medium.size());
startGame(medium.get(i2));// Get word from random index of ArrayList medium
case 3:
Random rand3 = new Random();
int i3 = rand3.nextInt(hard.size());
startGame(hard.get(i3));// Get word from random index of ArrayList hard
}//end switch
}//end getDifficulty
private static void startGame(String string) {
//StringBuilder secret = new StringBuilder(string); // if you can compare a StringBuilder to a String then this is not needed
String secret = string;
String word = "";
//"word" = 1 dash for each char in secret until letter guessed correctly replaces a dash
for(int i = 0; i < secret.length(); i++){ // display dashes for length of word
word += "-";
//System.out.print(word); //test
}
StringBuilder guessedWord = new StringBuilder(word);
System.out.println(guessedWord); // print number of dashes for word
//-------------------BEGIN GUESSING----------------------------\\
int numGuesses = 0;
char guess;
ArrayList<Character> lettersGuessed = new ArrayList<Character>();
while (secret.equals(guessedWord) == false && numGuesses < 6){ // while you did not win or lose
System.out.print("Guess Letter: ");
Scanner scan = new Scanner(System.in);
guess = scan.next().charAt(0);
for(int i = 0; i < guessedWord.length(); i++){ //check if letter has been guessed
if(guessedWord.charAt(i) == guess){ //if letter is in word more than once then print statements will print more than once. BUG
System.out.println("You Already Guessed This Letter!");
System.out.print("Guess Again: ");
guess = scan.next().charAt(0); // if letter is guessed already this will assume player guesses a new letter next turn. BUG
}
}
lettersGuessed.add(guess);//add letter to letters guessed
for(int i = 0; i < guessedWord.length(); i++){ // check for letter in word
if(guess == secret.charAt(i)){
guessedWord.setCharAt(i, guess);
}
}//end for
System.out.println(guessedWord);//print new guessedWord after checking for letter in word
if(secret.indexOf(guess) == -1 ){ //if letter not in secret then numGuesses goes up by 1 and while loop continues
numGuesses++;
System.out.println("Incorrect Guesses: " + numGuesses);
System.out.println(guessedWord);
System.out.println("Secret: " + secret);
}
}//end while
if(numGuesses == 6){
endGame(false);
}
else{
endGame(true);
}
}//end startGame
private static void endGame(boolean won) {
Scanner scan = new Scanner(System.in);
if(won){
System.out.println("Great Job, You Won!");
System.out.print("Would You Like To Play Again? Y or N: ");
char playAgain = scan.next().charAt(0);
if(playAgain == 'y' || playAgain == 'Y'){
initGame();
}else{
}
}else{
System.out.println("You Lost! Better Luck Next Time!");
System.out.print("Would You Like To Play Again? Y or N: ");
char playAgain1 = scan.next().charAt(0);
if(playAgain1 == 'y' || playAgain1 == 'Y'){
initGame();
}else{
}
}//end else
}//end endGame
}// end class