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

Make a hangman java program: Use sample below to make it! Please make the code a

ID: 3568080 • Letter: M

Question

Make a hangman java program: Use sample below to make it! Please make the code as simple as it can be!

Sample interaction (no enhancements):

Welcome to Hangman

----|

|   |

|   

|    

|    

|

|

-----------

-----

Guess a letter (- to give up)m

     *** Sorry, m is not in the word!

----|

|   |

|   O

|    

|    

|

|

-----------

-----

m,

Guess a letter (- to give up)e

     *** Nice!!

-e---

m,

Guess a letter (- to give up)c

     *** Sorry, c is not in the word!

----|

|   |

|   O

|   |

|    

|

|

-----------

-e---

m,c,

Guess a letter (- to give up)n

     *** Nice!!

-en-n

m,c,

Guess a letter (- to give up)o

     *** Nice!!

-enon

m,c,

Guess a letter (- to give up)t

     *** Sorry, t is not in the word!

----|

|   |

|   O

| /|

|    

|

|

-----------

-enon

m,c,t,

Guess a letter (- to give up)p

     *** Sorry, p is not in the word!

----|

|   |

|   O

| /|

|    

|

|

-----------

-enon

m,c,t,p,

Guess a letter (- to give up)f

     *** Sorry, f is not in the word!

----|

|   |

|   O

| /|

| /

|

|

-----------

-enon

m,c,t,p,f,

Guess a letter (- to give up)w

     *** Sorry, w is not in the word!

----|

|   |

|   O

| /|

| /

|

|

-----------

Sorry, you have no more guesses!

Would you like to try again(n to stop)?n

Bye now... thanks for playing!

Explanation / Answer

//Plays a game of hangman. I only got it to play once.

import java.util.*;
public class Hangman{
   public static void main(String[] args) {
       Scanner input = new Scanner(System.in); //Scanner utility
       System.out.println("Let's play Hangman!");
       int gameCount = 0;
       oneGame(input);

       }
  
  
// Plays one game.
   public static void oneGame(Scanner input) {
       String answer = HangmanUtils.getWord();
       String blank = "";
       for (int i = 0; i < answer.length(); i++) {
           blank += "_ ";
       }
       String newAnswer = newAnswer(answer);
       String misses="";
       System.out.println("Word: " + blank);
       System.out.println("Misses: " + misses);

//Runs guesses while missed guesses is at or below 6      
       while (misses.length() <= 6) {
           System.out.print("Guess a letter: ");
           String s = input.next();
           boolean missesGuessed = hasLetter(misses, s.toUpperCase());
           boolean letterAlreadyGuessed = hasLetter(blank, s.toUpperCase());

//Test if the current guess was already guessed and filed in missed guesses.          
           while (missesGuessed == true){
               System.out.print("Already guessed! Guess a letter: ");
               s = input.next();
               missesGuessed = hasLetter(misses, s.toUpperCase());
           }

//Test if the current guess was already guessed and filed in correct guesses.          
           while (letterAlreadyGuessed == true) {
               System.out.print("Already guessed! Guess a letter: ");
               s = input.next();
               letterAlreadyGuessed = hasLetter(blank, s.toUpperCase());
           }
          
           boolean letterGuessed = hasLetter(answer, s.toUpperCase());
           if (letterGuessed == false) {
               misses += s.toUpperCase();
           }
          
           blank = compareWord(s.toUpperCase(), newAnswer, blank);

//Stops the game if the word was correctly guessed before 6 misses
           if (newAnswer.equals(blank.substring(0, blank.length()-1))) {
               System.out.println("The answer was: " + answer);
               System.out.println("You WIN!");
               break;
           } else {
               System.out.println();
               System.out.println("Word: " + blank);
              
//If misses has a a char then program prints and at 6 program stops guess intake.              
               if (misses.length() >= 1) {
                   printMisses(misses);
                   if (misses.length() == 6) {
                       System.out.println();
                       System.out.println("The answer was: " + answer);
                       System.out.println("You LOSE!");
                       break;
                   }
               } else {
                   System.out.println("Misses = " + misses);
               }
           }
       }
   }
  
//Gets correct answer and places spaces in between each char to make comparison easier.
   public static String newAnswer(String answer) {
       String newAnswer = "";
       if (answer.length() > 0){
           newAnswer += answer.charAt(0);
       }
       for (int i = 1; i < answer.length(); i++) {
           newAnswer += " " + answer.charAt(i);
       }
       return newAnswer;
   }

//Prints and keeps track of missed guesses.  
   public static void printMisses(String misses) {
       System.out.print("Misses = " + misses.charAt(0));
       for(int i =1; i < misses.length(); i++) {
           System.out.print(", " + misses.charAt(i));
       }
       System.out.println();
   }
  
//Tests if the letter has already been guessed.
   public static boolean hasLetter(String answer, String s) {
       for(int i=0; i< answer.length(); i++) {
           if (answer.charAt(i)==s.charAt(0)) {
               return true;
           }
       }
       return false;
   }
  
//Replaces the correctly guessed char at the correct index  
   public static String compareWord(String s, String answer, String blank) {
       for(int i = 0; i < answer.length(); i++) {
           if(answer.charAt(i) == s.charAt(0)) {
               blank = blank.substring(0, i) + s + blank.substring(i + 1, blank.length());
           }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote