In this assignment, you will write a Java program that plays a simplified versio
ID: 3824830 • Letter: I
Question
In this assignment, you will write a Java program that plays a simplified version of "Hangman". The computer will pick a random word, and the user will guess letters until they've filled in the entire word. Here is the first part of a sample run of the program (user input is in italics):
If we keep guessing letters, then here is a potential end of the program run (again, user input is in italics):
Use an array that uses a file called words.txt and is placed in the correct place in this provided code:
/**
* Proj5 plays a simplified game of Hangman.
*
* @author Julie Thornton
* @version Project 5
*/
import java.util.*;
import java.io.*;
public class Proj5 {
public static Scanner s;
public static void main(String[] args) throws IOException {
s = new Scanner(System.in);
//Now, word is what the user is trying to guess
String word = pickWord("words.txt");
//Get the initial array of _ _ ... by calling init
//while the user hasn't solved the puzzle (call solved)
//let the user guess (call guessLetter)
//print the updated results (call printResults)
}
/**
* pickWord randomly picks a word for the puzzle from
* the input file
*
* @param filename The input file of dictionary words
* @return A random word from that file
*/
public static String pickWord(String filename) throws IOException {
//THIS METHOD IS COMPLETE - DON'T CHANGE IT!
Scanner inFile = new Scanner(new File(filename));
int size = Integer.parseInt(inFile.nextLine());
Random r = new Random();
int line = r.nextInt(size);
for (int i = 1; i < line; i++) {
inFile.nextLine();
}
return inFile.nextLine();
}
/**
* init builds an array of _ _ _ ... that is the same size
* as the word the user is trying to guess
*
* @param size The size of the word the user needs to guess
* @return A character array with size positions, all of which are _
*/
public static char[] init(int size) {
//Create a char array with length size (your parameter)
//Loop to initialize every spot in your array to _
//Return your char array
}
/**
* solved determines whether the user has correctly solved
* the puzzle
*
* @param partial An array like {'a', '_', '_', 'l', 'e'} (starting with all _,
but filled in with correct letters as the user guesses them)
* @param correct The word the user needs to guess (like "apple")
* @return Whether the user has solved the puzzle (all letters from
partial should match the letters in correct, with no _
remaining
*/
public static boolean solved(char[] partial, String correct) {
//THIS METHOD IS COMPLETE - DON'T CHANGE IT!
for (int i = 0; i < correct.length(); i++) {
if (partial[i] != correct.charAt(i)) return false;
}
return true;
}
/**
* guessLetter lets the user guess a letter, and then plugs that
* letter into the puzzle.
*
* @param orig The correct word the user is trying to guess
* @param cur An array like {'a', '_', '_', 'l', 'e'} (starting with all _,
but filled in with whatever correct letters the user has
guessed so far)
* @return Whether the letter guessed was in the orig word (boolean - true or false)
*
* We do not need to return the updated array because changing an array
* in a method changes the original array values (arrays are passed by reference)
*/
public static boolean guessLetter(String orig, char[] cur) {
//Ask the user to enter a letter
//Everytime you find that letter in orig (the "correct" word),
//replace the _ in cur with that letter
//For example, if cur is a _ _ l _, orig is "apple", and the user
//guesses p, you should update cur to be a p p l _
//Return whether the letter guessed was in orig
}
/**
* printResults prints the updated puzzle results after each guess
*
* @param cur The current puzzle, with all user guesses so far plugged in
(starts with all _, and may look something like {'a', '_', '_', 'l', 'e'})
* @param update Whether the latest user move guessed a new letter in the puzzle
*/
public static void printResults(char[] cur, boolean update) {
//If update is true, print cur as the current word
//(You will need to loop to print each character in cur one at a time)
//Otherwise, print that the latest letter wasn't in the word
}
}
Explanation / Answer
Hi below are the methods that needed to be completed:-
public static void main(String[] args) throws IOException {
s = new Scanner(System.in);
//Now, word is what the user is trying to guess
String word = pickWord("words.txt");
//Get the initial array of _ _ ... by calling init
char[] guessedWord=init(word.length());
//while the user hasn't solved the puzzle (call solved)
boolean guessFlag;
while(!solved(guessedWord,word)){
//let the user guess (call guessLetter)
guessFlag=guessLetter(word,guessedWord);
//print the updated results (call printResults)
printResults(guessedWord,guessFlag);
}
System.out.println("You guessed it!");
}
public static char[] init(int size) {
//Create a char array with length size (your parameter)
char arr[]=new char[size];
//Loop to initialize every spot in your array to _
for(int i=0;i<size;i++)
arr[i]='_';
//Return your char array
return arr;
}
public static boolean guessLetter(String orig, char[] cur) {
//Ask the user to enter a letter
System.out.print("Guess a letter :");
Scanner kbd=new Scanner(System.in);
char ltr;
ltr=kbd.next().charAt(0);
//Everytime you find that letter in orig (the "correct" word),
int charPos;
charPos=orig.indexOf(ltr);
//replace the _ in cur with that letter
//For example, if cur is a _ _ l _, orig is "apple", and the user
//guesses p, you should update cur to be a p p l _
boolean foundFlag=false;
while(charPos>=0){
foundFlag=true;
cur[charPos]=ltr;
charPos=orig.indexOf(ltr, charPos);
}
//Return whether the letter guessed was in orig
return foundFlag;
}
public static void printResults(char[] cur, boolean update) {
//If update is true, print cur as the current word
if(update==true){
System.out.print("Current word:");
//(You will need to loop to print each character in cur one at a time)
for(int i=0;i<cur.length;i++)
System.out.print(cur[i]);
System.out.println();
}
else
//Otherwise, print that the latest letter wasn't in the word
System.out.println("The letter is not in the word");
}
Hope this helps!