Problem Description: You are provided a functioning program for a ‘hangman’ styl
ID: 3805661 • Letter: P
Question
Problem Description: You are provided a functioning program for a ‘hangman’ style game. Users continually guess letters and may make up to five mistakes (i.e., guess a letter that is not present or re-guess a letter). The game should end after 5 mistakes or if the user correctly guesses the word. You should start with the provided starter code and read through and understand the code first.
Requirements: 1. Have the main class named ‘Hangman’ and the secret word should be ‘FIREUPCHIPS’. 2. You should modify the program such that it makes use of methods. You are free to choose what each method will do and its name. 3. Your program should work by receiving input from the keyboard. You may assume one character guess per line. 4. You need to provide appropriate method and program headers. 5. You should have inline steps (i.e., an algorithm for the main method and user defined methods as needed). 6. You are not allowed to use arrays. 7. Your output should match up with the sample output provided below. In particular, it should show the state of the hangman, the letters that have been uncovered and a message at the end indicating success or failure. Hints: 1. You will probably end up with around 4-6 methods. 2. You should complete the inline documentation (e.g., comments, steps, headers) 3. You should rename the variables if need be. 4. You should be using the starter code as a reference.
Starter Code
Explanation / Answer
import java.util.Scanner;
public class Hangman {
static Scanner scanner = new Scanner(System.in);
public static void printCurrentState(String unknownPositions,
String secretWord) {
// Step: Print out the current status of the word. If a particular
// position is 'unknown' (i.e., has a '*') then print an underscore
// otherwise print out the letter.
for (int i = 0; i < unknownPositions.length()
&& i < secretWord.length(); i++) {
if (unknownPositions.charAt(i) == '*') {
System.out.print("_");
} else {
System.out.print(secretWord.charAt(i));
}
}
System.out.println();
}
private static char askUserToGuess() {
char guess;
// Step: Prompt the user for a letter
System.out.print("Guess a letter: ");
String response = scanner.nextLine().toUpperCase();
if (response.length() > 0) {
guess = response.charAt(0);
} else {
guess = ' ';
}
return guess;
}
private static boolean isLetterunknown(String unknownPositions,
String secretWord, char guess) {
boolean isUnknownLetter = false;
for (int i = 0; i < unknownPositions.length(); i++) {
if (unknownPositions.charAt(i) == '*'
&& secretWord.charAt(i) == guess) {
isUnknownLetter = true;
}
}
return isUnknownLetter;
}
private static String updatePositions(String secretWord,
String unknownPositions, char guess) {
String prevUnknownPositions = unknownPositions;
unknownPositions = "";
for (int i = 0; i < secretWord.length(); i++) {
if (secretWord.charAt(i) == guess) {
unknownPositions += " ";
} else {
unknownPositions += prevUnknownPositions.charAt(i);
}
}
return unknownPositions;
}
public static boolean anyUnknownRemaining(String unknownPositions) {
// if 'unknown' (i.e., has a '*')
for (int i = 0; i < unknownPositions.length(); i++) {
if (unknownPositions.charAt(i) == '*') {
return true;
}
}
return false;
}
/**************************************************************************
* printHangman - Creates an ASCII art display of a hangman, with a given
* portion of the man present.
*
* int missCound - an integer value between 0 and 5 inclusive, representing
* how much of the man to display, with 5 being a full man
*/
public static void printHangman(int missCount) {
String status = "";
status += " =========== ";
status += " ||/ | ";
status += " || | ";
status += " || ";
status += (missCount >= 1) ? "0 " : " ";
status += " || ";
status += (missCount >= 2) ? "\|" : " ";
status += (missCount >= 3) ? "/ " : " ";
status += " || ";
status += (missCount >= 4) ? "| " : " ";
status += " || ";
status += (missCount >= 4) ? ",/ " : "";
status += (missCount >= 5) ? "\, " : " ";
status += " || ";
status += ",,,||,,,,,,,,,,,,,, ";
System.out.println(status);
}
public static void main(String[] args) {
int missCount = 0;
String secretWord = "FIREUPCHIPS"; // This is the word to guess
String unknownPositions = "***********"; // This represents which
// positions are unknown
char guess = ' ';
// Continually loop through and i) show current status, ii) get a letter
// iii) check if it is miss, iv) draw man and v) possibly exit loop
do {
printCurrentState(unknownPositions, secretWord);
guess = askUserToGuess();
// Step: Check if the letter is unknown
boolean isUnknownLetter;
isUnknownLetter = isLetterunknown(unknownPositions, secretWord, guess);
// Step: If letter is unknown, update unknown positions
if (isUnknownLetter) {
// Update unknownPositions by building a new string, keeping the
// previous value for a particular position unless the
// position was unknown before AND it guess matches a position
// in the secret word.
unknownPositions = updatePositions(secretWord, unknownPositions, guess);
} else {
// Letter was not unknown so increment missCount
missCount++;
}
printHangman(missCount);
if(!anyUnknownRemaining(unknownPositions)) {
break;
}
} while (missCount < 5);
if(!anyUnknownRemaining(unknownPositions)) {
System.out.println("Good Job. You guessed it right.");
} else {
System.out.println("Game Over. Better luck next time!!");
}
}
}
Saample Output:
___________
Guess a letter: G
===========
||/ |
|| |
|| 0
||
||
||
||
,,,||,,,,,,,,,,,,,,
___________
Guess a letter: F
===========
||/ |
|| |
|| 0
||
||
||
||
,,,||,,,,,,,,,,,,,,
F__________
Guess a letter: R
===========
||/ |
|| |
|| 0
||
||
||
||
,,,||,,,,,,,,,,,,,,
F_R________
Guess a letter: O
===========
||/ |
|| |
|| 0
|| |
||
||
||
,,,||,,,,,,,,,,,,,,
F_R________
Guess a letter: I
===========
||/ |
|| |
|| 0
|| |
||
||
||
,,,||,,,,,,,,,,,,,,
FIR_____I__
Guess a letter: E
===========
||/ |
|| |
|| 0
|| |
||
||
||
,,,||,,,,,,,,,,,,,,
FIRE____I__
Guess a letter: C
===========
||/ |
|| |
|| 0
|| |
||
||
||
,,,||,,,,,,,,,,,,,,
FIRE__C_I__
Guess a letter: H
===========
||/ |
|| |
|| 0
|| |
||
||
||
,,,||,,,,,,,,,,,,,,
FIRE__CHI__
Guess a letter: P
===========
||/ |
|| |
|| 0
|| |
||
||
||
,,,||,,,,,,,,,,,,,,
FIRE_PCHIP_
Guess a letter: S
===========
||/ |
|| |
|| 0
|| |
||
||
||
,,,||,,,,,,,,,,,,,,
FIRE_PCHIPS
Guess a letter: C
===========
||/ |
|| |
|| 0
|| |/
||
||
||
,,,||,,,,,,,,,,,,,,
FIRE_PCHIPS
Guess a letter: S
===========
||/ |
|| |
|| 0
|| |/
|| |
|| ,/
||
,,,||,,,,,,,,,,,,,,
FIRE_PCHIPS
Guess a letter: U
===========
||/ |
|| |
|| 0
|| |/
|| |
|| ,/
||
,,,||,,,,,,,,,,,,,,
Good Job. You guessed it right.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.