Stanford CS106A Hangman Game Lexicon Solution

17 Nov

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.

 * File:
 * -------------------------
 * 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.util.*;

public class HangmanLexicon {

	private ArrayList &lt;String&gt; wordList = new ArrayList &lt;String&gt; ();	

	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;
		} 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();

2 Responses to “Stanford CS106A Hangman Game Lexicon Solution”

  1. James June 14, 2012 at 4:00 pm #

    Wow, your code for this section is waay more compact than mine ended up. Partly bc I didn’t understand arrayLists, so setting up the array was painful… anyway. The only thing I’m not understanding in your code are the expressions < &gt.

    • Natasha Murashev June 14, 2012 at 9:12 pm #

      Hey James,

      That’s the “greater than” sign. When I embed the code into WordPress, it changes greater than to &gt

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s