This repository was archived by the owner on Aug 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinMaxPrunedAI.py
More file actions
59 lines (53 loc) · 2.39 KB
/
MinMaxPrunedAI.py
File metadata and controls
59 lines (53 loc) · 2.39 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
import MoveUtil
class MinMaxPrunedAI:
def __init__(self, color, numMovesAhead):
self.color = color
self.numMovesAhead = numMovesAhead
self.opponentColor = 'W' if self.color == 'B' else 'B'
self.pointMatrix = MoveUtil.getPointMatrix()
def evaluationFunction(self, board):
score = 0
opponentScore = 0
for i in range(len(board)):
for j in range(len(board[i])):
if board[i][j] == self.color:
score += self.pointMatrix[i][j]
return score
def minMax(self, node, alpha, beta):
if node.level == node.maxDepth:
score = self.evaluationFunction(node.board)
return (None, (score, score))
elif len(node.children) == 0:
(playerScore, opponentScore) = MoveUtil.getScore(node.board, self.color, self.opponentColor, 8)
if playerScore > opponentScore:
return (None, (10000000, 10000000))
else:
return (None, (-1000000, -1000000))
moveToTake = None
maxTempAlpha = alpha
minTempBeta = beta
for (move, childNode) in node.children:
if node.level % 2 == 0:
tempAlpha = self.minMax(childNode, alpha, beta)[1][1]
if tempAlpha > minTempBeta: # pruning
return (moveToTake, (alpha, beta))
if tempAlpha > maxTempAlpha:
maxTempAlpha = tempAlpha
moveToTake = move
else:
tempBeta = self.minMax(childNode, alpha, beta)[1][0]
if tempBeta < maxTempAlpha: # pruning
return (moveToTake, (alpha, beta))
if tempBeta < minTempBeta:
minTempBeta = tempBeta
moveToTake = move
if maxTempAlpha == minTempBeta:
return (moveToTake, (maxTempAlpha, minTempbeta)) if node.level != 0 else moveToTake
if node.level == 0:
return moveToTake
else:
return (moveToTake, (maxTempAlpha, minTempBeta))
def move(self, board, possibleMoves):
gameTree = MoveUtil.Node(board, self.color, 0, self.numMovesAhead)
chosenMove = self.minMax(gameTree, float("-inf"), float("inf"))
MoveUtil.updateBoard(board, possibleMoves, chosenMove, self.color)