-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathai.py
243 lines (210 loc) · 9.88 KB
/
ai.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
# -*- coding: utf-8 -*-
#EVERYTHING CONCERNING THE AI
import numpy as np
import game
import random
import copy
maxSize = float("inf") ##VALUE OF INFINITY
"""
@desc class of the Teeko AI
@param State $board - actual state of the board game
@param int $IAplayer - value indating whose player's turn it is
"""
class TeekoAI:
def __init__(self, board,IAplayer):
self.board = board
self.IAplayer = IAplayer
"""
@desc function that plays the game in easy mode, making random decisions for the AI.
"""
def playEasy(self):
if self.board.remainingPawns != 0:
while 1:
x = random.randint(0,4)
y = random.randint(0,4)
if self.board.place(int(x),int(y),False):
break
else:
while 1:
x = random.randint(0,4)
y = random.randint(0,4)
a = random.randint(0,4)
b = random.randint(0,4)
if self.board.move(int(x),int(y),int(a),int(b),False):
break
"""
@desc function that plays the game in medium or hard mode, depending on the value of 'mode' given
@param int $depth - depth of the current state in the minimax algorithm
@param int $mode - mode of the game (0 for intermediate, 1 for hard)
@return State $bestState - best next move for the IA to take
"""
def playMediumOrHard(self,depth,mode):
player = self.board.playerPlaying
bestScore = maxSize * -player
tempScore = 0
alpha = -maxSize
beta = maxSize
#Temporary board for testing each child
tempState = copy.deepcopy(self.board)
bestState = None
if self.board.remainingPawns != 0:
for x in range(5):
for y in range(5):
if tempState.place(x,y,False):
if player == -1:
tempScore = self.minimax(mode,tempState,depth-1,alpha,beta,False)
if tempScore <= bestScore:
bestScore = tempScore
bestState = tempState
elif player == 1:
tempScore = self.minimax(mode,tempState,depth-1,alpha,beta,True)
if tempScore >= bestScore:
bestScore = tempScore
bestState = tempState
tempState = copy.deepcopy(self.board)
else:
for x in range(5):
for y in range(5):
adjacents = tempState.getAdjacent(x,y)
for adjacent in adjacents:
if tempState.move(x,y,adjacent[0],adjacent[1],False):
if player == -1:
tempScore = self.minimax(mode,tempState,depth-1,alpha,beta,False)
if tempScore <= bestScore:
bestScore = tempScore
bestState = tempState
elif player == 1:
tempScore = self.minimax(mode,tempState,depth-1,alpha,beta,True)
if tempScore >= bestScore:
bestScore = tempScore
bestState = tempState
tempState = copy.deepcopy(self.board)
self.board = bestState
return bestState
"""
@desc minimax function
@param int $mode - mode of the game (0 for intermediate, 1 for hard)
@param State $childState - state of the board for each child
@param int $depth - depth of the current state in the minimax algorithm
@param int $alpha,beta - value used for the alpha beta pruning
@param bool $isMaximizing - allows the minimax to know when to maximize or minimize
@return int $bestScore - the best score pulled up from the algorithm
"""
def minimax(self,mode,childState,depth,alpha,beta,isMaximizing):
if childState.winner() != 0 or depth ==0:
return self.eval(childState,depth,mode)
if isMaximizing:
bestScore = -maxSize
tempScore = 0
tempState = copy.deepcopy(childState)
if tempState.remainingPawns != 0:
for x in range(5):
for y in range(5):
if tempState.place(x,y,False):
tempScore = self.minimax(mode,tempState,depth-1,alpha,beta,False)
alpha = tempScore
if tempScore >= bestScore:
bestScore = tempScore
if alpha >= beta:
return alpha
tempState = tempState = copy.deepcopy(childState)
else:
for x in range(5):
for y in range(5):
adjacents = tempState.getAdjacent(x,y)
for adjacent in adjacents:
if tempState.move(x,y,adjacent[0],adjacent[1],False):
tempScore = self.minimax(mode,tempState,depth-1,alpha,beta,False)
alpha = tempScore
if tempScore >= bestScore:
bestScore = tempScore
if alpha >= beta:
return alpha
tempState = tempState = copy.deepcopy(childState)
return bestScore
else:
bestScore = maxSize
tempScore = 0
tempState = tempState = copy.deepcopy(childState)
if tempState.remainingPawns != 0:
for x in range(5):
for y in range(5):
if tempState.place(x,y,False):
tempScore = self.minimax(mode,tempState,depth-1,alpha,beta,True)
beta = tempScore
if tempScore <= bestScore:
bestScore = tempScore
if alpha >= beta:
return beta
tempState = tempState = copy.deepcopy(childState)
else:
for x in range(5):
for y in range(5):
adjacents = tempState.getAdjacent(x,y)
for adjacent in adjacents:
if tempState.move(x,y,adjacent[0],adjacent[1],False):
tempScore = self.minimax(mode,tempState,depth-1,alpha,beta,True)
beta = tempScore
if tempScore <= bestScore:
bestScore = tempScore
if alpha >= beta:
return beta
tempState = tempState = copy.deepcopy(childState)
return bestScore
"""
@desc function eval, giving value to the boards when a winning combination is found or when depth has reached 0
@param State $childState - state of the board
@param int $depth - depth of the current state in the minimax algorithm
@param int $mode - mode of the game (0 for intermediate, 1 for hard)
@return int $value - the score of the board
"""
def eval(self,state,depth,mode):
if state.winner()!=0:
return maxSize * state.winner()
else:
value = 0
if mode == 0: ##MEDIUM LEVEL
for x in range(5):
for y in range(5):
if state.board[x][y] != 0:
pawnsWeight = self.stateWeightForPawn(state,x,y)
for a in range(5):
for b in range(5):
if state.board[a][b] != 0:
value = value + state.board[a][b] * pawnsWeight[a][b]
return value
elif mode == 1: ##HARD LEVEL
boardWeights = [
[0, 1, 0, 1, 0],
[1, 2, 2, 2, 1],
[0, 2, 3, 2, 0],
[1, 2, 2, 2, 1],
[0, 1, 0, 1, 0]
]
for x in range(5):
for y in range(5):
if state.board[x][y] != 0:
value = value + state.board[x][y] * boardWeights[x][y]
return value
"""
@desc function creating the weights of the pawn surrounding a chosen pawn
@param State $childState - state of the board
@param int $x,y - coordinates of the chosen pawn
@return array $stateWeight - weights of the emplacements near the pawn
"""
def stateWeightForPawn(self,state,x,y):
stateWeight = [
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]
]
nearPawns = state.getAdjacent(x,y)
for nearPawn in nearPawns:
stateWeight[nearPawn[0]][nearPawn[1]] = 2
remotePawnsCoordinates = [[x-1,y-2],[x+1,y-2],[x-1,y+2],[x+1,y+2],[x-2,y-1],[x-2,y+1],[x+2,y-1],[x+2,y+1]]
for remotePawn in remotePawnsCoordinates:
if remotePawn[0]>=0 and remotePawn[1]>=0 and remotePawn[0]<=4 and remotePawn[1]<=4:
stateWeight[remotePawn[0]][remotePawn[1]] = 1
return stateWeight