1

I am writing a small chess GUI with Python chess library. Yesterday, I ran my code a lot of times without any problem. Then I installed some other Python libraries. Today I noticed a strange mistake: board.legel_moves generates wrong moves. I tried to reinstall Python 3.13.1 and the latest Python chess library, the mistake still exists.

The code is very short:

import chess

board = chess.Board()
starting_position=board.fen()

for move in board.legal_moves:
    print(starting_position)
    board.set_fen(starting_position)
    print(move)
    board.push(move)
    print(f' board pushed move {board.fen()}')
    print()

wrong results start from the fifth move h7h6 till the end. It should be h2h3. I just show the first six examples.

rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
g1h3
 board pushed move rnbqkbnr/pppppppp/8/8/8/7N/PPPPPPPP/RNBQKB1R b KQkq - 1 1

rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
g1f3
 board pushed move rnbqkbnr/pppppppp/8/8/8/5N2/PPPPPPPP/RNBQKB1R b KQkq - 1 1

rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
b1c3
 board pushed move rnbqkbnr/pppppppp/8/8/8/2N5/PPPPPPPP/R1BQKBNR b KQkq - 1 1

rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
b1a3
 board pushed move rnbqkbnr/pppppppp/8/8/8/N7/PPPPPPPP/R1BQKBNR b KQkq - 1 1

rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
h7h6
 board pushed move rnbqkbnr/ppppppp1/7P/8/8/8/PPPPPPPP/RNBQKBNR b KQkq - 0 1

rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
g7g6
 board pushed move rnbqkbnr/pppppp1p/6P1/8/8/8/PPPPPPPP/RNBQKBNR b KQkq - 0 1

If I comment out the board push move part board.push(move), everything is normal.

rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
g1h3
 board pushed move rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1

rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
g1f3
 board pushed move rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1

rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
b1c3
 board pushed move rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1

rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
b1a3
 board pushed move rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1

rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
h2h3
 board pushed move rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1

rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
g2g3
 board pushed move rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1

If I comment out board.set_fen(starting_position) and add board.pop() at the end, no mistake.

Can anyone give me some suggestions? Thanks

3
  • I tried older version of Python chess: 1.10.1 or 1.9.4. Same problem still exists. Commented Dec 13, 2024 at 21:14
  • A guy on Github told me to copy board to bypass the problem. It works. Commented Dec 14, 2024 at 10:36
  • Hey welcome to StackOverflow ! If you found the answer to your problem may you post an answer to explain how you achieve it ? Commented Dec 15, 2024 at 11:20

1 Answer 1

1

I don't know why, but the push will affect the board. make a copy of the board, and no problem anymore.

import chess
import copy

board = chess.Board()
starting_position=board.fen()
new_board= copy.copy(board)

for move in board.legal_moves:
    print(starting_position)
    board=copy.copy(new_board)
    board.set_fen(starting_position)
    print(move)
    board.push(move)
    print(f' board pushed move {board.fen()}')
    print()

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.