CSC 270
Homework 5

In this assignment, we'll write the beginnings of a chess program.  In specific, we'll write the part of the program that decides whether a given move is legal or not.

What are the Rules?

For those unfamiliar with the rules of chess, here's a summary of the parts relevant to this program. There are two players, each with six kinds of pieces. No piece can move off the board; that is, the X and Y coordinates of each piece must always be in the range [1...8].
A King
can move one space in any direction: forwards, backwards, left, right, or on any of the four diagonals.
A Queen
can move as far as desired in a straight line in any direction: forwards, backwards, left, right, or on any of the four diagonals. (If there's a piece in the way along a particular straight line, she cannot move past it, but that's not relevant to this assignment.)
A Bishop
can move as far as desired in a straight line along any of the four diagonals, but not forwards, backwards, left, or right. (Again, he cannot move past another piece along the way, but that's not relevant to this assignment.)
A Rook
can move as far as desired in a straight line forwards, backwards, left, or right, but not diagonally. (See above about obstructed paths. There's also a special rule about "castling", which we'll ignore for this assignment.)
A Knight
can move in an L-shape, as though it were going two spaces forwards or backwards and one to either side (or, similarly, two spaces to the side and one space forwards or backwards. For example, if a knight were in the space below marked "K", it could move to any of the spaces marked "x":
               
      x   x    
    x       x  
        K      
    x       x  
      x   x    
               
               
A Pawn
can move one space forwards. (There are also rules about capturing and en passant, which are not relevant to this assignment.) For extra credit, implement the rule that on its first move, a pawn may move one or two spaces forwards; a pawn that has already moved at least once, regardless of whether that move was two spaces, can only move one space forwards.
Note that "forward" means different things for the two players. A "white" pawn can only move from the top of the board (low Y coordinates) towards the bottom of the board (high Y coordinates), while a "black" pawn can only move from the bottom towards the top.

If you want to make the program more interesting, add a class Board which keeps track of what piece is in what location, and have the isLegal method also detect (for Queen, Bishop, and Rook) whether a move is blocked by other pieces.


Last modified: Wed Oct 19 14:43:02 EDT 2005
Stephen Bloch / sbloch@adelphi.edu