Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Hangman Game help
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
a22asin




PostPosted: Sat Jan 11, 2014 3:14 pm   Post subject: 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;
        }
}
Sponsor
Sponsor
Sponsor
sponsor
Panphobia




PostPosted: Sun Jan 12, 2014 8:14 am   Post subject: 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




PostPosted: Sun Jan 12, 2014 12:03 pm   Post subject: RE:Hangman Game help

yay, one problem down, one more to go Smile . 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




PostPosted: Sun Jan 12, 2014 2:25 pm   Post subject: Re: Hangman Game help

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

Java:
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;
}

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

But then we do
Java:
isFound = Search(letter, mysteryWord);
                       
System.out.println(isFound[0]);

for (int i = 0; i < isFound.length; i++){
        fillWord[isFound[i]] = letter;
}

So what happens to fillWord[0] ?
a22asin




PostPosted: Sun Jan 12, 2014 2:52 pm   Post subject: Re: Hangman Game help

Dreadnought @ Sun Jan 12, 2014 2:25 pm wrote:
I don't really know Java but I think arrays are zero-initialized, so

Java:
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;
}

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

But then we do
Java:
isFound = Search(letter, mysteryWord);
                       
System.out.println(isFound[0]);

for (int i = 0; i < isFound.length; i++){
        fillWord[isFound[i]] = letter;
}

So what happens to fillWord[0] ?



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




PostPosted: Sun Jan 12, 2014 6:46 pm   Post subject: Re: Hangman Game help

I'm pretty sure that fillword is the mystery word as it appears to the user, hence fillword[0] is the first letter of the mystery word.


Your search probably looks correct, but notice that isFound[0] is always 0 (other positions are probably also 0), so what happens when we do the following?
Java:

fillWord[isFound[0]] = letter;

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




PostPosted: Sun Jan 12, 2014 8:26 pm   Post subject: Re: Hangman Game help

Dreadnought @ Sun Jan 12, 2014 6:46 pm wrote:
I'm pretty sure that fillword is the mystery word as it appears to the user, hence fillword[0] is the first letter of the mystery word.


Your search probably looks correct, but notice that isFound[0] is always 0 (other positions are probably also 0), so what happens when we do the following?
Java:

fillWord[isFound[0]] = letter;

(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




PostPosted: Sun Jan 12, 2014 8:40 pm   Post subject: Re: Hangman Game help

a22asin wrote:

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]

Sorry to be so insistent but I'm having trouble matching this to your code for Search.
Java:
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;   <----- My issue is here
            }
    }
    return index;
}

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[i] = i, otherwise it will remain 0 (since that is the value to which it is initialized.
I think you want a second variable to keep track of how many occurrences of the letter you have found, that way you could have something like
Java:

if (a.charAt(i) == l){
    index[j++] = i;
}


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




PostPosted: Sun Jan 12, 2014 8:46 pm   Post subject: Re: Hangman Game help

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


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




PostPosted: Sun Jan 12, 2014 9:12 pm   Post subject: Re: Hangman Game help

Dreadnought @ Sun Jan 12, 2014 6:46 pm wrote:
I'm pretty sure that fillword is the mystery word as it appears to the user, hence fillword[0] is the first letter of the mystery word.


Your search probably looks correct, but notice that isFound[0] is always 0 (other positions are probably also 0), so what happens when we do the following?
Java:

fillWord[isFound[0]] = letter;

(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




PostPosted: Mon Jan 13, 2014 7:41 am   Post subject: 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




PostPosted: Mon Jan 13, 2014 12:30 pm   Post subject: RE:Hangman Game help

i didnt think about that! The programs works now thanks!
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 12 Posts ]
Jump to:   


Style:  
Search: