Lab 8: A chess game

The goal of this assignment is to practice writing classes that implement interfaces.

1. Introduction

This project asks you to finish a program that allows two people to play chess on the computer. Most of your work will be in writing code that determines whether or not a move is legal. Most of the necessary information about the game is contained in this project description, but if you're unfamiliar with the rules of chess, you may find a rules of chess reference helpful.

A large portion of the program is already written. Clone the repository from the class directory to get started:

cd csci235
hg clone /cslab/class/csci235/lab8
cd lab8

Open Piece.java in xemacs. The Piece interface is a supertype for classes that represent the various pieces of chess. It declares methods to check what color the piece is, tell the piece what its position on the the board is, make a string representation of it, and determine whether a move is legal for that piece (more on that last part later...). Each kind of piece (King, Queen, Rook, etc) will have its own class that implements Piece. A piece also has a color, which we will indicate using a boolean value (true=black, false=red; red is chosen instead of white because red is slightly easier to see in the GUI).

ChessBoard is a class that represents a board. You will not need to make any changes to this file. In fact, you do not really need to know how it works (though it would be a helpful exercise for you to read it carefully and understand it), only what it does. It is an 8x8 grid that can contain chess pieces. Positions are indicated by row,column pairs. For example, the position in the upper lefthand corner is position 0 0, and the position in the lower righthand corner is position 7 7. You can ask it what piece is at a certain position; you can indicate you want to move a piece from one position to another; you can place a new piece on the board; and you can display it to the screen.

Chess has the main method (though most of the work is in the Chess constructor). It displays a window to the screen which shows the chess board. Pieces are marked by letters indicated what kind of piece it is. When it is a player's turn, he or she can click on a piece he or she wants to move (at which point the space the piece is currently on will turn green) and then click on the space to which the piece should move. If the program determines that such a move is possible, then the piece disappears from the original space and appears in the indicated space. (If the user changes his or her mind before clicking on the destination, clicking on the original space a second time unselects that piece.)

Try out the game as it is so far (you'll see that only queens are implemented, and that incorrectly):

javac *.java
java Chess &

Next open Chess.java in xemacs (if you haven't already); most of it is GUI stuff you can ignore. Note that the constructor sets up the board by calling the populate() method in class ChessSetup. If you look at ChessSetup.java, you'll find that it places the two queens in appropriate places. As you implement other kinds of pieces, you will need to update populate() to place them on the board.

Very important: You will have no reason to modify Chess, ChessBoard or Piece.

Your task is to fix the implementation of Queen and then to implement the other classes.

2. Queen

The problem with Queen is that the method canMoveTo always returns true. Queens can move any number of spaces vertically, horizontally, and diagonally. They may not, however, move through other pieces, and they cannot land on a space occupied by another piece of the same color (that is, we want to prevent a player from capturing his or her own piece by accident). This will be true for other pieces you implement. Rewrite the method so that it calculates if the indicated position is a legal one to move the queen based on the piece's current position.

3. King

Now write a new class King, also implementing Piece. Kings can move one space any direction. (Don't worry about the rule that kings can't move into check; our program will not determine if a king is in check.) Their string representation should be similar to queen, but with "K" instead of "Q".

Modify ChessSetup To place the kings in the correct positions in column 4, next to their queens.

4. Bishop, rook, knight

Now write classes Bishop, Rook, and Knight. Bishops can move any number of spaces diagonally; rooks can move any number of spaces vertically or horizontally; knights can move three spaces in L-shapes (for example, two spaces horizontally, one space vertically). For knights, output "H" for horse, since "K" already stands for kings. Knights, unlike other kinds of pieces, can move through other pieces.

Update ChessSetup. Each side has two of each of these pieces. Check the reference mentioned above if you need to know where to place them.

5. Pawns

Pawns are the most difficult. They must obey the following rules:

Before a pawn becomes a queen, it should print out "P", but afterwards, it should print out "Q".

Don't forget to update ChessSetup. 6. Extra credit

If you have time and would like to earn some extra credit, you may implement one or both of the following features:

7. Turn in

Use a2ps to print all the source (.java files for the classes that you wrote or modified (i.e., not Chess.java or ChessBoard.java).

In the directory where you have your files, run the command

/cslab/class/csci235/bin/handin lab8 .

Thomas VanDrunen, Cary Gray
Last modified: Mon Nov 1 08:17:58 CDT 2010