Results 1 to 13 of 13

Thread: Hangman problem

  1. #1
    LetsG0Blue is offline Member
    Join Date
    Feb 2013
    Posts
    68
    Rep Power
    0

    Default Hangman problem

    I'm a little lost, it's a simple hangman game w/o any lives. I don't believe my

    static boolean printCurrStatus(String strToGuess, String userInputs) and static void playGame()

    are working properly like they should. Could anybody take a look and point me in the right direction?

    Java Code:
    import java.util.*;
    
    class Hangman
    {
        static Scanner scan = new Scanner(System.in);
        static Random rand = new Random();
        
        // The following routine will determine if the character c
        // is inside the String str.  A true is returned if it is inside.
        
        static boolean isIn(char c, String str)
    
        {
        	if (c == str.length())		{
        		System.out.println("Letter found");
        		return true;
        	}
        	else		{
        		System.out.println("Letter isn't found.");
        		return false;
        	}
    
        }
        
        // If userInputs contains "ger" and strToGuess contains "tiger" then
        // the following routine prints out an output that looks something like:
        //
        // Current Status for userInpts=ger
        // _ _ g e r
        
        // This routine returns true if all letters were guessed, otherwise false is returned.
        
        
        static boolean printCurrStatus(String strToGuess, String userInputs)
        {
        	 char [] arrCharPrint = new char[strToGuess.length()];
             System.out.println("Current user input : " + userInputs);
        
             for(int i = 0; i < userInputs.length(); i++)
             {
               
                for(int j = 0; j < strToGuess.length(); j++) 
                {
                     if(userInputs.charAt(i) == strToGuess.charAt(j))
                     {
                         arrCharPrint[j] = strToGuess.charAt(j);
                     }
                     else if (i == 0)
                     {
                         arrCharPrint[j] = '_';
                     }
                }
             }
    		return true;
    			
        }
         
        static String getNextWordToGuess()
        {
        	String HiddenWord = " ";
        	
            for (int index = 1; index <= 10; index++){
              int word =  rand.nextInt(9);
        	
    		switch(word)
            {
            case 1: HiddenWord = "elephant"; 
             	break;
            case 2: HiddenWord = "tiger"; 
            	break;
            case 3: HiddenWord = "monkey"; 
            	break;
            case 4: HiddenWord = "baboon"; 
            	break;
            case 5: HiddenWord = "barbeque";
            	break;
            case 6: HiddenWord = "giraffe"; 
            	break;
            case 7: HiddenWord = "simple"; 
            	break;
            case 8: HiddenWord = "zebra";
            	break;
            case 9: HiddenWord = "porcupine"; 
            	break;
            case 10: HiddenWord = "puppy"; 
            	break;
            	}
            }
    		return HiddenWord;
        }
        
        // The following routine plays the hangman game. It calls getNextWordToGuess to
        // get the word that should be guessed.  It then has a loop which outputs the 
        // following prompt:
        // Enter next letter
        //
        // A String named userInputs stores all letters selected already.  
        // Then the routine printCurrStatus is called to print out the current status of
        // the guessed word.  If printCurrStatus returns true, we are done.
         
        static void playGame()
        {        
            String strToGuess = getNextWordToGuess();
            String blank = " _ ";
            String userInputs = null;
            
            for (int i = 0; i <= strToGuess.length(); i++)
            {
            	System.out.println("Enter next letter");
                userInputs = scan.next();
            }
          
    		printCurrStatus(userInputs, strToGuess);
    
        }
        
        public static void main(String[] args)
        {
        	Hangman startGame = new Hangman( );
        	startGame.playGame();
           
            
        }
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Hangman problem

    Quote Originally Posted by LetsG0Blue View Post
    I don't believe my

    static boolean printCurrStatus(String strToGuess, String userInputs) and static void playGame()

    are working properly like they should.
    You need to describe the problem. Compiler errors? post them, along with the exact code that didn't compile (so the line numbers match). Runtime Exceptions? ditto. Not getting the expected output? post details of expected and actual outputs, alone with inputs. Computer goe up in a puff of green smoke? run for the hills.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    LetsG0Blue is offline Member
    Join Date
    Feb 2013
    Posts
    68
    Rep Power
    0

    Default Re: Hangman problem

    Quote Originally Posted by DarrylBurke View Post
    You need to describe the problem. Compiler errors? post them, along with the exact code that didn't compile (so the line numbers match). Runtime Exceptions? ditto. Not getting the expected output? post details of expected and actual outputs, alone with inputs. Computer goe up in a puff of green smoke? run for the hills.

    db
    That last part was pretty funny. I can't get this to print the way it should.

    Like if it the random word is monkey it doesn't do a _ _ _ _ _ _



    Java Code:
     static boolean printCurrStatus(String strToGuess, String userInputs)
        {
             char [] arrCharPrint = new char[strToGuess.length()];
             System.out.println("Current user input : " + userInputs);
         
             for(int i = 0; i < userInputs.length(); i++)
             {
                
                for(int j = 0; j < strToGuess.length(); j++) 
                {
                     if(userInputs.charAt(i) == strToGuess.charAt(j))
                     {
                         arrCharPrint[j] = strToGuess.charAt(j);
                     }
                     else if (i == 0)
                     {
                         arrCharPrint[j] = '_';
                     }
                }
             }
            return true;

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default Re: Hangman problem

    Suppose String 'word' is the word to guess and 'correct' is a String that contains correct letters (they're already guessed); have a look at this:

    Java Code:
    word.replaceAll("[^"+correct+"]", "-");
    You have some reading to do: start reading the API documentation for the Pattern class ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    LetsG0Blue is offline Member
    Join Date
    Feb 2013
    Posts
    68
    Rep Power
    0

    Default Re: Hangman problem

    I'm completely lost... I don't mean to not give enough information, but I don't know what information to really give.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default Re: Hangman problem

    Quote Originally Posted by LetsG0Blue View Post
    I'm completely lost... I don't mean to not give enough information, but I don't know what information to really give.
    Did my reply scare you off? The code snippet does exactly what you want ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    112
    Rep Power
    0

    Default Re: Hangman problem

    if i were you i would do it with an Array for example

    at first time i would print out something like this

    Java Code:
    System.out.println(" _ " * SecretWord.length);
    then i would create an Array with length like the SecretWord for example if SecretWord was Apple the length of Apple is 5 so i would create an String Array = new String[5]

    like :

    Java Code:
    for(int i = 0; i < SecretWord.length ; i ++){
    	MyArray[i] = " _ " ;  
    }
    
    //so if i print out the Array 
    for(int i = 0 ; i < MyArray.length ; i ++){
    	System.out.print(MyArray[i]);
    }
    i will get :

    _ _ _ _ _

    and everytime that user give a letter i would check in which index there is and then i will put it in the right place in MyArray
    for example if user gives the letter ' l ' i will search in which index in my SecretWord the letter "l" exist in Apple it's in index 3 so i will go
    (k in my example would be 3)
    Java Code:
    MyArray[k]=usersInput
    and one more advice.. put all your words in an Array and then take a random item ... it would be much better
    Last edited by ShadowWalker; 03-29-2013 at 10:11 AM.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default Re: Hangman problem

    @ShadowWalker: please test your code before you post it; your code doesn't make sense in Java (it is Python-like code at best).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    112
    Rep Power
    0

    Default Re: Hangman problem

    dump... you are right :P
    this is full python System.out.println(" _ " * SecretWord.length);
    i am study java and python as well :P my mistake ;)

  10. #10
    LetsG0Blue is offline Member
    Join Date
    Feb 2013
    Posts
    68
    Rep Power
    0

    Default Re: Hangman problem

    I apologize I was just up late last night stressing out.

    My output is bolded my Letters Gussed (found in printCurrStatus) is printing out the word that needs to be found instead of the letters used. Also my _ _ _ _, isn't keeping track of all the letters that were found. In my output when 'e' is found the 'a' dissapears; this also is found in (printCurrStatus).

    But my output is this.

    Enter next letter:
    a
    _ a _ _ _ _ _ _
    Current user input : barbeque
    Enter next letter:
    d
    _ _ _ _ _ _ _ _
    Current user input : barbeque
    Enter next letter:
    e
    _ _ _ _ e _ _ e
    Current user input : barbeque


    Instead of hiding the word it is giving it out .

    Thank you Jos!

    Java Code:
    import java.util.*;
    
    class Hangman
    {
        static Scanner scan = new Scanner(System.in);
        static Random rand = new Random();
        
        static boolean isIn(char c, String str)
        {
    		int position = str.indexOf(c);  
    
    		for (int i = 0; i <= position; i++)
    		{
    			if (position != 0) 
    				return true;
    		}
    
    		return false;
    
        }
       
        static boolean printCurrStatus(String strToGuess, String userInputs)
        {
     
            for (int i = 0; i < strToGuess.length(); i++)
            {
                char c = userInputs.charAt(0);
                
            	if (isIn(c, strToGuess) == true)
            	{
            		System.out.print(strToGuess.charAt(i) + " ");
            	}
            	else if (strToGuess.charAt(i) == c)
            	{
            		System.out.print(strToGuess.charAt(i) + " ");
            	}
            	else
            		System.out.print(strToGuess.replaceAll("[^"+userInputs+"]", "-"));
            		System.out.print("Letters Guessed: " + userInputs);
            }
    		return false;
        }
        static String[] getNextWordToGuess()
        {
            String[] HiddenWord = new String[]{"elephant", "tiger", "monkey", "baboon", "barbeque", "giraffe", "simple", "zebra", "porcupine", "aardvark"};
    		
            return HiddenWord;
        }
        
        // The following routine plays the hangman game. It calls getNextWordToGuess to
        // get the word that should be guessed.  It then has a loop which outputs the 
        // following prompt:
        // Enter next letter
        //
        // A String named userInputs stores all letters selected already.  
        // Then the routine printCurrStatus is called to print out the current status of
        // the guessed word.  If printCurrStatus returns true, we are done.
         
    	static void playGame()
    	{        
        	Random r = new Random();
        	
    		String userInputs;
    		String strToGuess = getNextWordToGuess()[r.nextInt(getNextWordToGuess().length)];
    
    		for (int i = 0; i < strToGuess.length(); i++)
    		{
    			System.out.println("Enter next letter");
    			userInputs = scan.nextLine();
    
    			printCurrStatus(userInputs, strToGuess);
    		}
    	}
    
    	public static void main(String[] args)
    	{
    		Hangman startGame = new Hangman( );
    		startGame.playGame();
    
    
    	}
    }
    Last edited by LetsG0Blue; 03-29-2013 at 01:57 PM.

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Hangman problem

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  12. #12
    LetsG0Blue is offline Member
    Join Date
    Feb 2013
    Posts
    68
    Rep Power
    0

    Default Re: Hangman problem

    Quote Originally Posted by DarrylBurke View Post
    I'm sorry about that I'll make sure it's done in future posts, but I haven't been helped there.

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default Re: Hangman problem

    Near the bottom of this page you can find other threads about this 'Hangman' problem; in one of them I posted a complete spoiler (it uses regular expressions).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Hangman Radio Button Problem
    By Ahdood in forum New To Java
    Replies: 8
    Last Post: 12-01-2011, 02:09 AM
  2. Hangman with GUİ
    By KSBeyaz in forum New To Java
    Replies: 9
    Last Post: 11-20-2011, 06:45 PM
  3. Hangman
    By Feriscool in forum New To Java
    Replies: 23
    Last Post: 05-17-2011, 06:54 PM
  4. hangman
    By javaMike in forum Advanced Java
    Replies: 2
    Last Post: 11-14-2009, 09:06 AM
  5. Need help with Hangman!!!
    By chinasome in forum New To Java
    Replies: 10
    Last Post: 11-09-2008, 04:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •