notnil / chess

chess package for go
MIT License
521 stars 135 forks source link

No method to pop a move from MoveHistory #116

Closed alex-hunter3 closed 5 days ago

alex-hunter3 commented 2 years ago

I'm trying to implement a bot with this library powering the game. However, instead of deep copying the board while constructing the tree there should be a method to pop the last move off the MoveHistory as it would be a lot faster than copying the board for each node of the minimax tree.

Is there a function that exists currently?

LouieMartin commented 1 year ago

This is how I do it:

func negamax(depth int, alpha float64, beta float64, position *chess.Position) float64 {
    depth = int(math.Max(float64(depth), 0))

    if depth == 0 {
        return evaluate(position)
    }

    moves := position.ValidMoves()

    for _, move := range moves {
        new_position := position.Update(move)
        evaluation := -negamax(
            depth-1,
            -beta,
            -alpha,
            new_position,
        )

        if evaluation >= beta {
            return beta
        }

        alpha = math.Max(evaluation, alpha)
    }

    return alpha
}
notnil commented 5 days ago

I think this is what you are looking for: https://pkg.go.dev/github.com/notnil/chess#Position.Update

It skips redundant move validation and I follow a similar pattern to @LouieMartin