-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlphaBeta.py
43 lines (36 loc) · 1.38 KB
/
AlphaBeta.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
from BoardColor import *
from BoardMoves import *
import random
def alphabeta(position, depth, maxPlayer):
if depth == 0 or position.WinnerSide() is not None:
return position.evaluateScore(), position
if maxPlayer:
maxVal = float('-inf')
allBoards = getAllBoards(position, AI_COLOR_STR)
if not allBoards:
return float('-inf'), position
index = random.randint(0, len(allBoards) - 1)
bestMove = allBoards[index]
for move in allBoards[1:]:
value = alphabeta(position, depth - 1, False)[0]
maxVal = max(value, maxVal)
position.alpha = max(position.alpha, maxVal)
if position.beta <= position.alpha:
bestMove = move
break
return maxVal, bestMove
else: # minPlayer
minVal = float('inf')
allBoards = getAllBoards(position, PLAYER_COLOR_STR)
if not allBoards:
return float('inf'), position
index = random.randint(0, len(allBoards) - 1)
bestMove = allBoards[index]
for move in allBoards[1:]:
value = alphabeta(position, depth - 1, True)[0]
minVal = min(value, minVal)
position.beta = min(position.beta, minVal)
if position.beta <= position.alpha:
bestMove = move
break
return minVal, bestMove