CS 1410-20 Lab 9

A chess game can be represented by representing the pieces on the board. Every piece has a color and a position, but different pieces move in different ways.

  1. Define an interface IPiece that includes the operations isColor to determine whether the piece has the given color, atPosition to determine whether the piece is at a given position, and canMoveTo to determine whether the piece can move to a given position (ignoring the possibility that other pieces might be in the way).

    Implement the operations isColor and atPosition in a Piece abstract class, since the implementation will be the same for every chess piece.

  2. Implement Rook and Bishop as classes that extend the Piece class, and implement canMoveTo for each of them. Now that you have classes that you can instantiate, test all of your methods.

    (If you don’t know how a rook or bishop moves, ask a lab instructor.)

  3. Add canCapture to IPiece. The canCapture method takes another IPiece and determines whether the given piece can be captured. To capture a piece, it must be the opposite color of the capturing piece and at a place that the capturing piece can move to.

    You should be able to implement canCapture in Piece, instead of implementing it seperately in Bishop and Rook.

  4. Implement a Pawn class. A pawn is unusual in that it can move a special way on its first move, so a Pawn must record whether it has been moved or not (in addition to its color and position). In addition, canCapture is special for a pawn, because a pawn can capture pieces on squares where the pawn would not otherwise be allowed to move.

    (If you don’t know how a pawn moves or captures, ask a lab instructor.)


Last update: Thursday, November 11th, 2010
mflatt@cs.utah.edu