Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Topics This assignment will give you practice with file input, strings, and arrays. Instructions You are going to write a program that will allow the

Topics

This assignment will give you practice with file input, strings, and arrays.

Instructions

You are going to write a program that will allow the user to play a game like hangman (Links to an external site.)Links to an external site., where your program chooses a phrase from a text file and then displays blanks for the user to guess the word. The user will have a set number of incorrect guesses and if they do not guess the phrase within that number of tries, they will lose the game.

Max Wrong

You are to define a class constant for the maximum number of incorrect guesses that the user gets before they lose the game. You may choose this number. The more guesses the user gets, the easier the game.

You should print the number of tries the user gets in the intro text to the game.

Methods

At a minimum, your program should have the following static methods in addition to the main:

a method to load the input from a file into an array

a method to pick the puzzle for this game from the list of phrases

a method to play one game of word guess

a method to print the puzzle

a method to check if the puzzle is solved

a method to check if the guess was in the puzzle

Method: Load Input

You need to create a method that will read in phrases from a text file called hangman.txt

The first line in the file will be the number of phrases in the file. You should read this number in first so that you can construct an array of the appropriate size before reading in all the phrases.

Your method should return an array of all the phrases

Note that this method, in addition to the main, will need to throw a FileNotFoundException in the case that hangman.txt does not exist

Make your own hangman.txt or use mineYou are to create a text file to be used with your game with at least 10 phrases. Here's an example file that you can use:

10 Love, love me do Blackbird singing in the dead of night Ob-la-di, ob-la-da, life goes on, brah! Help! I need somebody. Well, shake it up, baby, now It's been a hard day's night all you've gotta do is call I wanna hold your hand I'm back in the U.S.S.R. we all live in a yellow submarine 

Method: Pick Puzzle

You should have a method that takes an array of phrases as a parameter and returns one of them at random to be the puzzle to be solved. (This method should return a String.)

Method: Play game

You should have a method that plays one game of the phrase guess.

You are expected to create a boolean array to track whether or not each letter in the puzzle should be displayed to the user. When a spot in this array is true, you display the letter to the user. When a spot in this array is false, you display an underscore ("_") to the user.

Before starting the guessing, you should mark all non-letter characters as characters to display. (e.g. You will see in the sample outputs below that some of the phrases may contain spaces (" "), apostrophes ("'"), commas (","), or other punctuation that should be displayed from the get-go.)

The user should be prompted to guess a letter as long as they have not guesses incorrectly more times than the MAX WRONG and they have not solved the puzzle.

If the user's guess is not in the puzzle, they should loose one of their tries for guessing; however, if the user does not guess incorrectly, they do not loose a try.

A message should be printed to the console to indicate whether the user's guess was in the puzzle. If it is, print how many times it occurred. If it isn't, print the number of tries they still have before the game ends.

If the user does not solve the puzzle correctly, the correct puzzle should be displayed at the end of the game.

Method: Print Puzzle

This method should print the puzzle to the screen according to the true/false values contained in the boolean array.

Method: Check if Solved

This method should return true if all the boolean values in the array are true; false, otherwise. The puzzle is solved when all the characters in the puzzle are displayed to the user.

Method: Check Guess

This method should check if the users guess is in the puzzle and should return the number of occurrences of the guess in the puzzle.

If the user's guess is entered in lowercase but appears in the puzzle in uppercase, it should still be marked to be displayed. Likewise, if the user's guess is entered in uppercase but appears in the puzzle in lowercase, it should also be marked to be displayed.

Hints and Other Notes

Note that you can solve this program creating exactly two arrays (one for the words read in from the file and one for the true/false values of whether or not to display a character). If you solve the problem this way, you will utilize string operation methods (like charAt) to look at each letter in the puzzle String. Alternatively, you can convert your puzzle from a string to an array and solve the problem treating the puzzle and whether or not to display each character as parallel arrays. Both ways are fine.

Remember that to lowercase a string, you say s.toLowerCase(), where s is a String. To lowercase a character, you say Character.toLowerCase(c), where c is a char

Remember that arrays are passed by reference to methods, so if you change an array in the method you do NOT need to return it to see the changes take place.

Sample Output

It is hard to tell below, but there ARE spaces between the different words in the puzzle as well as between each letter.

image text in transcribed

Example where the puzzle is solved

Let's play a game of word guess. You can guess wrong up to 3 times. _ _ _ _ ! _ _ _ _ _ _ _ _ _ _ _ _ _ . Guess a letter > h h was found in the puzzle 1 times. H _ _ _ ! _ _ _ _ _ _ _ _ _ _ _ _ _ . Guess a letter > E E was found in the puzzle 4 times. H e _ _ ! _ _ e e _ _ _ _ e _ _ _ _ . Guess a letter > L L was found in the puzzle 1 times. H e l _ ! _ _ e e _ _ _ _ e _ _ _ _ . Guess a letter > p p was found in the puzzle 1 times. H e l p ! _ _ e e _ _ _ _ e _ _ _ _ . Guess a letter > i i was found in the puzzle 1 times. H e l p ! I _ e e _ _ _ _ e _ _ _ _ . Guess a letter > n n was found in the puzzle 1 times. H e l p ! I n e e _ _ _ _ e _ _ _ _ . Guess a letter > s s was found in the puzzle 1 times. H e l p ! I n e e _ s _ _ e _ _ _ _ . Guess a letter > y y was found in the puzzle 1 times. H e l p ! I n e e _ s _ _ e _ _ _ y . Guess a letter > c c was not in the puzzle. You have 2 more incorrect tries. H e l p ! I n e e _ s _ _ e _ _ _ y . Guess a letter > d d was found in the puzzle 2 times. H e l p ! I n e e d s _ _ e _ _ d y . Guess a letter > o o was found in the puzzle 2 times. H e l p ! I n e e d s o _ e _ o d y . Guess a letter > m m was found in the puzzle 1 times. H e l p ! I n e e d s o m e _ o d y . Guess a letter > z z was not in the puzzle. You have 1 more incorrect tries. H e l p ! I n e e d s o m e _ o d y . Guess a letter > b b was found in the puzzle 1 times. You solved the puzzle! Good job! 

Example where the puzzle is not solved

Let's play a game of word guess. You can guess wrong up to 5 times. _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ Guess a letter > r r was found in the puzzle 1 times. _ _ _ _ _ _ _ r _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ Guess a letter > S S was found in the puzzle 1 times. _ _ _ _ _ _ _ r _ s _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ Guess a letter > t t was found in the puzzle 2 times. _ _ _ _ _ _ _ r _ s _ _ _ _ _ _ _ _ t _ _ _ _ _ _ _ _ _ _ _ _ t Guess a letter > l l was found in the puzzle 1 times. _ l _ _ _ _ _ r _ s _ _ _ _ _ _ _ _ t _ _ _ _ _ _ _ _ _ _ _ _ t Guess a letter > n n was found in the puzzle 4 times. _ l _ _ _ _ _ r _ s _ n _ _ n _ _ n t _ _ _ _ _ _ _ _ n _ _ _ t Guess a letter > e e was found in the puzzle 2 times. _ l _ _ _ _ _ r _ s _ n _ _ n _ _ n t _ e _ e _ _ _ _ n _ _ _ t Guess a letter > o o was found in the puzzle 1 times. _ l _ _ _ _ _ r _ s _ n _ _ n _ _ n t _ e _ e _ _ o _ n _ _ _ t Guess a letter > q q was not in the puzzle. You have 4 more incorrect tries. _ l _ _ _ _ _ r _ s _ n _ _ n _ _ n t _ e _ e _ _ o _ n _ _ _ t Guess a letter > x x was not in the puzzle. You have 3 more incorrect tries. _ l _ _ _ _ _ r _ s _ n _ _ n _ _ n t _ e _ e _ _ o _ n _ _ _ t Guess a letter > z z was not in the puzzle. You have 2 more incorrect tries. _ l _ _ _ _ _ r _ s _ n _ _ n _ _ n t _ e _ e _ _ o _ n _ _ _ t Guess a letter > v v was not in the puzzle. You have 1 more incorrect tries. _ l _ _ _ _ _ r _ s _ n _ _ n _ _ n t _ e _ e _ _ o _ n _ _ _ t Guess a letter > m m was not in the puzzle. You have 0 more incorrect tries. You have reached the max guesses. The puzzle was Blackbird singing in the dead of night 

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

Step: 3

blur-text-image

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 And Information Systems Uropean Conference Adbis 2020 Lyon France August 25 27 2020 Proceedings Lncs 12245

Authors: Jerome Darmont ,Boris Novikov ,Robert Wrembel

1st Edition

3030548317, 978-3030548315

More Books

Students also viewed these Databases questions

Question

Discuss the issues involved in estimation of share value

Answered: 1 week ago

Question

State the uses of job description.

Answered: 1 week ago

Question

Explain in detail the different methods of performance appraisal .

Answered: 1 week ago