-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.py
executable file
·328 lines (268 loc) · 7.3 KB
/
game.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
import os
from board import Board
import keypress
import ai,copy,math
import argparse
class Game(object):
# create a instance of Board and initialize the board to all zeroes
board = Board()
#
def __init__(self,b=None, score=0, win=2048, ai=False, step=False):
if b:
self.board.setBoardParams(b)
self.board.setWinGoal(win)
self.score = self.board.setScore(score)
self.ai = ai
self.step = step
def readMove(self):
return keypress.getKey()
def clrscr(self):
if os.name == 'nt':
os.system('cls')
else:
os.system('clear')
def game(self):
if not self.ai:
print self.board.board
while not self.board.won() and self.board.canMove():
# self.board.drawBoard(self.board.getCells())
key = self.readMove()
if key:
if self.board.move(key):
self.board.tileGen(1)
self.clrscr()
self.board.drawBoard()
else:
exit(0)
else:
print 'Press any key to begin...'
while True:
raw_input('')
self.clrscr()
self.board.drawBoard()
if self.board.highestTile()[0] > 512:
self.evalfn(self.board, True)
# raw_input(' ')
# d = self.dfs(copy.deepcopy(self.board))
# moves = ['up','left','right','down']
# d = [float('inf'),None]
# while depth<=1:
# for m in moves:
# b2 = copy.deepcopy(self.board)
# if b2.move(m):
# temp = 10*ai.manhatDist(b2.highestTile()) + (1.0/(b2.getEmptyCells()+0.001))
# print b2.highestTile()
# print ai.manhatDist(b2.highestTile())
# print 'empty = '+str(b2.getEmptyCells())
# print m
# print temp
# print '----'
# if d[0]>temp:
# d[0],d[1]=temp,m
# else:
# continue
# if self.board.highestTile()[0] < 256:
# d = self.expectimax(2)
# else:
# d = self.minimax(3)
if self.board.emptyTiles()<=7:
d = self.minimax(7)
else:
d=self.expectimax(2)
if d:
self.board.move(d)
self.board.tileGen(1)
else:
break
# if self.board.highestTile()[0] >= 512:
# raw_input(' ')
return self.board.score()
def minimax(self, depth):
vals = {}
moves = ['up','left','right','down']
depth = 2*depth
alpha = float('-inf')
beta = float('inf')
for m in moves:
b = copy.deepcopy(self.board)
if b.move(m):
vals[m] = self.value(b, depth, alpha, beta)
if vals:
return max(vals, key=vals.get)
else:
return None
def value(self, b, depth,alpha, beta):
if depth == 1:
return self.evalfn(b)
# b = copy.deepcopy(b)
if depth%2==0:
return self.minval(b,depth-1, alpha, beta)
else:
return self.maxval(b,depth-1, alpha, beta)
def maxval(self,state,depth,a,b):
v = float('-inf')
moves = ['up','left','right','down']
for m in moves:
s1 = copy.deepcopy(state)
i = 0
# while True:
# if s1.move(m):
# v = max(v,self.emvalue(s1, depth))
# break
# else:
# if not s1.filled() and s1.highestTile() > 512 and i < 1:
# i += 1
# s1 = copy.deepcopy(s1)
# s1.tileGen(1)
# else:
# break
if s1.move(m):
v = max(v,self.emvalue(s1, depth))
if v >= b:
return v
a = max(a,v)
return v
def minval(self, state, depth, a, b):
v = float('inf')
empty = state.emptyTiles()
for pos in empty:
s1 = copy.deepcopy(state)
s1.tileGen(1,pos)
v = min(v,self.value(s1, depth,a,b))
if v <= a:
return v
b = min(b,v)
return v
def expectimax(self, depth):
vals = {}
moves = ['up','left','right','down']
depth = 2*depth
for m in moves:
b = copy.deepcopy(self.board)
if b.move(m):
vals[m] = self.emvalue(b, depth)
if vals:
return max(vals, key=vals.get)
else:
return None
def emvalue(self, b, depth):
if depth == 1:
return self.evalfn(b)
# b = copy.deepcopy(b)
if depth%2==0:
return self.expval(b,depth-1)
else:
return self.emaxval(b,depth-1)
def emaxval(self,state,depth):
v = float('-inf')
moves = ['up','left','right','down']
for m in moves:
s1 = copy.deepcopy(state)
i = 0
# while True:
# if s1.move(m):
# v = max(v,self.emvalue(s1, depth))
# break
# else:
# if not s1.filled() and s1.getEmptyCells() > 4 and s1.highestTile()[0] > 512 and i < 1:
# s1 = copy.deepcopy(s1)
# s1.tileGen(1)
# i += 1
# else:
# break
if s1.move(m):
v = max(v,self.emvalue(s1, depth))
return v
def expval(self, state, depth):
v = 0
empty = state.emptyTiles()
for pos in empty:
s1 = copy.deepcopy(state)
s1.tileGen(1,pos)
v += (self.emvalue(s1, depth))*(1.0/len(empty))
return v
def evalfn(self, b2, p=False):
mono = b2.monotonicity3()
smooth = b2.smoothness8()
empty = -1000+6*b2.getEmptyCells()
o,n = b2.score(2)
high = b2.highestTile()
high_row = b2.getCells(row=high[1],column=None)
temp = [0,0]
for x in range(3):
if high_row[x]>=high_row[x+1]:
temp[0] += 1
else:
temp[1] += 1
blocked = 0
bonus = 0
# if p:
# print 'high row', high_row
# print 'temp',temp
if temp[1]==0:
blocked = 100
if 0 in high_row and high_row[3] != 0:
blocked -= 150
else:
blocked = -200
# if p:
# print 'blockd1', blocked
# else:
# bonus += 100
# blocked += bonus
# if p:
# print 'blocked2', blocked
# temp = 2.7*b2.getEmptyCells() + mono + math.log(b2.highestTile()[0]) + 0.1*smooth# - 2.7*ai.manhatDist(b2.highestTile())
# empty = b2.getEmptyCells()
# if empty/4==3:
# empty = 0
# else:
# empty = 12*(-800+6*empty)
deathPenalty = 0
if b2.filled() and not b2.canMove():
deathPenalty = -10000000
corner = ai.atCorner(high)
eW = 0
if (b2.board[0][2] != 0 and b2.board[0][3] != 0 and high > 128):
two = math.log(b2.board[0][2],2)
three = math.log(b2.board[0][3],2)
if two-three >= 5:
eW = -3
bw = 80.
acw = 4.
ew = 22.
mw = 10.
scw = 0.
sw = 1.
# if b2.getEmptyCells()<2:
# ew += 10
# bw -= 15
if high[0] < 8193:
# temp = 3*blocked + 4*ai.atCorner(high) + 18*empty + 2.3*mono + 1*(n-o) + 1.*smooth
temp = bw*blocked + acw* + ew*empty + mw*mono + scw*(n-o) + sw*smooth+deathPenalty
# temp = mono + deathPenalty + b2.sm()
# temp = 4*blocked + 5.7*ai.atCorner(high) + 17*empty + 10.3*mono + 1.11*smooth + deathPenalty#+ 1.12**(n-o)
else:
##### most recent
temp = 3*blocked + 4*ai.atCorner(high) + 25.5*empty + 2.8*smooth+ 1.8*mono
##### most recent
# else:
# temp = b2.sm() + 0.005*empty
# if p:
# print 'blocked:',bw*blocked
# print 'corner', acw*corner
# print 'empty', ew*empty
# print 'mono', mw*mono
# print 'score', scw*(n-o)
# print 'smooth', sw*smooth
return temp
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Your favourite 2048, now in the terminal! Play it or have an AI solve it for you')
parser.add_argument('--ai', action='store_true', help='AI Flag')
parser.add_argument('--step', action='store_true', help='Run the AI Step by Step')
args = parser.parse_args()
g = Game(ai = args.ai, step = args.step)
score = g.game()
if score == None:
print 'Game abandoned'
print "Game Finished, Score :",score