Constructor Summary Constructors Constructor HangmanMain() Method Summary All Methods Static Methods Concrete Methods Mo

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Constructor Summary Constructors Constructor HangmanMain() Method Summary All Methods Static Methods Concrete Methods Mo

Post by answerhappygod »

Constructor Summary Constructors Constructor Hangmanmain Method Summary All Methods Static Methods Concrete Methods Mo 1
Constructor Summary Constructors Constructor Hangmanmain Method Summary All Methods Static Methods Concrete Methods Mo 1 (130.9 KiB) Viewed 39 times
HangmanGame
public HangmanGame()
selectDifficulty
public String selectDifficulty(String difficulty)
Selects the appropriate word list file to use for the user's selected difficulty. Note that if "hard" is not entered (ignoring case), the default is using the easy word list.
Parameters:
difficulty - User-selected difficulty
Returns:
String name of .txt file to set wordList with
initializeWords
public void initializeWords(String file)
Initializes the wordList. Requires a String that names a file to read words in from. Reads the words in from that file and adds the words to the wordList ArrayList.
Parameters:
file - File to read words in from
wordSelect
public void wordSelect()
Selects a random word from the wordList and sets the game's hangman word.
guess
public void guess(String guess)
Controls the guessing functionality of the game.
Parameters:
guess - User's letter guess
correctGuess
public void correctGuess(String guess)
Controls functionality when user guesses a correct letter.
Parameters:
guess - User's letter guess
incorrectGuess
public void incorrectGuess(String guess)
Controls functionality when user enters an incorrect guess.
Parameters:
guess - User's letter guess
printWordProgress
public void printWordProgress()
Prints the current user's current progress in guessing the word.
Constructor Summary Constructors Constructor Hangmanmain Method Summary All Methods Static Methods Concrete Methods Mo 2
Constructor Summary Constructors Constructor Hangmanmain Method Summary All Methods Static Methods Concrete Methods Mo 2 (98.26 KiB) Viewed 39 times
HangmanMain needs to be writin.
Constructor Summary Constructors Constructor HangmanMain() Method Summary All Methods Static Methods Concrete Methods Modifier and Type Method Description static void main(String[] args) For this method, you will need to: Create a new HangmanGame object Call the printScreen() method (remember how to call methods on different objects!) Use getInput("Would you like easy or hard words? ") to get user input on what difficulty they would like Call initializeWords(), passing in select Difficulty(), which requires the input from the user you just got. Call wordSelect(). Print the initial hangman gallows by calling getZero(). Call print Word Progress() While the game is not finished, get input from the user on what letter they guess using getInput("Guess a letter: ") and call guess(), passing in the guess input. Methods inherited from class java.lang.Object clone, equals, finalize", getClass, hashCode", notify", notifyAll", toString, wait, wait, wait Constructor Details HangmanMain public HangmanMain() Method Details Description main public static void main(String[] args) For this method, you will need to: 1. Create a new HangmanGame object 2. Call the printScreen() method (remember how to call methods on different objects!) 3. Use getInput("Would you like easy or hard words?") to get user input on what difficulty they would like 4. Call initializeWords(), passing in select Difficulty(), which requires the input from the user you just got. 5. Call wordSelect(). 6. Print the initial hangman gallows by calling getZero(). 7. Call printWordProgress() 8. While the game is not finished, get input from the user on what letter they guess using getInput("Guess a letter: ") and call guess(), passing in the guess input. Parameters: args -
Constructor Details HangmanView public Hangmanview() Method Details getZero public void getzero() Prints an ASCII art String to represent the hangman with no parts on the board. Displayed when no incorrect guesses were made. getOne public void getone () Prints an ASCII art String to represent the hangman with one part on the board - the head. Displayed when one incorrect guess was made. get Two public void getTwo () Prints an ASCII art String to represent the hangman with two parts on the board - the head and body. Displayed when two incorrect guesses were made. getThree public void getThree() Prints an ASCII art String to represent the hangman with three parts on the board - the head, body, and right arm. Displayed when three incorrect guesses were made. getFour public void getFour() Prints an ASCII art String to represent the hangman with four parts on the board - the head, body, right arm, and left arm. Displayed when four incorrect guesses were made. getFive public void getFive() Prints an ASCII art String to represent the hangman with five parts on the board - the head, body, right arm, left arm, and right leg. Displayed when five incorrect guesses were made. get Six public void getsix() Prints an ASCII art String to represent the hangman with six parts on the board - the head, body, right arm, left arm, right leg, and left leg. Displayed when six incorrect guesses were made. getInput public string getInput(string" displayText) Displays text to console and then grabs user response. Parameters: splay - Text to display to console. Returns: String input from user printScreen public void printscreen() Prints a welcome screen for the Hangman game.
import ... public class HangmanGame { 3 HangmanView view = new HangmanView(); ArrayList String word; char[] wordArray; int wrongGuesses = 0; ArrayList boolean finished = false; /** * Selects the appropriate word list file to use for the user's selected difficulty. * Note that if "hard" is not entered (ignoring case), * the default is using the easy word list. * @param difficulty User-selected difficulty * @return String name of .txt file to set wordList with */ } public String select Difficulty (String difficulty) { if(difficulty.equalsIgnoreCase( anotherString: "hard")){ } } } else{ } wordList = new ArrayList(); /** * Initializes the wordList. Requires a String that names a file to read words in from. * Reads the words in from that file and adds the words to the wordList ArrayList. * @param file File to read words in from */ public void initializeWords (String file) { try{ } incorrectLetters = new ArrayList<>(); /** } return "HardHangmanWords.txt"; return "EasyHangmanWords.txt"; } /** * Selects a random word from the wordList and sets the game's hangman word. */ public void wordSelect(){ File fileToScan = new File(file); Scanner fileScanner = new Scanner (fileToScan); while (fileScanner.hasNext()) { } }catch (FileNotFoundException e) { System.err.println(e); wordList.add(fileScanner.next()); Random randy = new Random(); word = wordList.get(randy.nextInt (wordList.size())); wordArray = new char[word.length()]; for(int i = 0; i < word.length(); i++) { wordArray = '_'; /** * Controls the guessing functionality of the game. * @param guess er's lett guess */ public void guess (String guess) { guess.toLowerCase(); } else{ if(word.contains (guess)) { correctGuess (guess); incorrectGuess (guess); } printWord Progress(); * Controls functionality when user guesses a correct letter. * @param guess User's letter guess public void correctGuess (String guess) { for(int i = 0; i < word.length(); i++) { if(word.charAt(i) == guess.charAt(0)) { wordArray = guess.charAt(0);
/** * Controls functionality when user guesses a correct letter. * @param guess User's letter guess */ public void correctGuess (String guess) { for(int i = 0; i < word.length(); i++) { if(word.charAt(i) == guess.charAt(0)) { wordArray = guess.charAt(0); } } } for(char c wordArray) { if (c == '_'){ return; } } /** * Controls functionality when user enters an incorrect guess. * @param guess User's letter guess */ public void incorrectGuess (String guess) { wrongGuesses++; incorrectLetters.add(guess); } finished = true; System.out.println("You guessed it!"); } Collections.sort (incorrectLetters); if(wrongGuesses == 1){ view.getOne(); System.out.println("Incorrect letters: + incorrectLetters.toString()); } else if (wrongGuesses == 2) { view.getTwo(); } else if (wrongGuesses == 3){ view.getThree(); System.out.println("Incorrect letters: " + incorrect Letters.toString()); } } else if (wrongGuesses == 4){ view.getFour(); System.out.println("Incorrect letters: + incorrect Letters.toString()); } } else if (wrongGuesses == 5) { view.getFive(); System.out.println("Incorrect letters: " + incorrectLetters.toString()); } else{ System.out.println("Incorrect letters: + incorrectLetters.toString()); view.getSix(); /** * Prints the current user's current progress in guessing the word. */ public void printWord Progress () { for (char c wordArray) { System.out.print(c); System.out.println("You ran out of guesses. Better luck next time!"); finished = true; System.out.println();
import java.util.Scanner; public class HangmanView { Scanner scanner = new Scanner(System.in); /** * Prints an ASCII art String to represent the hangman with no parts on the board. * Displayed when no incorrect guesses were made. */ public void getZero () { System.out.println( } /** } 11 /** 11 11 11 * Prints an ASCII art String to represent the hangman with one part on the board - * the head. Displayed when one incorrect guess was made. */ public void getOne() { System.out.println( } "1 11 11 } /** * Prints an ASCII art String to represent the hangman with two parts on the board - * the head and body. Displayed when two incorrect guesses were made. */ public void getTwo () { System.out.println( 11 11 11 11 | 11 | 0 * Prints an ASCII art String to represent the hangman with three parts on the board - * the head, body, and right arm. Displayed when three incorrect guesses were made. */ public void getThree () { System.out.println( 11 +\n" + |\n" + _|\n" + |\n" + |\n" + ---\n"); | 0 | 11 +\n" + |\n" + _|\n" + |\n" + |\n" + ---\n"); | 0 / | +\n" + |\n" + |\n" + _|\n" + |\n" + ---\n"); } /** * Prints an ASCII art String to represent the hangman with four parts on the board - * the head, body, right arm, and left arm. Displayed when four incorrect guesses were made. */ public void getFour () { System.out.println( -+\n" + |\n" + \n" + |\n" + |\n" + ---\n"); --+ +\n" + | _|\n" + " \\ 0 / |\n" + "1 | |\n" + _|\n" + ---\n");
public void getFive () { System.out.println( } /** "I | |\n" + " \\ 0 / |\n" + I |\n" + } || } /** * Prints an ASCII art String to represent the hangman with six parts on the board - * the head, body, right arm, left arm, right leg, and left leg. Displayed when six incorrect guesses were made. */ public void getSix() { System.out.println( 11 h.getThree(); h.getFour(); h.getFive(); h.getSix(); || "1 |\n" + ---\n"); | |\n" + " \\ 0 / |\n" + "1 I |\n" + / \\ |\n" + ---\n"); 11 * Displays text to console and then grabs user response. * @param displayText Text to display to console. * @return String input from user */ public String getInput (String displayText) { System.out.println(displayText); return scanner.nextLine(); /** * Prints a welcome screen for the Hangman game. */ public void print Screen() { System.out.println("WELCOME TO HANGMAN!"); } public static void main(String[] args) { HangmanView h = new HangmanView(); h.getZero(); h.getOne(); h.getTwo();
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply