Lab 9: 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. After making a directory for this lab, download the following files from the class directory:

mkdir lab9
cd lab9
cp /homes/tvandrun/Public/cs235/lab9/* .

Open Piece.java in xemacs. The Piece interface is an 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 I found white to be difficult 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 kings are implemented, and that incorrectly):

javac Chess.java
java Chess &

Next open ChessSetup.java in xemacs. (The class Chess.java contains the main method and the GUI stuff. You can look there if you are curious, but you also can safely ignore it. It calls the populate() method of ChessSetup.) The method populate() places pieces on the board before the game begins. You will see that it currently places the two kings in appropriate spaces. This you will need to update as you implement other kinds of pieces.

Very important: This class and the classes that implement Piece are the only things that you may modify. You will have no reason to change Chess, ChessBoard, or Piece.

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

2. King

The problem with King is that the method canMoveTo always returns true. Kings can move one space vertically, horizontally, and diagonally. However, they may not move into 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). Rewrite the method so that it calculates if the indicated position is a legal one to move the king based on the piece's current position. (Don't worry about the rule that kings can't move into check; our program will not determine if a king is in check.)

Compile and test for this and every other stage in this lab.

3. Knight

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. For the upcoming pieces (bishop, rook, and knight), you will need to check that their path does not contain any other piece, since they cannot move through pieces. Knights, however, can move through other pieces, so that's not a concern here. You still need to make sure a knight does not land on a space containing a piece of its own color.

Also, modify ChessStup so that it places knights in the right place. Compile and test.

4. Bishop, rook, queen

Now write classes Bishop, Rook, and Queen. Bishops can move any number of spaces diagonally; rooks can move any number of spaces vertically or horizontally; queens can move any number of spaces either vertically or horizontally. However, none of these pieces may move through other pieces, so you must check that the desired move does that have any other piece on the path.

Update ChessSetup appropriately for each piece.

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

Create the script file as before, except you don't need to run it. Show all the classes that you wrote or modified.

 
a2ps (the name of the script file)

Thomas VanDrunen
Last modified: Thu Oct 22 12:41:54 CDT 2009