-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserAI.py
176 lines (132 loc) · 4.64 KB
/
UserAI.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
import sys
import copy
import time
from GridNode import GridNode
from Grid import Grid
BIG_NUMBER = sys.maxint
class UserAI:
# Internal helper function to facilitate
# for the recursive construction of the
# game search space tree
def __generateGameTreeHelper__(self, userTurn, gridNode, maxDepth, curDepth):
# If max depth is reached, then
# break
if (curDepth == maxDepth):
return
curGrid = gridNode.getGrid()
if (userTurn):
# Adds all possible user moves
userLeftMove = copy.deepcopy(curGrid)
userRightMove = copy.deepcopy(curGrid)
userUpMove = copy.deepcopy(curGrid)
userDownMove = copy.deepcopy(curGrid)
resultLeft = userLeftMove.tilt("left")
resultRight = userRightMove.tilt("right")
resultUp = userUpMove.tilt("up")
resultDown = userDownMove.tilt("down")
if (resultLeft[0] == True):
gridNode.addChildGrid(userLeftMove,
gridNode.getScore() + resultLeft[1])
if (resultRight[0] == True):
gridNode.addChildGrid(userRightMove,
gridNode.getScore() + resultRight[1])
if (resultUp[0] == True):
gridNode.addChildGrid(userUpMove,
gridNode.getScore() + resultUp[1])
if (resultDown[0] == True):
gridNode.addChildGrid(userDownMove,
gridNode.getScore() + resultDown[1])
else:
# Adds all permutation of 2, 4 tiles at
# all valid positions of the grid
for val in range(1, 3):
for i in range(curGrid.getGridSize()):
for j in range(curGrid.getGridSize()):
gameMove = copy.deepcopy(curGrid)
if (gameMove.addTile(val*2, i, j) == True):
gridNode.addChildGrid(gameMove,
gridNode.getScore())
# Recursive calls the helper function on all
# generated child nodes
for nodes in range(gridNode.getNumOfChildNodes()):
self.__generateGameTreeHelper__((not userTurn),
gridNode.getChildNodeAt(nodes),
maxDepth, curDepth+1)
# Given a grid state and depth, this function
# generates your game search tree
def generateGameTree(self, userTurn, grid, depth):
gridNode = GridNode(grid)
self.__generateGameTreeHelper__(userTurn, gridNode, depth, 0)
return gridNode
def __miniMaxTraversalHelper__(self, gridNode, depth, alpha, beta, maximizingPlayer):
if ((depth == 0) or
(gridNode.getNumOfChildNodes() == 0)):
return (gridNode.getScore() +
gridNode.getGrid().getHeuristic())
if (maximizingPlayer == True):
for i in range(gridNode.getNumOfChildNodes()):
childGridNode = self.generateGameTree(False,
gridNode.getChildNodeAt(i).getGrid(),
1)
val = self.__miniMaxTraversalHelper__(childGridNode,
depth-1,
alpha,
beta,
False)
if (val > alpha):
alpha = val
if (beta <= alpha):
break
return alpha
elif (maximizingPlayer == False):
for i in range(gridNode.getNumOfChildNodes()):
childGridNode = self.generateGameTree(True,
gridNode.getChildNodeAt(i).getGrid(),
1)
val = self.__miniMaxTraversalHelper__(childGridNode,
depth-1,
alpha,
beta,
True)
if (val < beta):
beta = val
if (beta <= alpha):
break
return bestValue
def miniMaxTraversal(self, gridNode, depth):
return self.__miniMaxTraversalHelper__(gridNode,
depth,
(-1 * BIG_NUMBER - 1),
BIG_NUMBER,
False)
def decisionMaker(self, grid):
gridNode = self.generateGameTree(True, grid, 1)
bestValue = -1 * BIG_NUMBER - 1
if (gridNode.getNumOfChildNodes() == 0):
return False
for i in range(gridNode.getNumOfChildNodes()):
hu_val = self.miniMaxTraversal(gridNode.getChildNodeAt(i), 8)
print "hu_val = " + str(hu_val)
if (hu_val > bestValue):
grid.setGrid(((gridNode.getChildNodeAt(i)).getGrid()).getGrid())
bestValue = hu_val
print "Making following move:"
grid.printGrid()
#time.sleep(1)
return True
def traversalHelper(gridNode, depth):
print "GridNode, depth = " + str(depth)
gridNode.getGrid().printGrid()
print "score = " + str(gridNode.getScore())
for i in range(gridNode.getNumOfChildNodes()):
traversalHelper(gridNode.getChildNodeAt(i), depth+1)
def main():
userAI = UserAI()
grid = Grid()
grid.addTile(2, 0, 0)
grid.addTile(2, 1, 0)
grid.addTile(4, 1, 0)
grid.printGrid()
userAI.decisionMaker(grid)
if __name__ == "__main__":
main()