0

I'm trying to make a chess engine.

def botmove(board):
  legal=list(board.legal_moves)#needed because --->TypeError: 'LegalMoveGenerator' object is not subscriptable
  boardhalfmove=board.pop()
  choosemove(board,boardhalfmove, legal)
  print("Computer moves:",move)
  movepiece(move,board)

However, I ran into an error where:

Traceback (most recent call last):
  File "main.py", line 32, in <module>
    botmove(board)
  File "/home/runner/chess-engine-goal-3minimax/move.py", line 70, in botmove
    choosemove(board,boardhalfmove, legal)
  File "/home/runner/chess-engine-goal-3minimax/move.py", line 49, in choosemove
    movepiece(str(i),board)
  File "/home/runner/chess-engine-goal-3minimax/move.py", line 4, in movepiece
    moveinput = str(board.push_san(movestr))
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/chess/__init__.py", line 3003, in push_san
    move = self.parse_san(san)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/chess/__init__.py", line 2970, in parse_san
    move = self.find_move(square(from_file, from_rank), to_square, promotion)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/chess/__init__.py", line 2272, in find_move
    raise ValueError(f"no matching legal move for {move.uci()} ({SQUARE_NAMES[from_square]} -> {SQUARE_NAMES[to_square]}) in {self.fen()}")
ValueError: no matching legal move for g8h6 (g8 -> h6) in rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1

That isn't good,especially the legal move part. I traced back to the source of the error:

boardhalfmove=board.pop()

I know about Python's quirky mutability, and though I know I can copy a list like this:

secondlist=list[:]#makes a copy

type(board) returns<class 'chess.Board'>.
How to I make a full copy of board?
Thanks in advance.

NRO
  • 76
  • 7
  • Have you already try to read this https://docs.python.org/3/library/copy.html or https://stackoverflow.com/questions/48338847/how-to-copy-a-class-instance-in-python ? – Elidor00 Jan 08 '21 at 23:49

1 Answers1

1

Generally, Python has a library named 'copy' which implements deepcopy of the basic objects. Try to use from copy import deepcopy and override the __eq__ function of Board.

For more information, take a look at How can I create a copy of an object in Python?

Noga K
  • 398
  • 1
  • 8