why is my negamax function choosing dumb moves? (Hex chess)

69 Views Asked by At
func GetEvaluation(Turn, NextPositions = Positions):
    var EvaluationTable = {
        White = 0,
        Black = 0
    }
    
    for Piece in NextPositions.Main.values():
        EvaluationTable[Piece.get_parent().get_parent().name] += AIValues[Piece.get_parent().name]
    
    return EvaluationTable[Turn] - EvaluationTable[ColorTable[Turn]]
func Negamax(NextPositions, Depth, Alpha, Beta, Turn, Started = true):
    if Depth == 0 or IsGameOver(NextPositions, Turn):
        return [GetEvaluation(Turn, NextPositions)]
    
    var MaxEvaluation = -INF
    var Data = []
    
    for Key in NextPositions[Turn].keys():
        var PlaceX = get_node("Positions").get_node(Key.left(1))
        var PlaceY = PlaceX.get_node(Key.right(-1))
        var NewPositions = GetPosition(PlaceX, PlaceY, NextPositions)
        
        for Select in NewPositions:
            if Select:
                var CreatePos = CreatePosition(Select, NextPositions[Turn][Key], NextPositions)
                var EvaluationTable = Negamax(CreatePos, Depth - 1, -Alpha, -Beta, ColorTable[Turn], false)
                var Evaluation = -EvaluationTable[0]
                
                if Evaluation >= Beta:
                    return [Evaluation]
                
                if Evaluation > MaxEvaluation:
                    MaxEvaluation = Evaluation
                    Alpha = max(Alpha, Evaluation)
                    
                    if Started:
                        Data = [PlaceX, PlaceY, Select.get_parent(), Select]
    
    return [MaxEvaluation, Data]

so for some reason my negamax function plays stupid moves like sacrificing pieces for no reason for example knight takes a pawn or whatever

I tried using move ordering and a quiesce search, but those don't seem to effect it that much, and the performance goes down the drain too after trying them.

am I doing something wrong in the code? keep in mind this is gdscript if you have any questions lmk

1

There are 1 best solutions below

0
D0RYU On
Negamax(CreatePos, Depth - 1, -Alpha, -Beta, ColorTable[Turn], false)

so I figured this out a while ago, but I was supposed to return -Beta, -Alpha instead of -Alpha, -Beta

I just overlooked the pseudo code I guess