-
Notifications
You must be signed in to change notification settings - Fork 9
/
Gui2048.java
executable file
·660 lines (472 loc) · 21.6 KB
/
Gui2048.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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
/*
* Name: Daniel Huang
* PID: A12440549
* Login:cs8bwacy
* Date:3/3/16
* File: Gui2048.java
* Source of help: PIAZZA
* This program would perform the same as the famous game 2048.
*
/** Gui2048.java */
/** PSA8 Release */
import javafx.application.*;
import javafx.scene.control.*;
import javafx.scene.*;
import javafx.scene.paint.*;
import javafx.scene.shape.*;
import javafx.scene.layout.*;
import javafx.stage.*;
import javafx.event.*;
import javafx.scene.input.*;
import javafx.scene.text.*;
import javafx.geometry.*;
import java.util.*;
import java.io.*;
/*
* Name: Gui2048 Class
* Purpose: Starts and perform all the operations in the 2048 game
* Parameter:none
* Return: void
* */
public class Gui2048 extends Application
{
private String outputBoard; // The filename for where to save the Board
private Board board; // The 2048 Game Board
private static final int TILE_WIDTH = 106;
private static final int TEXT_SIZE_LOW = 55; // Low value tiles (2,4,8,etc)
private static final int TEXT_SIZE_MID = 45; // Mid value tiles
//(128, 256, 512)
private static final int TEXT_SIZE_HIGH = 35; // High value tiles
//(1024, 2048, Higher)
// Fill colors for each of the Tile values
private static final Color COLOR_EMPTY = Color.rgb(204, 192, 179);
private static final Color COLOR_2 = Color.rgb(238, 228, 218);
private static final Color COLOR_4 = Color.rgb(237, 224, 200);
private static final Color COLOR_8 = Color.rgb(242, 177, 121);
private static final Color COLOR_16 = Color.rgb(245, 149, 99);
private static final Color COLOR_32 = Color.rgb(246, 124, 95);
private static final Color COLOR_64 = Color.rgb(246, 94, 59);
private static final Color COLOR_128 = Color.rgb(237, 207, 114);
private static final Color COLOR_256 = Color.rgb(237, 204, 97);
private static final Color COLOR_512 = Color.rgb(237, 200, 80);
private static final Color COLOR_1024 = Color.rgb(237, 197, 63);
private static final Color COLOR_2048 = Color.rgb(237, 194, 46);
private static final Color COLOR_OTHER = Color.BLACK;
private static final Color COLOR_GAME_OVER = Color.rgb(238, 228, 218, 0.73);
private static final Color COLOR_VALUE_LIGHT = Color.rgb(249, 246, 242);
// For tiles >= 8
private static final Color COLOR_VALUE_DARK = Color.rgb(119, 110, 101);
// For tiles < 8
/** Add your own Instance Variables here */
//stackpane as instance variable to achieve the gameover check function
private GridPane pane;
private Scene scene;
private StackPane stack;
//2-D array for tiles
private Rectangle[][] tiles;
//2-D array for text
private Text[][] texts;
/*
* Name: createTiles
* Purpose:helper method to intialize the tiles array
* Parameter:int xTiles, int yTiles
* Return: Rectangle[][]
* */
public Rectangle[][] createTiles(int xTiles, int yTiles) {
return new Rectangle[xTiles][yTiles];
}
/*
* Name: createTexts
* Purpose:helper method to intialize the texts array
* Parameter:int xTexts, int yTexts
* Return: Text[][]
* */
public Text[][] createTexts(int xTexts, int yTexts) {
return new Text[xTexts][yTexts];
}
/*
* Name: start
* Purpose:the vital method to start the game and set up the GUI
* Parameter:Stage primaryStage
* Return: void
* */
@Override
public void start(Stage primaryStage)
{
// Process Arguments and Initialize the Game Board
processArgs(getParameters().getRaw().toArray(new String[0]));
// Create the pane that will hold all of the visual objects
pane = new GridPane();
pane.setAlignment(Pos.CENTER);
pane.setPadding(new Insets(11.5, 12.5, 13.5, 14.5));
pane.setStyle("-fx-background-color: rgb(187, 173, 160)");
// Set the spacing between the Tiles
pane.setHgap(15);
pane.setVgap(15);
/** Add your Code for the GUI Here */
this.tiles=createTiles(board.GRID_SIZE,board.GRID_SIZE+1);
this.texts=createTexts(board.GRID_SIZE,board.GRID_SIZE+1);
//add the header "2048" to the pane
Text header = new Text();
header.setText("2048");
header.setFont(Font.font("Times New Roman", FontWeight.BOLD,
FontPosture.ITALIC, 50));
header.setFill(Color.BLACK);
//add the score
Text score=new Text();
score.setText("Score: "+board.getScore());
texts[board.GRID_SIZE-1][0]=score;
texts[board.GRID_SIZE-1][0].setFont(Font.font("Time New Roman",
FontWeight.BOLD, 20));
texts[board.GRID_SIZE-1][0].setFill(Color.BLACK);
//add the 2048 header and score to the gridpane
pane.add(header,0,0);
GridPane.setHalignment(header, HPos.LEFT);
pane.add(texts[board.GRID_SIZE-1][0],board.GRID_SIZE-1,0);
//add the tiles(rectangles) to the gridpane
for(int x=0; x<board.GRID_SIZE; x++ )
{
for(int y=1;y<board.GRID_SIZE+1;y++) {
Rectangle aTile = new Rectangle();
aTile.setWidth(100);
aTile.setHeight(100);
//add the newly created rectangle to the 2-D array to
//store its references for future use
tiles[x][y]=aTile;
//set the color of the tiles
// if(board.getGrid()[y-1][x]==0){
tiles[x][y].setFill(COLOR_EMPTY);
if(board.getGrid()[y-1][x]==2){
tiles[x][y].setFill(COLOR_2); }
if(board.getGrid()[y-1][x]==4){
tiles[x][y].setFill(COLOR_4); }
if(board.getGrid()[y-1][x]==8){
tiles[x][y].setFill(COLOR_8); }
if(board.getGrid()[y-1][x]==16){
tiles[x][y].setFill(COLOR_16); }
if(board.getGrid()[y-1][x]==32){
tiles[x][y].setFill(COLOR_32); }
if(board.getGrid()[y-1][x]==64){
tiles[x][y].setFill(COLOR_64); }
if(board.getGrid()[y-1][x]==128){
tiles[x][y].setFill(COLOR_128); }
if(board.getGrid()[y-1][x]==256){
tiles[x][y].setFill(COLOR_256); }
if(board.getGrid()[y-1][x]==512){
tiles[x][y].setFill(COLOR_512); }
if(board.getGrid()[y-1][x]==1024){
tiles[x][y].setFill(COLOR_1024); }
if(board.getGrid()[y-1][x]==2048){
tiles[x][y].setFill(COLOR_2048); }
if (board.getGrid()[y-1][x]>2048){
//for the tile values other than the above numbers,set it
//to black
tiles[x][y].setFill(COLOR_OTHER);}
//add the tile to the gridpane
pane.add(tiles[x][y],x,y);
//bind the grid with the texts array
//note the row/coloumn order in grid is different from
//the one in texts
Text tileValue=new Text();
tileValue.setText(Integer.toString((board.getGrid())[y-1][x]));
//store the tile value references in the 2d texts array
texts[x][y]=tileValue;
//set the font size based on the value of tile
if(board.getGrid()[y-1][x]<128) {
texts[x][y].setFont((Font.font("Time New Roman",
FontWeight.BOLD, TEXT_SIZE_LOW ))); }
if(board.getGrid()[y-1][x]<1024){
texts[x][y].setFont((Font.font("Time New Roman",
FontWeight.BOLD,TEXT_SIZE_MID ))); }
else {
texts[x][y].setFont((Font.font("Time New Roman",
FontWeight.BOLD, TEXT_SIZE_HIGH )));}
//set the color of tile text based on its value
if(board.getGrid()[y-1][x]<8){
texts[x][y].setFill(COLOR_VALUE_DARK);}
else {
texts[x][y].setFill(COLOR_VALUE_LIGHT);}
//show the text(tile value) only for the non-empty tiles
if(board.getGrid()[y-1][x]!=0) {
pane.add(texts[x][y],x,y);}
GridPane.setHalignment(texts[x][y], HPos.CENTER);
} }
this.stack=new StackPane();
stack.getChildren().addAll(pane);
this.scene = new Scene(stack);
//register the keyevent(listener)
scene.setOnKeyPressed(new myKeyHandler());
primaryStage.setTitle("Gui2048");
primaryStage.setScene(scene);
primaryStage.show();
}
/** Add your own Instance Methods Here */
/*
* Name: updateGUI
* Purpose: the helper method to update the GUI once a move is
* performed
* Parameter: none
* Return: void
* */
private void updateGUI() {
//update the score
texts[board.GRID_SIZE-1][0].setText("Score: "+board.getScore());
texts[board.GRID_SIZE-1][0].setFont(Font.font("Time New Roman",
FontWeight.BOLD, 20));
texts[board.GRID_SIZE-1][0].setFill(Color.BLACK);
for(int x=0;x<board.GRID_SIZE;x++){
for(int y=1;y<board.GRID_SIZE+1;y++) {
Rectangle aTile = new Rectangle();
aTile.setWidth(100);
aTile.setHeight(100);
//add the newly created rectangle to the 2-D array to
//store its references for future use
tiles[x][y]=aTile;
//set the color of the tiles
// if(board.getGrid()[y-1][x]==0){
tiles[x][y].setFill(COLOR_EMPTY);
if(board.getGrid()[y-1][x]==2){
tiles[x][y].setFill(COLOR_2); }
if(board.getGrid()[y-1][x]==4){
tiles[x][y].setFill(COLOR_4); }
if(board.getGrid()[y-1][x]==8){
tiles[x][y].setFill(COLOR_8); }
if(board.getGrid()[y-1][x]==16){
tiles[x][y].setFill(COLOR_16); }
if(board.getGrid()[y-1][x]==32){
tiles[x][y].setFill(COLOR_32); }
if(board.getGrid()[y-1][x]==64){
tiles[x][y].setFill(COLOR_64); }
if(board.getGrid()[y-1][x]==128){
tiles[x][y].setFill(COLOR_128); }
if(board.getGrid()[y-1][x]==256){
tiles[x][y].setFill(COLOR_256); }
if(board.getGrid()[y-1][x]==512){
tiles[x][y].setFill(COLOR_512); }
if(board.getGrid()[y-1][x]==1024){
tiles[x][y].setFill(COLOR_1024); }
if(board.getGrid()[y-1][x]==2048){
tiles[x][y].setFill(COLOR_2048); }
if (board.getGrid()[y-1][x]>2048){
//for the tile values other than the above numbers,set it
//to black
tiles[x][y].setFill(COLOR_OTHER);}
pane.add(tiles[x][y],x,y);
Text tileValue=new Text();
tileValue.setText
(Integer.toString((board.getGrid())[y-1][x]));
//store the tile value references in the 2d texts array
texts[x][y]=tileValue;
//set the font size based on the value of tile
if(board.getGrid()[y-1][x]<128) {
texts[x][y].setFont((Font.font("Time New Roman",
FontWeight.BOLD,TEXT_SIZE_LOW ))); }
if(board.getGrid()[y-1][x]<1024){
texts[x][y].setFont((Font.font("Time New Roman",
FontWeight.BOLD, TEXT_SIZE_MID ))); }
else {
texts[x][y].setFont((Font.font("Time New Roman",
FontWeight.BOLD,TEXT_SIZE_HIGH )));}
//set the color of tile text based on its value
if(board.getGrid()[y-1][x]<8){
texts[x][y].setFill(COLOR_VALUE_DARK);}
else {
texts[x][y].setFill(COLOR_VALUE_LIGHT);}
//show the text(tile value) only for the non-empty tiles
if(board.getGrid()[y-1][x]!=0) {
pane.add(texts[x][y],x,y);}
GridPane.setHalignment(texts[x][y], HPos.CENTER);
}
}
}
/*
* Name: gameOverCheck
* Purpose: the helper method to check if the game is over after
* a valid move; if so, it will stop the game and print
* the "Game Over!" message
* Parameter: none
* Return: void
* */
private void gameOverCheck(){
if(board.isGameOver())
{
Rectangle endCover=new Rectangle(scene.getWidth(),
scene.getHeight());
endCover.setFill(COLOR_GAME_OVER);
stack.getChildren().add(endCover);
Text endLine=new Text();
endLine.setText("Game Over!");
endLine.setFont((Font.font("Time New Roman",
FontWeight.BOLD,60 )));
endLine.setFill(COLOR_OTHER);
stack.getChildren().add(endLine);
}
}
/*
* Name: myKeyHandker class
* Purpose: this inner class implements the eventhandler interface
* and link the key input to the game
* Parameter: none
* Return: void
* */
private class myKeyHandler implements EventHandler<KeyEvent>
{
/*
* Name: handle
* Purpose: it detects the key input and perform the
* corresponding move
* Parameter: KeyEvent input
* Return: void
* */
@Override
public void handle(KeyEvent input)
{
//detect the input key only if the game is NOT over
if(!board.isGameOver()) {
switch(input.getCode()) {
case UP: if(board.canMove(Direction.UP)) {
System.out.println("Moving <UP>");
board.move(Direction.UP);
board.addRandomTile();
//update the GUI
updateGUI();
gameOverCheck();
}
break;
case DOWN: if(board.canMove(Direction.DOWN)) {
System.out.println("Moving <DOWN>");
board.move(Direction.DOWN);
board.addRandomTile();
//update the GUI
updateGUI();
gameOverCheck();
}
break;
case LEFT: if(board.canMove(Direction.LEFT)) {
System.out.println("Moving <LEFT>");
board.move(Direction.LEFT);
board.addRandomTile();
//update the GUI
updateGUI();
gameOverCheck();
}
break;
case RIGHT: if(board.canMove(Direction.RIGHT)) {
System.out.println("Moving <RIGHT>");
board.move(Direction.RIGHT);
board.addRandomTile();
//update the GUI
updateGUI();
gameOverCheck();
}
break;
case S:
//the following block takes care of
//the IOException
try {
board.saveBoard(outputBoard);
} catch (IOException e) {
System.out.println
("saveBoard threw an Exception");
}
//save the board to the output file
System.out.println
("Saving board to <2048.board> ");
break;
default: break;
}
}
}
}
/** DO NOT EDIT BELOW */
// The method used to process the command line arguments
private void processArgs(String[] args)
{
String inputBoard = null; // The filename for where to load
// the Board
int boardSize = 0; // The Size of the Board
// Arguments must come in pairs
if((args.length % 2) != 0)
{
printUsage();
System.exit(-1);
}
// Process all the arguments
for(int i = 0; i < args.length; i += 2)
{
if(args[i].equals("-i"))
{ // We are processing the argument that specifies
// the input file to be used to set the board
inputBoard = args[i + 1];
}
else if(args[i].equals("-o"))
{ // We are processing the argument that specifies
// the output file to be used to save the board
outputBoard = args[i + 1];
}
else if(args[i].equals("-s"))
{ // We are processing the argument that specifies
// the size of the Board
boardSize = Integer.parseInt(args[i + 1]);
}
else
{ // Incorrect Argument
printUsage();
System.exit(-1);
}
}
// Set the default output file if none specified
if(outputBoard == null)
outputBoard = "2048.board";
// Set the default Board size if none specified or less than 2
if(boardSize < 2)
boardSize = 4;
// Initialize the Game Board
try{
if(inputBoard != null)
board = new Board(inputBoard, new Random());
else
board = new Board(boardSize, new Random());
}
catch (Exception e)
{
System.out.println(e.getClass().getName() +
" was thrown while creating a " +
"Board from file " + inputBoard);
System.out.println("Either your Board(String, Random) " +
"Constructor is broken or the file isn't " +
"formated correctly");
System.exit(-1);
}
}
// Print the Usage Message
private static void printUsage()
{
System.out.println("Gui2048");
System.out.println("Usage: Gui2048 [-i|o file ...]");
System.out.println();
System.out.println
(" Command line arguments come in pairs of the "+
"form: <command> <argument>");
System.out.println();
System.out.println
(" -i [file] -> Specifies a 2048 board that " +
"should be loaded");
System.out.println();
System.out.println
(" -o [file] -> Specifies a file that should be " +
"used to save the 2048 board");
System.out.println
(" If none specified then the " +
"default \"2048.board\" file will be used");
System.out.println
(" -s [size] -> Specifies the size of the 2048" +
"board if an input file hasn't been");
System.out.println
(" specified. If both -s and -i" +
"are used, then the size of the board");
System.out.println
(" will be determined by the input" +
" file. The default size is 4.");
}
}