In this homework, we will implement the game Hangman . This is how your program
ID: 3690848 • Letter: I
Question
In this homework, we will implement the game Hangman. This is how your program will work.
It will begin with a welcome message.
Then it will ask the user to enter the name of a file containing several phrases. These are the phrases the player will be asked to guess. These phrases should be loaded into an array.
During each round, a phrase will be chosen randomly from this list for the user to guess. The player will try to guess the letters in the phrase. As she guesses, the letters she correctly guesses will be revealed. She will receive 10 points for each matching letter, and she will lose 5 points for each incorrect guess. After each guess, the current pattern of guessed and unguessed letters in the phrase will be revealed, as well as the Hangman picture. The Hangman picture will depict the gallows along with up to 7 segments: the head, the torso, the left arm, the right arm, the waist, the left leg, and the right leg. If the player makes 7 incorrect guesses, she loses that round, but she wins the round if she correctly guesses the phrase before making 7 incorrect guesses. The number of points the player earned during that round will be shown at the end of the round.
The player will be asked if she wants to play another round at the end of each round. Once the player decides that she no longer wishes to continue, the program will show the maximum score the player earned.
Your program will be graded as follows:
You use a function called welcome to print the welcome message. (1 point)
You load the phrases from a file identified by the user into an array of strings. The array should be flexible in size, meaning that you should distinguish between count and capacity. (3 points)
Your program uses a function called chooseRandomPhrase to select a random phrase from the list of phrases each round (2 points)
Your program generates and manages the pattern of guessed and unguessed letters. (3 points)
Your program correctly identifies and reveals letters as you guess them using a function called checkLetter. (3 points)
Your program keeps track of the score by rewarding each matching letter with 10 points and deducting 5 points for each incorrect guess. (2 points)
Your program uses a function called printHangman to print the current hangman picture based on the number of incorrect guesses (4 points)
Your program ends each round if either the player guesses the phrase or the player incorrectly guessed too many times. (2 points)
Your program asks the user if she wants to solve another puzzle at the end of each round and resets values appropriately (2 points)
Your program keeps track of and shows the maximum score at the end of the game. (2 points)
Your submit the program as Hangman_YourLastName.java. (1 point)
Your comment your code sufficiently (1 point)
So, this program is worth 25 points.
Note that 15 points will be deducted if the program doesn't compile, and 10 points will be deducted if it crashes in the middle of running it.
Here is some sample output:
************ WELCOME TO HANGMAN ************
We'll show you some phrases, and you'll
guess the letters. You'll get 10 points
for each correct guess, and you'll lose 5
points for each incorrect guess. Good luck!
********************************************
Enter the name of the file containing phrases: hangman.txt
__________
| |
|
|
|
|
|
==============
Current string: *** *******
Enter guess: I
Great. That letter was found.
__________
| |
|
|
|
|
|
==============
Current string: *** *I*****
Enter guess: M
Sorry. That letter was not found.
__________
| |
| 0
|
|
|
|
==============
Current string: *** *I*****
Enter guess: O
Sorry. That letter was not found.
__________
| |
| 0
| |
|
|
|
==============
Current string: *** *I*****
Enter guess: C
Sorry. That letter was not found.
__________
| |
| 0
| /|
|
|
|
==============
Current string: *** *I*****
Enter guess: T
Great. That letter was found.
__________
| |
| 0
| /|
|
|
|
==============
Current string: T** *I*****
Enter guess: H
Great. That letter was found.
__________
| |
| 0
| /|
|
|
|
==============
Current string: TH* *I*****
Enter guess: E
Great. That letter was found.
__________
| |
| 0
| /|
|
|
|
==============
Current string: THE *I***E*
Enter guess: G
Great. That letter was found.
__________
| |
| 0
| /|
|
|
|
==============
Current string: THE *IGG*E*
Enter guess: W
Great. That letter was found.
__________
| |
| 0
| /|
|
|
|
==============
Current string: THE WIGG*E*
Enter guess: L
Great. That letter was found.
__________
| |
| 0
| /|
|
|
|
==============
Current string: THE WIGGLE*
Enter guess: S
Great. That letter was found.
Congratulations. You have solved the string THE WIGGLES.
Your score was 85.
Try again? Y
__________
| |
|
|
|
|
|
==============
Current string: ****** ******
Enter guess: I
Sorry. That letter was not found.
__________
| |
| 0
|
|
|
|
==============
Current string: ****** ******
Enter guess: M
Great. That letter was found.
__________
| |
| 0
|
|
|
|
==============
Current string: M***** ******
Enter guess: O
Great. That letter was found.
__________
| |
| 0
|
|
|
|
==============
Current string: MO**** ******
Enter guess: J
Great. That letter was found.
__________
| |
| 0
|
|
|
|
==============
Current string: MOJ*** ******
Enter guess: X
Sorry. That letter was not found.
__________
| |
| 0
| |
|
|
|
==============
Current string: MOJ*** ******
Enter guess: Y
Sorry. That letter was not found.
__________
| |
| 0
| /|
|
|
|
==============
Current string: MOJ*** ******
Enter guess: N
Sorry. That letter was not found.
__________
| |
| 0
| /|
|
|
|
==============
Current string: MOJ*** ******
Enter guess: J
Sorry. That letter was not found.
__________
| |
| 0
| /|
| =
|
|
==============
Current string: MOJ*** ******
Enter guess: Q
Sorry. That letter was not found.
__________
| |
| 0
| /|
| =
| /
|
==============
Current string: MOJ*** ******
Enter guess: S
Great. That letter was found.
__________
| |
| 0
| /|
| =
| /
|
==============
Current string: MOJ*** **S***
Enter guess: Z
Sorry. That letter was not found.
Sorry. The phrase was MOJAVE DESERT.
Your score was 5.
Try again? N
Your max score was 85.
Thank you for playing.
Explanation / Answer
Hi, I have written complete program. please find it and let me know if i miss something
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class Chegg {
static int Score = 0;
static int maxScore = 0;
private static Scanner reader = new Scanner(System.in);;
public static void main(String[] args) {
String file = null;
ArrayList<String> al=new ArrayList<String>();;
boolean fileread = false;
do {
try {
file = getFilePath();
BufferedReader br = new BufferedReader(new FileReader(file));
{
for (String line; (line = br.readLine()) != null;) {
al.add(line);
}
// line is not visible here.
}
br.close();
fileread = true;
} catch (Exception e) {
System.out.println("Enter correct file path:" + e.getMessage());
}
} while (!fileread);
boolean playagain = true;
Random rand = new Random();
int size = al.size();
char ch;
welcomeMsg();
while (playagain) {
int ran = rand.nextInt(size);
int wg = 0;
char st[] = al.get(ran).toCharArray();
char[] sd = new char[st.length];
int i = 0;
for (; i < st.length; i++)
{
sd[i] = '*';
if(st[i]==' ')
sd[i]=' ';
}
buildhand(wg);
Score = 0;
while (wg < 7) {
System.out.println("Current String: " + String.valueOf(sd));
System.out.print("Enter guess: ");
ch = reader.next().charAt(0);
boolean b = checkChar(st, sd, ch);
if (b) {
System.out.println("Great.That letter was found.");
if (isComplete(sd)) {
System.out.println("Congratulations.You have sovled the string " + String.valueOf(st));
System.out.println("Your score was " + Score + ".");
if (Score > maxScore)
maxScore = Score;
break;
}
} else {
wg++;
if (wg == 7) {
System.out.println("Sorry. That letter was not found");
System.out.println("The phrase was " + String.valueOf(st));
System.out.println("your score was " + Score + ".");
if (Score > maxScore)
maxScore = Score;
break;
}
System.out.println("Sorry. That letter was not found");
}
buildhand(wg);
}
System.out.println("Try again?");
reader = new Scanner(System.in);
ch = reader.next().charAt(0);
if (ch == 'N' || ch == 'n') {
System.out.println("Your max score was:" + maxScore);
System.out.println("Thank you for playing");
playagain = false;
}
}
}
public static String getFilePath() {
String s = reader.nextLine();
return s;
}
public static void welcomeMsg() {
System.out.println("************ WELCOME TO HANGMAN ************ "
+ " We'll show you some phrases, and you'll "
+ " guess the letters. You'll get 10 points "
+ "for each correct guess, and you'll lose 5 "
+ "points for each incorrect guess. Good luck! "
+ "********************************************");
}
public static boolean isComplete(char[] sd) {
int i;
for (i = 0; i < sd.length; i++) {
if (sd[i] == '*') {
return false;
}
}
return true;
}
public static boolean checkChar(char[] sd, char[] st, char c) {
int i;
boolean b = false;
for (i = 0; i < sd.length; i++) {
if (sd[i] == c && st[i] == '*') {
Score += 10;
st[i] = c;
b = true;
}
}
if (!b)
Score -= 5;
return b;
}
public static void buildhand(int n) {
char c[] = { '|', 'o', '/', '|', '\', '=', '/', '\' };
System.out.println("________________");
System.out.println("|" + " " + c[0]);
if (n > 0)
System.out.println("|" + " " + c[1]);
if (n > 2)
System.out.print("|" + " " + c[2] + c[3]);
else if (n > 1)
System.out.println("|" + " " + c[3]);
if (n > 3)
System.out.println(c[4]);
if (n > 4)
System.out.println("|" + " " + c[5]);
if (n > 5)
System.out.print("|" + " " + c[6]);
if (n > 6)
System.out.println(c[7]);
System.out.println(" | | | | " + " ========================");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.