|
| 1 | +import java.util.Arrays; |
| 2 | +import java.util.Random; |
| 3 | +import java.util.Scanner; |
| 4 | + |
| 5 | +public class WordQuestDemo { |
| 6 | + |
| 7 | + public static void main(String[] args) { |
| 8 | + |
| 9 | + // Define the secret word and maximum allowed attempts |
| 10 | + String secretWord = getRandomWord(); |
| 11 | + int maxAttempts = 10; |
| 12 | + |
| 13 | + // Create a game board with underscores to represent unrevealed letters |
| 14 | + char[] gameBoard = new char[secretWord.length()]; |
| 15 | + |
| 16 | + // Initialize game board with underscores to represent unrevealed letters |
| 17 | + Arrays.fill(gameBoard, '_'); |
| 18 | + |
| 19 | + // Scanner to read player inpu |
| 20 | + Scanner scanner = new Scanner(System.in); |
| 21 | + |
| 22 | + System.out.println("Welcome to Word Quest!"); |
| 23 | + |
| 24 | + // Flag to check if the word has been fully revealed |
| 25 | + boolean wordNotRevealed = true; |
| 26 | + |
| 27 | + // Main game loop: runs while there are attempts left and word is not fully revealed |
| 28 | + while (maxAttempts > 0 && wordNotRevealed) { |
| 29 | + |
| 30 | + System.out.print("Current word: "); |
| 31 | + System.out.println(gameBoard); |
| 32 | + |
| 33 | + System.out.println(); |
| 34 | + System.out.print("Guess a letter: "); |
| 35 | + |
| 36 | + // Read the user's input, take the first character and convert it to uppercase |
| 37 | + String userInput = scanner.next().toUpperCase(); |
| 38 | + char guess = userInput.charAt(0); |
| 39 | + |
| 40 | + // Track if the guess is correct |
| 41 | + boolean isGuessCorrect = false; |
| 42 | + |
| 43 | + // Loop through each letter in the secret word and check if it matches the guess |
| 44 | + for (int i=0; i < secretWord.length(); i++) { |
| 45 | + |
| 46 | + if (secretWord.charAt(i) == guess) { |
| 47 | + // Reveal the correctly guessed letter on the game board |
| 48 | + gameBoard[i] = guess; |
| 49 | + isGuessCorrect = true; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + // Update game status based on the guess |
| 54 | + if (isGuessCorrect) { |
| 55 | + System.out.println("Good job! You found a match!"); |
| 56 | + |
| 57 | + // Check if there are still unrevealed letters |
| 58 | + wordNotRevealed = containsUnderscore(gameBoard); |
| 59 | + } else { |
| 60 | + System.out.println("Incorrect!"); |
| 61 | + |
| 62 | + // Decrement attempts for an incorrect guess |
| 63 | + maxAttempts--; |
| 64 | + } |
| 65 | + |
| 66 | + // Display remaining attempts after each guess |
| 67 | + System.out.println("Attempts remaining: " + maxAttempts); |
| 68 | + System.out.println(); |
| 69 | + } |
| 70 | + |
| 71 | + // End of game message based on whether the word was revealed |
| 72 | + if (wordNotRevealed) { |
| 73 | + System.out.println("You've run out of attempts. The secret word was: " + secretWord); |
| 74 | + } else { |
| 75 | + System.out.println("Success!!! You've revealed the secret word: " + secretWord); |
| 76 | + } |
| 77 | + |
| 78 | + scanner.close(); |
| 79 | + } |
| 80 | + |
| 81 | + private static boolean containsUnderscore(char[] gameBoard) { |
| 82 | + for (char temp : gameBoard) { |
| 83 | + if (temp == '_') { |
| 84 | + return true; |
| 85 | + } |
| 86 | + } |
| 87 | + return false; |
| 88 | + } |
| 89 | + |
| 90 | + private static String getRandomWord() { |
| 91 | + String[] words = {"Java", "Airplane", "Friend"}; |
| 92 | + |
| 93 | + Random random = new Random(); |
| 94 | + int index = random.nextInt(words.length); |
| 95 | + |
| 96 | + String theWord = words[index]; |
| 97 | + |
| 98 | + return theWord.toUpperCase(); |
| 99 | + } |
| 100 | +} |
0 commit comments