Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In java language Write a text-based program to play a game of Hangman. (If you’r

ID: 3689356 • Letter: I

Question

In java language

Write a text-based program to play a game of Hangman. (If you’re not familiar with the game, check out the Wikipedia page here.) Your program will read in a dictionary file (provided) and randomly choose a word. The user will then guess letters until they either guess the word or run out of guesses.

There is a sample program you can run posted to WebAccess.

To run the program, download the JAR file and the words.txt file to the same directory.

Navigate to that directory using "cd" (change directory) in either a terminal window (on a MAC) or a command window (on a PC- type "cmd" into the start box).

Use this command: java -jar Hangman.jar

There are two parts to this project. The first is writing the game and getting it working properly. The second is adding exception handling. I strongly recommend you complete Part I before moving on to Part II.

Part I: The Game (50 points)

Your game must follow these rules:

The user gets a pre-defined maximum number of wrong guesses. (My program uses 7.)

If a user guesses the same wrong letter twice, this letter should only count once towards the maximum wrong guesses.

The game should ignore the case (upper or lower) of guesses.

If the user guesses a correct letter, all instance of that letter should be revealed.

Here are some notes to help:

Below is pseudocode for the game. You are not required to use this approach. but you might find it helpful.

read in the list of words in the dictionary file

randomly choose a word from this list

while the user still has guesses left and they have not guessed the word

print the word (displaying guessed letters and blanks for non-guessed letters)

read the user’s guess

if the user hasn’t already guessed that letter

check if the guess is right or wrong

if the user didn’t guess the word, update the guesses remaining

Break your code up into methods. Do not have the entire game in the main method.

You will likely need several instance data variables to keep track of things. Below are some recommendations. You are not required to use these.

counters: numLetters (size of the selected word), numIncorrectGuesses, numLettersGuessedCorrectly,

char[] selectedWordArray- you might find it helpful to keep the characters of the selected word in a char[]. This will allow you to loop through the array and compare each letter to the user’s guess.

boolean[] guessedLetter- you might find it helpful to use a boolean[] that represents whether the letter at each position has been guessed. This will be useful when printing the word to the user (as letters and blanks).

ArrayList<Character> lettersGuessed- keep track of which letters have been guessed

Hint for testing: Print out the randomly selected word you are trying to guess. This makes for much easier testing!

Again, I strongly recommend getting the game working before moving on to Part II.

Part II: Exception Handling (50 points)

Add exception handling to cover three erroneous occurrences.

Note: I realize you could write a working game that accounts for these situations without using exception handling. But, for this project, you are required to use exception handling.

Situation One: The dictionary file does not exist.

Use an existing Java exception class to deal with this.

Your program should end in this situation because the game cannot be played with a dictionary.

The program should end gracefully with a nice message- not crash with an error.

Situation Two: The user enters a guess that is not a character (like + or $)

Create your own exception type to represent this situation.

When the situation occurs, throw an object of the type you just created. Catch the exception and print a message to the user about what went wrong.

The user continues on and enters a new guess. The invalid guess does not count against the user.

Hint: check out the Character class for help with detecting this situation!

Situation Three: The user enters a guess that is longer than one character (like aa or zb)

Create your own exception type to represent this situation.

When the situation occurs, throw an object of the type you just created. Catch the exception and print a message to the user about what went wrong.

The user continues on and enters a new guess. The invalid guess does not count against the user.

Your main method should not terminate because of any of these thrown exceptions. All thrown exceptions should be caught and handled.

Explanation / Answer

Hi below i have written a Pseudocode and a method run for Hangman game for your reference, Hope ths helps. :) Design - Pseudocode Repeat until user is done Play a game Ask if done Play a game: get a secret word make corresponding dashes set guesses so far to empty set done to false set bodyparts to 6 while not done show dashes show guesses so far get a guess process guess Make corresponding dashes: repeat secret length # of times: put - in dashes Process guess: check if guess is word or single letter if guess is letter: add guess to guesses so far look for guess in secret if find guess in secret put it in dashes where it belongs output good guess otherwise (not there) subtract 1 from bodyparts output bad guess output how many bodyparts are left if bodyparts is 0 output you lose done gets true if dashes same as secret output you win done gets true otherwise (guess is word): if guess equals secret output you win otherwise output you lose set done to true Change dashes to matching letter for position goes from 0 to secret length if character at position in secret matches letter put letter in that position in dashes add 1 to position */ import java.util.Scanner; import java.io.*; public class hangmanMethods { public static void main(String[] args) throws IOException { Scanner kb = new Scanner(System.in); char again = 'n'; String secret; StringBuffer dashes; final int MAXPARTS = 6; int bodyparts; boolean done; String guess; String guesses; char letter; Scanner infile = new Scanner(new FileReader("hangWords.txt")); do { // play a game secret = infile.next(); guesses = ""; done = false; bodyparts = MAXPARTS; // make dashes dashes = makeDashes(secret); while (! done) { System.out.println("Here is your word: " + dashes); System.out.println("Guesses so far: " + guesses); System.out.print("enter a guess (letter or word): "); guess = kb.next(); // process the guess if (guess.length() > 1) // process word guess { if (guess.equals(secret)) System.out.println("you win!"); else System.out.println("you lose"); done=true; } else // process single letter guess { letter = guess.charAt(0); guesses += letter; if (secret.indexOf(letter) < 0) // not there { --bodyparts; System.out.print("bad guess - "); } else // letter is in the secret { // put it in dashes where it belongs matchLetter(secret, dashes, letter); } System.out.println(bodyparts + " bodyparts are left"); if (bodyparts == 0) { System.out.println("you lose"); done = true; } if (secret.equals(dashes.toString())) { System.out.println("you win!"); done = true; } } // process single letter guess } // process whole guess if (infile.hasNext()) { System.out.print("play again (y/n)?: "); again = kb.next().charAt(0); } else System.out.println("thanks for playing (no more words)"); } while (infile.hasNext() && (again == 'Y' || again == 'y')); } // end of main /* Change dashes to matching letter for position goes from 0 to secret length if character at position in secret matches letter put letter in that position in dashes add 1 to position */ public static void matchLetter(String secret, StringBuffer dashes, char letter) { for (int index = 0; index
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote