Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: Java Hangman!

  1. #1
    Junior Member JavaManNoob's Avatar
    Join Date
    Oct 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java Hangman!

    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 == || diff == || diff == ){
                        
    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<Stringeasy = new ArrayList<String>();
            
    ArrayList<Stringmedium = new ArrayList<String>();
            
    ArrayList<Stringhard = 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() > && s.length() <= 9){    //hard 8-9 letters
                    
    hard.add(s);
                    
    //break;
                
    }
                if(
    s.length() > && s.length() <= ){    //medium 6-7 letters
                    
    medium.add(s);
                    
    //break;
                
    }
                if(
    s.length() > && 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 0secret.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<CharacterlettersGuessed = 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 0guessedWord.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 0guessedWord.length(); i++){    // check for letter in word
                    
    if(guess == secret.charAt(i)){
                        
    guessedWord.setCharAt(iguess);
                        
                    }
                    
                 }
    //end for
                 
    System.out.println(guessedWord);//print new guessedWord after checking for letter in word 
                 
                 
    if(secret.indexOf(guess) == -){    //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 
    Attached Files Attached Files
    Last edited by JavaManNoob; October 29th, 2012 at 02:22 AM. Reason: PHP CODE!


Similar Threads

  1. Hangman java question double letters
    By sparky971 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 9th, 2012, 02:01 PM
  2. Hangman game for JAVA Beginning class
    By shahravi88 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 12th, 2011, 04:15 PM
  3. Hangman Game
    By Snow_Fox in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 29th, 2010, 04:07 PM
  4. Hangman
    By Tycho91 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 19th, 2010, 07:04 AM
  5. Java hangman game help...
    By AnotherNoob in forum AWT / Java Swing
    Replies: 16
    Last Post: December 4th, 2009, 11:17 PM

Tags for this Thread