-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstartGame.py
121 lines (102 loc) · 4.06 KB
/
startGame.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
from tkinter import Frame, Label, CENTER
import agent
import gameRules
EDGE_LENGTH = 400
CELL_COUNT = 4
CELL_PAD = 10
UP_KEY = "'w'"
DOWN_KEY = "'s'"
LEFT_KEY = "'a'"
RIGHT_KEY= "'d'"
STEP_KEY = "'q'"
AGENT_LEARN = "'p'"
AGENT_PLOT = "'l'"
AGENT_PLOT_OPT = "'m'"
LABEL_FONT = ("Verdana", 40, "bold")
GAME_COLOR = "#a6bdbb"
EMPTY_COLOR = "#8eaba8"
TILE_COLORS = {2: "#daeddf", 4: "#9ae3ae", 8: "#6ce68d", 16: "#42ed71",
32: "#17e650", 64: "#17c246", 128: "#149938",
256: "#107d2e", 512: "#0e6325", 1024: "#0b4a1c",
2048: "#031f0a", 4096: "#000000", 8192: "#000000",}
LABEL_COLORS = {2: "#011c08", 4: "#011c08", 8: "#011c08", 16: "#011c08",
32: "#011c08", 64: "#f2f2f0", 128: "#f2f2f0",
256: "#f2f2f0", 512: "#f2f2f0", 1024: "#f2f2f0",
2048: "#f2f2f0", 4096: "#f2f2f0", 8192: "#f2f2f0",}
class Display(Frame):
def __init__(self):
Frame.__init__(self)
self.grid()
self.master.title('2048')
self.master.bind("<Key>", self.key_press)
self.commands = {UP_KEY: gameRules.actionUP,
DOWN_KEY: gameRules.actionDOWN,
LEFT_KEY: gameRules.actionLEF,
RIGHT_KEY: gameRules.actionRIGHT,
STEP_KEY: agent.agentStep,
}
self.grid_cells = []
self.build_grid()
self.init_matrix()
self.draw_grid_cells()
self.mainloop()
def build_grid(self):
background = Frame(self, bg=GAME_COLOR,
width=EDGE_LENGTH, height=EDGE_LENGTH)
background.grid()
for row in range(CELL_COUNT):
grid_row = []
for col in range(CELL_COUNT):
cell = Frame(background, bg=EMPTY_COLOR,
width=EDGE_LENGTH / CELL_COUNT,
height=EDGE_LENGTH / CELL_COUNT)
cell.grid(row=row, column=col, padx=CELL_PAD,
pady=CELL_PAD)
t = Label(master=cell, text="",
bg=EMPTY_COLOR,
justify=CENTER, font=LABEL_FONT, width=5, height=2)
t.grid()
grid_row.append(t)
self.grid_cells.append(grid_row)
def init_matrix(self):
self.matrix = gameRules.initialize_game()
def draw_grid_cells(self):
for row in range(CELL_COUNT):
for col in range(CELL_COUNT):
tile_value = self.matrix[row][col]
if not tile_value:
self.grid_cells[row][col].configure(
text="", bg=EMPTY_COLOR)
else:
self.grid_cells[row][col].configure(text=str(
tile_value), bg=TILE_COLORS[tile_value],
fg=LABEL_COLORS[tile_value])
self.update_idletasks()
def key_press(self, event):
continueGame = True
key = repr(event.char)
if key == AGENT_PLOT:
agent.plotAnalysis()
if key == AGENT_PLOT_OPT:
agent.plotConAnalysis()
if key == AGENT_LEARN:
move_count = 0
while continueGame:
self.matrix, continueGame, endTime = agent.agentStep(self.matrix,40, 30)
if continueGame:
self.matrix = agent.placeNewTile(self.matrix)
self.draw_grid_cells()
move_count += 1
if key == STEP_KEY:
self.matrix, move_made, endTime = agent.agentStep(self.matrix, 20, 30)
if move_made:
self.matrix = agent.placeNewTile(self.matrix)
self.draw_grid_cells()
move_made = False
elif key in self.commands:
self.matrix, move_made, _ = self.commands[repr(event.char)](self.matrix)
if move_made:
self.matrix = gameRules.placeNewTile(self.matrix)
self.draw_grid_cells()
move_made = False
gamegrid = Display()