2

I am interested in making a chess algorithm. For this, I will be using the python-chess library. However, to make a good algorithm I need to be able to return the opposing persons legal moves even if it isn't their turn. So in the start of the game it would return

board.legal_moves() -> [A2A4, A2A1, B2B4, B2B1, ect...]

I was wondering if during the start of the game I could say something like

board.enemy_legal_moves() > [A7A5, A7A6, ect...]

or if there is any algorithmic way I could do it. Thank you for your time!

KetZoomer
  • 2,701
  • 3
  • 15
  • 43
  • 4
    "However, to make a good algorithm I need to be able to return the opposing persons legal moves even if it isn't their turn." - no you don't. – user2357112 Feb 20 '21 at 18:03

1 Answers1

1

A solution would be to keep track of the legal moves in a list from the beginning of the game:

legal_moves = []

current_legal_moves = board.legal_moves()
legal_moves.append(current_legal_moves)

Then you can simply get the list of your enemy's legal moves by:

enemy_legal_moves = legal_moves[-1]

This will obviously not work if you want to call this for the first move in the game. If you really need to do that, then you can either:

  1. From the start position switch to black and run the board.legal_moves(). Then take this list and add as start value like this: legal_moves = [black_legal_moves_start_pos]

  2. Write the list of blacks 20 first legal moves from the start position and add it to legal_moves as in option 1 above.

eligolf
  • 1,682
  • 1
  • 6
  • 22