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 4.02piece.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 King(Piece):
pass

class Queen(Piece):
pass

class Rook(Piece):
pass

class Bishop(Piece):
pass

class Knight(Piece):
pass

class Pawn(Piece):
pass

Note that the Piece class needs color as an argument for object creation. We create two attributes, named self.name and self.color, in the class.

Also note the keep_reference(self, model) method definition. Since the Piece class is nothing but an extension of the Model class, we need to get a reference to the Model class in this method to communicate with it.

Similarly, the Model class needs a reference to the new Piece class. Accordingly, we add this as an import to the Model class, as follows (see 4.02model.py):

import piece

Finally, we need a method that takes a string pertaining to the name of a given piece object and creates a new piece object. For example, we need a method that, given the arguments (Pawn, black) or simply ("p"), dynamically creates a new Pawn object with the color attribute defined as black.

Accordingly, let's define a helper method in the piece.py file but outside the Piece class, as follows (see 4.02piece.py):

def create_piece (piece, color='white'):
if isinstance(piece, str):
if piece.upper() in SHORT_NAME.keys():
color = "white" if piece.isupper() else "black"
piece = SHORT_NAME[piece.upper()]
piece = piece.capitalize()
if piece in SHORT_NAME.values():
return eval("{classname} (color)".format(classname=piece))
raise exceptions.ChessError("invalid piece name: '{}'".format(piece))

To support the preceding method, add the following constant to the configurations.py file (see 4.02):

SHORT_NAME = {
'R':'Rook', 'N':'Knight', 'B':'Bishop', 'Q':'Queen', 'K':'King', 'P':'Pawn'
}

The preceding code simply takes a single character as an input. It then gets the full name for the corresponding piece class (for example, if a p is given, it gets the full name, which is Pawn). It then checks the case of the character and defines the color variable as white if the input character is uppercase. Otherwise, the color is set to black. It then dynamically creates a corresponding piece object.

This concludes the iteration. We have created the Piece class and all of its subclasses, and we have the ability to create Piece objects dynamically from a given input character. This class is simply an extension of the Model class, and each of the two classes can access each other's methods by keeping a reference to each other.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.212.160