Stanford CS106A Hangman Game Lexicon Solution

Wohoo! I am officially done with the Hangman game, and I did it all on my own. I am starting to suspect that the first part of the class is really difficult to weed out the non-serious students. Now that I got past the Breakout game, everything seems to be a lot more straightforward. The last part of the Hangman Game is to import a much longer list of words from the HangmanLexicon.txt file. The instructions for this part of the game are as follows:

Here is my solution:


If you watch Lectures 15 and 16, this problem becomes pretty straight-forward. You can also find my solution on gist.

<pre>
/*
 * File: HangmanLexicon.java
 * -------------------------
 * This file contains a stub implementation of the HangmanLexicon
 * class that you will reimplement for Part III of the assignment.
 */

import acm.util.*;
import java.io.*;
import java.util.*;

public class HangmanLexicon {

	private ArrayList <String> wordList = new ArrayList <String> ();	

	public HangmanLexicon() {
	//adds the individual words in the file to the array list
		try {
			BufferedReader hangmanWords = new BufferedReader(new FileReader("HangmanLexicon.txt"));
			while(true) {
				String line = hangmanWords.readLine();
				if(line == null) break;
				wordList.add(line);
			}
			hangmanWords.close();
		} catch (IOException ex) {
			throw new ErrorException(ex);
		}
	}

/** Returns the word at the specified index. */
	public String getWord(int index) {
		return wordList.get(index);

	}

	/** Returns the number of words in the lexicon. */
	public int getWordCount() {
		return wordList.size();
	}	
}
</pre>

Enjoy the article? Join over 20,000+ Swift developers and enthusiasts who get my weekly updates.