-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayerAI_3.py
122 lines (84 loc) · 3.51 KB
/
PlayerAI_3.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
# -*- coding: utf-8 -*-
"""
Created on Wed Jun 7 16:20:38 2017
@author: p_mal
"""
from random import randint
from BaseAI import BaseAI
from math import log
from math import fabs
import time
class PlayerAI(BaseAI):
def __init__(self):
self.time_limit=0
def getMove(self, grid):
self.time_limit=time.clock()+0.2
result=(0,None)
result=self.maximize(grid,float("-inf"),float("inf"),1)
return result[1]
def maximize(self,grid,alpha,beta,depth):
moves=grid.getAvailableMoves()
maxmove=-1
maxutility=float("-inf")
if depth>5 or not grid.canMove() or time.clock()>self.time_limit:
return (self.heuristic(grid),None)
for move in moves:
newgrid=grid.clone()
newgrid.move(move)
result=self.minimize(newgrid,alpha,beta,depth+1)
if result[0]>maxutility:
maxmove=move
maxutility=result[0]
if maxutility>=beta:
break
if maxutility>alpha:
alpha=maxutility
return (maxutility,maxmove)
def minimize(self,grid,alpha,beta,depth):
cells=grid.getAvailableCells()
minutility=float("inf")
if depth>5 or not grid.canMove() or time.clock()>self.time_limit:
return (self.heuristic(grid),None)
result=[float("inf"),None]
for cell in cells:
newgrid=grid.clone()
newgrid.insertTile(cell,self.NewTileValue())
result=self.maximize(newgrid,alpha,beta,depth+1)
if result[0]<minutility:
minutility=result[0]
if minutility<=alpha:
break
if minutility<beta:
beta=minutility
return (minutility,-1)
def NewTileValue(self):
NewTileValue = [2, 4]
probability= 0.9
if randint(0, 99) < 100 * probability:
return NewTileValue[0]
else:
return NewTileValue[1]
def heuristic(self,grid):
#emptycells
emptycells=len(grid.getAvailableCells())
#maxtile
maxtile=grid.getMaxTile()
#score, smoothness and monotocity
score=0
smoothness=0
monotocity=0
weights=[[6,5,4,3],[5,4,3,2],[4,3,2,1],[3,2,1,0]]
for i in range(4):
for j in range(4):
score+=grid.map[i][j]*weights[i][j]
for i in range(4):
for j in range(3):
if grid.map[i][j]>=grid.map[i][j+1] and grid.map[i][j]!=0:
monotocity+=log(grid.map[i][j],2)
if grid.map[j][i]>=grid.map[j+1][i] and grid.map[j][i]!=0:
monotocity+=log(grid.map[j][i],2)
if grid.map[i][j]==grid.map[i][j+1] and grid.map[i][j]!=0:
smoothness+=log(grid.map[i][j],2)
if grid.map[j][i]==grid.map[j+1][i] and grid.map[j][i]!=0:
smoothness+=log(grid.map[j][i],2)
return 0.5*emptycells+10*log(maxtile,2)+0.2*monotocity+2*smoothness+score