Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Create two methods for a Hangman game in Java. The setUniqueWord() method goes through an ArrayList of input words and chooses a word at random

Create two methods for a Hangman game in Java. The setUniqueWord() method goes through an ArrayList of input words and chooses a word at random that has not been used in the hangman game before. The guessLetter(char guess) method will take a letter that is guessed by the user and reveal it in the word if it is correct. For example, if the word that is chosen for the game is "puzzle", the user would see ******, and guess a letter. If they guessed "u", then they would see " *u**** "

The following classes have been provided:

HangmanHW.java

import java.util.*; public class HangmanHW { private int nGuess=7; // The number of wrong guesses (default 7 ) private ArrayList inputWords; // Input list of valid words private String targetWord; // Word trying to be guessed in the current game private HashSet usedWords = new HashSet<>(); // Current usedWords of past games private String currentWord; // this is the current word trying to be guessed (E.g., fishbowl ); private String currentWordDisplayString; // This is the current word that is displayed during the game // E.g., *is*b*wl as the player is guessing it. public HangmanHW(int nGuess, ArrayList inputWords ){ this.nGuess = nGuess; this.inputWords = inputWords; } public int getNGuess( ) { return nGuess; } public void setUniqueWord( ) { // description: go through the inputWords and return one that has not been used before // // class properties: // inputWords - an arrayList of valid HangMan words // for example, if // inputWords are // Banana, Paper, Scissors, Rock, Pattern, notebook, simple, salesdate // usedWords are // Banana, Paper, Scissors, Rock, Pattern // Then Sets // currentWord to either notebook, simple, salesdate ranomly // and updated usedWords currentWord = ""; } public int guessLetter( char guess ) { // input: char guess -> the letter being guessed // returns: int nChanged ... number of letters being changed // sets: currentWordDisplayString to the changed letter // e.g. currentWordDisplayString = "*****" // guess = "p"; // currentWordDisplayString will be set to "*pp**" int nChange = 0; currentWordDisplayString = ""; return nChange; } public void setInitialCurrentWordDisplay( ) { int sz = currentWord.length(); currentWordDisplayString = ""; for( int i=0; i 

----------------------------------------------------------------------------------------------------------------------------------------

HangManGetInputWords.java import java.util.ArrayList;

public interface HangManGetInputWords {

ArrayList getInputWords();

}

----------------------------------------------------------------------------------------------------------------------------------------

HangManGetGameProductionData.java

import java.util.ArrayList; class HangManGetGameProductionData implements HangManGetInputWords { @Override public ArrayList getInputWords() { ArrayList inWords = new ArrayList<>(); inWords.add("apple"); inWords.add("baker"); inWords.add("charlie"); inWords.add("dog"); inWords.add("fox"); inWords.add("green"); return inWords; } } 

----------------------------------------------------------------------------------------------------------------------------------------

driveHangManHWSln.java

import java.util.ArrayList; import java.util.Scanner; public class driveHangManHWSln { public static void main(String[] args) { HangManGetInputWords inW = new HangManGetGameProductionData(); ArrayList inWords = inW.getInputWords(); HangmanHW hm = new HangmanHW( 7, inWords); String word = hm.getUniqueWord(); String displayWord = hm.getCurrentWordDisplayString(); System.out.printf(" --- Starting DIsplay word->%s nGuesses=%s", displayWord, hm.getNGuess()); boolean playingThisGame = true; while ( playingThisGame == true) { char letter = askQuestion( "Guess a letter-> ", displayWord); int res = hm.guessLetter( letter ); int nGuess = hm.getNGuess(); if ( res == -1 ) { System.out.printf(" Nope not there res=%s displayword:%s nGuess=%s", res, displayWord, nGuess ); } else { System.out.printf(" Found it res=%s nGuess=%s", res, displayWord, nGuess ); } displayWord = hm.getCurrentWordDisplayString(); if ( nGuess == 0 ) { System.out.printf(" Sorry... Game over displayWord:%s Out of guesses:%s", displayWord, nGuess); } if ( displayWord.indexOf( '*' ) == -1 ) { System.out.printf(" Fantasic you got displayWord:%s with nGuess:%s left", displayWord, nGuess ); } } } private static char askQuestion(String question, String displayWord) { Scanner s = new Scanner(System.in); System.out.printf(" %s", question); String str = s.next(); return str.charAt(0); } } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Instant Reference

Authors: Gruber, Martin Gruber

2nd Edition

0782125395, 9780782125399

More Books

Students also viewed these Databases questions

Question

1. Socialization policy in mass media?

Answered: 1 week ago