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

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




PostPosted: Sun May 19, 2013 5:40 pm   Post subject: Help With Java Hangman!!

Hi I'm new here and was wondering if anybody knows how to create a java hangman game for HSA consoles. It's a project for school and the teacher hasn't taught us how to read the strings and check if a letter guessed is in the word or not. I need help with that. Here's my program so far.

Here's the project outline:
Your CPT involves designing a game of hangman.
Requirements:
1. Predefined categories should be set up using multiple data files. Populate the
data files with predefined lists of words.
2. Allow the user to choose from the list of predefined categories.
3. Randomly select a word from the predefined category list.
4. The progress of the game should be displayed to the user in a graphical format.
Use the draw commands.
5. The user should be informed when they win or lose a game.
6. Allow the user to try a new game.
Bonus:

Keep a list of high scores.
Evaluation:
Your program will be evaluated based on the following criteria:
Functionality. Does it work?
Attractive output.
Internal documentation (comments, variable names).
Effective use of programming structures.
Efficiency.
Error traps.
Try again option.
Creativity.
Effort.

My program so far:
Java:

// The "Hangman_CPT" class.
import java.awt.*;
import hsa.Console;
import java.io.*;

public class Hangman_CPT
{
    static Console c;           // The output console

    public static void main (String[] args) throws IOException
    {
        c = new Console ();

        String fileName, word1, word2, word3, word4, word5, word6, word7, word8, guess, Wguess;

        int howMany, random;

        howMany = 0;

        c.println ("What category do you want?");
        c.println ("animals");
        c.println ("movies");
        c.println ("sports");
        c.println ("Enter the file name");
        fileName = c.readLine ();
        fileName = fileName + ".txt";

        BufferedReader input;
        input = new BufferedReader (new FileReader (fileName));

        word1 = input.readLine ();
        word2 = input.readLine ();
        word3 = input.readLine ();
        word4 = input.readLine ();
        word5 = input.readLine ();
        word6 = input.readLine ();
        word7 = input.readLine ();
        word8 = input.readLine ();

        // read data until end of file to determine number of records
        while (word1 != null)
        {
            howMany++;
            word1 = input.readLine ();
            word2 = input.readLine ();
            word3 = input.readLine ();
            word4 = input.readLine ();
            word5 = input.readLine ();
            word6 = input.readLine ();
            word7 = input.readLine ();
            word8 = input.readLine ();
        }

        input.close (); // close data file so we can open again to start at top

        input = new BufferedReader (new FileReader (fileName)); // open the file again for reading

        // declare arrays once number of records is known
        String word_1[] = new String [howMany];
        String word_2[] = new String [howMany];
        String word_3[] = new String [howMany];
        String word_4[] = new String [howMany];
        String word_5[] = new String [howMany];
        String word_6[] = new String [howMany];
        String word_7[] = new String [howMany];
        String word_8[] = new String [howMany];

        // read data from file into arrays
        for (int count = 0 ; count < howMany ; count++)
        {
            word_1 [count] = input.readLine ();
            word_2 [count] = input.readLine ();
            word_3 [count] = input.readLine ();
            word_4 [count] = input.readLine ();
            word_5 [count] = input.readLine ();
            word_6 [count] = input.readLine ();
            word_7 [count] = input.readLine ();
            word_8 [count] = input.readLine ();
        }

        input.close ();

        c.clear ();

        int numOfChars;

        Wguess = "Wrong Guesses:";

        if (fileName.equals ("animals.txt")) // prints the category for the user
        {
            c.println ("Category = Animals");
        }
        else
        {
            if (fileName.equals ("movies.txt"))
            {
                c.println ("Category = Movies");
            }
            else
            {
                if (fileName.equals ("sports.txt"))
                {
                    c.println ("Category = Sports");
                }
            }
        }

        // for (int count = 0 ; count < howMany ; count++)
        // {
        //     c.println (word_1 [count]);
        //     c.println (word_2 [count]);
        //     c.println (word_3 [count]);
        //     c.println (word_4 [count]);
        //     c.println (word_5 [count]);
        //     c.println (word_6 [count]);
        //     c.println (word_7 [count]);
        //     c.println (word_8 [count]);
        // }

        random = (int) (Math.random () * 8) + 1; // random number from 1 to 8

        for (int count = 0 ; count < howMany ; count++) // selection to determine the appropriate word pertaining to the number
        {
            if (random == 1)
            {
                c.println (word_1 [count]);
                numOfChars = word_1 [count].length ();
                c.println ("The word is " + numOfChars + " characters long.");
                while (true)
                {
                    c.print ("Please enter your guess: ");
                    guess = c.readLine ();
                    if (guess.equalsIgnoreCase (word_1 [count]))
                    {
                        break;
                    }
                    else
                    {

                        c.println (Wguess);
                        Wguess = Wguess + " " + guess;
                    }
                }
            }
            else
            {
                if (random == 2)
                {
                    c.println (word_2 [count]);
                    numOfChars = word_2 [count].length ();
                    c.println ("The word is " + numOfChars + " characters long.");
                    while (true)
                    {
                        c.print ("Please enter your guess: ");
                        guess = c.readLine ();
                        if (guess.equalsIgnoreCase (word_2 [count]))
                        {
                            break;
                        }
                        else
                        {
                            Wguess = Wguess + " " + guess;
                            c.println (Wguess);
                        }
                    }
                }
                else
                {
                    if (random == 3)
                    {
                        c.println (word_3 [count]);
                        numOfChars = word_3 [count].length ();
                        c.println ("The word is " + numOfChars + " characters long.");
                        while (true)
                        {
                            c.print ("Please enter your guess: ");
                            guess = c.readLine ();
                            if (guess.equalsIgnoreCase (word_3 [count]))
                            {
                                break;
                            }
                            else
                            {
                                Wguess = Wguess + " " + guess;
                                c.println (Wguess);
                            }
                        }
                    }
                    else
                    {
                        if (random == 4)
                        {
                            c.println (word_4 [count]);
                            numOfChars = word_4 [count].length ();
                            c.println ("The word is " + numOfChars + " characters long.");
                            while (true)
                            {
                                c.print ("Please enter your guess: ");
                                guess = c.readLine ();
                                if (guess.equalsIgnoreCase (word_4 [count]))
                                {
                                    break;
                                }
                                else
                                {
                                    Wguess = Wguess + " " + guess;
                                    c.println (Wguess);
                                }
                            }
                        }
                        else
                        {
                            if (random == 5)
                            {
                                c.println (word_5 [count]);
                                numOfChars = word_5 [count].length ();
                                c.println ("The word is " + numOfChars + " characters long.");
                                while (true)
                                {
                                    c.print ("Please enter your guess: ");
                                    guess = c.readLine ();
                                    if (guess.equalsIgnoreCase (word_5 [count]))
                                    {
                                        break;
                                    }
                                    else
                                    {
                                        Wguess = Wguess + " " + guess;
                                        c.println (Wguess);
                                    }
                                }
                            }
                            else
                            {
                                if (random == 6)
                                {
                                    c.println (word_6 [count]);
                                    numOfChars = word_6 [count].length ();
                                    c.println ("The word is " + numOfChars + " characters long.");
                                    while (true)
                                    {
                                        c.print ("Please enter your guess: ");
                                        guess = c.readLine ();
                                        if (guess.equalsIgnoreCase (word_6 [count]))
                                        {
                                            break;
                                        }
                                        else
                                        {
                                            Wguess = Wguess + " " + guess;
                                            c.println (Wguess);
                                        }
                                    }
                                }
                                else
                                {
                                    if (random == 7)
                                    {
                                        c.println (word_7 [count]);
                                        numOfChars = word_7 [count].length ();
                                        c.println ("The word is " + numOfChars + " characters long.");
                                        while (true)
                                        {
                                            c.print ("Please enter your guess: ");
                                            guess = c.readLine ();
                                            if (guess.equalsIgnoreCase (word_7 [count]))
                                            {
                                                break;
                                            }
                                            else
                                            {
                                                Wguess = Wguess + " " + guess;
                                                c.println (Wguess);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        if (random == 8)
                                        {
                                            c.println (word_8 [count]);
                                            numOfChars = word_8 [count].length ();
                                            c.println ("The word is " + numOfChars + " characters long.");
                                            while (true)
                                            {
                                                c.print ("Please enter your guess: ");
                                                guess = c.readLine ();
                                                if (guess.equalsIgnoreCase (word_8 [count]))
                                                {
                                                    break;
                                                }
                                                else
                                                {
                                                    Wguess = Wguess + " " + guess;
                                                    c.println (Wguess);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }



        c.println ("You guessed correctly!");
        c.println ("You win!");



        // Place your program here.  'c' is the output console
    } // main method
} // Hangman_CPT class


there are eight words in each text file and they are randomly chosen.

Thanks a lot for helping!



Mod Edit:
Please wrap you code in either of the following in order to preserve whitespace (indentation) and to highlight the syntax.
code:

[syntax="java"] ... code ... [/syntax]

[code] ... code ... [/code ]
Sponsor
Sponsor
Sponsor
sponsor
Zren




PostPosted: Sun May 19, 2013 6:24 pm   Post subject: RE:Help With Java Hangman!!

You seem to repeating code a lot. Perhaps you could try moving the game logic (guessing a single word) to it's own function. Pass the current word you're using as a parameter.

Eg:

Java:

doHangmanGame("Do the fluffly bunny dance!");

List<String> shuffledPhrases = new ArrayList<String>(phrases); // Copy the array or list.
Collections.shuffle(shuffledPhrases);
for (String phrase : shuffledPhrases) { // This is a for each loop which will iterate through the array.
  doHangmanGame(word);
}


Note that Collections.shuffle will randomly sort the array it's given. It "returns" void.

As for reading a variable amount of words from file. You could have the first line of the file contain the number of words in the file. Then create an array that large and store the string in it. OR you could use an ArrayList which will allocate an ever increasing amount of memory/resize the array every time you reach the limit.

In the loop below, we first call fin.readline() and assign it to the 'line' variable. Then we do the comparison with the value we assigned.
To explain, it's sort of like if we replaced line = fin.readline() with the function assignVariable(line, fin.readline()) which returned the second parameter after the asignment.
Note: All operations you write are really just functions that we assign syntax to make it easier to write in.
Java:

List<String> phrases = new ArrayList<String>();
String line;
while (line = fin.readline() != null) {
  phrases.append(line);
}
robs247




PostPosted: Tue May 21, 2013 12:51 pm   Post subject: Re: Help With Java Hangman!!

I don't know what that does. I use java hsa console, so if u know it then it would be much appreciated
Zren




PostPosted: Tue May 21, 2013 6:38 pm   Post subject: RE:Help With Java Hangman!!

Oh crap. Didn't realize you're using Ready To Program.

I also misstook your "read the strings" to be File IO, but you seem to already have that done.

I never used RTP but you could probably check out the documentation aka javadocs, whiiiiich don't appear to be online anywhere.

Perhaps someone else knows where they are?
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  [ 4 Posts ]
Jump to:   


Style:  
Search: