Question
Your goal is to write a program that facilitates two players play this game. Initially, the program prompts the Player 1 to type the secret
Your goal is to write a program that facilitates two players play this game. Initially, the program prompts the Player 1 to type the secret word (Player 2 closes his/her eyes, so the (s)he can not the word typed by Player 1). The program reads the word. Prints 100 blank lines, so that Player 2 can not see the secret word. From this point, the program interacts with Player 2. During each round, the program displays the current partial word, prints the hangman picture, displays the number of guesses left. Then the program asks whether Player 2 wishes to guess a character of word. If Player 2 chooses to guess a character, then it asks Player 2 to enter the character. Based on the character, the program updates the partial word, and guesses remaining, and the game proceeds to next round. If Player 2 chooses to guess the word, then the program asks the Player 2 to enter the word. Based on the word Player 2 enters, the programs determines the winner.
Create a class name Hangman. This class should have following methods.
public static String createPartialWord(String secretWord). This method takes a String named secretWord as parameter and returns the initial partial word. All the characters in the 8 partial word are -, and the length of the initial partial word is the sam was the length of the secretWord. For example, createPartialWord("program") returns -------.
public static String replaceChar(String word, char c, int i). This method takes three parameters: A String named word, a char named c and an int named i. It returns a new string formed by replacing the ith character of word with c. For example, replaceString("run", a, 1) returns the string ran. If word does not have any character at index i, then this method simply returns word. For example, replaceString("run", a, 3) returns run.
public static String updatePartialWord(String partial, String secret, char c). This method takes three parameters: A String named partial, a String named secret, and a char named c. This method does the following: For every index i at which the character c appears in secret, it replaces the ith character of partial with c, and returns the new string formed.
For example,
updatePartialWord("-----", "tease", e) returns -e--e
updatePartialWord("-e--e", "tease", a) returns -ea-e
updatePartialWord("beach", "ranch", a) returns baach
updatePartialWord("test", "just", c) returns test This method must make a call to the method replaceChar.
private static void printHangman(int guessLeft). This method takes an int representing the guesses left (for player 2) as parameter and prints the hangman picture. The method is given below. Just type the code in your program.
public static void printHangman(int guessLeft) {
String HEAD = " ";
String BODY = " ";
String LEGS = " ";
String LEFTARM = " ";
String RIGHTARM = " ";
System.out.println("_____");
System.out.println("| |");
if (guessLeft < 6) {
HEAD = "()"; }
System.out.println("| " + HEAD);
if (guessLeft < 5) {
BODY = "||"; }
if (guessLeft < 4) {
LEFTARM = "\\"; }
if (guessLeft < 3) {
RIGHTARM = "/"; }
System.out.println("| " + LEFTARM + BODY + RIGHTARM);
if (guessLeft < 2) {
LEGS = "/"; }
if (guessLeft < 1) {
LEGS += "\\"; }
System.out.println("| " + LEGS);
System.out.println("|_____ "); }
The main method must do the following tasks:
1. Reads secretWord from Player 1, and prints 100 blank lines.
2. Forms initial partial word by calling the method createPartialWord
3. Interacts with Player 2 by repeating the following steps until the game is over.
Display the partial word, number of guesses remaining, print Hangman. Hangman must be printed by making a call to the method printHangman.
Prompts Player 2 to guess either word or character (Player 2 types word or char).
If Player 2 chooses to guess the secret word, then it reads the word from player 2 and determines the winner.
If the Player 2 chooses to guess a character, then it reads the character and updates the partial word (if the guessed character is in secret word). Updating partial word must be done by calling the method updatePartialWord. If the guessed character is not in the secret word, then player 2 is docked a guess.
Note that the game is over any one of the following happens
Player 2 chooses to guess secret word and guesses correctly.
Player 2 chooses to guess secret word and guesses incorrectly
partial word equals secret word
Player 2 has 0 guesses left
When the game ends, the program must declare the winner and print hangman picture. While interacting with players, your program must print appropriate descriptive messages (see sample runs).
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started