Today you will be completing a program that enables the program's user to play the game of Hangman. Let's first review the game of Hangman:
Now that we've reviewed the game, here is what you need to do:
Begin by starting a new project in Eclipse. Name your project HangmanLab. Next, download the following file to your HangmanLab folder as you've done in prior labs.
The Hangman class implements the game in the main method, which uses two additional static methods. The code for this class is organized into sections. You'll be completing the code in the sections indicated below:
Review the code provided, but do not change the code at this point - you'll be doing that in the tasks below. Familiarize yourself with the code sections, the flow of execution, and the constants and variables used. The following is a summary of the program's main loop that repeats so long as the game hasn't been won and the player hasn't used all their guesses:
Note we're using a separate method, named printHangman, to output the hangman picture using characters. We've put this code in its own method to make the main method more readable. Now, in the main method we only have to ask that the printHangman be done with the method call: printHangman(badGuessesCount);. Below shows what this output looks like for a point during the execution of a Hangman game (with parts of the hangman filled in for each wrong guess):
___ | | | 0 | \|/ | | | | __|__ g e o g _ _ p _ _ Wrong guesses: t l s b
Note the blue boxes just to the right of the scroll bar in Eclipse's editor window. These boxes indicate sections that have been tagged as TODO items. Click on one of the blue boxes and it will take you to the corresponding "TODO" item. You can use this feature in your own programs by putting comments in your code that begin with TODO like the comments you see in the Hangman program.
Go to section 3B. of the program and complete the code as indicated for each of the TODO items in that section. Check with your Lab TA that your code is correct before moving on to the next task.
Next go to section 4B of the program and complete the code as indicated for this TODO item. You should be able to run your program to see if the new code you added works as expected (but the game isn't finished).
Next go to section 4E of the program and review the code there. Once you understand what this code is doing then complete the code as indicated for this TODO item. Run your program and make sure that the code you've added works correctly.
Next go to section 4C of the program and complete the code as indicated for this TODO item. Run your program and make sure that the code you've added works correctly.
When this task is completed, show your Lab TA.
Switch so that your partner is in control of the keyboard
At this point our game is nearly complete, but it doesn't detect when a game has been won. We'll use a separate method to detect this. You'll be learning more about methods in lecture soon, but we'll give them a try now. They're a very useful way to reduce redundancy and make our programs more organized. (You just need to use a main method in programming assignment 1. In program 2 you'll be using methods.)
In addition to main, we've already been using another method to display the hangman picture. We use the method by coding the method call statement: printHangman(badGuessesCount);. A method call has the name of the method possibly followed by additional information in parentheses that we call arguments (aka parameter values). Our hangman picture displaying method needs one argument, the number of bad guesses, so that it can determine how much of the hangman picture to draw.
The return type of this method is void indicating it doesn't return any answer. Often methods return some answer to the calling program using a return statement, which we'll describe further below. Methods that return something would need to say the type of the answer they return instead of void. Take a look at this print method to see the layout used for coding methods.
For this task, you'll complete the gameWon method that is passed the array of correct guesses and returns a boolean value (i.e., true/false). If the array of correct guesses doesn't have any blanks (i.e., '_') then all of the characters in the word have been guessed and the game is won. Go to section 6A of the program and complete the gameWon method as indicated for this TODO item. We've already coded the call of method gameWon in the condition of the program's main game loop (while loop at top of section 4). We've also provided the shell for the method in which you'll fill the method's body.
For our main game loop to work, we need gameWon to return either true or false depending on whether or not the game has been won. We'll use a return statement as in this example: return false;. The method ends as soon as a return statement is executed and the method's call is replaced with its return value. In our program main calls gameWon, which determines if the game was won, and then returns either true or false accordingly.
You should now have a working Hangman game! To make the game more fun, uncomment the code in section 3A so that a random word is chosen from the secret word's list. Now run the program to play the game! Verify that your game is working correctly.
Your work for programming assignments must be submitted electronically so that we can run your programs against a suite of tests. We've developed a form for submitting your work that will enable you to submit files from any computer connected to the internet. We'd like you to give it a try to help confirm that your account has been properly configured. Click on the "Forms Page" in the "Resources" section fo the CS 302 menu and login using your CS user ID and password. From the menu choose "Hangman" and then "Submit Files". Follow the instructions on the page to submit your Hangman.java program file. The form only allows files with the correct name to be submitted. Check this out by trying to submit a file with the wrong name and see what happens.
Note both lab partners must submit the Hangman.java file to receive full credit for this lab. Use the CS 302 Forms Page to submit and also backup your programming assignment work:
When you and your partner have submitted your program, show your working program to your Lab TA and show them that you've both submitted your program.
In the lab time remaining, make the following improvements to the program:
You are welcome to use additional static methods if you feel they would be helpful on these challenge tasks. Remember the first line of a method, which is called the heading, is in this form:
public static <return type> <methodName> (<comma separated list of parameters>)
Where the return type is the Java type of the return value (if no value is returned, use void for the return type), method name is an identifier you choose, and the parameters are pairs of type name followed by parameter name (separated by commas if you use more than one parameter). Refer to the two methods in our program for examples.
Let your Lab TA know which of these challenge tasks you've completed.