Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hangman! Objective: Write a game of Hangman using two classes. The first class w

ID: 2247505 • Letter: H

Question

Hangman! Objective: Write a game of Hangman using two classes. The first class will have all the back end data and will contain: . Member Variables o The secret word o The disguised word where each unknown letter is replaced by a question mark (?). For example, if the word is abracadabra and the letters a,b,e have been guessed, the disguised word would be ab?a?a?ab?a o The number of guesses made o The number of incorrect guesses o Library of words . Constructors o A default constructor initializing each value to a default value . Accessors and Mutators for all member variables. Make sure they check for valid values! . Methods o loadLibrary- This method loads o resetGame o makeGuess this returns a Boolean value if the letter was found in the secret word. Also it passes a character as a parameter. This will add to the number of guesses made, check to see if the letter was found, and if the letter was found it replaces the question marks in the disguised word with that letter. Keep in mind this may be decomposable into a couple of functions, but that is up to you. o isFound returns a true if the hidden word is discovered. Next write another class that runs th some points I do expect to see is e game. This can be done in several different ways, but . At the start of each game, t selects from1 of 5 secret words . At each update the user is shown the disguised word and the number of guesses they have made . Also they should be shown a little diagram of a person. Initially it shows the gallows, then for every missed letter the picture adds this in the following order o Head o Body o One Arm o Both Arms o Left Leg o Right Leg . After the user is shown their current state, they are then prompted to enter a letter or type something to quit the game . Once the user has successfully guessed the secret word, or has had more than 7 unsuccessful guess, the game is over and the user is prompted whether or not they want to play again. If they do the process starts all over

Explanation / Answer

Hangman.java

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

class Hangman{

public static String[] library={"application","blueberry","contradiction","dustbin","eatable"};

public static int index=0;

public String word="";

public String dis_word="";

public int no_of_guesses=0;

public int no_of_incorrect_guesses=0;

public Hangman(){

word=library[index];

for (int i=0;i<word.length();i++)

dis_word=dis_word+"?";

no_of_guesses=0;

no_of_incorrect_guesses=0;

}

public void loadLibrary(){index=(index+1)%5;}

public void resetGame(){

index=(index+1)%5;

word=library[index];

for (int i=0;i<word.length();i++)

dis_word=dis_word+"?";

no_of_guesses=0;

no_of_incorrect_guesses=0;

}

public boolean makeGuess(char c){

for (int i=0;i<word.length();i++){

if(word.charAt(i)==c){

char[] ar=dis_word.toCharArray();

ar[i]=c;

dis_word=String.valueOf(ar);

System.out.println(dis_word);

}

}

return (isFound(dis_word));

}

public boolean isFound(String dis_word){

return (dis_word.equals(word));

}

}

HangmanDriver.java

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

import java.util.*;

class HangmanDriver{

public static void main(String args[]){

while(play()){}

}

public static boolean play(){

boolean found=false;

System.out.println("Welcome to Hangman!");

Hangman h = new Hangman();

while(true){

System.out.println("You have made "+h.no_of_guesses + " guesses");

System.out.println(h.dis_word);

System.out.println("Enter a letter");

try{

Scanner s = new Scanner(System.in);

char c = s.nextLine().charAt(0);

//System.out.println((int)c);

if(!h.makeGuess(c)){

h.no_of_guesses++;

if(h.word.indexOf(c)==-1){

h.no_of_incorrect_guesses++;

if(h.no_of_incorrect_guesses>=7)break;

}

printFigure(h.no_of_incorrect_guesses);

}

else{

h.no_of_guesses++;

found=true;

break;

}

}catch(Exception e){

System.out.println(e);

}

}

if(found){

System.out.println("YOU WON!");

}

else{

System.out.println("GAME OVER!");

System.out.println("The word was " + h.word );

}

System.out.println("Would you like to play again? (Yes/No)");

Scanner s = new Scanner(System.in);

String str = s.nextLine();

if(str.equalsIgnoreCase("no"))return false;

else{

h.resetGame();

return true;

}

}

private static void printFigure(int i){

if(i==0){

System.out.println("-----------");

System.out.println("| |");

System.out.println("| ");

System.out.println("| ");

System.out.println("| ");

System.out.println("| ");

System.out.println("| ");

}

if(i==1){

System.out.println("-----------");

System.out.println("| |");

System.out.println("| O");

System.out.println("| ");

System.out.println("| ");

System.out.println("| ");

System.out.println("| ");

}

if(i==2){

System.out.println("-----------");

System.out.println("| |");

System.out.println("| O");

System.out.println("| |");

System.out.println("| ");

System.out.println("| ");

System.out.println("| ");

}

if(i==3){

System.out.println("-----------");

System.out.println("| |");

System.out.println("| O");

System.out.println("| |");

System.out.println("| |");

System.out.println("| ");

System.out.println("| ");

}

if(i==4){

System.out.println("-----------");

System.out.println("| |");

System.out.println("| O");

System.out.println("| \|");

System.out.println("| |");

System.out.println("| ");

System.out.println("| ");

}

if(i==5){

System.out.println("-----------");

System.out.println("| |");

System.out.println("| O");

System.out.println("| \|/");

System.out.println("| |");

System.out.println("| ");

System.out.println("| ");

}

if(i==6){

System.out.println("-----------");

System.out.println("| |");

System.out.println("| O");

System.out.println("| \|/");

System.out.println("| |");

System.out.println("| / ");

System.out.println("| ");

}

if(i==7){

System.out.println("-----------");

System.out.println("| |");

System.out.println("| O");

System.out.println("| \|/");

System.out.println("| |");

System.out.println("| / \");

System.out.println("| ");

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote