Programming Assignment 12: Hangman

Estimated reading time: 10 minutes
Estimated time to complete: 90–120 minutes (plus debugging time)
Prerequisites:
Starter code: hangman-student.zip
Collaboration: not permitted

Overview

So, it turns out one of the goals of this course is to better prepare you for 187. What better way to check how things are going than to give you a (slightly modified) version of one of the earlier 187 assignments? In this assignment, you’ll complete an implementation of a children’s game called Hangman. (What is the deal with computer science and morbid children’s games as toy problems?) Instead of using Java’s Collections framework, you’ll implement a simplified version of a singly-linked list to store the lists of letters used in the game.

IMPORTANT NOTE: You should not use any classes from the java.util package, or any arrays, in your implementation. The main point of this assignment is to build and use your own linked list! I know it will feel really weird not to just write new ArrayList<>() here, but that’s how almost all of 187 goes! You’ll (almost) never write code without the Collections or arrays once you’re out of 187, but in 187 you will be forbidden from using various parts of your normal toolkit in order to force you to master linked data structures. Start practicing now!

We’ve provided a set of unit tests to help with automated testing. The Gradescope autograder includes a few more tests, including tests you do not have access to. We have left the names of these tests visible as a hint to what they’re testing, but you will likely need to write some tests of your own to complete this assignment.

Note that if you run into trouble with the Eclipse debugger mysteriously quitting during unit tests, it’s due to the timeout rule that we use to catch infinite loops:

@Rule
public Timeout globalTimeout = Timeout.seconds(10); // 10 seconds

Comment out the above two lines in all test files, and the debugger will no longer exit (and test cases can now get stuck in infinite loops).

Goals

  • Build a program to support the game of Hangman.
  • Implement a simple singly-linked list.
  • Use this linked list to support the game.
  • Practice writing unit tests.
  • Test code using unit tests.
  • Prepare for 187.
  • Finally complete the relentless avalanche of 190D programming assignments.

Downloading and importing the starter code

As in previous assignments, download and save (but do not decompress) the provided archive file containing the starter code. Then import it into Eclipse in the same way; you should end up with a hangman-student project in the “Project Explorer”.

What to do

Broadly, there are two parts to this assignment: building the linked list implementation, and then using it to build the game of hangman.

The linked list

There are two classes you’ll need to complete. First fill out the Node class, which should be very straightforward. Remember to correctly initialize the instance variables in the constructor.

Then move to the LinkedList class. Use the Node class to build the linked list, just like we demonstrated in lecture with lists quickly earlier in the semester and in more detail later with stacks and queues. Note that the class starts with a head and tail pointer: you’ll need to use both. Also note that it never removes elements, so you won’t have to deal with that complication. How do you write contains? Your code has to traverse the list, starting at the head, checking each Node‘s value. If it the value is in the node, great, return true. If you reach the end of the list, return false. How do you traverse the list? By following the next references (using getNext) until you reach the end – in a correctly-constructed list, the last node has a next value of null.

Implementing LinkedListGameModel

LinkedListGameModel supports a game of Hangman, but doesn’t actually play it. See GameDriver for a driver class with a main method that will run the game. (The game won’t actually work until your implementation in LinkedListGameModel is correct.)

You’ll need to implement each instance method in LinkedListGameModel, and add some instance variables to support the game. What variables? Well, you’ll need to keep track of the word (in a String, most likely), and the list of guesses made so far. Normally you might reach for an ArrayList, but remember: you’re getting ready for 187. Use your LinkedList implementation! You might also keep track of some other things (like the state of the game or perhaps the current String representation of the board), or you might choose to compute these on the fly. The only public methods in this class that should change the class’s state (that is, modify its instance variables) are the constructor and doMove; everything else just returns some fact about the current state of the game but does not change it!

Other notes

In the actual 187 version of this assignment, the autograder will give you a zero if it detects use of arrays or java.util classes anywhere in your code. But this ain’t 187, and not everyone in the course plans to take 187. So I’ve disabled these checks in the autograder; if you must, you can use arrays or whatnot to complete this assignment, and you’ll probably find it trivial to do so. But I strongly, strongly encourage you to figure out how to do this assignment using only the LinkedList class you build yourself, especially if you’re planning to take 187 next semester.

Sample output

Once the game model works, the driver’s output will look something like:

_ _ _ _ _ _
pick a letter: i
Good guess!

[i]

_ i _ _ _ _
pick a letter: i
You guessed i already!
guess: _ i _ _ _ _

[i]

_ i _ _ _ _
pick a letter: n
Good guess!

[i, n]

_ i n n _ _
pick a letter: m
Bad guess!

|--------|
[i, n, m]

_ i n n _ _
pick a letter: w
Good guess!

|--------|
[i, n, m, w]

w i n n _ _
pick a letter: q
Bad guess!

|
|
|
|
|
|
|--------|
[i, n, m, w, q]

w i n n _ _
pick a letter: x
Bad guess!

-----
|
|
|
|
|
|
|--------|
[i, n, m, w, q, x]

w i n n _ _
pick a letter: k
Bad guess!

-----|
|    |
|
|
|
|
|
|--------|
[i, n, m, w, q, x, k]

w i n n _ _
pick a letter: g
Bad guess!

-----|
|    |
|    O
|
|
|
|
|--------|
[i, n, m, w, q, x, k, g]

w i n n _ _
pick a letter: h
Bad guess!

-----|
|    |
|    O
|    |
|    |
|
|
|--------|
[i, n, m, w, q, x, k, g, h]

w i n n _ _
pick a letter: o
Bad guess!

-----|
|    |
|    O
|    |
|    |
|   /
|
|--------|
[i, n, m, w, q, x, k, g, h, o]

w i n n _ _
pick a letter: e
Good guess!

-----|
|    |
|    O
|    |
|    |
|   /
|
|--------|
[i, n, m, w, q, x, k, g, h, o, e]

w i n n e _
pick a letter: r
Good guess!

-----|
|    |
|    O
|    |
|    |
|   /
|
|--------|
[i, n, m, w, q, x, k, g, h, o, e, r]
You won!
The word was winner!
Number of guesses: 12

Submitting the assignment

When you have completed the changes to your code, you should export an archive file containing the src/ directory from your Java project. To do this, follow the same steps as from Assignment 01 to produce a .zip file, and upload it to Gradescope.

Remember, you can resubmit the assignment as many times as you want, until the deadline. If it turns out you missed something and your code doesn’t pass 100% of the tests, you can keep working until it does.