Convention for naming locations on the chessboard

In order to assign unique identifiers to every square on the chessboard, we will mark the squares along the x axis by using the letters A to H. We will mark the y axis by using the numbers 1 to 8.

Accordingly, the squares on the chessboard will be identified as follows:

Thus, A1 denotes the leftmost square at the bottom of the chessboard. Currently, it is occupied by a white rook. The C3 position is currently empty, E8 has a black king, and A8 has a black rook.

Let's add this to the configurations.py file (see 4.02):

X_AXIS_LABELS = ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H')
Y_AXIS_LABELS = (1, 2, 3, 4, 5, 6, 7, 8)

Now, if you want to represent the chessboard at any point in time, all you need is a mapping of the location to the chess piece at that location. Looks like a perfect candidate for storing as a Python dictionary.

Thus, the initial position of all the chess pieces on the chessboard can be represented as follows:

START_PIECES_POSITION = {
"A8": "r", "B8": "n", "C8": "b", "b", "G8": "n", "H8": "r",
"A7": "p", "B7": "p", "C7": "p", "p", "G7": "p", "H7": "p",
"A2": "P", "B2": "P", "C2": "P", "P", "G2": "P", "H2": "P",
"A1": "R", "B1": "N", "C1": "B", "D8": "q", "E8": "k", "F8":
"D7": "p", "E7": "p", "F7": "D2": "P", "E2": "P", "F2":
"D1": "Q", "E1": "K", "F1":"B", "G1": "N", "H1": "R"
}

We need this data to get started. So, let's add this as a constant to the configurations.py file (see 4.02).

Now, let's move on to code the Model class for our program. We have already decided that we will use a Python dictionary to store the position of chess pieces on the chessboard. We can go ahead and add a dictionary attribute to the class.

However, we will take a slightly different approach.

Let's make the Model class a child class of the built-in dictionary class, as follows:

class Model(dict): 

Thus, the self variable that refers to the current class object instance will also have all the properties and methods that are available to the dictionary. All the methods that are available to the standard dictionary class can now be called on the Model object (self).

So now we can define a method that returns the short name of the chess piece at that position when it's given a position on the chessboard, as follows (see 4.02model.py):

def get_piece_at(self, position):
return self.get(position)

If there is no chess piece at the position, this returns None rather than giving a KeyError exception.

Next, let's add some more important attributes to the Model class, as follows (see 4.02model.py):

captured_pieces = { 'white': [], 'black': [] }
player_turn = None
halfmove_clock = 0
fullmove_number = 1
history = []

The half-move_clock keeps a track of the number of turns played since the last pawn's advance or the last capture. This is used to determine whether a draw can be claimed under the fifty-move rule.

The full-move number is a count that is incremented by 1 after every move of a black piece. This is used to track the overall length of a game.

Finally, let's add another method that, given the row-column tuple for a square, returns its alphanumeric position (for example, an input of (1, 2) returns B3):

def get_alphanumeric_position(self, rowcol):
if self.is_on_board(rowcol):
row, col = rowcol
return "{}{}".format(X_AXIS_LABELS[col], Y_AXIS_LABELS[row])

Next, let's define an associated helper method to ensure that we only process mouse clicks that occur on the Canvas widget and not anywhere else in the root window, as follows:

def is_on_board(self, rowcol):
row, col = rowcol
return 0 <= row <= 7 and 0 <= col <= 7

There is not much that can be added to the Model class for now until we lay down the code logic to handle the chess pieces.

We can define the rules for all the chess pieces within the Model class, but that would make the Model class too bulky.

Therefore, let's define the chess piece-related logic in a new file named piece.py. Since this is inherently a part of the Model class but it is defined in a new file, let's add a reference to the Model class within this file.
(see 4.02piece.py)

Let's do this next.

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

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