THIS IS FOR JAVA
I have to write a method for a game of Hangman. The word the
user is trying to guess is made up of hashtags like so " ######
"
If the user guesses a letter correctly then that letter is revealed
on the hashtags like so "##e##e##"
If the user guesses incorrectly then it increments an int variable named count " ++count; "
String guessWord(String guess,String word, String pound) In this method, you compare the character(letter) read from the user to the letters in the original word, then if the letter is present in the original word, then place theletter/lettersin thepound. Return the updated poundstring.
Here is my code so far to help
import java.util.Arrays;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public class Project_4 {
public static void main(String[] args) throws
FileNotFoundException
{
// have user
enter name for file
String nameOfFile =
"dictionary.txt";
String[]
wordBank = readFile(nameOfFile);
String thisWord =
chooseRandomWord(wordBank);
System.out.println(thisWord);
createPound(thisWord);
System.out.println(createPound(thisWord));
}
public static String guessWord(char guess, String
word, String dashes)
{
// construct a scanner to read
user guesses
Scanner keyboard = new
Scanner(System.in);
// create char variable to hold
user guess
char userGuess =
keyboard.nextLine();
}
public static String[] readFile(String filename)
throws FileNotFoundException
{
//read the file
Scanner file = new Scanner(new
File(filename));
// read number of lines inside
file
int numString = 0;
while (file.hasNextLine())
{
++numString;
file.nextLine();
}
file.close();
// now we read the file into a
string array
Scanner keyboard = new Scanner(new
File(filename));
String[] words = new String[numString];
// using a for loop to read in each line to the
array
for (int index = 0; index < numString; ++index)
{
words[index] =
keyboard.nextLine();
}
keyboard.close();
return words;
}
public static String createPound(String
chosenWord)
{
String
newWord;
char[]
newWordChar = chosenWord.toCharArray();
for(int idx = 0;
idx < chosenWord.length(); ++idx) {
newWordChar[idx] = '#';
}
newWord =
String.valueOf(newWordChar);
return newWord;
}
public static String chooseRandomWord(String[]
words)
{
int numString = 0;
for (numString =
0; numString < words.length; ++numString);
int random = (int) (Math.random() *
numString);
String chosenWord = words[random];
return chosenWord;
}
public static void hangmanImage(int count, String
word)
{
if (count == 1) {
System.out.println("Wrong guess, try again");
System.out.println();
System.out.println();
System.out.println();
System.out.println();
System.out.println("___|___");
System.out.println();
}
if (count == 2)
{
System.out.println("Wrong guess, try again");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println("___|___");
}
if (count == 3)
{
System.out.println("Wrong guess, try again");
System.out.println(" ____________");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" | ");
System.out.println("___|___");
}
if (count == 4)
{
System.out.println("Wrong guess, try again");
System.out.println(" ____________");
System.out.println(" | _|_");
System.out.println(" | / \\");
System.out.println(" | | |");
System.out.println(" | \\_ _/");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println("___|___");
}
if (count == 5)
{
System.out.println("Wrong guess, try again");
System.out.println(" ____________");
System.out.println(" | _|_");
System.out.println(" | / \\");
System.out.println(" | | |");
System.out.println(" | \\_ _/");
System.out.println(" | |");
System.out.println(" | |");
System.out.println(" |");
System.out.println("___|___");
}
if (count == 6)
{
System.out.println("Wrong guess, try again");
System.out.println(" ____________");
System.out.println(" | _|_");
System.out.println(" | / \\");
System.out.println(" | | |");
System.out.println(" | \\_ _/");
System.out.println(" | |");
System.out.println(" | |");
System.out.println(" | / \\ ");
System.out.println("___|___ / \\");
}
if (count == 7)
{
System.out.println("GAME OVER!");
System.out.println(" ____________");
System.out.println(" | _|_");
System.out.println(" | / \\");
System.out.println(" | | |");
System.out.println(" | \\_ _/");
System.out.println(" | _|_");
System.out.println(" | / | \\");
System.out.println(" | / \\ ");
System.out.println("___|___ / \\");
System.out.println("GAME OVER! The word was " + word);
}
}
}
Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate
the question. Thank You.
Project_4.java
package c8;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Project_4 {
static int count=0;
public static void main(String[] args) throws
FileNotFoundException
{
// have user enter name for
file
String nameOfFile =
"dictionary.txt";
String[] wordBank =
readFile(nameOfFile);
String thisWord =
chooseRandomWord(wordBank);
System.out.println(thisWord);
String
poundString=createPound(thisWord);
Scanner keyboard = new
Scanner(System.in);
// create char variable to hold
user guess
while(true){
System.out.println("The word so far is >>>>
"+poundString);
System.out.println("Enter your guess > ");
char userGuess =
keyboard.nextLine().charAt(0);
poundString
=guessWord(userGuess,thisWord,poundString);
if(poundString.equalsIgnoreCase(thisWord)){
System.out.println("Congratulations!!! You found
the word!!!!");
break;
}
if(count>=7){
break;
}
}
}
public static String guessWord(char guess, String
word, String dashes){
String newString="";
boolean found = false;
for(int
i=0;i<word.length();i++){
if(word.charAt(i)==guess){
newString += guess;
found = true;
}else{
newString += dashes.charAt(i);
}
}
if(!found){
count++;
hangmanImage(count, word);
}
return newString;
}
public static String[] readFile(String filename)
throws FileNotFoundException
{
//read the file
Scanner file = new Scanner(new File(filename));
// read number of lines inside file
int numString = 0;
while (file.hasNextLine())
{
++numString;
file.nextLine();
}
file.close();
// now we read the file into a
string array
Scanner keyboard = new Scanner(new
File(filename));
String[] words = new
String[numString];
// using a for loop to read in each line to the array
for (int index = 0; index <
numString; ++index) {
words[index] =
keyboard.nextLine();
}
keyboard.close();
return words;
}
public static String createPound(String
chosenWord)
{
String newWord;
char[] newWordChar =
chosenWord.toCharArray();
for(int idx = 0; idx <
chosenWord.length(); ++idx) {
newWordChar[idx]
= '#';
}
newWord =
String.valueOf(newWordChar);
return newWord;
}
public static String chooseRandomWord(String[]
words)
{
int numString = 0;
for (numString = 0; numString <
words.length; ++numString);
int random = (int) (Math.random() * numString);
String chosenWord =
words[random];
return chosenWord;
}
public static void hangmanImage(int count, String
word)
{
if (count == 1) {
System.out.println("Wrong guess, try again");
System.out.println();
System.out.println();
System.out.println();
System.out.println();
System.out.println("___|___");
System.out.println();
}
if (count == 2)
{
System.out.println("Wrong guess, try again");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println("___|___");
}
if (count == 3)
{
System.out.println("Wrong guess, try again");
System.out.println(" ____________");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" | ");
System.out.println("___|___");
}
if (count == 4)
{
System.out.println("Wrong guess, try again");
System.out.println(" ____________");
System.out.println(" | _|_");
System.out.println(" | / \\");
System.out.println(" | | |");
System.out.println(" | \\_ _/");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println("___|___");
}
if (count == 5)
{
System.out.println("Wrong guess, try again");
System.out.println(" ____________");
System.out.println(" | _|_");
System.out.println(" | / \\");
System.out.println(" | | |");
System.out.println(" | \\_ _/");
System.out.println(" | |");
System.out.println(" | |");
System.out.println(" |");
System.out.println("___|___");
}
if (count == 6)
{
System.out.println("Wrong guess, try again");
System.out.println(" ____________");
System.out.println(" | _|_");
System.out.println(" | / \\");
System.out.println(" | | |");
System.out.println(" | \\_ _/");
System.out.println(" | |");
System.out.println(" | |");
System.out.println(" | / \\ ");
System.out.println("___|___ / \\");
}
if (count == 7)
{
System.out.println("GAME OVER!");
System.out.println(" ____________");
System.out.println(" | _|_");
System.out.println(" | / \\");
System.out.println(" | | |");
System.out.println(" | \\_ _/");
System.out.println(" | _|_");
System.out.println(" | / | \\");
System.out.println(" | / \\ ");
System.out.println("___|___ / \\");
System.out.println("GAME OVER! The word was " + word);
}
}
}
where my text file is
dictionary.txt
first
second
third
fourth
fifth
output
Negative case.
Get Answers For Free
Most questions answered within 1 hours.