-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheuristicBot.py
More file actions
218 lines (197 loc) · 6.7 KB
/
heuristicBot.py
File metadata and controls
218 lines (197 loc) · 6.7 KB
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
import TicTacToe as ttt
import numpy as np
import random
def randomPlayer(availableMoves):
"""
returns a random move
"""
return random.choice(availableMoves)
def countPattern(grid, count, pattern):
"""
counts the number of rows with 'count' number of 'pattern'
"""
rcno = [0, 1, 2]
patternCount = 0
totalCount = 0
# vertical and horizontal
for i in rcno:
# for one line
# checking the row
patternCount = 0
for j in rcno:
if grid[i, j] != pattern and grid[i, j] != 0:
patternCount = 0
break
if grid[i, j] == pattern:
patternCount += 1
if patternCount == count:
totalCount += 1
# checking the column
patternCount = 0
for j in rcno:
if grid[j, i] != pattern and grid[j, i] != 0:
patternCount = 0
break
if grid[j, i] == pattern:
patternCount += 1
if patternCount == count:
totalCount += 1
# diagonals
posDia = 0
negDia = 0
for i in rcno:
if grid[i, i] != pattern and grid[i, i] != 0:
posDia = 4
if grid[i, i] == pattern:
posDia += 1
if grid[i, 2-i] != pattern and grid[i, 2-i] != 0:
negDia = 4
if grid[i, 2-i] == pattern:
negDia += 1
if posDia == count:
totalCount += 1
if negDia == count:
totalCount += 1
return totalCount
def getHeuristic(grid, move, player):
"""
places a move and checks heuristics.
Heuristics :
100 : three pieces in a row,
10 : two pieces in a row,
-10 : two opponent pieces in a row,
-100 : three opponent pieces in a row.
"""
tempGrid = grid.copy()
ttt.checkMoveAndPlace(tempGrid, move, player)
noOf2 = countPattern(tempGrid, 2, player)
noOf3 = countPattern(tempGrid, 3, player)
noOf2Opp = countPattern(tempGrid, 2, (player % 2)+1)
noOf3Opp = countPattern(tempGrid, 3, (player % 2)+1)
heuristic = 0 + 10*noOf2 + 100*noOf3 + (-10*noOf2Opp) + (-100*noOf3Opp)
return heuristic
def oslAgent(grid, availableMoves, player):
"""
one-step-lookahead
1. places all the available moves and get heurisitcs
2. choose the move with highest heuristic
3. return the move position
"""
heuristics = dict(zip(availableMoves, [getHeuristic(
grid, move, player) for move in availableMoves]))
maxValue = max(heuristics.values())
maxHeuristics = [key for key in heuristics.keys(
) if heuristics[key] == maxValue]
return random.choice(maxHeuristics)
def getHeuristic2(grid, player):
"""
checks heuristics.
Heuristics :
100 : three pieces in a row,
10 : two pieces in a row,
-10 : two opponent pieces in a row,
-100 : three opponent pieces in a row.
"""
noOf2 = countPattern(grid, 2, player)
noOf3 = countPattern(grid, 3, player)
noOf2Opp = countPattern(grid, 2, (player % 2)+1)
noOf3Opp = countPattern(grid, 3, (player % 2)+1)
heuristic = 0 + 10*noOf2 + 100*noOf3 + (-10*noOf2Opp) + (-100*noOf3Opp)
return heuristic
def minimax(grid, depth, move, isMaximizing, player):
"""
if terminal node or depth = 0 then
return the heuristic value of node
if maximizing then
value = -infinity
for every child of node
value = max(value, minimax(child, depth-1, minimizing))
return value
else
value = infinity
for every child of node
value = min(value, minimax(child, depth-1, maximizing))
return value
"""
available = [(i, j) for i in range(3)
for j in range(3) if grid[i, j] == 0]
if ttt.gameOver(grid, player, False)[0] or depth == 1:
return getHeuristic2(grid, player)
if isMaximizing:
value = -np.Inf
for validMove in available:
childGrid = grid.copy()
ttt.checkMoveAndPlace(childGrid, validMove, player)
value = max(value, minimax(
childGrid, depth-1, validMove, False, player))
return value
else:
value = np.Inf
for validMove in available:
childGrid = grid.copy()
ttt.checkMoveAndPlace(childGrid, validMove, (player % 2) + 1)
value = min(value, minimax(
childGrid, depth - 1, validMove, True, player))
return value
def nslScoreMove(steps, grid, move, player):
"""
scores each move for nslAgent
"""
tempGrid = grid.copy()
ttt.checkMoveAndPlace(tempGrid, move, player)
score = minimax(tempGrid, steps, move, False, player)
return score
def nslAgent(steps, grid, availableMoves, player):
"""
n-step-lookahead
1. look n step ahead
2. select the best move using minimax
3. return the move position
"""
# get scores for every available move using minimax
# select the max scores
# return a random move from max scores
heuristics = dict(zip(availableMoves, [nslScoreMove(
steps, grid, move, player) for move in availableMoves]))
maxValue = max(heuristics.values())
maxHeuristics = [key for key in heuristics.keys(
) if heuristics[key] == maxValue]
return random.choice(maxHeuristics)
def main():
grid = np.zeros((3, 3))
availableMoves = [(r, c) for r in range(3) for c in range(3)]
player1 = 1
player2 = 2
win1 = 0
win2 = 0
showGrid = True
noOfMatches = 1
for _ in range(noOfMatches):
grid = np.zeros((3, 3))
availableMoves = [(r, c) for r in range(3) for c in range(3)]
if showGrid:
ttt.showGrid(grid)
while True:
# p1Move = randomPlayer(availableMoves)
# p1Move = oslAgent(grid, availableMoves, player1) # one-step-lookahead agent
# n-step-lookahead agent
# p1Move = nslAgent(5, grid, availableMoves, player1)
p1Move = eval(input('>>Enter your move : '))
availableMoves.pop(availableMoves.index(p1Move))
gameStatus = ttt.gameMove(grid, p1Move, player1, showGrid)
if not gameStatus[0]:
if gameStatus[1] == 1:
win1 += 1
break
# p2Move = oslAgent(grid, availableMoves, player2) # one-step-lookahead agent
# n-step-lookahead agent
p2Move = nslAgent(3, grid, availableMoves, player2)
availableMoves.pop(availableMoves.index(p2Move))
gameStatus = ttt.gameMove(grid, p2Move, player2, showGrid)
if not gameStatus[0]:
if gameStatus[1] == 2:
win2 += 1
break
print(f'>> 1 wins : {win1}, 2 wins : {win2}')
if __name__ == '__main__':
main()