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 pathMinMaxAI.py
More file actions
42 lines (38 loc) · 1.56 KB
/
MinMaxAI.py
File metadata and controls
42 lines (38 loc) · 1.56 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
import MoveUtil
class MinMaxAI:
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):
if node.level == node.maxDepth:
return self.evaluationFunction(node.board)
elif len(node.children) == 0:
(playerScore, opponentScore) = MoveUtil.getScore(node.board, self.color, self.opponentColor, 8)
if playerScore > opponentScore:
return 10000000
else:
return -1000000
moves = [(move, self.minMax(childNode)) for (move, childNode) in node.children]
moveScorePair = None
if node.level % 2 == 0: #max node
moveScorePair = max(moves, key=lambda x: x[1])
else:
moveScorePair = min(moves, key=lambda x: x[1])
if node.level == 0:
return moveScorePair[0]
else:
return moveScorePair[1]
def move(self, board, possibleMoves):
gameTree = MoveUtil.Node(board, self.color, 0, self.numMovesAhead)
chosenMove = self.minMax(gameTree)
MoveUtil.updateBoard(board, possibleMoves, chosenMove, self.color)