-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguessTheNumber_gui.java
500 lines (401 loc) · 15.5 KB
/
guessTheNumber_gui.java
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.RadioButton;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.util.Random;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
import javafx.animation.Interpolator;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCodeCombination;
import javafx.scene.layout.Border;
import javafx.scene.layout.BorderStroke;
import javafx.scene.layout.BorderStrokeStyle;
import javafx.scene.layout.BorderWidths;
import javafx.scene.layout.CornerRadii;
import javafx.scene.paint.Color;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;
import javafx.util.Duration;
public class GuessTheNumberr extends Application
{
private int target,
numGuesses,
pts = 0,
atp = 0,
lower = 0,
upper = 0,
i = 0,
root_width = 500,
root_height = 525;
private String cssFile = "./css/-fx-mid-style.css";
private Label points = new Label("Points : " + pts),
attempts = new Label("Attempts Left : " + atp),
label = new Label("Enter your guess"), messageLabel = new Label();
private TextField textField = new TextField();
private Button button = new Button("Guess"),
restart = new Button("Restart"),
home = new Button("Back to Home");
private RadioButton radioButton = new RadioButton("");
private Stage stage;
private Media s0 = new Media(new File("sound/start.mp3").toURI().toString()),
s1 = new Media(new File("sound/click.mp3").toURI().toString()),
s2 = new Media(new File("sound/click4.mp3").toURI().toString()),
s3 = new Media(new File("sound/click3.mp3").toURI().toString()),
s4 = new Media(new File("sound/loss.mp3").toURI().toString());
private MediaPlayer mp;
/**
* The Starting window of the game
* @param primaryStage Main Stage of the Game
*/
@Override
public void start(Stage primaryStage)
{
bootxpsiace(0, 500);
stage = primaryStage;
points = new Label("Points : " + pts);
label = new Label("CHOOSE A LEVEL"); // Create the GUI components
Button lv1 = new Button("Level 1 (0 to 10)"),
lv2 = new Button("Level 2 (0 to 500)"),
lv3 = new Button("Level 3 (-500 to 500)"),
lv4 = new Button("Custom Level");
lv1.setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent event)
{
bootxpsiace(1, 250);
startlevel(1);
}
});
lv2.setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent event)
{
bootxpsiace(1, 250);
startlevel(2);
}
});
lv3.setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent event)
{
bootxpsiace(1, 250);
startlevel(3);
}
});
lv4.setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent event)
{
bootxpsiace(1, 250);
l4rangechoice();
}
});
// Set up the layout
VBox root = new VBox(root_width / 15);
root.setPadding(new Insets(root_height / 20));
root.setAlignment(Pos.CENTER);
root.getChildren().addAll(points, label, lv1, lv2, lv3, lv4);
setborder(root);
// Set up the scene and the stage
Scene scene = new Scene(root, root_width, root_height);
scene.getStylesheets().add(cssFile);
primaryStage.setTitle("Guess the Number Game");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* A Function to add deserving points to the user
* @param v An integer that contains the Number of guesses/attempts left after the answer is Guessed Correctly, to give points accordingly
*/
private void adpts(int v)
{
pts += Math.ceil(v / Math.sqrt(v));
/* ptshw.setText("Points : "+pts); */
}
/* A Function to Update the "Points" label with the newly added points */
private void showpts()
{
points.setText("Points : " + atp);
}
/**
* A Function to Create a Basic Vbox root at once
* @param root the Vertical Box Layout of the level the user is playing
*/
private void CreateBasicGUI(VBox root)
{
/* Create the GUI components */
points.setText("Points : " + pts);
attempts.setText("Attempts Left : " + atp);
label.setText("Guess a number between " + lower + " & " + upper);
messageLabel.setText("");
button.setText("Guess");
restart.setText("Restart");
home.setText("Back to Home");
/* Set up the layout */
root.setPadding(new Insets(root_height / 20));
root.setAlignment(Pos.CENTER);
root.getChildren().addAll(points, attempts, label, textField, button, messageLabel);
}
/***
* A Function to Calculaate for the guessed number and do the task accordingly
* @param guess textfiels variable to get inputed string
* @param root VBox element to set GUI within conditions
* @param i High vaue to add subtracted points from
*/
private void CalcBrainET(int guess, VBox root, int i)
{
textField.setText("");
textField.requestFocus();
bootxpsiace(3, 150);
numGuesses++;
if (guess < target)
{
messageLabel.setText("Enter a number Greater than " + guess);
}
else if (guess > target)
{
messageLabel.setText("Enter a number Less than " + guess);
}
else
{
/* creating winning sound */
bootxpsiace(2, 150);
/* adding deserving points */
adpts(i + 1 - numGuesses);
/* Updating Points */
showpts();
/* Updating the message label with congratulations */
messageLabel.setText("Correct ! You Won !\nYou guessed " + target + " in " + numGuesses + " attempts.");
/* Removing all the elements from the screen */
root.getChildren().clear();
/* Adding the elements again to the screen */
root.getChildren().addAll(points, messageLabel, restart, home);
}
if (--atp == 0 && guess != target)
{
/* creating loosing sound */
bootxpsiace(-1, 150);
/* Updating the message label with loss */
messageLabel.setText("You Lost !\nYou couldn't guess " + target + " in " + numGuesses + " attempts.");
/* Removing all the elements from the screen */
root.getChildren().clear();
/* Adding the elements again to the screen */
root.getChildren().addAll(points, messageLabel, restart, home);
/* Ending the statements here */
return;
}
/* updating the number of guesses left/available for further use */
attempts.setText("Attempts Left : " + atp);
}
/**
* A Function for the selected level to start in
* @param lvl The number of level of the User's choice
*/
private void startlevel(int lvl)
{
switch (lvl) {
case 1:
lower = 0;
upper = 10;
break;
case 2:
lower = 0;
upper = 500;
break;
case 3:
lower = -500;
upper = 500;
break;
case 4:
break;
default:
return;
}
/* Generate a random number between the lower to upper ranger values */
target = new Random().nextInt(upper - lower + 1) + lower;
/* Variable to save user's input */
numGuesses = 0;
/* available trial points(guesses) */
atp = (int) Math.ceil(0.2 * Math.sqrt(upper + Math.abs(lower)));
atp += Math.abs(atp - 3) < 8 ? Math.abs(atp - 3) : 0;
atp = (upper - lower <= 3) ? 1
: (upper - lower > 3 && upper - lower <= 7) ? 2
: (upper - lower > 550 && upper - lower < 1249) ? atp + 1
: (upper - lower > 1250) ? (int) (atp - 0.25 * atp) : atp;
// i = available trial points
i = atp;
/* Create the layout3 */
VBox root = new VBox(root_height / 15);
/* Do the other Necessary/Bsic Tasks including changing/resetting elements */
CreateBasicGUI(root);
button.setOnAction(new EventHandler<ActionEvent>()
{
@Override
/* Set up the event handler for the button */
public void handle(ActionEvent event)
{
/* Processing further according to input */
CalcBrainET(Integer.parseInt(textField.getText()), root, i);
}
});
restart.setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent event) {
bootxpsiace(1, 250);
startlevel(lvl);
}
});
/* Back to home */
home.setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent event) {
start(stage);
}
});
setborder(root);
/* Setting up the scene */
Scene scene = new Scene(root, root_width, root_height);
scene.getStylesheets().add(cssFile);
/* Setting up the stage */
stage.setTitle("Guess a Number btw " + lower + " & " + upper);
/* Setting scene on stage */
stage.setScene(scene);
/* Showing the stage */
stage.show();
}
/* A Function to Take input of the Range if the Custom level is Selected by the User */
private void l4rangechoice()
{
label.setText("Enter The Lower & Upper Range");
TextField low = new TextField(),
up = new TextField();
button.setText("Enter into Game");
low.setPromptText("Enter The Lower Range");
up.setPromptText("Enter The Upper Range");
/* Set up the event handler for the range submit button */
button.setOnAction(new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent event)
{
lower = Integer.parseInt(low.getText());
upper = Integer.parseInt(up.getText());
bootxpsiace(1, 250);
startlevel(4);
}
});
/* Create the layout */
VBox root = new VBox(root_height / 15);
/* Set up the layout */
root.setPadding(new Insets(root_height / 20));
root.setAlignment(Pos.CENTER);
root.getChildren().addAll(label, low, up, button);
//Adding a colourfull gradient border to the layout
setborder(root);
/* Setting up the scene */
Scene scene = new Scene(root, root_width, root_height);
scene.getStylesheets().add(cssFile);
/* Setting up the stage */
stage.setTitle("Range Input");
/* Setting scene on stage */
stage.setScene(scene);
/* Showing the stage */
stage.show();
}
/**
* A Function to create a coloured border to a layout
* @param root the vertical box layout provided to put borders on
*/
private void setborder(VBox root)
{
/* complimentry thing */
root.setAlignment(Pos.CENTER);
/* Selecting the Colours to put on the motion gradient */
Color[] colors = Stream.of("darkorange", "tomato", "deeppink", "blueviolet", "steelblue", "cornflowerblue",
"lightseagreen", "#6fba82", "chartreuse", "crimson").map(Color::web).toArray(Color[]::new);
List<Border> list = new ArrayList<>();
int mills[] = { -200 };
/* Setting the Keyframes */
KeyFrame keyFrames[] = Stream.iterate(0, i -> i + 1).limit(1000)
.map(i -> new LinearGradient(0, 0, 1, 1, true, CycleMethod.NO_CYCLE,
new Stop[] { new Stop(0, colors[i % colors.length]),
new Stop(1, colors[(i + 1) % colors.length]) }))
.map(lg -> new Border(
new BorderStroke(lg, BorderStrokeStyle.SOLID, new CornerRadii(5), new BorderWidths(3))))
.map(b -> new KeyFrame(Duration.millis(mills[0] += 200),
new KeyValue(root.borderProperty(), b, Interpolator.EASE_IN)))
.toArray(KeyFrame[]::new);
Timeline timeline = new Timeline(keyFrames);
timeline.setCycleCount(Timeline.INDEFINITE);
/* Playing the Motion Gradient Timeline */
timeline.play();
}
/**
* A Function to Select the Sound according to the situation or event
* @param x the number of sound from the files accordin the current stage to event happening
*/
private void mpc(int x)
{
switch (x) {
case -1:
mp = new MediaPlayer(s4);
break;
case 0:
mp = new MediaPlayer(s0);
break;
case 1:
mp = new MediaPlayer(s1);
break;
case 2:
mp = new MediaPlayer(s2);
break;
case 3:
mp = new MediaPlayer(s3);
break;
}
}
// Adding a sound to the overall application (int Sound Number, int Delay) , along with a delay for the program to get load.
private void bootxpsiace(int x, int ms)
{
try
{
Thread.sleep(ms);
mpc(x);
mp.play();
}
catch (InterruptedException ex)
{
Thread.currentThread().interrupt();
}
}
/* THE MAIN FUNCTION */
public static void main(String[] args)
{
launch(args);
}
}