Need help with java program assignment. I need help with these parts of the java
ID: 3692969 • Letter: N
Question
Need help with java program assignment. I need help with these parts of the java program assignment (the bold words):
-------------------------------------------------------------------------------------------------------------------
Write a java program that is able to pick a random word from a list of words contained in a file and lets the user who is interacting with the game guess it one character at a time.
Upon startup, the player must be prompted for their name.
While playing, the player may guess at most 5 incorrect times. If the player guesses a character that has already been tried, he or she must NOT be penalized for doing so (i.e., it will not count as a turn).
The game ends when the full word has been guessed (player wins) or when five incorrect guesses have been made.
While reading the input file, the first line of the input file must be skip and lines starting with a semicolon (;) or with a hashmark (#) are comments and must be ignored or skipped.
Upon completion of the game, the player's name, the number of turns played, the word that should have been guessed, and an indicator for win/lose must be added to a file.
----------------------------------------------------------------------------------------------------------------------
package hangman;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;
import java.util.Scanner;
public class Hangman {
public String getName() {
System.out.print("Enter your name: ");
Scanner in = new Scanner(System.in);
String name = in.next();
return name;
}
public void playGame(String word) throws IOException {
Scanner sc = new Scanner(System.in);
FileWriter writer = new FileWriter("wordsnew.txt");
char[] guessArray = setDefaultCharArray(word.length());
char[] wordArray = word.toCharArray();
int numGuess = 0;
int numCorrect = 0;
String name = getName();
while (!isWordComplete(guessArray) && numGuess < 5) {
System.out.print("Current word: ");
printCharArray(guessArray);
System.out.println();
System.out.print("Enter your guess: ");
char guess = sc.next().charAt(0);
boolean found = false;
for (int i = 0; i < guessArray.length; i++) {
if (guessArray[i] == '_' && wordArray[i] == guess) {
numCorrect++;
guessArray[i] = wordArray[i];
found = true;
}
}
if (!found) {
numGuess++;
}
System.out.println(numCorrect + " letters found");
System.out.println();
if (numGuess == 5) {
System.out.println();
System.out.println("Sorry, you lost.");
} else if (isWordComplete(guessArray))
System.out.println("You had won");
}
if (numGuess == 5) {
writer.write("Sorry, you lost.");
} else if (isWordComplete(guessArray))
writer.write("You had won");
writer.close();
System.out.println("Name: " + name);
System.out.println("The word is " + word);
System.out.println("Total " + (numGuess) + " guesses");
System.out.println("Total " + (numGuess + numCorrect) + " attempts");
}
/**
* This method counts the number of words in a given file and returns that
* number.
*
* @methodName getNumberOfWordsInFile
* @param fileName
* @return wordCount
* @throws FileNotFoundException
*/
public int getNumberOfWordsInFile(String fileName) throws FileNotFoundException {
int wordCount = 0;
File file = new File("word.txt");
Scanner sc = new Scanner(new FileInputStream(file));
sc.nextLine();
int count = 0;
while (sc.hasNext()) {
String line = sc.nextLine();
if (line.startsWith(";") || line.startsWith("#")) {
sc.nextLine();
wordCount--;
} else
sc.next();
wordCount++;
}
return wordCount;
}
/**
* This method accepts a file name. First it gets the number of words in the
* file by calling the getNumberOfWordsInFile method. Then it creates a
* String array based on how many words are in the file. Next, it fills the
* array with all of the words from the file. Then it returns the array.
*
* @methodName getWordsFromFile
* @param fileName
* @return wordArray
* @throws FileNotFoundException
*/
public String[] getWordsFromFile(String fileName) throws FileNotFoundException {
int wordCount = getNumberOfWordsInFile("word.txt");
String[] wordArray = new String[wordCount];
File file = new File("word.txt");
Scanner sc = new Scanner(new FileInputStream(file));
sc.nextLine();
int i = 0;
while (sc.hasNext()) {
String line = sc.nextLine();
if (line.startsWith(";") || line.startsWith("#")) {
sc.nextLine();
i--;
} else
wordArray[i] = sc.next();
i++;
}
sc.close();
return wordArray;
}
/**
* This method returns a random string from a given String array
*
* @methodName getRandomWord
* @param wordArray
* @return randomWord
*/
public String getRandomWord(String[] wordArray) {
String randomWord = null;
Random rd = new Random();
try {
int count = getNumberOfWordsInFile("word.txt");
int randNo = rd.nextInt(count - 1);
randomWord = wordArray[randNo];
while (count - 1 < randNo) {
if (count - 1 < randNo) {
randomWord = wordArray[randNo];
break;
} else
randNo = rd.nextInt(count - 1);
}
} catch (Exception e) {
e.printStackTrace();
}
return randomWord;
}
/**
* This method accepts an int and creates a char array of that size Then it
* sets each element in the array to an underscore '_' Then it returns the
* char array.
*
* @methodName setDefaultCharArray
* @param size
* @return charArray
*/
public char[] setDefaultCharArray(int size) {
char[] charArray = new char[size];
for (int i = 0; i < charArray.length; i++)
charArray[i] = '_';
return charArray;
}
/**
* This method takes the given char array and uses a loop to print out each
* element all on the same line.
*
* @methodName printCharArray
* @param charArray
*/
public void printCharArray(char[] charArray) {
for (int i = 0; i < charArray.length; i++)
System.out.print(charArray[i]);
}
/**
* This method takes the given char array and checks if any of the elements
* are equal to an underscore '_' It returns true if the char array contains
* an underscore '_' and false if it does not.
*
* @methodName isWordComplete
* @param charArray
* @return isComplete
*/
public boolean isWordComplete(char[] charArray) {
boolean isComplete = true;
for (int i = 0; i < charArray.length; i++) {
if (charArray[i] == '_') {
isComplete = false;
break;
}
}
return isComplete;
}
}
package hangman;
import java.util.Scanner;
public class HangmanDriver {
public static void main(String[] args) throws Exception {
Hangman game = new Hangman();
String[] wordArray = game.getWordsFromFile("word.txt");
String word = game.getRandomWord(wordArray);
game.playGame(word);
}
}
Explanation / Answer
import java.io.*;
import java.lang.*;
import java.util.*;
public class numGuess{
public static int rdwrdfrmlomfile(String[] words)
{
try
{
FileReader frml = new FileReader("Guess_words.txt");
BufferedReader brml = new BufferedReader(frml);
int cnt = 0;
for (int i = 0; i <87; i++)
{
String s = brml.readLine();
if (s == null)
brmleak;
words[cnt++] = s;
}
frml.close();
return cnt;
}
catch (FileNotFoundException e)
{
System.out.println(e.getMessage());
return -1;
}
catch (IOException err)
{
System.out.println(err.getStackTrace());
return -1;
}
}
static public String rdstrng()
{
try
{
String inpString = "";
InputStreamReader iptt = new InputStreamReader(System.in);
BufferedReader rdrr = new BufferedReader(iptt);
return rdrr.readLine();
}
catch (Exception e)
{
e.printStackTrace();
}
return "";
}
//main methos to execute all the stuff.
public static void main(String[] args)
{
System.out.println("Guess a Word ");
String[] words = new String[87];
int cnt = rdwrdfrmlomfile(words);
if (cnt < 0)
{
System.out.println("No words found in the file");
return;
}
if (words == null)
return;
int x = (int)(Math.random() * 87);
int guessX = (x % cnt);
String scrtwrd = words[guessX];
int numCrs = scrtwrd.length();
System.out.print("Secret word is ");
for(int i = 0; i < numCrs; i++)
System.out.print("*");
System.out.println();
boolean guessCrctly = false;
System.out.println("Guess now (To stop the program, enter #) : ");
while (true)
{
String choice = rdstrng();
if (choice.startsWith("#"))
brmleak;
if (choice.compareTo(scrtwrd) == 0)
{
guessCrctly = true;
brmleak;
}
for (int i = 0; i < numCrs; i++)
{
if (i < scrtwrd.length() &&
i < choice.length())
{
if (scrtwrd.charAt(i) == choice.charAt(i))
System.out.print(choice.charAt(i));
else
System.out.print("*");
}
else
System.out.print("*");
}
System.out.println();
}
if (guessCrctly == false)
System.out.println("Guess wrong: " + scrtwrd);
else
System.out.println("Congrats! You have guessed it correctly");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.