Question

In: Computer Science

Please create a Hangman game in Java language. For this game a random word will be...

Please create a Hangman game in Java language. For this game a random word will be selected from the following list (abstract, cemetery, nurse, pharmacy, climbing). You should keep a running tab of the user’s score and display it on screen; the user begins with 100 points possible if they correctly guess the word without any mistakes. For every incorrect letter which is guessed, 10 points should be taken away from what is left of their total possible points. For every correct letter which is guessed, their score is left unchanged. The user’s score should never be lower than 0, or exceed the mentioned points possible. Keep track and print a list of the player's high scores at the end.

Solutions

Expert Solution

import java.util.Scanner;

public class Hangmangame {

        static String[] totalWords = { "abstract", "cemetery", "nurse", "pharmacy", "climbing" };
        static String guessString = totalWords[(int) (Math.random() * totalWords.length)];
        static String userGuessWord = new String(new char[guessString.length()])
                        .replace("\0", "_");
        static int points = 100;

        /* Main */
        public static void main(String[] args) {
                Scanner scan = new Scanner(System.in);
                
                System.out.println("Starting with " + points + " points");
                while (points >= 0 && userGuessWord.contains("_")) {
                        System.out.println("Its time for guess letter");
                        System.out.println(userGuessWord);
                        String userGuessLetter = scan.next();
                        checkForHang(userGuessLetter);
                }
                scan.close();
        }

        /* processing user guess */
        public static void checkForHang(String userGuessLetter) {
                String tempString = "";
                for (int i = 0; i < guessString.length(); i++) {
                        if (guessString.charAt(i) == userGuessLetter.charAt(0)) {
                                tempString += userGuessLetter.charAt(0);
                        } else if (userGuessWord.charAt(i) != '_') {
                                tempString += guessString.charAt(i);
                        } else {
                                tempString += "_";
                        }
                }

                if (userGuessWord.equals(tempString)) {
                        points -= 10;
                        System.out.println("Your guess was wrong. 10 points deducted.");
                        System.out.println("Points: " + points);
                        System.out.println();
                } else {
                        userGuessWord = tempString;
                }
                if (userGuessWord.equals(guessString)) {
                        System.out.println("Amazing!! You guessed the correct one.. "
                                        + guessString);
                }
        }

}
**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

How would I create a Hangman game that chooses  a random word and the player needs to...
How would I create a Hangman game that chooses  a random word and the player needs to guess it, letter by letter using  JavaScript and jQuery to allow user interaction. The content and the style of the page must be updated based on user’s input.
In JAVA Create a simple guessing game, similar to Hangman, in which the user guesses letters...
In JAVA Create a simple guessing game, similar to Hangman, in which the user guesses letters and then attempts to guess a partially hidden phrase. Display a phrase in which some of the letters are replaced by asterisks: for example, G* T*** (for Go Team). Each time the user guesses a letter, either place the letter in the correct spot (or spots) in the phrase and display it again or tell the user the guessed letter is not in the...
Write python code so that in a game of hangman the word ‘apple’ is displayed as...
Write python code so that in a game of hangman the word ‘apple’ is displayed as “ _ _ _ _ _ “ and is usable code for any random words. Include in this code how to after the player has guessed a correct letter replace the _ with the correct guess shown as “ _ p _ _ _”
Create a Hangman game using C++ by QT creator. please separate each code that will insert...
Create a Hangman game using C++ by QT creator. please separate each code that will insert in hider files and cpp files and use an accurate screeshoots if needed. Thanks in advance.
For this assignment, you will create a command-line version of the game ​Hangman. You should work...
For this assignment, you will create a command-line version of the game ​Hangman. You should work in a group of two on the project and not view Hangman code of other students or found on-line. Submit this project-- your .java file-- here on Canvas. For this assignment you will research on StringBuilder Class (​Use this link​) and use it in your code. Functional requirements (rubric) ● Your game should have a list of at least ten phrases of your choosing...
In python Using the example code from the HangMan program in our textbook, create a Word...
In python Using the example code from the HangMan program in our textbook, create a Word Guessing Game of your choice. Design a guessing game of your own creation. Choose a theme and build your words around that theme. Keep it simple. Note: I would highly recommend closely reviewing the HangMan code and program from Chapter 10 before starting work on this project. You can run the program via my REPL (Links to an external site.). Using python Similar to...
Hangman We're going to write a game of hangman. Don't worry, this assignment is not nearly...
Hangman We're going to write a game of hangman. Don't worry, this assignment is not nearly as difficult as it may appear. The way hangman works (for this assignment - we are doing a simplified game) is as follows: the computer will choose a word. (For this version, a word is selected from a static list encoded into the program -- so the words are pretty limited). Let's say the word is "cocoa". the computer shows the user how many...
C++ language You will create a Hangman class. Possible private member variables: int numOfCharacters; //for the...
C++ language You will create a Hangman class. Possible private member variables: int numOfCharacters; //for the secret word char * secretWord; char *guessedWord; public: //please create related constructors, getters, setters,constructor() constructor() You will need to initialize all member variables including the two dynamic variables. destructor() Please deallocate/freeup(delete) the two dynamic arrays memories. guessALetter(char letter) 1.the function will check if the letter guessed is included in the word. 2. display the guessed word with related field(s) filled if the letter guessed...
Can you create a player vs computer Hangman game on MATLAB using nested loops, for loops,...
Can you create a player vs computer Hangman game on MATLAB using nested loops, for loops, if loops, while loops, arrays and conditional execution, as well as creating an image of the game board. The list below must be considered in the code. - Selecting a word from a dictionary (a text file) - Reading a single letter from the user - Building a character array showing the letters matched so far - Keeping count of the number of guessed...
Please use Java language in an easy way with comments! Thanks! Create a calculator that uses...
Please use Java language in an easy way with comments! Thanks! Create a calculator that uses an array of buttons for numbers from 0-9, the . for decimals, and for operators +, -, * ,/ and = as well as a JTextfield that displays the current value or the result. Use ActionListeners and LayoutManagers appropriately. The example below shows how to determine which button has been clicked. ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { System.out.println(actionEvent.getActionCommand()); }...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT