The chessboard logic

Before we allow chess pieces to move on click of the mouse button, we must have a record of all possible movement options on the board. At every move, we also need to check that it is a legitimate turn for a given player, and that the proposed move should not cause a check on the king.

Now a check may occur on the king, not only from a piece that was moved, but from any other piece on the board as a consequence of such movement. Thus, after every move, we need to calculate the possible moves for all the pieces of the opponent.

Accordingly we will need two methods to:

  • Keep track of all available moves for a player
  • Verify if there is a check on the king

Let's add the code for the preceding methods into our Board class. (See code 4.05: chessboard.py)

Engage Thrusters

Step 1: Tracking all Available Moves

The code for keeping track of all available moves for a player is as follows:

def all_moves_available(self, color):
   result = []
   for coord in self.keys():
     if (self[coord] is not None) and self[coord].color == color:
        moves = self[coord].moves_available(coord)
        if moves: result += moves
   return result

The description of the code is listed as follows:

  • We have already coded our moves_available method in the previous iteration. This method simply iterates through every item in the dictionary and appends the moves_available result for each piece of a given color in a list named result.

Step 2: Getting Current Position of King

Before we code the method to verify if a king is in check, we first need to know the exact position of the king. Let's define a method to get the current position of the king, as follows (see code 4.05: chessboard.py):

def position_of_king(self, color):
   for pos in self.keys():
     if is instance(self[pos], pieces.King) and self[pos].color == color:
        return pos

The preceding code simply iterates through all items in the dictionary. If a given position is an instance of the King class, it simply returns its position.

Step 3: Verifying if King is under Check

Finally, we define a method to verify if the king is under check from the opponent as follows:

def king_in_check(self, color):
   kingpos =  self.position_of_king(color)
   opponent = ('black' if color =='white' else 'white')
   for pieces in self.iteritems():
     if kingpos in self.all_moves_available(opponent):
        return True
     else:
        return False

The description of the code is listed as follows:

  • We first obtain the current position of the king, and the color of the opponent.
  • We then iterate through all possible moves for all pieces of the opponent. If the position of the king coincides with any position from all possible moves, the king is under check, and we return True, else we return False.

Objective Complete – Mini Debriefing

This completes our objectives for the iteration. We are now in a position to check for all available moves for a player at a given point in the game. We can also verify if a king is under check from the opponent team.

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

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