|
| 1 | +package io.github.project516.NumberGuessingGame; |
| 2 | + |
| 3 | +import org.teavm.jso.browser.Window; |
| 4 | +import org.teavm.jso.dom.html.*; |
| 5 | + |
| 6 | +/** |
| 7 | + * Web-based GUI for the Number Guessing Game using TeaVM. This class provides a browser-native |
| 8 | + * interface that reuses the core game logic from the existing classes. Created specifically for |
| 9 | + * TeaVM compilation to JavaScript for GitHub Pages deployment. |
| 10 | + */ |
| 11 | +public class WebGUI { |
| 12 | + // Game state - reusing existing game logic classes |
| 13 | + private int targetNumber; |
| 14 | + private int numberOfGuesses; |
| 15 | + private RandomNumber randomGenerator; |
| 16 | + private CheckGuess guessChecker; |
| 17 | + |
| 18 | + // DOM elements |
| 19 | + private HTMLDocument document; |
| 20 | + private HTMLInputElement guessInput; |
| 21 | + private HTMLButtonElement submitButton; |
| 22 | + private HTMLButtonElement newGameButton; |
| 23 | + private HTMLElement feedbackElement; |
| 24 | + private HTMLElement guessCountElement; |
| 25 | + |
| 26 | + /** |
| 27 | + * Main entry point for the web application. TeaVM will call this method when the page loads. |
| 28 | + * |
| 29 | + * @param args command line arguments (not used in web context) |
| 30 | + */ |
| 31 | + public static void main(String[] args) { |
| 32 | + new WebGUI().initialize(); |
| 33 | + } |
| 34 | + |
| 35 | + /** Initializes the web GUI and starts a new game. */ |
| 36 | + public void initialize() { |
| 37 | + // Initialize game objects - reusing existing classes |
| 38 | + randomGenerator = new RandomNumber(); |
| 39 | + guessChecker = new CheckGuess(); |
| 40 | + |
| 41 | + // Get the HTML document |
| 42 | + document = Window.current().getDocument(); |
| 43 | + |
| 44 | + // Create the game UI |
| 45 | + createGameUI(); |
| 46 | + |
| 47 | + // Start a new game |
| 48 | + startNewGame(); |
| 49 | + } |
| 50 | + |
| 51 | + /** Creates the game UI elements in the HTML document. */ |
| 52 | + private void createGameUI() { |
| 53 | + // Get or create the game container |
| 54 | + HTMLElement gameContainer = (HTMLElement) document.getElementById("game-container"); |
| 55 | + if (gameContainer == null) { |
| 56 | + gameContainer = (HTMLElement) document.createElement("div"); |
| 57 | + gameContainer.setId("game-container"); |
| 58 | + document.getBody().appendChild(gameContainer); |
| 59 | + } |
| 60 | + |
| 61 | + // Clear existing content |
| 62 | + gameContainer.setInnerHTML(""); |
| 63 | + |
| 64 | + // Create title |
| 65 | + HTMLElement title = (HTMLElement) document.createElement("h1"); |
| 66 | + title.setInnerHTML("Number Guessing Game"); |
| 67 | + gameContainer.appendChild(title); |
| 68 | + |
| 69 | + // Create instructions |
| 70 | + HTMLElement instructions = (HTMLElement) document.createElement("p"); |
| 71 | + instructions.setInnerHTML("Guess a number between 1 and 100!"); |
| 72 | + gameContainer.appendChild(instructions); |
| 73 | + |
| 74 | + // Create guess count display |
| 75 | + guessCountElement = (HTMLElement) document.createElement("p"); |
| 76 | + guessCountElement.setId("guess-count"); |
| 77 | + guessCountElement.setInnerHTML("Number of guesses: 0"); |
| 78 | + gameContainer.appendChild(guessCountElement); |
| 79 | + |
| 80 | + // Create input field |
| 81 | + guessInput = (HTMLInputElement) document.createElement("input"); |
| 82 | + guessInput.setType("number"); |
| 83 | + guessInput.setAttribute("min", "1"); |
| 84 | + guessInput.setAttribute("max", "100"); |
| 85 | + guessInput.setAttribute("placeholder", "Enter your guess"); |
| 86 | + guessInput.setId("guess-input"); |
| 87 | + gameContainer.appendChild(guessInput); |
| 88 | + |
| 89 | + // Create submit button |
| 90 | + submitButton = (HTMLButtonElement) document.createElement("button"); |
| 91 | + submitButton.setInnerHTML("Submit Guess"); |
| 92 | + submitButton.setId("submit-button"); |
| 93 | + submitButton.addEventListener("click", evt -> submitGuess()); |
| 94 | + gameContainer.appendChild(submitButton); |
| 95 | + |
| 96 | + // Allow Enter key to submit |
| 97 | + guessInput.addEventListener( |
| 98 | + "keypress", |
| 99 | + evt -> { |
| 100 | + if ("Enter".equals(((HTMLKeyboardEvent) evt).getKey())) { |
| 101 | + submitGuess(); |
| 102 | + } |
| 103 | + }); |
| 104 | + |
| 105 | + // Create feedback area |
| 106 | + feedbackElement = (HTMLElement) document.createElement("p"); |
| 107 | + feedbackElement.setId("feedback"); |
| 108 | + feedbackElement.setInnerHTML(" "); |
| 109 | + gameContainer.appendChild(feedbackElement); |
| 110 | + |
| 111 | + // Create new game button (initially hidden) |
| 112 | + newGameButton = (HTMLButtonElement) document.createElement("button"); |
| 113 | + newGameButton.setInnerHTML("New Game"); |
| 114 | + newGameButton.setId("new-game-button"); |
| 115 | + newGameButton.getStyle().setProperty("display", "none"); |
| 116 | + newGameButton.addEventListener("click", evt -> startNewGame()); |
| 117 | + gameContainer.appendChild(newGameButton); |
| 118 | + } |
| 119 | + |
| 120 | + /** Starts a new game by resetting the game state. */ |
| 121 | + private void startNewGame() { |
| 122 | + // Generate new target number using existing RandomNumber class |
| 123 | + targetNumber = randomGenerator.number(100); |
| 124 | + numberOfGuesses = 0; |
| 125 | + |
| 126 | + // Reset UI |
| 127 | + guessInput.setValue(""); |
| 128 | + guessInput.setDisabled(false); |
| 129 | + submitButton.setDisabled(false); |
| 130 | + newGameButton.getStyle().setProperty("display", "none"); |
| 131 | + feedbackElement.setInnerHTML(" "); |
| 132 | + feedbackElement.setClassName(""); |
| 133 | + updateGuessCount(); |
| 134 | + |
| 135 | + // Focus on input |
| 136 | + guessInput.focus(); |
| 137 | + } |
| 138 | + |
| 139 | + /** Processes the user's guess and updates the UI with feedback. */ |
| 140 | + private void submitGuess() { |
| 141 | + String input = guessInput.getValue().trim(); |
| 142 | + |
| 143 | + if (input.isEmpty()) { |
| 144 | + showFeedback("Please enter a number!", "error"); |
| 145 | + return; |
| 146 | + } |
| 147 | + |
| 148 | + int guess; |
| 149 | + try { |
| 150 | + guess = Integer.parseInt(input); |
| 151 | + } catch (NumberFormatException e) { |
| 152 | + showFeedback("Invalid input! Please enter a valid number.", "error"); |
| 153 | + guessInput.setValue(""); |
| 154 | + return; |
| 155 | + } |
| 156 | + |
| 157 | + // Validate guess using existing CheckGuess class |
| 158 | + try { |
| 159 | + guessChecker.check(guess); |
| 160 | + } catch (IllegalArgumentException e) { |
| 161 | + showFeedback(e.getMessage(), "error"); |
| 162 | + guessInput.setValue(""); |
| 163 | + return; |
| 164 | + } |
| 165 | + |
| 166 | + numberOfGuesses++; |
| 167 | + updateGuessCount(); |
| 168 | + |
| 169 | + // Check the guess |
| 170 | + if (guess > targetNumber) { |
| 171 | + showFeedback("Too high! Try a lower number.", "high"); |
| 172 | + } else if (guess < targetNumber) { |
| 173 | + showFeedback("Too low! Try a higher number.", "low"); |
| 174 | + } else { |
| 175 | + // Correct guess! |
| 176 | + showFeedback( |
| 177 | + String.format( |
| 178 | + "🎉 Congratulations! You guessed it in %d %s!", |
| 179 | + numberOfGuesses, numberOfGuesses == 1 ? "guess" : "guesses"), |
| 180 | + "success"); |
| 181 | + guessInput.setDisabled(true); |
| 182 | + submitButton.setDisabled(true); |
| 183 | + newGameButton.getStyle().setProperty("display", "block"); |
| 184 | + newGameButton.focus(); |
| 185 | + } |
| 186 | + |
| 187 | + guessInput.setValue(""); |
| 188 | + guessInput.focus(); |
| 189 | + } |
| 190 | + |
| 191 | + /** |
| 192 | + * Updates the feedback message and applies the appropriate CSS class. |
| 193 | + * |
| 194 | + * @param message the feedback message to display |
| 195 | + * @param cssClass the CSS class to apply (error, high, low, success) |
| 196 | + */ |
| 197 | + private void showFeedback(String message, String cssClass) { |
| 198 | + feedbackElement.setInnerHTML(message); |
| 199 | + feedbackElement.setClassName("feedback-" + cssClass); |
| 200 | + } |
| 201 | + |
| 202 | + /** Updates the guess count display. */ |
| 203 | + private void updateGuessCount() { |
| 204 | + guessCountElement.setInnerHTML("Number of guesses: " + numberOfGuesses); |
| 205 | + } |
| 206 | + |
| 207 | + /** Keyboard event interface for handling key presses. */ |
| 208 | + private interface HTMLKeyboardEvent extends HTMLEvent { |
| 209 | + String getKey(); |
| 210 | + } |
| 211 | + |
| 212 | + /** Base HTML event interface. */ |
| 213 | + private interface HTMLEvent extends org.teavm.jso.JSObject {} |
| 214 | +} |
0 commit comments