0

i'm trying to create a chess simulator.

consider this scenario: there is a black rook (instance object of Rook class) in square 2B called rook1. there is a white rook in square 2C called rook2.

when the player moves rook1 to square 2C , the i should remove rook2 object from memory completely.

how can i do it?

P.S. i'v already tried del rook2 , but i don't know why it doesn't work.

2
  • 2
    Python has automatic memory management. Generally, you shouldn't have to worry about this unless you are keeping references around to the object in question. Without a minimal reproducible example it is not possible to help you other than to speak in generalities. Commented Mar 4, 2019 at 17:53
  • 1
    We need more information about your code, in order to help you :) Commented Mar 4, 2019 at 17:53

1 Answer 1

4

Trying to remove objects from memory is the wrong way to go. Python offers no option to do that manually, and it would be the wrong operation to perform anyway.

You need to alter whatever data structure represents your chess board so that it represents a game state where there is a black rook at c2 and no piece at b2, rather than a game state where there is a black rook at b2 and a white rook at c2. In a reasonable Python beginner-project implementation of a chess board, this probably means assigning to cells in a list of lists. No objects need to be manually removed from memory to do this.

Having rook1 and rook2 variables referring to your rooks is unnecessary and probably counterproductive.

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

1 Comment

Agreed, your data structure needs a way to represent a piece as taken/removed from the board instead of clearing it from memory.

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.