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.
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...
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...
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...
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...
Java Project Requirements: 1.Write a Java program that plays a word game with a user. The...
Java Project Requirements: 1.Write a Java program that plays a word game with a user. The program asks the user questions and then creates a paragraph using the user’s answers. 2.The program must perform the following: a.Uses a Scanner object to ask the user: (The program asks for no other information) i.Full Name (First and Last name only) - stores this Full Name in one String object variable. ii.Age – must be read in as an int. iii.Profession or expected...
Using Java (Swing) language(please hard code)... Create a program that has a textfield for the user...
Using Java (Swing) language(please hard code)... Create a program that has a textfield for the user to type in a set of input. Below that textfield have the following controls to show string manipulations: (1) A button that will change the entire textfield’s current text to uppercase. (2) A button with its own textfield for a search value, that will tell the position the search value appears in the textfield above. (3) A button that reports the current number of...
please, solve this problem.: implementing with java language create a bank account management system according to...
please, solve this problem.: implementing with java language create a bank account management system according to the following specifications: BankAccount Abstract Class contains the following constructors and methods: BankAccount(name, balance): a constructor that creates a new account with a name and starting balance. getBalance(): a method that returns the balance of a specific account. abstract deposit(amount): abstract method to be implemented in both Checking and SavingAccount classes. abstract withdraw(amount): abstract method to be implemented in both Checking and SavingAccount classes....
JAVA LANGUAGE Required Tasks: In Eclipse, create a Java Project as CS-176L-Assign5. Create 3 Java Classes...
JAVA LANGUAGE Required Tasks: In Eclipse, create a Java Project as CS-176L-Assign5. Create 3 Java Classes each in its own file Student.java, StudentList.java, and Assign5Test.java. Copy your code from Assignment 4 into the Student.java and StudentList.java Classes. Assign5Test.java should contain the main method. Modify StudentList.java to use an ArrayList instead of an array. You can find the basics of ArrayList here: https://www.w3schools.com/java/java_arraylist.asp In StudentList.java, create two new public methods: The addStudent method should have one parameter of type Student and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT