python-chess icon indicating copy to clipboard operation
python-chess copied to clipboard

[feature request] add `board.san_move_stack` similar to `board.move_stack` to get san format moves

Open HarshilPatel007 opened this issue 1 year ago • 1 comments

Hi @niklasf , This is the same as https://github.com/niklasf/python-chess/issues/1065. I just re-defined the issue to the feature request to simplify it. It'll be good to have a board.san_moves_stack to get the moves in san format just like how we get the moves board.move_stack. It'll allow users to get moves in san format directly without doing any conversion ops. Currently to get the san move, we've to define (get) it before pushing a move to the board. So, having a method to get the moves in san format directly, adds up a great UX/DX imo.

HarshilPatel007 avatar Feb 07 '24 06:02 HarshilPatel007

Maintaining an additional stack like that has significant runtime overhead for everyone who doesn't need it.

But a custom wrapper could always provide both in sync. Something along the lines of:

import chess

class WithSanStack:
    def __init__(self, board):
        self.board = board

        replay = board.root()
        self.san_stack = [replay.san_and_push(m) for m in board.move_stack]

    def push(self, move):
        self.san_stack.append(self.board.san_and_push(move))

    def pop(self):
        self.san_stack.pop()
        return self.board.pop()

The implementation of __init__ also demonstrates how to create such a stack for an existing board. Depending on the use case that could also just be done on-demand, without a wrapper class.

niklasf avatar Feb 07 '24 18:02 niklasf