Question
Java Programming Language Topic: String, Method, Math.Random, Basic Syntax For this problem, you will implement a variation of the classic word game Hangman. Here are
Java Programming Language
Topic: String, Method, Math.Random, Basic Syntax
For this problem, you will implement a variation of the classic word game Hangman.
Here are the requirements for your game:
-
The computer must select a word at random from the list of available words that was provided in words.txt. The functions for loading the word list and selecting a random word have already been provided for you.
-
The game must be interactive; the flow of the game should go as follows:
-
At the start of the game, let the user know how many letters the computer's word contains.
-
Ask the user to supply one guess (i.e. letter) per round.
-
The user should receive feedback immediately after each guess about whether their guess appears in the computer's word.
-
After each round, you should also display to the user the partially guessed word so far, as well as letters that the user has not yet guessed.
-
Some additional rules of the game:
-
A user is allowed 8 guesses. Make sure to remind the user of how many guesses s/he has left after each round. Assume that players will only ever submit one character at a time (A-Z).
-
A user loses a guess only when s/he guesses incorrectly.
-
If the user guesses the same letter twice, do not take away a guess - instead, print a message letting them know they've already guessed that letter and ask them to try again.
-
The game should end when the user constructs the full word or runs out of guesses. If the player runs out of guesses (s/he "loses"), reveal the word to the user when the game ends.
Output of a losing game
import java.util.ArrayList; import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException;
public class Hangman{ final String WORDLIST_FILENAME = "words.txt";
public Hangman(){ ArrayList wordList = loadWords(); String secretWord = chooseWord(wordList).toLowerCase(); startGame(secretWord); } /** * Returns a list of valid words. Words are strings of lowercase letters. * Depending on the size of the word list, this function may * take a while to finish. */ ArrayList loadWords(){ ArrayList wordList = new ArrayList(); System.out.println("Loading word list from file..."); try{ Scanner input = new Scanner(new File(WORDLIST_FILENAME)); while(input.hasNext()){ wordList.add(input.next()); } } catch(FileNotFoundException ex){ System.out.println("File not found"); } System.out.println(wordList.size() + " " + "words loaded."); return wordList; } /** * wordlist (list): list of words (strings) * Returns a word from wordlist at random */ String chooseWord(ArrayList wordList){ //FILL IN YOUR CODE HERE...
} /** * secretWord: string, the word the user is guessing * lettersGuessed: list, what letters have been guessed so far * returns: boolean, True if all the letters of secretWord are in lettersGuessed; * False otherwise */ boolean isWordGuessed(String secretWord, ArrayList lettersGuessed){ //FILL IN YOUR CODE HERE...
} /** * secretWord: string, the word the user is guessing * lettersGuessed: list, what letters have been guessed so far * returns: string, comprised of letters and underscores that represents * what letters in secretWord have been guessed so far. */ String getGuessedWord(String secretWord, ArrayList lettersGuessed){ //FILL IN YOUR CODE HERE...
} /** * lettersGuessed: list, what letters have been guessed so far * returns: string, comprised of letters that represents what letters have not * yet been guessed. */ String getAvailableLetters(ArrayList lettersGuessed){ //FILL IN YOUR CODE HERE...
} /** * secretWord: string, the secret word to guess. * * Starts up an interactive game of Hangman. * * At the start of the game, let the user know how many * letters the secretWord contains. * * Ask the user to supply one guess (i.e. letter) per round. * * The user should receive feedback immediately after each guess * about whether their guess appears in the computers word. * * After each round, you should also display to the user the * partially guessed word so far, as well as letters that the * user has not yet guessed. * * Follows the other limitations detailed in the problem write-up. */ void startGame(String secretWord){ //FILL IN YOUR CODE HERE...
} public static void main(String[] args){ Hangman hangman = new Hangman(); } }
The Output of a winning game should look like this: Loading word list from file 55900 words loaded Welcome to the game, Hangman! I am thinking of a word that is 4 letters long You have 8 guesses left Available letters abcdefghiiklmopgrstuwwzvz Please guess a letter: a Good guessa- - You have 8 guesses left Available letters: bcdefghiklmopgrst Please guess a letter: a Oops! You've already ques sed that letter: a You have 8 guesses left Available letters: bcdefghiiklmnopgrstuwwzVE Please guess a letter: s Oops! That letter is not in my word: _a You have 7 ruesses left Available letters: bcdefghijklmnopgrtuwzyz Please guess a letter: t Good guess: ta t You have 7 ruesses left Available letters: bcdefghiiklnnopgruwwzvz Please guess a letter: r Oops! That letter is not in my word: ta_ t You have guesses left Available letters Please guess a letter: m Oops! That letter is not in my word: ta t You have 5 guesses left Available letters: bcdefghiiklnopguwwzv Please guess a letter: c Good guess: tact Congratulations, you won! Loading word lise from file... 55900 0rd loaded . elcame to the game Hangman! I am thinking of a word that is 4 letters lang You have 8 guesses left Available letter : abcde fghk1mnopg-'tumrx v Please guess a 1 Oops! That letter is not in my word letter a You have 7 guesses left Ava ailable Lete: bsdefghij Please guess a letterb Oops! That letter is not in my word knopars turavE You have guesses left Available Letters: cdefhiiklmoporsturwxyE Please guess a letter: c Oops! That letter is not in my word You have 5 guesses left. Available letter : de fgh1k1mnopgr tumrxw Please guess a letter: a Oops! That letter is not in my word You hae 4 guesses left Available Ietters: efghi-kmn Please guess a letter: e Good guess: e You hae 4 guesses left Ava klmopgrstauwRVE ailable Lette: fghii Please guess a letter Oops! Thae Letter is not in d: You hae 3 guesses left Please guess a letter Oops! That letter is not in my ard : e You hae2 guesses left Available Lettermngpgrtuny Please guess a letter: h Oops! Thae Letter is not in d: You have 1 guesses le Available Ietters: iimnopgrsturwv Please guess a letter:i Oops! That letter is not in y word: e Boszy, you ran ous of uesses. The word va eStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started