-
Notifications
You must be signed in to change notification settings - Fork 0
/
pythonSolver.py
278 lines (241 loc) · 8.68 KB
/
pythonSolver.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
from tkinter import *
from tkinter import messagebox
import random
import colors
# ----------------------------------------------------------------------------------------------------------------------
class GameBoard:
bg_color = {
"2": "#fcefe6",
"4": "#f2e8cb",
"8": "#f5b682",
"16": "#f29446",
"32": "#ff775c",
"64": "#e64c2e",
"128": "#ede291",
"256": "#fce130",
"512": "#ffdb4a",
"1024": "#f0b922",
"2048": "#fad74d"
}
color = {
"2": "#695c57",
"4": "#695c57",
"8": "#ffffff",
"16": "#ffffff",
"32": "#ffffff",
"64": "#ffffff",
"128": "#ffffff",
"256": "#ffffff",
"512": "#ffffff",
"1024": "#ffffff",
"2048": "#ffffff"
}
def __init__(self):
self.n = 4
self.window = Tk()
self.window.title('Solver 2048')
self.gameArea = Frame(self.window, bg=colors.EMPTY_CELL_COLOR)
self.board = []
self.gridCell = [[0] * 4 for i in range(4)]
self.compress = False
self.merge = False
self.moved = False
self.score = 0
for i in range(4):
rows = []
for j in range(4):
l = Label(self.gameArea, text='', bg='azure4', font=('arial', 22, 'bold'), width=4, height=2)
l.grid(row=i, column=j, padx=7, pady=7)
rows.append(l)
self.board.append(rows)
self.gameArea.grid()
# ------------------------------------------------------------------------------------------------------------------
# Methods for manipulation with grid
def reverse(self):
for ind in range(4):
i = 0
j = 3
while i < j:
self.gridCell[ind][i], self.gridCell[ind][j] = self.gridCell[ind][j], self.gridCell[ind][i]
i += 1
j -= 1
def transpose(self):
self.gridCell = [list(t) for t in zip(*self.gridCell)]
def compressGrid(self):
self.compress = False
temp = [[0] * 4 for i in range(4)]
for i in range(4):
cnt = 0
for j in range(4):
if self.gridCell[i][j] != 0:
temp[i][cnt] = self.gridCell[i][j]
if cnt != j:
self.compress = True
cnt += 1
self.gridCell = temp
def mergeGrid(self):
self.merge = False
for i in range(4):
for j in range(4 - 1):
if self.gridCell[i][j] == self.gridCell[i][j + 1] and self.gridCell[i][j] != 0:
self.gridCell[i][j] *= 2
self.gridCell[i][j + 1] = 0
self.score += self.gridCell[i][j]
self.merge = True
def random_cell(self):
cells = []
for i in range(4):
for j in range(4):
if self.gridCell[i][j] == 0:
cells.append((i, j))
curr = random.choice(cells)
i = curr[0]
j = curr[1]
self.gridCell[i][j] = 2
def can_merge(self):
for i in range(4):
for j in range(3):
if self.gridCell[i][j] == self.gridCell[i][j + 1]:
return True
for i in range(3):
for j in range(4):
if self.gridCell[i + 1][j] == self.gridCell[i][j]:
return True
return False
# ------------------------------------------------------------------------------------------------------------------
# Methods for paint grid with bg and colors
def paintGrid(self):
for i in range(4):
for j in range(4):
if self.gridCell[i][j] == 0:
self.board[i][j].config(text='', bg='azure4')
else:
self.board[i][j].config(text=str(self.gridCell[i][j]),
bg=self.bg_color.get(str(self.gridCell[i][j])),
fg=self.color.get(str(self.gridCell[i][j])))
# ----------------------------------------------------------------------------------------------------------------------
# Class for main game
class Game:
def __init__(self, gameBoard):
self.board = gameBoard
self.end = False
self.won = False
self.score = 0
self.highestNumber = 0
def strategy(self):
key = randomkey()
self.link_keys(key)
self.board.window.after(2, self.strategy)
def start(self):
self.board.random_cell()
self.board.random_cell()
self.board.paintGrid()
self.board.window.after(2, self.strategy)
self.board.window.mainloop()
def link_keys(self, key):
if self.end or self.won:
return
self.board.compress = False
self.board.merge = False
self.board.moved = False
pressedKey = key
if pressedKey == 'Up':
self.board.transpose()
self.board.compressGrid()
self.board.mergeGrid()
self.board.moved = self.board.compress or self.board.merge
self.board.compressGrid()
self.board.transpose()
elif pressedKey == 'Down':
self.board.transpose()
self.board.reverse()
self.board.compressGrid()
self.board.mergeGrid()
self.board.moved = self.board.compress or self.board.merge
self.board.compressGrid()
self.board.reverse()
self.board.transpose()
elif pressedKey == 'Left':
self.board.compressGrid()
self.board.mergeGrid()
self.board.moved = self.board.compress or self.board.merge
self.board.compressGrid()
elif pressedKey == 'Right':
self.board.reverse()
self.board.compressGrid()
self.board.mergeGrid()
self.board.moved = self.board.compress or self.board.merge
self.board.compressGrid()
self.board.reverse()
else:
pass
self.board.paintGrid()
flag = 0
for i in range(4):
for j in range(4):
if self.board.gridCell[i][j] == 2048:
flag = 1
break
if flag == 1: # found 2048
self.won = True
messagebox.showinfo('2048', message='You Won!')
return
for i in range(4):
for j in range(4):
if self.board.gridCell[i][j] == 0:
flag = 1
break
if not (flag or self.board.can_merge()):
self.end = True
messagebox.showinfo('2048', message='Game Over!\nSorry, you will close this window too many times '
'ff:)\n\nAnd again.')
if self.board.moved:
self.board.random_cell()
if self.end or self.won:
matrix = self.board.gridCell
cells = []
for i in matrix:
for j in i:
cells.append(j)
cells.sort()
self.highestNumber = cells[-1]
self.score = self.board.score
self.board.window.quit()
self.board.window.destroy()
return
self.board.paintGrid()
# ----------------------------------------------------------------------------------------------------------------------
# Random Key Generator for returning a random key choice
def randomkey():
keys = ["Up", "Down", "Left", "Right"]
chooseKey = random.choice(keys)
return chooseKey
# ----------------------------------------------------------------------------------------------------------------------
# Class for statistics and their printing on console through game playing, if user wants to play it by AI
class Stats:
def __init__(self, iteration):
self.iteration = iteration
self.stats = []
self.play30()
def play30(self):
for i in range(self.iteration):
game = Game(GameBoard())
game.start()
self.stats.append([game.highestNumber, game.score])
def getStats(self):
print("Stats:\n")
print("-----------------------------")
print("#\tHighest\t\tScore\tW/L")
j = 1
w = 0
l = 0
for i in self.stats:
if i[0] >= 2048:
print(j, ".\t", i[0], "\t\t", i[1], "\tW")
w += 1
else:
print(j, ".\t", i[0], "\t\t", i[1], "\tL")
l += 1
j += 1
print("-----------------------------")
print(f"\n\nRandom strategy ->\t\tWin: {w} Lost: {l}")