Question

JAVA Project Please have the code written in JAVA This is a game I am trying...

JAVA Project Please have the code written in JAVA

This is a game I am trying to develop. Please let me know if its possible and what the base code is.

  1. Prompt User “Hello! Welcome to the game.”

  2. Prompt User “To begin the game, please enter a letter”

  3. Random word generator, will set the String to a new word

  4. After the user inputs a letter, the computer will enter a loop

    1. Once in the loop the computer will test each letter in the word to see if the users input will match the word

    2. If the letter matches one of the letters in the code; it will display the letter in the string of words. Here is an example _T_T____T in the word STATEMENT if a T was entered

  5. The computer will display this and then next prompt the user to enter another letter

  6. If the next letter is not included in the given word, the computer will display to the user

    failed attempt. 8 attempts left before you lose.

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

Please find the code below...

Given code will first user to enter a word. Than it will go in loop and will ask for guess.

Instead of asking word you can add random word and rest code will be same.

Hangman.java

package classes3;

import java.util.Scanner;

public class Hangman {

   public static void main(String[] args) {
       System.out.println("----------Welcome to Hangman-----------");
       System.out.print("Enter a word : ");
       Scanner sc = new Scanner(System.in);
       String word = sc.nextLine();
       char gameWord[] = word.toCharArray();
       for(int i=0;i<gameWord.length;i++){
           gameWord[i] = '_';
       }
       int incorrectGuess = word.length();
       while(true && incorrectGuess>0){
           System.out.print("So far the word is : ");
           printGuessWord(gameWord);
           System.out.println("You have "+incorrectGuess+" incorrect guess left.");
           try{
                   System.out.println("Enter your guess : ");
                   String guess = sc.nextLine();
                   if(guess.length()==1){
                       char guessChar = guess.charAt(0);
                       boolean found = false;
                       for(int i=0;i<word.length();i++){
                           if(word.charAt(i)==guessChar){
                               gameWord[i] = guessChar;
                               found = true;
                           }
                       }
                       if(found){
                           System.out.println("That's right! "+guessChar+" is in the word.");
                       }else{
                           System.out.println("Sorry! "+guessChar+" isn't in the word.");
                           incorrectGuess--;
                       }
                   }else{
                       throw new Exception();
                   }
           }catch(Exception e){
               System.out.println("Incorrect input");
           }
           if(isGameFinish(gameWord)){
               break;
           }
       }
       if(incorrectGuess==0){
           System.out.println("Sorry you loose.");
       }else{
           System.out.print("Congratulations! The word was ");
           printGuessWord(gameWord);
       }

   }

   private static boolean isGameFinish(char[] gameWord) {
       for(int i=0;i<gameWord.length;i++){
           if(gameWord[i]=='_'){
               return false;
           }
       }
       return true;
   }

   private static void printGuessWord(char[] gameWord) {
       for(int i=0;i<gameWord.length;i++){
           System.out.print(gameWord[i]);
           if(i!=gameWord.length-1){
               System.out.print(" ");
           }
       }
       System.out.println();


   }

}

output:

E Console 3 <terminated> Hangman (2) Java Application] C:Program Files\Javajre7\binjavaw.e Enter a wOrd:STATEMENT So far the

Add a comment
Know the answer?
Add Answer to:
JAVA Project Please have the code written in JAVA This is a game I am trying...
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
  • I have to use java programs using netbeans. this course is introduction to java programming so...

    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) You will implement a menu-based system for Hangman Game. Hangman is a popular game that allows one player to choose a word and another player to guess it one letter at a time. Implement your system to perform the following tasks: Design...

  • Hello need assistance with Java program: You are asked to create a program that will be...

    Hello need assistance with Java program: You are asked to create a program that will be used to create quizzes and test users based on these quizzes. 1.    Prompt a secret word and proceed if it matches a secret code. 2.    Your program should have two modules: quiz creation mode and test mode. User will be prompted in the beginning to choose from these two modes. For example: “Welcome to the quiz master: please enter 1 if you want to...

  • java code. James needs to create a computer text based game where some user has to...

    java code. James needs to create a computer text based game where some user has to enter as many words with the given letters as the given word. A good start would be writing a method that checks if a method has the same letters as the other. include pseudo code+ Input-Listen and Silent have the same word that's all information I have is a Anagram program that need to be done.ASAP just help me with the pseudo code of...

  • I am trying to make a word guessing game on java, where there are four letters...

    I am trying to make a word guessing game on java, where there are four letters and you have 10 guesses to try and guess the letter combination. When I try to play, it says every combination of letters I enter is correct. For example: "You have 10 guesses left. Enter your guess: wxyz There are 4 correct letter placements. Congrats!" I emailed my professor and she said one of my methods is wrong because I'm only checking for specific...

  • Do in Python please provide screenshots aswell. Here are the requirements for your game: 1. The...

    Do in Python please provide screenshots aswell. Here are the requirements for your game: 1. The computer must select a word at random from the list of avail- able words that was provided in words.txt. The functions for loading the word list and selecting a random word have already been provided for you in ps3 hangman.py. 2. The game must be interactive; the flow of the game should go as follows: • At the start of the game, let the...

  • I am supposed to reduce redundancy in the code and also make unknown inputs, output "unknown"....

    I am supposed to reduce redundancy in the code and also make unknown inputs, output "unknown". Step 1: Your development manager likes your program but is concerned about code duplication. Roger left a note on your desk, asking about using a feature for automatically converting strings to enumerations. Change your ToCommand method to employ this JAVA library method. (Hint: Look for a valueOf method). After converting your ToCommand method, run the program and enter an invalid command (one that does...

  • Help please, write code in c++. The assignment is about making a hangman game. Instructions: This...

    Help please, write code in c++. The assignment is about making a hangman game. Instructions: This program is part 1 of a larger program. Eventually, it will be a complete Hangman game. For this part, the program will Prompt the user for a game number, Read a specific word from a file, Loop through and display each stage of the hangman character I recommend using a counter while loop and letting the counter be the number of wrong guesses. This...

  • PROGRAM DESCRIPTION: In this assignment, you will be creating a memory matching game in C++. In t...

    c++ PROGRAM DESCRIPTION: In this assignment, you will be creating a memory matching game in C++. In this game, the user will need to match up the pairs symbols A,B,C,D,E on a 4x4 array. For example, the array could be initialized like the following: In this case, X represents an empty slot. The goal is for the user to match the A to the A, the B to the B, etc, until all pairs are matched up to win the...

  • 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...

  • 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...

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