Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The purpose of this assignment is to use String, StringBuilder, arrays, and objects. The application in this assignment will play the word game of hangman.

The purpose of this assignment is to use String, StringBuilder, arrays, and objects. The application in this assignment will play the word game of hangman. The Eclipse project will consist of two classes:

The application class named HangmanApplication

The game class named Hangman

Details on each of these are provided below.

Create an Eclipse project named Hangman. Use a package name of edu.seminolestate.hangman. (You can substitute your domain name for edu.seminolestate if you wish.) This project will have two classes - Hangman and HangmanApplication. The HangmanApplication class will have the main method.

The following requirements apply to the Hangman class. Here is the UML class diagram (see below)

Create a constant named dictionary that contains all the mystery words the player may need to guess. Use the code below. Notice letters are not repeated in the same word. For example, if the letter s appears in a word it will appear only once. This makes the game easier to code since you only have to determine if each guessed letter occurs once.

private final String[] dictionary = { "trouble", "copyright", "problem", "form", "problem", "mythical", "discover", "consume", "document", "flame", "flow", "chart", "magnetic", "working", "predict", "subordinate", "making", "copy", "troublemaking", "uncopyrighted", "unmaledictory", "unpredictably" };

Create a private variable named incorrectLettersGuessed that can hold the letters that were guessed incorrectly. This should be a char[ ]. (As explained below, you will add a letter is added to this array when the player guesses one that is not in the mystery word.) Create a public method named getIncorrectLetters with no parameters that returns this char array as a String. We want to return it as a String to make it easy for our application programmer to display it.

Create a private StringBuilder variable named mysteryWordWithDashes. (As explained below, this StringBuilder will be filled with a dash for each letter in the mystery word. The dashes will eventually be replaced with each letter correctly guessed by the player.) Create a public getMysteryWordWithDashes method that returns this StringBuilder as a String. We want to return it as a String to make it easy for our application programmer to display it.

Create a constructor for the Hangman class. In this constructor generate a random number between zero and the last element in the dictionary. Do this programmatically using the array's length so the code doesn't need to be changed if more words are added. Use this number to select the mystery word from the dictionary. This constructor will be called each time a new game is started. That process selects a new mystery word. You will need a way to keep track of which word was selected from the dictionary. You can do this anyway you wish. I recommend you just create a new private String variable named mysteryWord and store the mystery word there. This constructor is also the best place to create the dashes in the StringBuilder variable (mysteryWordWithDashes) described above.

Create an isCorrectLetter method. This method should have one char parameter. Compare this parameter to the letters in the mystery word. Return true if the parameter character matches a letter in the mystery word otherwise return false. Do not consider the case of the letter. In other words E and e match, R and r match, etc. The isCorrectLetter method must update the StringBuilder variable (mysteryWordWithDashes) to replace the appropriate dash with the correctly guessed letter. This method must also update the char [ ] (incorrectLettersGuessed) that stores the incorrectly guessed letters. Guessing the same correct letter more than once doesn't count against a player.

Create a isCorrectWord method. This method should have one String parameter. Compare this parameter to the mystery word. Return true if the parameter word matches the mystery word otherwise return false. Make the comparison case insensitive. That is, if the mystery word is "tinder" then all of the following match "Tinder", "TINDER", "tinDER". Guessing a word that is NOT the mystery word counts as a missed letter.

Create a isWinner method. This method indicates a player has won when he/she has guessed all the letters in the mystery word. This method will have no parameters and return a boolean. Returning true indicates the player has guessed all the letters in the mystery word. Returning false indicates the player has not guessed all the letters in the mystery word.

The player gets 7 incorrect guesses before losing the game. Create a private symbolic constant for this number. Use the symbolic constant instead of the number 7 in all places in your code. Create a public method named getNumberOfGuessesLeft that returns the number of guesses remaining. Guessing the same correct letter more than once has no effect on the game. Guessing the same incorrect letter more than once reduces the number of guesses remaining each time. The incorrect letter should be added to the array of incorrectly guessed letters (incorrectLettersGuessed) each time so it may appear more than once.

Create a public method named getMysteryWord that returns the mystery word as a String. This method will have no parameters.

You can add other variables and method as needed to the Hangman class.

The following requirements apply to the HangmanApplication class.

Start a new game by creating a Hangman object.

Display a menu to ask the player what he/she wants to do. Below is an example:

Choose an option. 1. Guess a letter 2. Guess the word 3. Quit

End the application if the player selects the quit option. The application must continue to play until the player selects the quit option from the menu.

Ask the player to enter a letter if he/she chooses option 1. Read the letter. Use the Hangman object to call the method in the Hangman class that checks whether the letter is in the mystery word or not. There are four game "situations" you need to look for.

The letter is in the the mystery word and it is the last correct letter in the mystery word. Determine this by using the Hangman object to call the method in the Hangman class that checks whether all correct letters were guessed. This is called isWinner in the requirements above. Display a message on the console indicating the player won and display the mystery word. Start a new game by instantiating a new Hangman object. Continue the game by displaying the menu.

The letter is in the the mystery word but there are other un guessed letters still left. Determine this by using the Hangman object to call the method in the Hangman class that returns the number of guesses remaining. If the number of guesses remaining is greater than zero, display the number of guesses remaining, display the list of incorrect letters guessed, and display the mystery word with its dashes for un guessed letters. Continue the game by displaying the menu.

The letter is not in the mystery word but the player has more guesses. In this case display a message indicating the letter was not in the mystery word. Also display the number of guesses remaining, display the list of incorrect letters guessed, and display the mystery word with its dashes for un guessed letters. Continue the game by displaying the menu.

The letter is not in the mystery word and the player has no more guesses. In this case display a message indicating the player has lost the game. Use the Hangman object to call the method that returns the mystery word (without the dashes for the missing letters). Display this correct word. Start a new game by instantiating a new Hangman object. Continue the game by displaying the menu.

Ask the player to enter a word if he/she chooses option 2. Read the word. Use the Hangman object to call the method in the Hangman class that checks whether the word is in the mystery word or not. There are two game "situations" you need to look for.

The player correctly guessed the mystery word. In this case display a message indicating the player won. Then start a new game by creating a new Hangman instance. USe this new object to display the new mystery word (the one with dashes). Resume processing by displaying the menu.

The player incorrectly guesses the mystery word. In this case display a message indicating the wrong word was guessed. Also display the number of guesses remaining, display the list of incorrect letters guessed, and display the mystery word with its dashes for un guessed letters.

Watch this video (Links to an external site.)Links to an external site. to see how the application should work.

All input for this project must use a Scanner object. All output must be written to the console using System.out.

image text in transcribed

Hangman UML Class Diagram Hangman NUMBER WRONG GUESSES ALLOWED int dictionary String mystery Word String mysteryWordWithDashes StringBuilder numberOfIncorrectGuesses int numberof CorrectGuesses nt incorrect LettersGuessed char Hangman isCorrectLetter (letter: char) boolean isCorrect Word (word: String) boolean getMystery Word String get Mystery WordWithDashes String getIncorrectLetters String getNumberofGuessesLeft int isWinner boolean Hangman UML Class Diagram Hangman NUMBER WRONG GUESSES ALLOWED int dictionary String mystery Word String mysteryWordWithDashes StringBuilder numberOfIncorrectGuesses int numberof CorrectGuesses nt incorrect LettersGuessed char Hangman isCorrectLetter (letter: char) boolean isCorrect Word (word: String) boolean getMystery Word String get Mystery WordWithDashes String getIncorrectLetters String getNumberofGuessesLeft int isWinner boolean

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases 11th British National Conference On Databases Bncod 11 Keele Uk July 7 9 1993 Proceedings Lncs 696

Authors: Michael F. Worboys ,Anna F. Grundy

1993rd Edition

3540569219, 978-3540569213

More Books

Students also viewed these Databases questions

Question

What is the preferred personality?

Answered: 1 week ago