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