The rules of chess say that for castling it is imperative that for the king and rook it was the first move. Calculating only one position of a piece to establish exact rules is possible if castling is not enough, you need to at least make a move counter. The display of the figures I have is as follows: I cycle through a two-dimensional array and if we find a match then draw a sprite:

for y in board: for x in y: if x == 'leftRook' or x == 'rightRook': black_rook() 

If the first move is with the King and respectively the first with leftRook or rightRook, then we return True. However, is it possible to avoid such redefinition in the names and leave only Rook, that is, from this:

 board = ['leftRook', ' ', ' ', ' ', 'King ', ' ', ' ', 'rightRook'] 

this:

 board = ['Rook', ' ', ' ', ' ', 'King', ' ', ' ', 'Rook'] 

However, in this case, consider the move, because the name is the same, the class is one, and even if you define the parameter "first_move" and transfer it to __init__ along with the values ​​of "x", "y", then such a construction will work only on one of rooks and once, because the names are the same. How to count for each rook its moves separately in the case when the left and right rooks have the same name?

 class Figure(object): def __init__(self, x, y): self.x = x self.y = y class black_rook(Figure): "" правила хода "" class white_rook(Figure): "" правила хода "" 
  • Calculating only one position of a piece to establish exact rules is possible if castling is not enough, you need to at least make a move counter. I would just add the WasMoved property to the shape. Initially False, for any move = True. - Akina
  • @Akina, I also had this idea. Now the cycle goes through each cell and then draws a sprite. In fact, I do not change the position of the figure, but I make the “initial cell” from where the empty figure started, and the “final”, respectively, I give the name leftRook and it turns out that the cycle re-draws the figure and thus all its properties in init are acquired by the new one. How else to do I do not know yet - This4fun
  • And review your implementation of the board. Particularly interesting in this vein is the space in the string of the king. Use objects (links) instead of strings. - vp_arth
  • one
    Do not forget about the variant when the figure was moving, but at the moment it occupies the same position as at the beginning of the game. Castling in this case is still prohibited. By the way, the alternative is the flag CastlingPossible, which controls all three pieces (or fields) on each move, and is reset on the first change. - Akina
  • @ This4fun, it’s better to keep not the lines on the board, but the references to the objects (figures), where there is no object - there is None . When moving an object (including castling), we simply write this object in a new place, and in the old one write None . A new object is not created. - insolor

2 answers 2

It depends on how you keep the number of moves. If you use a dictionary, then as a key, use an object, not its name.

In general, it is better to store not lines with the names of figures, but objects on the board. You can make a common base class for figures (approximately as shown in your question). All common methods and properties that do not depend on the type of figure should be placed in this class (for example, I would make an empty transfer rule to the base class, and add a method for determining possible moves for the figure, and in each individual figure I would simply change rules of movement, and possible moves would be calculated from the base class method).

You do not need to create separate classes for white and black pieces: you can simply indicate in the object to which player the figure belongs.

When you move around the board, the figure is recorded at the new places, the old place is recorded Null . It is not necessary to create a new object, just the old value is written in a new place. At the same time, the objects do not multiply (for all movements you had 4 copies of the rook at the beginning of the game (2 for each player) and will remain).

Specifically for castling, you can add a flag to the base class, whether the piece has moved since the beginning of the game or not. On this flag and determine one of the conditions for the possibility of castling.