Project 6: A chess game

The goal of this assignment is to practice writing classes that implement interfaces and to pull out redundant code into abstract classes.

1. Introduction

This project asks you to finish a program that allows two people to play chess on the computer. 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. Download the following files from the class directory:

cp /homeemp/tvandrun/pub/241/Chess.java .
cp /homeemp/tvandrun/pub/241/ChessBoard.java .
cp /homeemp/tvandrun/pub/241/Piece.java .
cp /homeemp/tvandrun/pub/241/Queen.java .

Open these in Xemacs and inspect them. Feel free to discuss this part of the project (ie, how the program in its original form works) with your classmates.

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, Queeen, 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=white, false=black).

ChessBoard is a class that repressents 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 lefthand 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. It sets up a new chess board (in a separate method, setup() and then repeatedly asks a user what move he or she would like to make. The user indicates this by entering the current coordinates of the piece followed by the coordinates of the position the piece should be moved to. For example, if the user wanted to move a piece from position 3, 4 to position 5, 1, he or she should enter

3 4 5 1

If the "from" position holds a piece of that user's color and if the move is legal by the rules of chess, then the board gets updated, and it switches to the other player's turn. Otherwise, the program keeps printing the board unchanged, asking for a move from the same user until a valid move is indicated.

Very important: setup is the only part of Chess that you may modify. You also will have no reason to change ChessBoard or Piece until Part 6

Try it out. Two queens will appear on the board. Move them around. Then move one to capture the other one. It disappears. Notice that the program doesn't exit at this point. It doesn't know how to determine if the game is over, so just hit control-C to end it.

The problem is, only Queen has been implemented, and it's implemented wrongly. Fixing and finishing the program is your task for this project.

2. Queen

The problem with Queen is that the method canMoveTo always returns true. Queens can move any number of spaces vertically, horizonally, and diagonally. 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 the setup method in Chess 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 horizonally, one space vertically). For knights, output "H" for horse, since "K" already stands for kings.

Update setup. 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:

Determining whether or not a pawn is capturing a piece is difficult, so you may ignore that qualification (but see extra credit below). In other words, you can assume a pawn can move either ahead or diagonally any time. However, you should implement the fact that the pawn can move ahead two only on it's first move, and you should make it behave like a queen after it has made it to the other end of the board.

Notice this is more complicated than the earlier pieces. Think about what instance variables you will need to keep track of all this.

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

Don't forget to update setup.

6. Extra things

To receive full credit on this project, implement the following features:

7. Extra credit

To receive extra credit on this project, implement one or both of the following features:

Document any extra credit parts in a way that is readily noticeable.

But... even in the extra credit parts, you may make no changes to Chess.main()--- only Chess.setup() and the classes you write.

8. Turn in

Create the script file as before (cat, rm, compile, and run), showing all the classes that you wrote or modified (so... everyting except ChessBoard.java). Play the game for a while to show that it works.

 > a2ps -P sp (the name of the script file)

(This will print "two up", meaning two pages shown next to each other on one pice of paper. If you use a2ps on a Java file, it will format it nicely like in the handouts I've given in class. The command lpr works similarly except it does no formatting and doesn't print two up by default.)

Then turn in the hard copy by 5:00, Mon, Mar 26.


Thomas VanDrunen
Last modified: Thu Mar 15 13:06:55 CDT 2007