Java Program There is a so hard program. Need someone help me,please! Write a te
ID: 3689316 • Letter: J
Question
Java Program
There is a so hard program. Need someone help me,please!
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 words.txt and randomly choose a word. The user will then guess letters until they either guess the word or run out of guesses.
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 lettersGuessed- keep track of which letters have been guessed
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.
Allow the user to play multiple games. Keep track of the number of wins, losses, and the win percentage. Print this information at the end of each game.
Some words form words.txt:
I
a
ad
ah
al
am
an
ar
as
at
au
ax
ba
shafted
shahdom
shakers
shakeup
shakier
shakily
shaking
shallop
shallot
shallow
shamans
shamble
shaming
kerfs
ketal
ketch
sag
sam
san
sao
sap
sat
saw
sax
say
sci
sea
...
Thank you very much for your work!
Explanation / Answer
This is the best i Could do...!!!!
import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class Hangman {
public static void main(String[] args) {
Scanner Input = new Scanner(System.in);
String[] CollectionOfWords = {"","I","a","ad","ah","al","am","an","ar","as","at","au","ax","ba","shafted","shahdom","shakers","shakeup","shakier","shakily","shaking","shallop","shallot","shallow","shamans","shamble","shaming","kerfs","ketal","ketch","sag","sam","san","sao","sap","sat","saw","sax","say","sci","sea"};
int radmNumber = (int) Math.ceil(Math.random() * CollectionOfWords.length);
int counter = 10;
String radmWord = "banana"; //CollectionOfWords[radmNumber];
char[] genRadmLetter = radmWord.toCharArray();
char[] genRadmLetter2 = radmWord.toCharArray();
for (int x = 0; x < genRadmLetter.length; x++) {
genRadmLetter[x] = '?';
}
Set<Character> guesses = new HashSet<Character>();
do {
System.out.println("Guess a letter.");
System.out.println(String.valueOf(genRadmLetter));
System.out.println("Hello. Guess a letter.");
char guessedLetter = Input.next().charAt(0);
if (guesses.contains(guessedLetter)) {
System.out.println("You've used this guess, guess again");
} else {
guesses.add(guessedLetter);
boolean found = false;
for (int rw = 0; rw < genRadmLetter2.length; rw++) {
if (genRadmLetter2[rw] == guessedLetter) {
genRadmLetter[rw] = guessedLetter;
found = true;
}
}
if (!found) {
System.out.println("Wrong letter, try again.");
counter = counter - 1;
System.out.println("Lives left: " + counter);
}
}
boolean result = Arrays.equals(genRadmLetter, genRadmLetter2);
if (result == true) {
break;
}
if (counter == 0) {
break;
}
} while (counter != 0);
if (counter == 0) {
System.out.println("You lose. The word was: " + radmWord);
} else {
System.out.println("Well done, you have guessed the word.");
System.out.println("Your final score is: " + counter);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.