, , , , ,

Stanford CS106A Assignment 4: Hangman

Course: Stanford’s CS106A: Programming Methodology

Assignment 4: Hangman

Assignment 4 is distributed after lecture 14.

Goal: Learn how to manipulate Java strings.

Implementing a Hangman game is pretty straight-forward, easier perhaps than assignment 3.

One thing to note is the dictionary. To load it locally, when running from within the IDE (whatever IDE you use, eclipse, netbeans, etc) you can use something like:

  BufferedReader rd = new BufferedReader(
    new FileReader("HangmanLexicon.txt")

But if you wanted to build this into a standalone .jar and embed it as an applet, or run it via double clicking a .jar, your probably want to use something like this:

Snapshot of where to put the lexicon in the project

Netbeans snapshot: where to put the lexicon in the project.

(in this example I use a ‘resources’ folder inside the ‘src’ folder of my project, this folder ends up into the .jar. Netbeans adds it by default. I don’t know about eclipse.)

/* class constant */
private final String LexiconPath = "/resources/HangmanLexicon.txt";


  InputStream stream = this.getClass().getResourceAsStream(LexiconPath);
  BufferedReader rd = new BufferedReader(
    new InputStreamReader(stream)

Run it as an applet inside the browser: HangmanApplet

Hangman.java (view | download)
HangmanCanvas.java (view | download)
HangmanLexicon.java (view | download)