-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUI.py
693 lines (630 loc) · 22.7 KB
/
UI.py
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
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
## 2048 game user interface (UI), using tkinter
from tkinter import *
from board import Board
from AI import AI
import string
import sys
class UI:
def __init__(self):
self.GameBoard = Board(4) # default board size is 4
def init(self, data):
# board size settings
data.size = self.GameBoard.size
data.margin = 25
data.titlePlace = 150
data.cellSize = (data.width - data.margin * 2) / data.size
data.board = self.GameBoard.board
data.finishAdding = False
data.newTileIndex = None, None # index of the new adding tile
# start page modes
data.startPage = True
data.playerMode = False
data.AImode = False
# size, level selection page modes
data.customizeSizeMode = False
data.selectingBoardSize = False
data.levelSelectionMode = False # only applicable for AI mode
# game page modes
data.inGame = False
# game state settings (Game Over, Game Paused)
data.reach2048 = False # reach 2048 does not necessarily means game over here
data.continuePlaying = False # continue the game after reaching 2048
data.cannotMove = False # do not have any valid move means game over
data.paused = False
# time settings
data.timerDelay = 1000
data.timeCounter = 0
# color settings
data.colors = {
0: "#D7CCC8",
2: "#FFFDE7",
4: "#FBC02D",
8: "#F9A825",
16: "#FFA000",
32: "#F57F17",
64: "#F44336",
128: "#FFEB3B",
256: "#FFD600",
512: "#FDD835",
1024: "#FFFF8D",
2048: "#FFFF00",
4096: "#616161",
8192: "#424242",
16384: "#212121",
32768: "#03A9F4",
65536: "#039BE5",
131072: "#0288D1",
}
# AI settings
data.AIstep = 0
data.AI = None
def resetGameBoard(self, data):
self.GameBoard = Board(data.size)
data.board = self.GameBoard.board
data.cellSize = (data.width - data.margin * 2) / data.size
data.timeCounter = 0
data.reach2048 = False
data.continuePlaying = False
data.cannotMove = False
def mousePressed(self, event, data):
# three buttons in the start page
if data.startPage:
# enter the player mode
if data.width // 4 <= event.x <= data.width * (
3 / 4
) and data.height // 2 <= event.y <= data.height * (3 / 5):
data.playerMode = True
data.customizeSizeMode = True
data.startPage = False
# enter the AI mode
elif data.width // 4 <= event.x <= data.width * (3 / 4) and data.height * (
3 / 5 + 1 / 30
) <= event.y <= data.height * (7 / 10 + 1 / 30):
data.AImode = True
data.customizeSizeMode = True
data.startPage = False
# exit button
elif data.width * (5 / 6) <= event.x <= data.width * (
29 / 30
) and data.height * (9 / 10) <= event.y <= data.height * (29 / 30):
sys.exit()
# customize size page
elif data.customizeSizeMode:
# press the empty "size button" to enter the board size
if data.width // 2 <= event.x <= data.width * (
3 / 4
) and data.height // 2 <= event.y <= data.height * (3 / 5):
data.selectingBoardSize = True
# press the finish button to enter the game state
elif data.width * (3 / 8) <= event.x <= data.width * (
5 / 8
) and data.height * (1 / 4 + 1 / 15) <= event.y <= data.height * (
7 / 20 + 1 / 15
):
self.resetGameBoard(data)
if data.AImode:
data.levelSelectionMode = True
else:
data.inGame = True
data.customizeSizeMode = False
# level selection mode (only applicable for AI mode)
elif data.levelSelectionMode:
assert data.AImode and not data.playerMode
# novice
if data.width // 4 <= event.x <= data.width * (
3 / 4
) and data.height // 2 <= event.y <= data.height * (3 / 5):
data.AI = AI(self.GameBoard, 0)
data.levelSelectionMode = False
data.inGame = True
# competent
elif data.width // 4 <= event.x <= data.width * (3 / 4) and data.height * (
3 / 5 + 1 / 30
) <= event.y <= data.height * (7 / 10 + 1 / 30):
data.AI = AI(self.GameBoard, 1)
data.levelSelectionMode = False
data.inGame = True
# expert
elif data.width // 4 <= event.x <= data.width * (3 / 4) and data.height * (
7 / 10 + 1 / 15
) <= event.y <= data.height * (4 / 5 + 1 / 15):
data.AI = AI(self.GameBoard, 2)
data.levelSelectionMode = False
data.inGame = True
def keyPressed(self, event, data):
# customize size mode
if data.customizeSizeMode:
if data.selectingBoardSize and event.keysym in string.digits:
if int(event.keysym) in range(4, 10):
data.size = int(event.keysym)
elif int(event.keysym) == 1: # enter 1 to represent size = 10
data.size = 10
data.selectingBoardSize = False
# in game mode
if data.inGame:
# pause the game (can be retrieved)
if event.keysym == "p":
data.paused = not data.paused
# player cannot move in the AI mode
if data.AImode:
pass
else:
# making moves based on the user's pressing keys
if not self.GameBoard.gameOver():
if self.GameBoard.reaches2048():
data.reach2048 = True
if not data.continuePlaying:
data.inGame = False
canMove = False
if data.paused:
print(
"the game is paused now! you cannot move until the game is unpaused"
)
else:
direction = event.keysym
if direction == "Up":
canMove = self.GameBoard.moveUp()
elif direction == "Down":
canMove = self.GameBoard.moveDown()
elif direction == "Left":
canMove = self.GameBoard.moveLeft()
elif direction == "Right":
canMove = self.GameBoard.moveRight()
# add a new number after each legal move
if canMove:
(
data.newTileIndex,
data.newTileNum,
) = self.GameBoard.addNewTile()
print("data.newTileIndex: ", data.newTileIndex)
else:
print("cannot move in this direction")
self.GameBoard.printBoard()
data.board = self.GameBoard.board
data.finishAdding = False # set to False for next move
else:
data.cannotMove = True
data.inGame = False
print("Game Over!")
else:
# restart the game only when the current game is over
if event.keysym == "r":
data.inGame = True
self.resetGameBoard(data)
if data.AImode:
self.init(data) # for AI mode, directly return to the start page
# press c to continue playing the game
elif event.keysym == "c" and data.reach2048 and not data.cannotMove:
data.inGame = True
data.continuePlaying = True
## Graphic drawing functions below
# home page
def drawStartPage(self, canvas, data):
# start page
canvas.create_rectangle(0, 0, data.width, data.height, fill="#ECEFF1")
canvas.create_text(
data.width // 2,
data.height // 4,
text="2048",
fill="#FDD835",
font="Oswald 90 bold",
)
canvas.create_text(
data.width // 2,
data.height * (3 / 8),
text="by Yilun Wu",
fill="light coral",
font="Caladea 40",
)
# player mode button
canvas.create_rectangle(
data.width // 4,
data.height // 2,
data.width * (3 / 4),
data.height * (3 / 5),
fill="lemon chiffon",
)
canvas.create_text(
data.width // 2,
data.height * (11 / 20),
text="Player",
font="Arial 40 bold",
)
# AI mode button
canvas.create_rectangle(
data.width // 4,
data.height * (3 / 5 + 1 / 30),
data.width * (3 / 4),
data.height * (7 / 10 + 1 / 30),
fill="lemon chiffon",
)
canvas.create_text(
data.width // 2,
data.height * (13 / 20 + 1 / 30),
text="AI",
font="Arial 40 bold",
)
# settings button
canvas.create_rectangle(
data.width // 4,
data.height * (7 / 10 + 1 / 15),
data.width * (3 / 4),
data.height * (4 / 5 + 1 / 15),
fill="lemon chiffon",
)
canvas.create_text(
data.width // 2,
data.height * (3 / 4 + 1 / 15),
text="Settings",
font="Arial 40 bold",
)
# quit button
canvas.create_rectangle(
data.width * (5 / 6),
data.height * (9 / 10),
data.width * (29 / 30),
data.height * (29 / 30),
fill="light grey",
)
canvas.create_text(
data.width * (9 / 10),
data.height * (14 / 15),
text="Quit",
font="Arial 30 bold",
)
# customize size page
def drawCustomizeSizePage(self, canvas, data):
canvas.create_rectangle(0, 0, data.width, data.height, fill="cyan")
canvas.create_text(
data.width // 2,
data.height // 4,
text="Select Your Size Here!",
font="Arial 50 bold",
fill="purple",
)
# sizes (4-10)
canvas.create_text(
data.width // 4,
data.height * (11 / 20),
text="Board Size(Width):",
font="Arial 30",
)
if data.selectingBoardSize:
canvas.create_rectangle(
data.width // 2,
data.height // 2,
data.width * (3 / 4),
data.height * (3 / 5),
fill="light goldenrod",
)
else:
canvas.create_rectangle(
data.width // 2,
data.height // 2,
data.width * (3 / 4),
data.height * (3 / 5),
fill="lemon chiffon",
)
canvas.create_text(
data.width * (5 / 8),
data.height * (11 / 20),
text=data.size,
font="Arial 30",
)
canvas.create_text(
data.width * (7 / 8),
data.height * (11 / 20),
text="(4-10)",
font="Arial 30",
)
# finish button
canvas.create_rectangle(
data.width * (3 / 8),
data.height * (1 / 4 + 1 / 15),
data.width * (5 / 8),
data.height * (7 / 20 + 1 / 15),
fill="lemon chiffon",
)
canvas.create_text(
data.width // 2,
data.height * (3 / 10 + 1 / 15),
text="Finish!",
font="Arial 35",
)
# AI mode level selection page
def drawLevelSelectionPage(self, canvas, data):
canvas.create_rectangle(0, 0, data.width, data.height, fill="cyan")
canvas.create_text(
data.width // 2,
data.height // 4,
text="Select a Level!",
font="Arial 55 bold",
fill="purple",
)
# novice mode
canvas.create_rectangle(
data.width // 4,
data.height // 2,
data.width * (3 / 4),
data.height * (3 / 5),
fill="lemon chiffon",
)
canvas.create_text(
data.width // 2,
data.height * (11 / 20),
text="Novice",
font="Arial 35 bold",
)
# competent mode
canvas.create_rectangle(
data.width // 4,
data.height * (3 / 5 + 1 / 30),
data.width * (3 / 4),
data.height * (7 / 10 + 1 / 30),
fill="lemon chiffon",
)
canvas.create_text(
data.width // 2,
data.height * (13 / 20 + 1 / 30),
text="Competent",
font="Arial 35 bold",
)
# expert mode
canvas.create_rectangle(
data.width // 4,
data.height * (7 / 10 + 1 / 15),
data.width * (3 / 4),
data.height * (4 / 5 + 1 / 15),
fill="lemon chiffon",
)
canvas.create_text(
data.width // 2,
data.height * (3 / 4 + 1 / 15),
text="Expert",
font="Arial 35 bold",
)
# game page
def drawCell(self, canvas, data, row, col):
# draw every cell
currNum = data.board[row][col]
cellBoundsWidth = 2.5
if (
row,
col,
) == data.newTileIndex and not data.finishAdding: # new added tile with contrasting color
canvas.create_rectangle(
data.margin + data.cellSize * col,
data.margin + data.titlePlace + data.cellSize * row,
data.margin + data.cellSize * (col + 1),
data.margin + data.titlePlace + data.cellSize * (row + 1),
fill="cyan",
width=cellBoundsWidth,
)
data.finishAdding = True
data.newTileIndex = None, None
else:
canvas.create_rectangle(
data.margin + data.cellSize * col,
data.margin + data.titlePlace + data.cellSize * row,
data.margin + data.cellSize * (col + 1),
data.margin + data.titlePlace + data.cellSize * (row + 1),
fill=data.colors[currNum],
width=cellBoundsWidth,
)
def drawBoard(self, canvas, data):
# draw the board by filling every cells(using draw cells)
for row in range(data.size):
for col in range(data.size):
self.drawCell(canvas, data, row, col)
if data.board[row][col]:
textSize = 180 // data.size
canvas.create_text(
data.margin + data.cellSize / 2 + col * data.cellSize,
data.titlePlace
+ data.margin
+ data.cellSize / 2
+ row * data.cellSize,
text=data.board[row][col],
font=("Arial", textSize),
fill="black",
)
def drawGamePage(self, canvas, data):
canvas.create_rectangle(0, 0, data.width, data.height, fill="#EFEBE9")
canvas.create_text(
data.width // 4,
data.titlePlace // 2,
text="2048",
font="Arial 60 bold",
fill="#795548",
)
if data.reach2048:
canvas.create_text(
data.width // 4,
data.titlePlace * (3 / 4),
text="Reached 2048",
font="TimesNewRoman 30 bold",
fill="red",
)
if data.AImode: # AI mode
canvas.create_text(
data.width // 2,
data.titlePlace // 2,
text="Step:" + str(data.AIstep),
font="Arial 23 bold",
fill="purple",
)
else: # player mode
canvas.create_text(
data.width // 2,
data.titlePlace // 2,
text="Time:" + str(data.timeCounter),
font="Arial 23 bold",
fill="purple",
)
canvas.create_text(
data.width * (3 / 4),
data.titlePlace // 2,
text="Score:" + str(self.GameBoard.score),
font="Arial 23 bold",
fill="purple",
)
self.drawBoard(canvas, data)
# Game paused
if data.paused:
canvas.create_rectangle(
0, data.height // 3, data.width, data.height * (2 / 3), fill="gold"
)
canvas.create_text(
data.width // 2,
data.height // 2,
text="Game Paused!",
font="TimesNewRoman 35 bold",
fill="red",
)
def drawReach2048Page(self, canvas, data):
# reach 2048
canvas.create_rectangle(0, 0, data.width, data.height, fill="#EEEBE9")
canvas.create_text(
data.width // 2,
data.height // 4,
text="Game Score: " + str(self.GameBoard.score),
font="Arial 30 bold",
fill="purple",
)
canvas.create_text(
data.width // 2,
data.height // 2,
text="Congratulations!",
font="Arial 50 bold",
fill="red",
)
canvas.create_text(
data.width // 2,
data.height * (3 / 5),
text="you get 2048 and WIN!",
font="Arial 50 bold",
fill="red",
)
canvas.create_text(
data.width // 2,
data.height * (3 / 4),
text="press 'r' to restart",
font="Arial 30 bold",
fill="purple",
)
# game over after reaching 2048
if not data.cannotMove:
canvas.create_text(
data.width // 2,
data.height * (4 / 5),
text="press 'c' to continue",
font="Arial 30 bold",
fill="purple",
)
# game over page
def drawGameOverPage(self, canvas, data):
canvas.create_rectangle(0, 0, data.width, data.height, fill="#EEEBE9")
canvas.create_text(
data.width // 2,
data.height // 2,
text="You LOSE",
font="Arial 50 bold",
fill="black",
)
canvas.create_text(
data.width // 2,
data.height // 4,
text="Game Score: " + str(self.GameBoard.score),
font="Arial 30 bold",
fill="purple",
)
canvas.create_text(
data.width // 2,
data.height * (3 / 4),
text="press 'r' to restart",
font="Arial 30 bold",
fill="purple",
)
def redrawAll(self, canvas, data):
# start page
if data.startPage:
self.drawStartPage(canvas, data)
# customize size page
elif data.customizeSizeMode:
self.drawCustomizeSizePage(canvas, data)
# AI mode level selection page
elif data.levelSelectionMode:
assert data.AImode and not data.playerMode and not data.customizeSizeMode
self.drawLevelSelectionPage(canvas, data)
# in game page
elif data.inGame:
self.drawGamePage(canvas, data)
# reach 2048 page
elif data.reach2048:
self.drawReach2048Page(canvas, data)
# game over page
elif data.cannotMove:
self.drawGameOverPage(canvas, data)
def AImove(self, data):
print("AI playing")
print("step %d:" % data.AIstep)
data.AI.nextMove()
# update the board after each AI's move
self.GameBoard.board = data.AI.GameBoard.board
self.GameBoard.printBoard()
data.board = data.AI.GameBoard.board
data.AIstep += 1
def timerFired(self, data):
if data.inGame and not (data.reach2048 or data.cannotMove) and not data.paused:
data.timeCounter += 1
if data.AImode:
data.timerDelay = 200
if not self.GameBoard.gameOver():
# move twice in each timer delay period
self.AImove(data)
else:
data.inGame = False
if self.GameBoard.reaches2048():
data.reach2048 = True
else:
data.cannotMove = True
def runGame(self, width, height): # tkinter starter code
def redrawAllWrapper(canvas, data):
canvas.delete(ALL)
canvas.create_rectangle(
0, 0, data.width, data.height, fill="white", width=0
)
self.redrawAll(canvas, data)
canvas.update()
def mousePressedWrapper(event, canvas, data):
self.mousePressed(event, data)
redrawAllWrapper(canvas, data)
def keyPressedWrapper(event, canvas, data):
self.keyPressed(event, data)
redrawAllWrapper(canvas, data)
def timerFiredWrapper(canvas, data):
self.timerFired(data)
redrawAllWrapper(canvas, data)
# pause, then call timerFired again
canvas.after(data.timerDelay, timerFiredWrapper, canvas, data)
# Set up data and call init
class Struct(object):
pass
data = Struct()
data.width = width
data.height = height
data.timerDelay = 500 # milliseconds
root = Tk()
root.title("2048")
root.resizable(width=False, height=False) # prevents resizing window
self.init(data)
# create the root and the canvas
canvas = Canvas(root, width=data.width, height=data.height)
canvas.configure(bd=0, highlightthickness=0)
canvas.pack()
# set up events
root.bind("<Button-1>", lambda event: mousePressedWrapper(event, canvas, data))
root.bind("<Key>", lambda event: keyPressedWrapper(event, canvas, data))
timerFiredWrapper(canvas, data)
# and launch the app
root.mainloop() # blocks until window is closed