

-----------------------------------

a22asin

Sat Jan 11, 2014 3:14 pm


Hangman Game help

-----------------------------------

Hi, so i gott create a hangman sorta game where it randomly picks a word from a doc file, then it shows the user '-' for each letter. Itll ask the user for a letter and if its foudn, it replaces the - and shows the word so far. The user only has 10 tries, i got it to work, sorta. Im having trouble at 2 parts: for some reason, it replaces the 1st letter with the letter u enter in, even if the letter is not the 1st and my 2nd trouble is trying to determine if the letter is not in the word and decrease the number of tries left. I have tried the last part different ways, but im not getting it to work properly.

[code]public class MysteryWordGame {

	public static void main(String[] args)throws FileNotFoundException  {
		
		Scanner key = new Scanner (System.in);
		
		int size = 50;

		String[] word = new String[size];
		word = fillArray(size);
		int rand = randomNumGen(size);
		
		String mysteryWord = word[rand];
		char[] fillWord = new char [mysteryWord.length()];
		
		for (int i = 0; i < mysteryWord.length(); i++){
			fillWord[i] = '-';
		}
		
		System.out.println(mysteryWord);
		System.out.print ("The mystery word is: ");
		for (int i = 0; i < mysteryWord.length(); i++){
			System.out.print(fillWord[i]);
		}
		
		boolean isComplete = false;
		int tries = 10;
		int[] isFound = new int[mysteryWord.length()];
		
		
		while (tries != 0 && !isComplete){
			System.out.print ("\nWhat letter would you like to try?: ");
			String input = key.next();
			char letter = input.charAt(0);
			isFound = Search(letter, mysteryWord);
			
			System.out.println(isFound[0]);

				for (int i = 0; i < isFound.length; i++){
					fillWord[isFound[i]] = letter;
				}
				for (int i = 0; i < mysteryWord.length(); i++){
					System.out.print(fillWord[i]);
				}
		}
		
	}
	
	public static String[] fillArray(int s)throws FileNotFoundException {
		Scanner file = new Scanner (new File("words.txt"));
		String[] word = new String[s];
		for (int i = 0; i < word.length; i++){
			word[i] = file.nextLine();
		}
		return word;
	}
	
	public static int randomNumGen(int size){
		return (int)(Math.random()*50);	
	}

	public static int[] Search(char l, String a){
		int[] index = new int[a.length()];
		for(int i = 0; i < a.length(); i++){
			if (a.charAt(i) == l){
				index[i] = i;
			}
		}
		return index;
	}
}[/code]


-----------------------------------

Panphobia

Sun Jan 12, 2014 8:14 am


RE:Hangman Game help

-----------------------------------

For your second problem of determining if a letter is not in a word. There is a String function in java that lets you know if a character sequence is in a particular String, it returns a boolean value as you might guess, I believe it is called, "contains".


-----------------------------------

a22asin

Sun Jan 12, 2014 12:03 pm


RE:Hangman Game help

-----------------------------------

yay, one problem down, one more to go :) . Now i just cant figure out why the letter i enter always comes as the 1st letter and the then in the right location. (only when the letter is found).

ex. if i is found in the word, it shows this:
i---i---
but the 1st letter isnt an i. It does keeps changes to all letters enterd.


-----------------------------------

Dreadnought

Sun Jan 12, 2014 2:25 pm


Re: Hangman Game help

-----------------------------------

I don't really know Java but I think arrays are zero-initialized, so

public static int
returns an array where the ith entry is either 0 or i.

But then we do
isFound = Search(letter, mysteryWord); 
                        
System.out.println(isFound
So what happens to fillWord[0] ?


-----------------------------------

a22asin

Sun Jan 12, 2014 2:52 pm


Re: Hangman Game help

-----------------------------------

I don't really know Java but I think arrays are zero-initialized, so

public static int
returns an array where the ith entry is either 0 or i.

But then we do
isFound = Search(letter, mysteryWord); 
                        
System.out.println(isFound
So what happens to fillWord


fillWord[0] is the 1st locating of the letter found in the word. I had the program print the values, and the values are correct when searching, but idk where it goes wrong.


-----------------------------------

Dreadnought

Sun Jan 12, 2014 6:46 pm


Re: Hangman Game help

-----------------------------------

I'm pretty sure that fillword is the mystery word as it appears to the user, hence fillwordalways 0 (other positions are probably also 0), so what happens when we do the following?

fillWord
(This is the first iteration of one of your for loops)


-----------------------------------

a22asin

Sun Jan 12, 2014 8:26 pm


Re: Hangman Game help

-----------------------------------

I'm pretty sure that fillword is the mystery word as it appears to the user, hence fillwordalways 0 (other positions are probably also 0), so what happens when we do the following?

fillWord
(This is the first iteration of one of your for loops)

well, not exactly, isFound[0] is the value determined by the search method. Its the 1st occurance of the letter in the mysteryWord. so if the word is character and the user enters a, then isFound[0] is 2, isFound[1] = 4. Therefore fillWord[isFound[0]] is equivalent to fillWord[2]

And yes, fillWord is the mysteryWord (which is the random word) as it appears to the user and is initially filled with '-'


-----------------------------------

Dreadnought

Sun Jan 12, 2014 8:40 pm


Re: Hangman Game help

-----------------------------------


well, not exactly, isFound
Sorry to be so insistent but I'm having trouble matching this to your code for Search.
public static int
I admit I don't really know Java too well, but I interpret this as saying that if the ith letter of your word is the letter you're searching for, then index
if (a.charAt(i) == l){ 
    index

Again, I apologize if I'm overlooking something obvious or not understanding something fundamental about Java.


-----------------------------------

a22asin

Sun Jan 12, 2014 8:46 pm


Re: Hangman Game help

-----------------------------------

[code] if (a.charAt(i) == l){ 
                                index[i] = i; 
                        } [/code]

this just means that if the letter at 'i' is the inputted letter, then index[i] is i, therefore keeping track of the location found


-----------------------------------

a22asin

Sun Jan 12, 2014 9:12 pm


Re: Hangman Game help

-----------------------------------

I'm pretty sure that fillword is the mystery word as it appears to the user, hence fillwordalways 0 (other positions are probably also 0), so what happens when we do the following?

fillWord
(This is the first iteration of one of your for loops)

going back to what u said, u where right, i didnt realize that its always 0, didnt make sense to me, but how would i counter this without effecting the 1st letter?


-----------------------------------

Dreadnought

Mon Jan 13, 2014 7:41 am


Re: Hangman Game help

-----------------------------------

You could initialize the array to some invalid value (like -1) and then check if the value is non-negative when you substitute letters in the mystery word.

You could also, just have search return an array containing 0's and 1's (0 if the letter is not found, 1 if it is) and then check the result of Search.

And there are many other ways to do this.


-----------------------------------

a22asin

Mon Jan 13, 2014 12:30 pm


RE:Hangman Game help

-----------------------------------

i didnt think about that! The programs works now thanks!

