COP 3330 Homework 2

$30.00

Category: Tags: , , , You will Instantly receive a download link for .zip solution file upon Payment || To Order Original Work Click Custom Order?

Description

5/5 - (7 votes)

Problem: Hangman
You will write a program that implements the game hangman.
The program should be capable of running multiple games until the user wants to quit. To this end, the
program should output a menu at the beginning of execution:
1. Start a new game.
2. Quit.
For each game, the rules are as follows:
1) First, the program decides on a secret word to guess. The secret word is chosen at random (using
the Random class [do not use Math.random() in your program]) from the list of words provided in
words.txt
a. File format: The first line of words.txt contains an integer n, indicating the number of
words in the file. The next n lines each contain one word. Each word contains one or
more uppercase letters. You are guaranteed that words.txt exists in the same directory as
your source file.
2) After choosing the secret word, the program displays blanks (underscores) representing the letters
in the word at the beginning of the game.
3) For each turn, ask the user to guess a letter, and one of the following should happen:
a. If the user entered an invalid guess, then display a message to prompt for a new guess
input. The invalid guess should not be counted against the user as one of the five wrong
tries s/he can make. A guess is invalid if it is not a lowercase or uppercase letter.
b. If the user entered a letter that s/he has tried before, display a message to indicate so.
This repeated guess of the same letter should not be counted against one of the five
wrong tries s/he can make.
c. If the user entered a new letter and the letter appears in the word, display a message to
indicate so. Then for the next turn, each occurrence of the letter is revealed. Thus, in the
middle of the game, the word shown is a mixture of letters and blanks (underscores), the
blanks indicating the locations of letters that haven’t been guessed yet by the user. If the
user has correctly guessed all letters of the word, the user wins, display a message to
indicate so (with the revealed secret word). If the user wins, the game ends and the
program should go back to the starting menu.
d. If the user entered a new letter but the letter does not appear in the word, display a
message to indicate so. This guess is counted as a wrong try for the user. If the user has
made five wrong guesses, then s/he loses, display a message to indicate so (with the
secret word). If the user loses, the game ends and the program should go back to the
starting menu.
Output Details
For each turn of the game, your program will show the user the current state of the board. In the beginning,
the current state of the board is all underscore characters, one per letter in the word. There should be a
space between the letters/underscores on the board. For example, if the secret word was “BASEBALL”,
then in the beginning the user would see:
_ _ _ _ _ _ _ _
After the user guesses a letter, if they guess a letter in the word, then in the subsequent display, print out the
capital version of that letter in the appropriate slot(s), instead of underscores. Once a letter has been
guessed, it should always be printed out instead of an underscore on all subsequent boards. In our example,
if the user guesses ‘B’ for his/her first letter, then the subsequent board to be displayed should look like:
B _ _ _ B _ _ _
If the user guesses ‘e’ for his/her second letter, then the next board displayed should look like:
B _ _ E B _ _ _
If the user makes five wrong guesses before the board is completed, print out:
Sorry, you have not guessed the correct word in time. You lose.
The correct word was:
X X X X
If the user guesses the word correctly before guessing five incorrect letters, then print out:
Congratulations, you win!
Here is the final board:
X X X X
where the X’s represent the letters in the whole word. Optionally, when printing the state of the board, you
may print an ASCII art representation of the state of the hangman board. See Sample Run 2.pdf for
an example of what this may look like. Feel free to come up with your own creative ASCII art for this part
of the assignment! You may earn up to 10 points of extra credit for printing the ASCII art version of the
board.
Sample Runs
See Sample Run 1.pdf and Sample Run 2.pdf for two sample runs of the program. Note that the
sample runs are NOT comprehensive tests. You should test your program with different data than is shown
there based on the specifications given. The user input is given in bold and italics while the program
output is in plain text.
Deliverables
Submit your source code (Hangman.java) over WebCourses. You must send your source file as an
attachment using the “Add Attachments” button. Assignments that are typed into the submission box will
not be accepted.
Restrictions
1. Your program must compile using Java 6.0 or later on the command line.
2. Your program must reside in a class called Hangman, inside source file Hangman.java.
3. Your program should never crash.
a. You must anticipate all possible ways a user can crash your program, and handle the
situations appropriately.
b. The only way to exit your program is when the user enters the number 2 from the starting
menu. Your program should never terminate as a result of an unhandled Exception.
c. The TAs will use the exact same file words.txt when grading your assignment. Thus,
it will have identical format and content.
4. You must use the Random class to help choose the secret word, and the random choice must be
over all possible words in words.txt.
Documentation
Your code should include a header comment with the following information: file name, your name, course
number and date.
/*
Author:

Date:
Course: COP 3330-01
*/
You WILL lose credit if this information is not found in the beginning of your source code. You should
also include inline comments to describe different parts of your program.
Execution and Grading Instructions (This is for TAs)
Points Total: 100
1. Make a directory.
2. Download Hangman.java into the directory.
3. Copy words.txt into the same directory. The directory should contain two files:
Hangman.java and words.txt.
4. Check the source code for header comments, inline comments and the use of Random object
required by the assignment.
5. Open command prompt and go to the directory. Compile the program with “javac
Hangman.java”.
6. Run the program with “java Hangman”. Play the game a few times.
Points Breakdown
Execution: 80
Documentation: 20
Hints:
Below is a list of hints to help you with your homework. You do not need to do the assignment in this way
for full credit.
1. To simplify the implementation, you should make use of static variables and methods as shown in
class.
2. At the very beginning, your program should read all words in words.txt into a String array.
3. To choose a random secret word, pick an integer between 0 and X, then use this integer as an
index into your array of words. You can decide what X should be.
4. For the menu choices, you can use catch the NumberFormatException as shown in class, or
simply use if-else statements. Be sure to check that the input choice is either 1 or 2, but not
another integer.
5. You can use a boolean array of size 26 to keep track of which letter has been guessed by the
user.
6. You can use Arrays.fill method to fill the board with underscores.
7. You can use .toCharArray() method on a String object to convert the String into a char array.
8. You can use new String(char[]) constructor to convert a char array into a String object.
9. You can use .toUpperCase() and .toLowerCase() methods on a String object to convert
letters in the String into all uppercase or all lowercase.
10. You can use Character.isLetter(char) method to determine whether a char is a letter.