CS 161 Homework 7 - Hangman

Due Fri Nov 15 at 11:59pm

Overview

For this assignment, you will be making a simple version of the word game Hangman, This game will be played in the terminal window. In this game, the computer picks a random word from a pre-supplied list. The player then guesses a letter that they think may appear in the word. The computer checks whether the letter appears in the word and either reveals all instances of it (if it exists) or takes away one of the player's lives. The computer then displays the progress that the player has made towards guessing the word. The game continues until either all of the letters have been guessed (and the player wins) or all of the player's lives are used up (and the player loses). In either case, the computer should announce this event and the game should end.

An example exchange is below. Read this carefully--your program should be able to produce very similar output! (This is one of the ways I will test your code).

Welcome to Hangman!
The word is: _ _ _ _ _ 
Guess a letter: a
The word is: _ _ a _ _ 
Guess a letter: e
Computer: I'm sorry, the word has no 'e'. You have 4 lives left.
The word is: _ _ a _ _ 
Guess a letter: s
The word is: _ _ a s s 
Guess a letter: e
Computer: You already guessed 'e'! You have 4 lives left.
Guess a letter: l
The word is: _ l a s s 
Guess a letter: c
Congratulations, the word is 'class'! You figured out the word with 4 lives remaining.
Would you like to play again? (Y/N)

If the player loses, they should see (as an example):

No more lives remaining. The word was 'class'.
Would you like to play again? (Y/N)

If the user enters "Y" when asked to play again, the variables in the game (the word to guess, the guessed letters, the number of lives) should reset and the game should continue. Otherwise, the program ends.

Note that the player is told the size of the word before they start guessing. The player is also able to guess a previously guessed letter without penalty.

This assignment should be completed individually. You are welcome to ask for help (either from me or from your classmates), but remember the Gilligan's Island rule!

Objectives

Necessary Files

You will need a copy of the Hwk7.zip file. The BlueJ project in this file contains two classes:

The project also contains two word lists: words1500.txt (the default) and words127000.txt (a bigger list). Note that these lists do not contain exactly that number of words---you'll need to get the size of the word list that you're using; do not "hard-code" the value!

And last but certainly not least, the zip file also contains the README.txt that you will need to fill out and turn in along with your assignment.

Remember to extract the files from the zip folder before proceeding!

Implementation Notes

This assignment will be different from the last few assignments in that your task is less clearly defined. Below are a list of details and requirements for the program--however, it is up to you to determine what methods you will need to make and how those methods will interact.

Be sure and read through all of the assignment carefully. Think about what variables you will need, what your functions should do, etc.

This will be more difficult than previous assignments, but I know you are all up to the task! There are lots of hints in the program description to help you on your way. And of course if you get stuck or need or just want to talk things over, please let me know and I'm happy to help :)

Assignment Details

As with any class you create, you should start by thinking about and declaring the instance variables. What attributes will your program have?

After you've determined your instance variables, you will want to initialize them in your constructors. The constructor signatures have been provided for you, but you will need to fill these in!

Now that you've set up your HangmanGame object, you'll want to start adding methods to represent each piece of functionality your program will need. Below is a list of "internal" functionality that your program will likely need to have, along with further hints and details:

Once you have this internal functionality implemented as "helper" methods, you can use those helpers to write the method(s) that will let a user play your game!

Testing

Be sure and text your code thoroughly. Use the "testing" constructor with a single word in your list will let you not need to guess at the computer's choosen word. Use copious System.out.println() calls to print out what is going on in your program (but be sure to comment them out when you are done!)

Documentation

Include lots of inline comments that explain your code, both to help me and to help you keep your place. Documentation is worth quite a bit in this assignment.

In addition, every one of your methods (including all of your helper methods) should have a full Javadoc comment (complete with @param and @return tags). Hint: Fill in these comments along with the skeleton (the method signatures of all your classes) and your pseudocode when you first start--that way you will know what each method is supposed to do!

Development Schedule

This is a big assignment--a bit bigger than any we've done before. You will want to start on this right away, and do a little bit each and every day. Try to add one piece of functionality per day, testing as you go. Work for an hour or so; if you get stuck you can put it down and come back later (this is often enough), or ask me for help. Develop iteratively---you should build a small piece, test that it works, and then move on to the next bit.

Below is a suggested order of steps, as well as good deadlines for getting each step working (if you haven't figure something out by the listed date, definitely ask for help!!):

  1. Mon 11/04: Create (and initialize!) instance variables and constructors. This is also a good time to add in method signatures and documentation!
  2. Wed 11/06: Be able to print out a String version of the word-so-far, and check if a letter is inside a word
  3. Fri 11/08: Be able to replace a letter in the word-so-far, and count the number of letters left to guess
  4. Mon 11/11: Ask the player for letters one after another replacing the letter in the word-so-far.
  5. Wed 11/13: Be able to play multiple games of Hangman; check if they finished the game or are out of lives, and if they guessed a previously-guessed letter.
  6. Fri 11/15: A fully completed and polished game, with full documentation

Scheduling Note: This homework is due the day after the second midterm. This means you'll also likely want to block out time for studying (though this homework does draw on many topics we've covered, so will be good practice). You may notice that there is not much of a jump between steps 5 and 6; this is intentional, so you have time to study as well as complete the homework.

Extensions

There is other functionality you might consider adding. They might be fun (or at leas good practice), and you can earn up to 5 points of extra credit for completing them. Only start on these once you have finished the rest of the assignment and you are sure that works. Be sure to save a copy of your working code in case you don't finish these!

Submitting Your Assignment

Grading

This assignment will be graded on approximately the following criteria: