In: Computer Science
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.
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.