-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayerAI2.py
469 lines (359 loc) · 11.4 KB
/
PlayerAI2.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
#!/usr/bin/env python
# coding:utf-8
from random import randint
from BaseAI import BaseAI
import math
from Grid import directionVectors
import sys
from Displayer import Displayer
# direction vectors
directionVectors = (UP_VEC, DOWN_VEC, LEFT_VEC, RIGHT_VEC) = ((-1, 0), (1, 0), (0, -1), (0, 1))
vecIndex = [UP, DOWN, LEFT, RIGHT] = range(4)
class PlayerAI(BaseAI):
def getMove(self, grid):
moves = grid.getAvailableCells()
alpha = {'score': float("-inf"), 'direction': None}
beta = {'score': float("inf"), 'direction': None}
newGrid = grid.clone()
result = self.alphaBeta(newGrid, 5, alpha, beta, True, None, grid, True)
#print "RESULT " + str(result)
return result['direction']
# def getMove(self, grid):
# # I'm too naive, please change me!
# moves = grid.getAvailableMoves()
#
# #
# bestScore = 0
# bestMove = 0
# isMove = False
# i = 0
# for m in moves:
# print "MOVE"
# print m
#
# newGrid = grid.clone()
# newGrid.move(m)
# currentScore = self.getHeuristicScore(newGrid, grid.score)
#
# if currentScore > bestScore or bestScore == 0:
# bestScore = currentScore
# bestMove = m
# isMove = True
# i += 1
# print "\n"
# return bestMove
# return moves[randint(0, len(moves) - 1)] if moves else None
def getNewTileValue(self):
possibleNewTileValue = [2, 4]
defaultPossibility = 0.7
possibility = defaultPossibility
if randint(0, 99) < 100 * possibility:
return possibleNewTileValue[0]
else:
return possibleNewTileValue[1]
def alphaBeta2(self, grid, depth, a, b, maximizingPlayer, lastMove, oldGrid):
if depth == 0:
test = { 'score': self.getHeuristicScore(grid, oldGrid.score), 'direction': lastMove}
return test
if not grid.canMove():
return {'score': 0, 'direction': lastMove}
isFirstDepth = False
if lastMove == None:
isFirstDepth = True
if maximizingPlayer == True:
moves = grid.getAvailableMoves()
for m in moves:
newGrid = grid.clone()
newGrid.move(m)
if isFirstDepth:
lastMove = m
ab = (self.alphaBeta(newGrid, depth - 1, a, b, False, lastMove, grid))
if a['score'] < ab['score']:
a = ab
print "SCORE " + str(a)
print lastMove
print m
if isFirstDepth:
lastMove = m
#lastMove = m
#if b <= a:
# break
return { 'score': a['score'], 'direction': lastMove}
else:
try:
moves = grid.getAvailableCells()
for c in moves:
newGrid = grid.clone()
newGrid.insertTile(c, self.getNewTileValue())
ab = self.alphaBeta(newGrid, depth - 1, a, b, True, lastMove, grid)
if b['score'] > ab['score']:
b = ab
#if b <= a:
# break
return { 'score': b['score'], 'direction': lastMove}
except:
print "Unexpected error:", sys.exc_info()[0]
def alphaBeta(self, grid, depth, a, b, maximizingPlayer, lastMove, oldGrid, first):
# print "\n\nnew AB!"
if depth == 0:
test = { 'score': self.getHeuristicScore(grid, oldGrid.score), 'direction': lastMove}
#print "here is heuristic " + str(test)
return test
if not grid.canMove():
return {'score': 0, 'direction': lastMove}
# if grid.canMove == false -> return 0
moves = grid.getAvailableMoves()
# print "depth " + str(depth)
if maximizingPlayer == True:
# print moves
for m in moves:
#if first:
#print "\n\nMOVE " + str(m) + "______________________________________________________"
newGrid = grid.clone()
newGrid.move(m)
#print "true moved " + str(m) + " depth " + str(depth)
#displayer = Displayer()
#displayer.display(newGrid)
#print "A SCORE " + str(a)
ab = (self.alphaBeta(newGrid, depth - 1, a, b, False, m, grid, False))
#print "ab is " + str(ab) + " and depth " + str(depth)
#print "a score " + str(a['score'])
#print "ab score " + str(ab['score'])
if a['score'] < ab['score']:
#print "~~~DEPTH~~~" + str(depth)
#print "A SCORE " + str(a)
#print "\nab should be " + str(ab['score'])
#print "true moved " + str(m) + " depth " + str(depth)
# print a
a = ab
lastMove = m
#if first:
#print "SCORE " + str(a)
#print lastMove
#print m
# lastMove = m
#print "BETA " + str(b)
if not b['score'] is float("inf"):
if b['score'] <= a['score']:
#print "BETA BREAK " + str(b) + " alpha " + str(a)
break
# print "I'm breaking a " + str(a) + "\n b " + str(b)
# break
#print "----a under score " + str(a)
return { 'score': a['score'], 'direction': lastMove}
else:
# print "false"
moves = grid.getAvailableCells()
# moves = self.getNewTileValue()
for c in moves:
newGrid = grid.clone()
newGrid.insertTile(c, self.getNewTileValue())
#print "--------------------insert TILE "
#displayer = Displayer()
#displayer.display(newGrid)
ab = self.alphaBeta(newGrid, depth - 1, a, b, True, lastMove, grid, False)
#ab['score'] += b['score']
if b['score'] > ab['score']:
#print "\nfalse ab should be " + str(ab['score'])
# print "false moved depth " + str(depth)
# print ab
b = ab
# lastMove = m
# b = min(b, self.alphaBeta(newGrid, depth - 1, a, b, True, m, None), key=lambda p: p[0])
if b['score'] <= a['score']:
break
# print str({ 'score': b, 'direction': lastMove})
return { 'score': b['score'], 'direction': lastMove}
def getHeuristicScore(self, newGrid, oldScore):
score = 0
emptyWeight = 1
closeWeight = 0
orderWeight = 1
scoreWeight = 1
nearWeight = 0.6
cornerWeight = 2
debug = False
# (1): actual score
addScore = newGrid.score - oldScore
# print "scc " + str(addScore)
if addScore > 0:
addScore = math.log(addScore) / math.log(2)
# (2): empty fields
# the higher the score, the more important the empty fields
# highest score * available fields
fields = len(newGrid.getAvailableCells())
emptyFields = math.log(fields * newGrid.getHighestValue()) / math.log(2)
# (3): how close are our numbers?
# closescore = self.getCloseNumberScore(newGrid)
# score += closescore * 0.5
# close = self.smoothness(newGrid) * math.log(newGrid.getHighestValue()) / math.log(2)
# closescore = close
# (4): are we close to a great corner order?
# order = self.getOrderScore(newGrid)
order = self.getOrder(newGrid)
corner = self.getOrderScore(newGrid)
near = self.getNearByScore(newGrid)
if near == 0:
near = addScore
# if corner == 0:
# nearWeight *= 2
# scoreWeight *= 2
# #
score = corner * cornerWeight + near * nearWeight + scoreWeight * addScore + emptyFields * emptyWeight + order * orderWeight
# print directionVectors
if debug:
print "SCORE " + str(scoreWeight * addScore)
print "EMPTY " + str(emptyFields * emptyWeight)
# print emptyFields * emptyWeight
# score += emptyfields
#print "CLOSE " + str(closescore * closeWeight)
print "ORDER " + str(order * orderWeight)
print "NEAR " + str(near * nearWeight)
print "CORNER " + str(corner)
print "last score " + str(score)
#
return score
def getCloseNumberScore(self, grid):
score = 0
i = 0
for x in grid.map:
j = 0
for y in x:
# up
if i > 0:
if grid.map[i - 1][j] == y:
score += y
# if grid.map[i-1][j] == y/2 or grid.map[i-1][j] == y*2:
# score += 0.05 * y
# down
if i < (len(grid.map) - 1):
if grid.map[i + 1][j] == y:
score += y
# if grid.map[i+1][j] == y/2 or grid.map[i+1][j] == y*2:
# score += 0.05 * y
# left
if j > 0:
if grid.map[i][j - 1] == y:
score += y
# if grid.map[i][j-1] == y/2 or grid.map[i][j-1] == y*2:
# score += 0.05 * y
# right
if j < (len(x) - 1):
if grid.map[i][j + 1] == y:
score += y
# if grid.map[i][j+1] == y/2 or grid.map[i][j+1] == y*2:
# score += 0.05 * y
j += 1
i += 1
return score
def getNearByScore(self, grid):
score = 0
for x in range (0, 4):
for y in range (0, 4):
if grid.map[x][y] != 0:
for v in directionVectors:
a = v[0]
b = v[1]
cX = x
cY = y
while grid.gridInsertOk((cX + a, cY + b)):
if grid.map[cX][cY] == grid.map[cX + a][cY + b] and grid.map[cX][cY] != 0:
score += math.log(grid.map[cX][cY]) / math.log(2)
elif grid.map[cX][cY] > 0 and grid.map[cX + a][cY + b] > 0 and (grid.map[cX][cY] == grid.map[cX + a][cY + b] / 2 or grid.map[cX][cY] / 2 == grid.map[cX + a][cY + b]):
if grid.map[cX][cY] > grid.map[cX + a][cY + b]:
score += math.log(grid.map[cX + a][cY + b] / 2) / math.log(2) / 2
else:
score += math.log(grid.map[cX][cY] / 2) / math.log(2) / 2
# elif grid.map[cX][cY] != 0 and grid.map[cX + a][cY + b] != 0:
# score -= math.log(grid.map[cX][cY])/ math.log(2) - math.log(grid.map[cX + a][cY + b]) / math.log(2)
cX = cX + a
cY = cY + b
return score
def getOrder(self, grid):
scores = [0, 0, 0, 0]
# right/left
for x in range(0, 4):
current = 0
next = current + 1
# find the next value
while next < 4 and grid.map[x][next] == 0:
next += 1
while next < 4:
# get the current and the next value
# if next >= 4:
# next -= 1
currentVal = math.log(grid.map[x][current]) / math.log(2) if grid.map[x][current] > 0 else 0
nextVal = math.log(grid.map[x][next]) / math.log(2) if grid.map[x][next] > 0 else 0
if currentVal >= nextVal:
scores[0] += (nextVal - currentVal)
elif nextVal > currentVal:
scores[1] += (currentVal - nextVal)
current = next
next += 1
# up/down
for y in range(0, 4):
current = 0
next = current + 1
# find the next value
while next < 4 and grid.map[next][y] == 0:
next += 1
while next < 4:
# get the current and the next value
# if next >= 4:
# next -= 1
currentVal = math.log(grid.map[current][y]) / math.log(2) if grid.map[current][y] > 0 else 0
nextVal = math.log(grid.map[next][y]) / math.log(2) if grid.map[next][y] > 0 else 0
#
if currentVal >= nextVal:
scores[2] += (nextVal - currentVal)
elif nextVal > currentVal:
scores[3] += (currentVal - nextVal)
current = next
next += 1
# print scores
lr = 0
if scores[0] == 0:
lr = scores[1]
elif scores[1] == 0:
lr = scores[0]
elif scores[0] >= scores[1]:
lr = scores[0]
elif scores[1] > scores[0]:
lr = scores[1]
ud = 0
if scores[2] == 0:
ud = scores[3]
elif scores[3] == 0:
ud = scores[2]
elif scores[2] >= scores[3]:
ud = scores[2]
elif scores[3] > scores[2]:
ud = scores[3]
return lr + ud
def getOrderScore(self, grid):
score = 0
# since there are just 4 rows and 4 columns, I can score it statically
highestVal = grid.getHighestValue()
x = 0
y = 0
# left up corner
if grid.map[0][0] == highestVal:
score += highestVal
x = 0
y = 0
# left down corner
if grid.map[3][0] == highestVal:
score += highestVal
x = 3
y = 0
# right up corner
if grid.map[0][3] == highestVal:
score += highestVal
x = 0
y = 3
# right down corner
if grid.map[3][3] == highestVal:
score += highestVal
x = 3
y = 3
return (math.log(score)) if score > 0 else 0