Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

hey dear i just need help with update my code i have the hangman program i just want to draw the body of hang man

hey dear

i just need help with update my code i have the hangman program i just want to draw the body of hang man when the player lose every attempt program is going to draw the body of hang man until the player lose all his/her all attempts and hangman be hanged and show in the display you can write the program in java language:

this is the code bellow:

import java.util.Random;

import java.util.Scanner;

public class Hangmann {

// helper method to generate a String containing n number of '*' characters

static String generateAsteriskString(int n) {

String s = "";

// looping and appending n number of '*'s to s

for (int i = 0; i < n; i++) {

s += "*";

}

return s;

}

// helper method to make char represented by c visible in hidden string if

// it exist on secret word

static String replace(String secret, String hidden, char c) {

String s = "";

// looping through secret string

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

// if current char in secret word is same as c, appending c to

// result String

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

s += c;

} else {

// otherwise appending what hidden word already had in this

// position to result String

s += hidden.charAt(i);

}

}

// returning result String

return s;

}

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

// creating an array of words

String[] words = { "write", "that", "program", "java", "friend", "cool" };

// random number generator

Random random = new Random();

// maximum attempts (missed attempts) per word

int MAX_ATTEMPTS = 8;

String ch = "y";

// looping until user choose to quit

while (ch.equalsIgnoreCase("y")) {

// generating a random index between 0 and words.length-1, taking

// word at this index as the secret word

String secretword = words[random.nextInt(words.length)];

// initializing number of misses to 0

int misses = 0;

// creating a String of same length containing asterisks, as hidden

// word

String hidden = generateAsteriskString(secretword.length());

// looping until hidden and secret word are same or until all

// attempts are exhausted

while (!hidden.equals(secretword) && misses < MAX_ATTEMPTS) {

// asking and reading a letter

System.out.print("(Guess) Enter a letter in word " + hidden

+ " > ");

// reading a word, converting to lower case, taking first

// character

char letter = scanner.next().toLowerCase().charAt(0);

// checking if this character is already made visible (exist on

// hidden)

if (hidden.contains("" + letter)) {

System.out.println("\t" + letter

+ " is already in the word");

}

// checking if this character is not in secret word

else if (!secretword.contains("" + letter)) {

System.out.println("\t" + letter + " is not in the word");

misses++; // its a miss

} else {

// otherwise updating hidden word by calling replace method

hidden = replace(secretword, hidden, letter);

}

}

// displaying word and misses.

// using 'time' instead of 'times' if misses equals 1 (singular

// form)

if (misses == MAX_ATTEMPTS) {

System.out

.println("Sorry! You could not guess the word. You missed "

+ MAX_ATTEMPTS

+ " attempts, the word was "

+ secretword);

} else if (misses == 1) {

System.out.println("The word is " + secretword

+ ". You missed " + misses + " time");

} else {

System.out.println("The word is " + secretword

+ ". You missed " + misses + " times");

}

// asking and reading user choice whether to continue or not

System.out

.print("Do you want to guess another word? Enter y or n> ");

ch = scanner.next();

}

}

}

drawing it means only give a shape to hangman it means give a picture of hangman or design

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

Students also viewed these Databases questions

Question

Identify who may be responsible for performance appraisal.

Answered: 1 week ago

Question

Explain the performance appraisal period.

Answered: 1 week ago