Question

hey dear i just need help with update my code i have the hangman program i...

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

here i modify the code and add Jframe to show the picture of hangman after attempts Max times . and miss word every time the oops.png is showing ..

here is the code ---

import java.util.Random;
import javax.swing.*;
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");

//using Jframw to create panel and show the image of hangman every time when max attempts complete.
JFrame jjframe=new JFrame();
JLabel label=new JLabel(new ImageIcon("oops.png")); //oops.png is a picture when user miss the character every time
//then the oops.png open every time . you can close the window or you can continue with the guess word.

jjframe.setBounds(150,150,200,200); //setting the bounds of jframe on the window screen
label.setBounds(10,10,100,100);//setting the bounds of label on the Jframe screen
jjframe.add(label); // adding the label into jframe
jjframe.setVisible(true); //for showing the picture .
jjframe.setResizable(false);

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) {
JFrame jframe=new JFrame();
JLabel max=new JLabel(new ImageIcon("hangman.png")); //hangman.png picture is showing when user attempts Max times .
//ImageIcon iii=new ImageIcon("oops.png");
jframe.setBounds(150,150,250,250);
max.setBounds(10,10,250,250);
jframe.add(max);
jframe.setVisible(true);
jframe.setResizable(false);


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();

}

}

}

and here is the sample output ---

here you can add image when user guess the right word .

i hope it would be helpful for you . Thank You !

Add a comment
Know the answer?
Add Answer to:
hey dear i just need help with update my code i have the hangman program i...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA Hangman Your game should have a list of at least ten phrases of your choosing.The...

    JAVA Hangman Your game should have a list of at least ten phrases of your choosing.The game chooses a random phrase from the list. This is the phrase the player tries to guess. The phrase is hidden-- all letters are replaced with asterisks. Spaces and punctuation are left unhidden. So if the phrase is "Joe Programmer", the initial partially hidden phrase is: *** ********** The user guesses a letter. All occurrences of the letter in the phrase are replaced in...

  • Need help with this C program? I cannot get it to compile. I have to use...

    Need help with this C program? I cannot get it to compile. I have to use Microsoft Studio compiler for my course. It's a word game style program and I can't figure out where the issue is. \* Program *\ // Michael Paul Laessig, 07 / 17 / 2019. /*COP2220 Second Large Program (LargeProg2.c).*/ #define _CRT_SECURE_NO_DEPRECATE //Include the following libraries in the preprocessor directives: stdio.h, string.h, ctype.h #include <stdio.h> /*printf, scanf definitions*/ #include <string.h> /*stings definitions*/ #include <ctype.h> /*toupper, tolower...

  • For a C program hangman game: Create the function int play_game [play_game ( Game *g )]...

    For a C program hangman game: Create the function int play_game [play_game ( Game *g )] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) (Also the link to program files (hangman.h and library file) is below the existing code section. You can use that to check if the code works) What int play_game needs to do mostly involves calling other functions you've already...

  • Create a hangman program. Sample output from your program should look like the following: Current Status...

    Create a hangman program. Sample output from your program should look like the following: Current Status for userInputs= _ _ _ _ _ _ Enter next letter: a Current Status for userInputs=a _ _ _ _ _ _ Enter next letter: e Current Status for userInputs=ae _ _ _ _ e _ Enter next letter: i Current Status for userInputs=aei _ _ _ _ e _ Enter next letter: o Current Status for userInputs=aeio _ o _ _ e _...

  • I need help with this. I need to create the hangman game using char arrays. I've...

    I need help with this. I need to create the hangman game using char arrays. I've been provided with the three following classes to complete it. I have no idea where to start. HELP!! 1. /** * This class contains all of the logic of the hangman game. * Carefully review the comments to see where you must insert * code. * */ public class HangmanGame { private final Integer MAX_GUESSES = 8; private static HangmanLexicon lexicon = new HangmanLexicon();...

  • I need help modifying this code Problem: Alphabet Monster that gets offended Write a program that...

    I need help modifying this code Problem: Alphabet Monster that gets offended Write a program that simulates a monster which acts happy when fed a letter, and unhappy when fed any other character. As long as the user keeps feeding the monster, the monster keeps asking for more, until it is offered a digit -- at that point the monster is offended and the program ends! Input Validation: • None required. Requirements: • See Alphabet Monster, LAB 6 • Use...

  • Original question IN JAVA Show the user a number of blank spaces equal to the number...

    Original question IN JAVA Show the user a number of blank spaces equal to the number of letters in the word For cat, you would show _ _ _. Prompt a user to guess a letter until they have run out of guesses or guessed the full word By default, the user should be able to make 7 failed guesses before they lose the game. If the user guesses a letter that is in the target word, it does not...

  • This is for C programming: You will be given files in the following format: n word1...

    This is for C programming: You will be given files in the following format: n word1 word2 word3 word4 The first line of the file will be a single integer value n. The integer n will denote the number of words contained within the file. Use this number to initialize an array. Each word will then appear on the next n lines. You can assume that no word is longer than 30 characters. The game will use these words as...

  • I need my c++ code converted to MASM (assembly language). The instructions below: write an assembly...

    I need my c++ code converted to MASM (assembly language). The instructions below: write an assembly program that does the following; 1. count and display the number of words in the user input string. 2. Flip the case of each character from upper to lower or lower to upper. For example if the user types in:   "Hello thEre. How aRe yOu?" Your output should be: The number of words in the input string is: 5 The output string is : hELLO...

  • Hi I need help with a java program that I need to create a Airline Reservation...

    Hi I need help with a java program that I need to create a Airline Reservation System I already finish it but it doesnt work can someone please help me I would be delighted it doesnt show the available seats when running the program and I need it to run until someone says no for booking a seat and if they want to cancel a seat it should ask the user to cancel a seat or continue booking also it...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT