Making the game functional

Now that we have all the chess pieces and chessboard-related validation rules in place, let's add life to our chess program. In this iteration, we will make our chess game fully functional.

The objective of this iteration is to move the chess pieces with a click of the left mouse button. When a player clicks on a chess piece, the code should first check whether it is a legitimate turn for that chess piece.

On the first click, the chess piece that needs to be moved is selected, and all the allowed moves for that chess piece are highlighted on the chessboard. The second click should be performed on the destination square. If the second click is done on a valid destination square, the chess piece should move from the source square to the destination square.

We also need to code the events of capturing chess pieces and the king being in check. The other attributes that need to be tracked include a list of the captured chess pieces, the half-move clock count, the full-move number count, and the history of all the previous moves.

You may recall that we created a dummy method which is bound to the left-click event. The method, for now, simply prints the row and column value on the console.

Let's modify this method, as follows (see 4.06view.py):

def on_square_clicked(self, event):
clicked_row, clicked_column = self.get_clicked_row_column(event)
position_of_click = self.controller.get_alphanumeric_position
((clicked_row, clicked_column))
if self.selected_piece_position: # on second click
self.shift(self.selected_piece_position, position_of_click)
self.selected_piece_position = None
self.update_highlight_list(position_of_click)
self.draw_board()
self.draw_all_pieces()

The following is a description of the preceding code:

  • The first part of the code calculates the coordinates for the chess piece on which you clicked. Based on the calculated coordinates, it stores the corresponding letter notation in a variable named position_of_click.
  • It then tries to assign the piece variable to the corresponding piece instance. If there is no piece instance on the clicked square, it simply ignores the click.
  • The second part of the method checks whether this is the second click that was intended to move a chess piece to a destination square. If this is the second click, it calls the shift method, passing the source and destination coordinates as its two arguments.
  • If the shift method succeeds, it sets all the previously set attributes to their original empty values and calls the draw_board and draw_pieces methods to redraw the chessboard and chess pieces.

While coding the desired functionality for the on_square_clicked method, we called several new methods from within it. We need to define these new methods.

Keep an eye on the on_square_clicked method. This is the central method around which all the other methods will evolve over the course of our attempts to make the chess game functional.

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

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