Creating a Piece class
Think about it. We need to define rules for all the different chess pieces. Some attributes and methods such as color will be common to all the chess pieces, while other attributes/methods such as rules for movement will vary for each chess piece.
First, we'll define a new Piece
class. This class will have the attributes and methods that are common to all the chess pieces. Then, we will define classes for every individual piece as a subclass of this parent Piece
class. We can then override all the attributes and methods in these individual classes.
The code will look like this (see code 4.02
– piece.py
):
from configurations import * class Piece(): def __init__(self, color): self.name = self.__class__.__name__.lower() if color == 'black': self.name = self.name.lower() elif color == 'white': self.name = self.name.upper() self.color = color def keep_reference(self, model): self.model = model class...