Duke Computer Science Shield

Assignment 0: Hangman

Due September 10th

Computer Science 201

The Big Picture

See The Medium Pictures and The Little Pictures for the specifics of what you're going to do and how to proceed. The Big Picture is to give you a bird's-eye view of what's going to happen. I recommend you read this page through, start to finish, before you snarf any code!

Hangman is a traditional children's game, typically played with words. However, there's nothing special about words: you can play Hangman while guessing words, or cities, or Canadian game shows or even lists of lists of lists. This "categorical Hangman" is something you can do for extra credit.

For the main body of the assignment, you'll be implementing traditional, guess-a-word-one-letter-at-a-time Hangman. You'll also do a statistical analysis of the words used in Hangman. This assignment shares some history with an assignment from CompSci 6, but adds the analytical steps (and it's in Java).

As the first assignment, Hangman seeks to be approachable, and has four specific objectives:

  1. Understanding how classes interact in Java, particularly calling each others' methods.
  2. Becoming very familiar with Strings, arrays, loops, and printing in Java.
  3. Starting to think analytically about data (an approach we'll be building on all semester).
  4. Understanding the difference between primitive types and object types.

The Medium Pictures

An assignment in three parts.

Part 0: Statistical Analysis (Ours)
Part 0 is about getting your feet wet with the provided code. One of the classes we provide, HangmanFileLoader, provides a method for getting a word for the player to guess. Note that you don't need to understand how HangmanFileLoader works! For part 0, you'll complete the HangmanStats class to answer a statistical question. Specifically, you should compute an estimate of the number of four-letter Hangman words, five-letter Hangman words, and so on up to 20-letter Hangman words. You can do this by repeatedly calling loader.getRandomWord(n), (where n is the desired length) and keeping track of the number of unique words returned.

Part 1: Statistical Analysis (Yours)
You should pose your own statistical question about the Hangman words and the getRandomWord method. Any non-trivial question is fine: one example (which you should feel free to use) is:
How many times do I have to have to generate a random word before I get a repeat?
Whatever your question is, you will be writing code to answer it. Note: because you'll be using getRandomWord, your code will have probabilistic, not deterministic, behavior. This means your answer will be statistical!

Part 2: Hangman: The Game
Write a program to play a console-based (not graphical) guess-a-word game of Hangman. The player should get to specify how many letters are in the word, and how many mistakes they get to make before they lose the game. (See the examples below.) Details on how to organize the game, including the methods you should write, and how it will be graded, are in The Little Pictures.

A Sample Run
Here's a sample run of the Hangman game. You do not need to follow this format exactly, but you should include with each turn the following:

  • The current guess at the secret word (blanks and correct guesses).
  • The number of misses left until hanging occurs.
  • The letters that have been guessed incorrectly.
Below, things the player types are in italics; everything else is printed by the program.

Number of letters in word: 8
Number of guesses to hanging: 7
_ _ _ _ _ _ _ _
Misses left: 7
Letters guessed so far:
Guess letter: e
No e!
_ _ _ _ _ _ _ _
Misses left: 6
Letters guessed so far: e
Guess letter: a
No a!
_ _ _ _ _ _ _ _
Misses left: 5
Letters guessed so far: e a
Guess letter: o
No o!
_ _ _ _ _ _ _ _
Misses left: 4
Letters guessed so far: e a o
Guess letter: i
Found i!
_ _ _ i _ _ i _
Misses left: 3
Letters guessed so far: e a o i
Guess letter: s
No s!
_ _ _ i _ _ i _
Misses left: 2
Letters guessed so far: e a o i s
Guess letter: u
No u!
_ _ _ i _ _ i _
Misses left: 1
Letters guessed so far: e a o i s u
Guess letter: c
Found c!
c _ _ i _ _ i c
Misses left: 1
Letters guessed so far: e a o i s u c
Guess letter: n
No n!
You've been hung! The word was cyrillic.

The Little Pictures

At this point, you should have a pretty good bird's-eye view of Assignment 0. The Little Pictures explain the steps you're going to take to complete it and how it will be graded.

You'll be submitting three things: a writeup (parts 0 and 1, above), your implementation of Hangman (part 2, above). and a README file describing your implementation. All three should be submitted in the same project in the usual way. Your writeup should be in .pdf format, and your README in .txt format.

Step 0:
Pause to collect your thoughts. Then rock out to (what's left of) Led Zeppelin, in a song that could have been written just for this assignment. Make sure to note your rocking out in your writeup.

Step 1: Code
Snarf the code! You'll find four pieces of code provided:

  1. HangmanStats.java is what you'll use and modify in parts 0 and 1 of the assignment: the statistical analysis. Note that it has a public static void main(String[] args), which means it can be executed as a program.
  2. HangmanFileLoader.java loads a list of words from a file, and has a method getRandomWord to return a random word. The sample code in HangmanStats.java and HangmanGame.java show examples of using this class. You shouldn't need to modify this code at all, nor understand exactly how it works; just how to use it.
  3. The amusingly-named HangmanExecutor.java has the main method that is the starting point of the game you write. It creates a HangmanGame and then calls the play method. You can complete the assignment without modifying this file; if you do modify it, explain what you've done in your README.
  4. HangmanGame.java is the game-playing code, and where you'll be doing the majority of your work. You'll modify the play method, and you may choose to add new methods and member variables to this class.
Take a few minutes to familiarize yourself with the code, particularly HangmanStats.java and HangmanGame.java.

Step 2: Doing Part 0
We wrote code (not provided) to call getRandomWord 10,000 times for word lengths between 4 and 10. The results of one run are below:

Word Length Number of Words
42209
53792
64959
75412
85379
94947
104059
Note that this process is random! If we ran our code again, we'd get different (but similar) numbers. You'll need to decide on a strategy to compute the number of different words for each length from 4 to 20 (inclusive), implement that strategy, and then describe your methods and results in your writeup. Be sure to submit the code you used to do your statistical analysis, and use as many charts, tables, or other figures as you think are appropriate.

Step 3: Doing Part 1
Come up with an interesting statistical question about the behavior of getRandomWord on the provided Hangman words. Then, write the code to answer your question, and answer the question in your writeup. Be sure to include the code you wrote! As before, be as verbose and graphical as you deem helpful.

Step 4: Doing Part 2
Finally, implement the Hangman game. Your implementation should be reasonably robust against player errors: for example, if they guess the same letter twice, that shouldn't count as two missed guesses. Don't go overboard worrying about robustness: the graders aren't malicious. Most likely, all of the code you write will be in HangmanGame.java. You should document your approach and the details of your code in your README file.

Grading
The entire assignment is graded on a ten-point scale. The writeup for parts 0 and 1 is worth four points, the Hangman game is worth four points, and the remaining two points are for code clarity, code style, and your README.

Extra Credit
There are two possibilities for extra credit described below. These don't stack: doing both will not get you more extra credit than doing only one.

If you've finished Parts 0, 1, and 2, and feel like a part 3, there is some extra credit available. Implement "categorical hangman" by finding a new list of words (cite your source!) and giving the player a choice of which category to play from. Note that picking the category should be part of the player's interaction with the game, like picking the word length or number of allowed mistakes.

Another option for extra credit is to make your Hangman look more like the actual game: with "pictures." Implement some character art to draw a man being incrementally hanged as the player makes mistakes. You'll need to figure out how this should interact with the player's choice of how many guesses they have; document your decision in your README.

To make sure that we grade your extra credit, be sure to point out that you've implemented it in your README!

The README
Like every assignment (but not APTs), you should include a README text file in your submission. It should include:

  • Your name.
  • When you started and finished the assignment, and your best estimate of how long it took.
  • The credits: who helped you. Recalling the collaboration policy from the syllabus, be sure to give credit where credit is due. Also, take credit where it's due: if you helped someone else, say so! If you didn't talk to anybody about this assignment, say that.
  • Anything we've specifically said should be included in the README.
  • Any feedback you have about the assignment itself; we're always trying to make these assignments better. If you'd rather that feedback be anonymous, there's a link for that on the course page.
You can add a text file to your code using File -> New -> Untitled Text File in Eclipse.

Code Style
Fact: code is hard to read.
How you lay out your code, and your program, make a big difference in how easy it is to understand. There are (at least!) four people who need to understand your code: you (while writing it), anybody you ask for help, your grader, and you (six months from now, when you look at it to figure out how you did something). While the following rules are not set in stone, they provide a good place to start:

  • Think about how your code is going to be organized before you've written it. Good design matters!
  • Give your classes, variables, and methods descriptive names. Naming your variables foo and bar doesn't say much; xPosition and yPosition are much better.
  • Use Java's conventions on naming. ClassesAreNamedLikeThis: words are run together, and each word is capitalized. Similarly, methodsAreNamedLikeThis: words run together, and all but the first word are capitalized. Variables are named like methods.
  • Indent your code properly. Roughly speaking, this means "everything inside a curly brace gets indented one extra level." See the code we provide for how this should look. To make this easy, Eclipse can do it for you: select your code with the mouse, and then do Source -> Correct Indentation.
  • Don't let your lines get too long. Although Java doesn't care how long your lines are, long lines are much harder to read. The closest thing there is to a universal standard is 80 characters. Eclipse makes this easy, too: in Preferences -> Text Editors, turn on "Print Margin" and set it to 80. That will add a vertical line at the 80-character mark.

Remember: easy-to-understand code makes your grader happy. Happy graders are friendly graders!