notnil / chess

chess package for go
MIT License
508 stars 126 forks source link

No method to pop a move from MoveHistory #116

Open alex-hunter3 opened 1 year ago

alex-hunter3 commented 1 year 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
}