Hangman Game

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 2984 Views - Last Post: 06 February 2009 - 11:47 AM Rate Topic: -----

#1 mrmichaela   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 03-February 09

Hangman Game

Posted 03 February 2009 - 09:16 AM

Greetings,
I am new to java and I would like to ask you if in my indexWord() the storing of each String's character is correct? And if not can you suggest me some other way to do it!
(My program is at the early stages yet as you can see :D)

Cheers

 import java.util.*;
import acm.program.*;

public class Hangman extends ConsoleProgram {
	
	public String[] wordList = new String[3];
	public Random rand = new Random();
	
	
	
	public void run(){
		setWords();
		pickWord();
		indexWord(pickWord());
	}
	
	//This class stores some words in an array for the game
	public void setWords(){
		String s1 = "lagoon";
		String s2 = "java";
		String s3 = "dog";
		
		
		wordList[0] = s1;
		wordList[1] = s2;
		wordList[2] = s3;
	}
	
	
	
	//This class picks a random word from the wordList array using the random generator
	public String pickWord(){
		
		String currentWord = "";
		int randomNum = rand.nextInt(3);
		
		currentWord = wordList[randomNum];
		//println(currentWord);		
		
		return currentWord;
	}
	
	
	
	//This class indexes and stores each character from a given string to an array
	public char[] indexWord(String str){
		
		char[] wordCh = new char[str.length()];
		
		for(int i = 0; i <= str.length(); i++){
			wordCh[i] = str.charAt(i);
			//println(wordCh[i]);
					  
		}
		
		return wordCh;
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Hangman Game

#2 mcamardo   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 29-June 08

Re: Hangman Game

Posted 03 February 2009 - 05:29 PM

What about using the contains command?

if (currentword.contains(userinput))
{
  // show in gui
}
else
{
  // add part to the hangman sprite
}


This post has been edited by mcamardo: 03 February 2009 - 05:30 PM

Was This Post Helpful? 0
  • +
  • -

#3 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7507
  • View blog
  • Posts: 15,558
  • Joined: 16-October 07

Re: Hangman Game

Posted 03 February 2009 - 05:48 PM

View Postmrmichaela, on 3 Feb, 2009 - 10:16 AM, said:

ask you if in my indexWord() the storing of each String's character is correct?


Yes, it will work. However, the String class has a method that will do the work for you. There are a few other standard methods that might help. I've included them in the code:

class Hangman {
	// define your list in the object
	private String[] wordList = new String[] {
		"lagoon", "java", "dog"
	};
	private Random rand = new Random();
	
	// not quite sure what this is doing.
	public void run(){
		indexWord(pickWord());
	}
	
	public String pickWord(){
		return this.wordList[rand.nextInt(this.wordList.length)];
	}
	
	public char[] indexWord(String str){
		return str.toCharArray();
	}
}


Was This Post Helpful? 0
  • +
  • -

#4 mrmichaela   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 03-February 09

Re: Hangman Game

Posted 04 February 2009 - 11:36 AM

import java.util.*;

public class Hangman{
	
	public static String wordList[] = {"lagoon", "java", "dog"};	
	public static Random rand = new Random();
	
	
	
	
	public static void main(String[] args){
		int tries = 8;
		String word = pickWord();
		
		Scanner key = new Scanner(System.in);
		System.out.println("Please type a letter: ");
		String inputKey = key.nextLine();
		
		char[] wordArr = word.toCharArray();
		char guess = inputKey.charAt(0);
		System.out.println(word + "\n");
	

		
		for(int i = 0; i <= word.length(); i++){
			if(guess == wordArr[i]){
				System.out.println("Right!");
			} else {
				System.out.println("Nada!");
			}
		}
		
		//System.out.println(numChar);
		
		
	}
	
	
	
	
	public static String pickWord(){
		String currentWord = wordList[rand.nextInt(wordList.length)];
		return currentWord;
	}
} 


Thank you all for your replies!
I changed my code a little bit and now I have a new problem.

In my for loop in main() I want to check if the guess character is equal to a character already contained in my wordArr array but when I execute the if statement I get an error!

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
at assingments.Hangman.main(Hangman.java:28)

However some times it runs fine!
Any guesses with that one?

Cheers,
Mike
Was This Post Helpful? 0
  • +
  • -

#5 OrganizedChaos   User is offline

  • D.I.C Head

Reputation: 39
  • View blog
  • Posts: 153
  • Joined: 29-November 08

Re: Hangman Game

Posted 04 February 2009 - 11:39 AM

for(int i = 0; i <= word.length(); i++)

Should be
for(int i = 0; i < word.length(); i++)


That's why you get the OutOfBounds error - you're trying to access an element in the array that's not there.
Remember: Arrays begin counting at 0, whereas word.length() gives you the number of characters in the word.
If you have the word PIG, word.length() = 3. If you store the letters of PIG in an array, they would take up spots 0, 1, and 2.

This post has been edited by OrganizedChaos: 04 February 2009 - 11:41 AM

Was This Post Helpful? 0
  • +
  • -

#6 mrmichaela   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 03-February 09

Re: Hangman Game

Posted 04 February 2009 - 11:54 AM

Yeap! Worked now!
Thank you very much!!!
Was This Post Helpful? 0
  • +
  • -

#7 koki   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 79
  • Joined: 02-January 09

Re: Hangman Game

Posted 04 February 2009 - 11:58 AM

these posts are really helpful... i was trying to implement that game ... and now i will start because of you guys thanks to all of you :) :)
Was This Post Helpful? 0
  • +
  • -

#8 mrmichaela   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 03-February 09

Re: Hangman Game

Posted 05 February 2009 - 10:15 AM

import java.util.*;

public class Hangman{
	
	public static String wordList[] = {"lagoon", "java", "dog"};	
	public static Random rand = new Random();
	
	
	
	
	public static void main(String[] args){
		int tries = 7;
		
		String word = pickWord();
		do{
			Scanner key = new Scanner(System.in);
			System.out.println("Please type a letter: ");
			String inputKey = key.nextLine();
			
			char[] wordArr = word.toCharArray();
			
			char[] emptyArr = new char[wordArr.length];
			for(int i = 0; i < word.length(); i++){
				emptyArr[i] = '*';
			}
			
			char guess = inputKey.charAt(0);
			//System.out.println(word + "\n");
				
			int sumChars = 0;
			
			for(int i = 0; i < word.length(); i++){
				if(guess == wordArr[i]){
					emptyArr[i] = guess;
					sumChars += 1;
					
				}
			}
											
			if(sumChars != 0){
				System.out.println("\n");
				for(int i = 0; i < word.length(); i++){
					System.out.print(" " + emptyArr[i] + " ");
				}
				System.out.println("\nThere are " + sumChars + " " + "'" + guess + "'" +" in the word!");
			} else {
				System.out.println("\nNone " + guess + " in the word!");
			}
						
			tries -= 1;
			
			if(tries == 0){
				System.out.println("Ooops! Busted"); break;
			}
					
			System.out.println("\n" + tries + " tries remaining!\n");
		
		}while(tries >= 0);	
			
	}
	
	
	
	
	public static String pickWord(){
		String currentWord = wordList[rand.nextInt(wordList.length)];
		return currentWord;
	}
}
 


Greetings again!
I've done some more coding, but I have some trouble printing the whole emptyArr array.
For example when the missing word is "java", and the user guesses the letter 'a' it will print,
" * a * a ", which is correct, but when it loops and the user tries again and he guesses 'j', it will appear only " j * * * ". I can't understand why is this happening. I store the guess variable in my emptyArr[] but it can't hold it in it while looping is going on.

So with this happening I can't write the code for when a user guesses all the missing letters in his "tries limit", and finds the missing word!

Any ideas with this one??

Thank you very much!

Cheers
Was This Post Helpful? 0
  • +
  • -

#9 mostyfriedman   User is offline

  • The Algorithmi
  • member icon

Reputation: 729
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Hangman Game

Posted 05 February 2009 - 10:31 AM

ok seems that on each iteration you reset the String to ****
Was This Post Helpful? 0
  • +
  • -

#10 mrmichaela   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 03-February 09

Re: Hangman Game

Posted 05 February 2009 - 10:34 AM

View Postmostyfriedman, on 5 Feb, 2009 - 09:31 AM, said:

ok seems that on each iteration you reset the String to ****


Yeah exactly! But how can I write it so I can avoid the reset??
Thanks very much for the immediate response!

Cheers
Was This Post Helpful? 0
  • +
  • -

#11 mostyfriedman   User is offline

  • The Algorithmi
  • member icon

Reputation: 729
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Hangman Game

Posted 05 February 2009 - 10:36 AM

ok, can you comment your code so i can understand it better
Was This Post Helpful? 0
  • +
  • -

#12 mrmichaela   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 03-February 09

Re: Hangman Game

Posted 05 February 2009 - 10:47 AM

import java.util.*;

public class Hangman{
	
	public static String wordList[] = {"lagoon", "java", "dog"};	
	public static Random rand = new Random();
	
	
	
	
	public static void main(String[] args){
		int tries = 4;
		
		String word = pickWord();
		do{
			Scanner key = new Scanner(System.in);
			System.out.println("Please type a letter: ");
			String inputKey = key.nextLine();
			
			//Creates an array and fills it with the characters contained in the current missing word
			char[] wordArr = word.toCharArray();
			
			//Creates another array with same length as the wordArr and fills it with * characters
			//Each * character represents that some letter is missing
			char[] emptyArr = new char[wordArr.length];
			for(int i = 0; i < word.length(); i++){
				emptyArr[i] = '*';
			}
			
			//Converts from string to character the users guess
			char guess = inputKey.charAt(0);
			//System.out.println(word + "\n");
			
			//The sum of guessed characters. Just for printing reasons
			int sumChars = 0;
			
			//Imports the user's guess into the emptyArr. (Here I think something is going wrote)
			for(int i = 0; i < word.length(); i++){
				if(guess == wordArr[i]){
					emptyArr[i] = guess;
					sumChars += 1;
					
				}
			}
			
			//Checks if the user's guess hit any hidden characters and prints appropriate messages
			if(sumChars != 0){
				System.out.println("\n");
				for(int i = 0; i < word.length(); i++){
					System.out.print(" " + emptyArr[i] + " ");
				}
				System.out.println("\nThere are " + sumChars + " " + "'" + guess + "'" +" in the word!");
			} else {
				System.out.println("\nNone " + guess + " in the word!");
			}
						
			tries -= 1;
			
			//Checks if the user has any more tries and if not prints appropriate message
			if(tries == 0){
				System.out.println("Ooops! Busted"); break;
			}
					
			System.out.println("\n" + tries + " tries remaining!\n");
		
		}while(tries >= 0);	
			
	}
	
	
	
	//Picks a random word from the wordList array
	public static String pickWord(){
		String currentWord = wordList[rand.nextInt(wordList.length)];
		return currentWord;
	}
}
 


Of course!
Hope this helps....

Cheers
Was This Post Helpful? 0
  • +
  • -

#13 mostyfriedman   User is offline

  • The Algorithmi
  • member icon

Reputation: 729
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Hangman Game

Posted 05 February 2009 - 10:51 AM

alright found it :)
the part where you create an array and fill it with '*' take it out of the do while
import java.util.*;

public class Prog3{
   
	public static String wordList[] = {"lagoon", "java", "dog"};	
	public static Random rand = new Random();
   
   
   
   
	public static void main(String[] args){
			int tries = 7;
			String word = pickWord();
		char[] wordArr = word.toCharArray();
		char[] emptyArr = new char[wordArr.length];
			for(int i = 0; i < word.length(); i++){
				emptyArr[i] = '*';
			}
		do{
			Scanner key = new Scanner(System.in);
			System.out.println("Please type a letter: ");
			char guess = key.nextLine().charAt(0);
		   
					   
			
		   
			//System.out.println(word + "\n");
			   
			int sumChars = 0;
		   
			for(int i = 0; i < word.length(); i++){
				if(guess == wordArr[i]){
					emptyArr[i] = guess;
					sumChars += 1;
				   
				}
			}
										   
			if(sumChars != 0){
				System.out.println("\n");
				for(int i = 0; i < word.length(); i++){
					System.out.print(" " + emptyArr[i] + " ");
				}
				System.out.println("\nThere are " + sumChars + " " + "'" + guess + "'" +" in the word!");
			} else {
				System.out.println("\nNone " + guess + " in the word!");
			}
					   
			tries -= 1;
		   
			if(tries == 0){
				System.out.println("Ooops! Busted"); break;
			}
				   
			System.out.println("\n" + tries + " tries remaining!\n");
	   
		}while(tries >= 0);	
		   
	}
   
   
   
   
	public static String pickWord(){
		String currentWord = wordList[rand.nextInt(3)];
		return currentWord;
	}
}
 


now you have a working game ;), the thing was, on each iteration you were recreating a new array of '*' and the previous guesses would be deleted

This post has been edited by mostyfriedman: 05 February 2009 - 10:56 AM

Was This Post Helpful? 1
  • +
  • -

#14 mrmichaela   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 03-February 09

Re: Hangman Game

Posted 06 February 2009 - 03:58 AM

Yeap helped a lot!!
Thank you one more time :)
Was This Post Helpful? 0
  • +
  • -

#15 mostyfriedman   User is offline

  • The Algorithmi
  • member icon

Reputation: 729
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Hangman Game

Posted 06 February 2009 - 05:08 AM

no problem ;), you can encourage all who helps you around this forum by clicking the this post was helpful option ;)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2