Closed glennsamuel32 closed 4 years ago
Your PGN format is very strange. The move numbers are (for example, in the original PGN and looking at the bottom of the first section of it) 59. Rf1 Rha8, and then a new section starts as 2. Qd2 Nf4. Strange.
Anyway, you might use the pieces()
method of chess.Board()
to somehow count the pieces on the chessboard. Just note that it takes 2 arguments: piece_type
and color
.
This is actually a very common format...the pgn I posted is from Chessbase... I wish I could translate what you wrote into a py script but I'm not a developer :(:( I appreciate any help on this...
Here you go. An example for using visitors, basically forwarding everything to FileExporter
, unless the limit is hit:
import sys
import chess
import chess.pgn
MIN_PIECES = 8
class MinimumPieceCountExporter(chess.pgn.FileExporter):
def begin_game(self):
self.above = True
def visit_board(self, board):
self.above = chess.popcount(board.occupied) > MIN_PIECES
if self.above:
super().visit_board(board)
def visit_move(self, board, move):
if self.above:
super().visit_move(board, move)
def visit_comment(self, comment):
if self.above:
super().visit_comment(comment)
def visit_nag(self, nag):
if self.above:
super().visit_nag(nag)
def begin_variation(self):
if self.above:
super().begin_variation()
def end_variation(self):
if self.above:
super().end_variation()
def visit_result(self, result):
super().visit_result(result if self.above else "*")
def result(self):
return True
def main(arg):
visitor = MinimumPieceCountExporter(sys.stdout)
with open(arg) as pgn:
while chess.pgn.read_game(pgn, Visitor=lambda: visitor):
pass
# Usage: python script.py in.pgn > out.pgn
if __name__ == "__main__":
for arg in sys.argv[1:]:
main(arg)
Hello, I was wondering if there was a script that can detect the number of pieces on the board (user defined), and then delete all the remaining moves after that.
For example, this is the original pgn...
So if define 8 pieces left, all remaining moves will be deleted and the new pgn will be...
Thanks a lot !!
Regards, Glenn