CS 161 Homework 7 - Hangman

Due Fri Apr 10 Sun Apr 12 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.

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

No more lives remaining. The word was 'class'.

Note that the player can see the size of the word before they start guessing (e.g., each letter is shown as an '_' (underscore). 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: wordsSmall.txt (the default) and wordsHuge.txt (a bigger list). Note that 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. The description at the top (and the grading rubric below) gives 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.

I'll provide some hints below to help you think through how to make this program. 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 chosen word. Use copious System.out.println() calls to print out what is going on in your program (but be sure to remove/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--as large as any we've done before (in part because it's a bit less structured). However, there are no new concepts here: just methods, arrays, and control statements. 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. Wed 04/01: Read through the assignment; create (and initialize!) instance variables and constructors. This is also a good time to add in method signatures and documentation!
  2. Fri 04/03: Be able to print out a String version of the word-so-far, and check if a letter is inside a word
  3. Mon 04/06: Be able to replace a letter in the word-so-far, and count the number of letters left to guess
  4. Thu 04/09: Use the Scanner to ask the player for letters one after another replacing the letter in the word-so-far.
  5. Fri 04/10: Check if the user finished the game or are out of lives (and so the game ends), and if they guessed a previously-guessed letter.

Scheduling Note: This homework is due the day after the second midterm (similar to the schedule for Hwk 4). This means you'll also likely want to block out time for studying--though this homework does draw on most of the topics that will be on the midterm. Plan accordingly; if you start early, you can get this done well before the midterm.

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% 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

  1. Double-check that your program is complete and works correctly. If your program doesn't compile, I can't give you credit for it!
  2. Make sure that your code is commented. Give lots of inline comments, and include a JavaDoc method comment for each method in your program.
  3. You will also need to fill out the README.txt file. You can open this file in BlueJ by double-clicking on the white paper icon in the upper left corner of the class window. You should place the answer to each question below the question box, replacing the "<Replace this with your response!>". Remember to save your changes (select "Class > Save" from the menu).

    • The README as a few more questions that normal, be sure and fill it out completely!
  4. You should compress the entire Hwk7 folder into a single zip file (like you did for Lab A), and then submit it to the Hwk7 Submission page on Moodle.
    • Be sure an upload the ENTIRE project folder--that is what includes all your work!
  5. This assignment is due at midnight on Fri, Apr 10 Sun, Apr 12.

Grading

This assignment will be graded out of 28 points.