CS 124, Fall 2021
Labs 13: Hangman Continued

For the final two labs of the semester, you will continue working on the Hangman game that was begun in Lab 12 (or on your alternative project, if that's what you decided to do). This week, you will implement the actual game logic. In Part 3 of the lab, you should just be adding polish.

Word List

You should already have the GUI for your program pretty much ready to go. The next part of the project is to implement the game logic. That is, you should make it possible for the user to play the game.

First of all, you will need a list of possible words for the game. Your program should read the list of words from a text file that is part of the program. The file /classes/cs124_Eck/wordlist.txt is the same list of 404 words that was used in Lab 6; however, the first line of the file, which contained the number of words, has been removed. Note that all words in the list are 12 characters long or less, so your program does not have to accommodate words longer than 12 characters. You also have the option of using your own list of words.

The list of words should be part of the program. A file that is actually part of a program is called a resource file. To include the word list as a resource file, the file wordlist.txt must be added to the src folder of your project. However, you will still need a way to read the data from the file. There is a not-very-pretty API for reading general resource files. Here is a method that you can use in your program to read the file:

/**
 * Reads a list of words from a file.
 * @param filename the name of the resource file
 * @return an ArrayList containing the list of words read.
 */
private ArrayList<String> readWordList(String filename) {
    try {
        ArrayList<String> words;
        Scanner in;
        in = new Scanner(getClass().getClassLoader()
                            .getResource(filename).openStream());
        words = new ArrayList<String>();
        while (in.hasNext()) {
            words.add(in.next());
        }
        in.close();
        return words;
    }
    catch (Exception e) {
        throw new IllegalArgumentException("Can't read " + filename);
    }
}

Using this method, youc an call readWordList("wordlist.txt") to read the file and get the list of words for the program in an ArrayList. Both ArrayList and Scanner will have to be imported into your program. When starting a game, select a random word from the list. Note that the words from my list are lower-case. You might want to convert to upper-case.

It can be convenient to have the characters from the word in an array instead of in a string. Once you have gotten a word from the list, I suggest that you make an array of type char[] of the same length as the word and copy all the characters from the word into the array. (Actually, a String already has an instance method for making such an array; use it if you can find it.) It would also be convenient to have an array of the same length to hold the letters guessed so far, with some character, such as an underscore, to hold the places of letters that have not yet been guessed.

Game Logic

As you make progress on your GUI, you can start adding in the implementation of the game logic. That is, you should make it possible for the user to play the game. Almost all of the game action, except for some setup in the start() method, takes place in the event handler methods for the buttons (or menu items).

You will need to store some global state data for the game. This includes, for example, the list of words, the buttons, the word that the user is trying to guess, the number of incorrect guesses that the user has made, and maybe a boolean variable to record whether or not the game is over. Remember that global data are stored in instance variables. The values of the state variables change in response to events, and they are used (by a draw() method or otherwise) to decide what to draw. You should carefully analyze what has to happen in your event handlers and what needs to be displayed, based on the state of the game, at any given time both during a game and when a game is over.