Checks and Pins

Now the game is extended to include two fundamental rules of chess.The king cannot move into check and a piece cannot move if the king is lying behinnd it. To do this we have to know when the king is in check. Knowing this, a whole new world of possibilites opens up. Printing of the check symbol becomes possible.

Checks

The whole board is a matrix and each square is either empty or occupied as defined in the piece class. If the king is in check then there is a piece such that it has a path to the king. For all but knights, the path cannot be blocked. If it is then the king is not in check. Because each piece moves differently we have to search in different ways for their checking possibilities. The bishops can only check along the diagnol, the rooks along rows and columns, and the queen can do either. Pawns can only check along a diagnol that is one square from it. Finally, the knights can check in their normal moving pattern. Starting from the kings square we check in all directions until we reach a piece. If that piece does not have a path to the king, then we discontinue the search in that direction. If a check is found, then it's done. An algorithm for this is both beautiful in logic and full of the most subtle complexities. We search the board for the checking piece, and when it's found the search is ended!

Pins

With the checking algorithm created, checking for a pin is easy. Before the move by the other side is made, we check to see if his king is checked by that. If so, there is a pin or an illegal move is being made. A seperate algorithm for pins is not needed. It's just another way of using the checking algorithm.

start