Hey I am trying to implement negamax with alpha-beta and quiesce search with 4 players but it is a little different. For example, lets say 0 = Red
, 1 = Blue
, 2 = Yellow
, and 3 = Green
, and Red and Green are on the same team meaning 2 turns will be the maximizer and 2 turns will be the minimizer. Here is my implementation based on the wiki.
def search(self, position, alpha, beta, depth, root=True):
self.nodes += 1
best_move = []
if position in self.history and not root: return 0 # prevent a three-fold repetition moves when the engine is winning.
bestscore = -999999
if depth == 0 or position.is_final: return self.quiesce(position, alpha, beta)
for move in position.moves():
if position.turn in (0, 2): score = -self.search(position.move(move), -beta, -alpha, depth - 1, False)
else: score = self.search(position.move(move), alpha, beta, depth - 1, False)
if score >= beta: return score # fail-soft beta-cutoff
if score > bestscore:
bestscore = score
if root: best_move = move
if score > alpha: alpha = score
if not root: return bestscore
else: return best_move
def quiesce(self, position, alpha, beta):
if position.is_final: return position.score + position.value() # this position is final so just return the evaluation.
stand_pat = position.score + position.value()
if stand_pat >= beta: return beta
if alpha < stand_pat: alpha = stand_pat
for move in position.moves():
if move[2] != 1: continue # continue through the moves until we encounter another capture.
if position.turn in (0, 2): score = -self.quiesce(position.move(move), -beta, -alpha)
else: score = self.quiesce(position.move(move), alpha, beta)
if score >= beta: return beta
if score > alpha: alpha = score
return alpha
This seems to not be working properly, I figured if the next player to move is on my team we should not switch alpha and beta and just not reverse the value. Any suggestion on anything I am doing wrong.