Modify the to test if each letter the user entered is one of the letters in the
ID: 3846668 • Letter: M
Question
Modify the to test if each letter the user entered is one of the letters in the word. If the letter guessed is in the word, display a message to the user that they guessed correctly, if not display a message that the user guessed incorrectly. Add a score variable that will keep track of the number of incorrect guesses. If the user guesses incorrectly, increment the score variable. Display the score at the end of
design and program
Modify the to allow for iteration. Increase the number of guesses to 10 to solve the word. Display the word to the user with each letter as a special character such as ********. Create an array of correct letters guessed such as: char[] guessed = new char[26]; You will need counter also to keep track of how many letters are in the guessed array. You do not need to keep track of incorrectly guessed letters. Week 7 – Arrays and Output
design and program
This is your final modification to the project. Modify the of the program so that the word is stored as an array. You can use code such as the following: char[] word =
"happy".ToCharArray();
Where "happy" is replaced with your name. Add a for loop to display each letter in the word character array. If it has been guessed correctly, display the correct letter, if not display the special character. Optional: Instead of a fixed number of guesses (10), stop the program once the user guesses the correct You will be creating a Hangman program. In this game, the computer will have a hard coded word and the user will enter one letter at a time until he or she has guesses the correct word.Ascore wil be tallied that will be the number of incorrect guesses the user has. So a smaller score is better. Initially the word will be displayed as a list of special characters such as or The user will input one letter at a time and each input will be compared with the word. If the guess is correct the letter will be displayed rather than the special character. Each incorrect guess will increment the score. When the user guesses the correct word the game will be over. Below is an example of the program running:
Explanation / Answer
Code:-
import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.Arrays;
import java.util.Scanner;
class Prog{
public static void main(String[] args) {
String[] words = {"writer", "that", "program"};
int r_Number = (int) (Math.random() * words.length);
char[] enteredLetters = new char[words[r_Number].length()];
int t_Count = 0;
boolean wordIsGuessed = false;
do {
switch (enterLetter(words[r_Number], enteredLetters)) {
case 0:
t_Count++;
break;
case 1:
t_Count++;
break;
case 2:
break;
case 3:
wordIsGuessed = true;
break;
}
} while ((!(wordIsGuessed)));
System.out.println(" The word is " + words[r_Number] +
" You missed " + (t_Count -findEmptyPosition(enteredLetters)) +
" time(s)");
}
public static int enterLetter(String word, char[] inputLetters) {
System.out.println("(Guess) Enter a letter in word ");
if (! printWord(word, inputLetters))
return 3;
System.out.print(" > ");
Scanner input = new Scanner(System.in);
int emptyPosition = findEmptyPosition(inputLetters);
char userInput = input.nextLine().charAt(0);
if (inEnteredLetters(userInput, inputLetters)) {
System.out.println(userInput + " is already in the word");
return 2;
}
else if (word.contains(String.valueOf(userInput))) {
inputLetters[emptyPosition] = userInput;
return 1;
}
else {
System.out.println(userInput + " is not in the word");
return 0;
}
}
public static boolean printWord(String word, char[] inputLetters) {
boolean asteriskPrinted = false;
for (int i = 0; i < word.length(); i++) {
char letter = word.charAt(i);
if (inEnteredLetters(letter, inputLetters))
System.out.print(letter);
else {
System.out.print('*');
asteriskPrinted = true;
}
}
return asteriskPrinted;
}
public static boolean inEnteredLetters(char letter, char[] inputLetters) {
return new String(inputLetters).contains(String.valueOf(letter));
}
public static int findEmptyPosition(char[] inputLetters) {
int i = 0;
while (inputLetters[i] != 'u0000') i++;
return i;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.