forked from humbhenri/pyOthello
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminimax.py
30 lines (25 loc) · 1.11 KB
/
minimax.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
# Minimax module
# Implements a generic minimax with alpha-beta prunning algorithm for games like chess or reversi.
# Thu Feb 18 21:54:38 Humberto Pinheiro
INFINITY = 100000
class Minimax( object ):
def __init__( self, heuristic_eval ):
""" Create a new minimax object
player - current player's color
opponent - opponent's color
"""
self.heuristic_eval = heuristic_eval
# error: always return the same board in same cases
def minimax( self, board, parentBoard, depth, player, opponent, alfa=-INFINITY, beta=INFINITY):
bestChild = board
if depth == 0:
return (self.heuristic_eval( parentBoard, board, depth, player, opponent ), board)
for child in board.next_states( player ):
score,newChild = self.minimax( child, board, depth-1,opponent, player, -beta, -alfa)
score = -score
if score > alfa:
alfa = score
bestChild = child
if beta <= alfa:
break
return (self.heuristic_eval( board, board, depth, player, opponent), bestChild)