Question

You will implement a menu-based system for Hangman Game. Hangman is a popular game that allows one player to choose a word an

If the user selects H or h: Your program must call method play Game that takes users name as parameter and start the Hangman

I have to use java programs using netbeans. this course is introduction to java programming so i have to write it in a simple way using till chapter 6 (arrays) you can use (loops , methods , arrays)

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

Here is the solution to your problem. If the answer is satisfying, please do an upvote. Also if you have any doubts regarding the submitted solution, please do ask in the comments. In the comments, you can also give me feedback. I will respect your suggestion and try to improve my answers in the future.

import java.util.*;

import java.util.Random;

import java.io.*;

import java.lang.Character;

class Main

{

static boolean checkPassword(String pass)

{

boolean n=false,u=false,l=false,d=false;

if(pass.length()==8)

n=true;

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

{

if(Character.isUpperCase(pass.charAt(i)))

u=true;

if(Character.isLowerCase(pass.charAt(i)))

l=true;

if(Character.isDigit(pass.charAt(i)))

d=true;

}

return (n && u && l && d);

}

static void playGame(String name)

{

Scanner sc=new Scanner(System.in);

String[] words = {"terminator", "banana", "computer", "cow", "rain", "water" };

Random random = new Random();

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

String asterisk = new String(new char[word.length()]).replace("\0", "-");

int count = 0;

System.out.println("Welcome "+name+", Let's Play Hangman\n\n");

String pwrd="";

while (count < 3 && asterisk.contains("-"))

{

      System.out.println(name+", Guess any letter in the word");

      System.out.println(asterisk);

      String guess = sc.next();

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

{

if(guess.charAt(0)==pwrd.charAt(i))

{

System.out.println("\n"+name+",You have already checked this letter, Please try another!\n");

continue;

}

}

String newasterisk = "";

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

{

if (word.charAt(i) == guess.charAt(0))

{

newasterisk += guess.charAt(0);

}

else if (asterisk.charAt(i) != '-')

{

newasterisk += word.charAt(i);

}

else

{

newasterisk += "-";

}

}

if (asterisk.equals(newasterisk))

{

count++;

System.out.println("Wrong Guess, "+name);

}

else

{

asterisk = newasterisk;

}

if(asterisk.equals(word))

{

System.out.println("Correct! You win "+name+"! The word was " + word);

}

}

if(count>2)

{

System.out.println("You loose "+name+"! The word was " + word);

}

}

public static void main(String[] args)

{

Scanner sc=new Scanner(System.in);

System.out.print("Enter the password: ");

String pass=sc.nextLine();

if(checkPassword(pass)==false)

System.exit(0);

String choice;

String name="";

while(true)

{

System.out.println("\nHangman Entertainment");

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

System.out.println("P- Player Name");

System.out.println("H- Hangman Game");

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

System.out.print("Please choose: ");

choice=sc.nextLine();

if(choice.charAt(0)=='P' || choice.charAt(0)=='p')

{

System.out.print("Please Enter your Name: ");

name=sc.nextLine();

}

else if(choice.charAt(0)=='H' || choice.charAt(0)=='h')

{

playGame(name);

}

else if(choice.charAt(0)=='E' || choice.charAt(0)=='e')

{

System.exit(0);

}

else

{

System.out.println("Please choose a valid choice.\n");

}

}

}

}

Add a comment
Know the answer?
Add Answer to:
I have to use java programs using netbeans. this course is introduction to java programming so...
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
  • 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...

  • Please help with this Intro to programming in C assignment! Intro to Programming in C-Large Program...

    Please help with this Intro to programming in C assignment! Intro to Programming in C-Large Program 3 - Hangman Game Assignment purpose: User defined functions, character arrays, c style string member functions Write an interactive program that will allow a user to play the game of Hangman. You will need to: e You will use four character arrays: o one for the word to be guessed (solution) o one for the word in progress (starword) o one for all of...

  • C++ Hangman (game) In this project, you are required to implement a program that can play...

    C++ Hangman (game) In this project, you are required to implement a program that can play the hangman game with the user. The hangman game is described in https://en.wikipedia.org/wiki/Hangman_(game) . Your program should support the following functionality: - Randomly select a word from a dictionary -- this dictionary should be stored in an ASCII text file (the format is up to you). The program then provides the information about the number of letters in this word for the user to...

  • Create the game hangman using c code: Program description In the game of hangman, one player...

    Create the game hangman using c code: Program description In the game of hangman, one player picks a word, and the other player has to try to guess what the word is by selecting one letter at a time. If they select a correct letter, all occurrences of the letter are shown. If no letter shows up, they use up one of their turns. The player is allowed to use no more than 10 incorrect turns to guess what the...

  • Overview In this exercise you are going to recreate the classic game of hangman. Your program...

    Overview In this exercise you are going to recreate the classic game of hangman. Your program will randomly pick from a pool of words for the user who will guess letters in order to figure out the word. The user will have a limited number of wrong guesses to complete the puzzle or lose the round. Though if the user answers before running out of wrong answers, they win. Requirements Rules The program will use 10 to 15 words as...

  • JAVA PROGRAMMING (Game: hangman) Write a hangman game that randomly generates a word and prompts the...

    JAVA PROGRAMMING (Game: hangman) Write a hangman game that randomly generates a word and prompts the user to guess one letter at a time, as shown in the sample run. Each letter in the word is displayed as an asterisk. When the user makes a correct guess, the actual letter is then displayed. When the user finishes a word, display the number of misses and ask the user whether to continue to play with another word. Declare an array to...

  • Hangman is a game for two or more players. One player thinks of a word, phrase...

    Hangman is a game for two or more players. One player thinks of a word, phrase or sentence and the other tries to guess it by suggesting letters or numbers, within a certain number of guesses. You have to implement this game for Single Player, Where Computer (Your Program) will display a word with all characters hidden, which the player needed to be guessed. You have to maintain a dictionary of Words. One word will be selected by your program....

  • PLEASE DO IN JAVA You can use any source for the dictionary or just a few...

    PLEASE DO IN JAVA You can use any source for the dictionary or just a few words are fine! description The purpose of this assignment is to practice with ArrayLists (and hopefully, you'll have some fun). As far as the user knows, play is exactly as it would be for a normal game of hangman, but behind the scenes, the computer cheats by delaying settling on a mystery word for as long as possible, which forces the user to use...

  • Using GUI to ask user for an alphabet input and to graphically display the hangman. During...

    Using GUI to ask user for an alphabet input and to graphically display the hangman. During each round, the player will enter an input, and the program will display the hangman accordingly to the input. Main method with a loop that will provide a hint and prompt the user for an input after each round. String getWord() method returns a random word from our list to use for the game. Void hangman(String guess) method that will receive the user input...

  • This is Matlab. Need answers with proper comments, please. Thanks In this part, you need to...

    This is Matlab. Need answers with proper comments, please. Thanks In this part, you need to write a simplified "Hangman" game: https:/len.wikipedia.org/wikilHangman (gamee WSA9 part5 Welcome to a simplified Hangman-style guessing g Here is a random word selected from the 100 most common nouns in the English language. For this, you will need to find and download a dictionary of the 100 most common English nouns and Pleas uess a letters Nothing guessed, exitting... > WSA9 parts store as a...

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