CS151 – Fall 2007: Programming Project 7
(last modified 11/30 4:05 PM)

Due: Friday, December 14, 2007, 11:59:59:999 p.m. EST

 

In this project you will revisit the chess piece classes we worked on in Lab 10 as an introduction to inheritance.  You will implement a class for each of the different kinds of chess pieces and a class implements some of the functionality needed to play a game of chess.  One of your goals is to handle as many potential sources of errors as possible using Java exception classes.  Imagine that someone who knows very little about chess may end up playing your game.  To practice reading from and writing to files the game will have functionality allowing its state to be saved and restored.  Another goal is to use inheritance (super and subclasses) to minimize the amount of code duplication -- the enemy of any good implementation.

 

See http://en.wikipedia.org/wiki/Chess, other online resources, me, the TAs, or any other reference source you need to understand the basics of the game. Don't try to implement the class for a piece before you understand what it's allowable moves are.  If you are unsure at any point, please ask.  Don't hesitate to mail questions to the class mailing list (cs151).

 

Joint projects by teams of two people will be allowed.  However, if you choose to work as a team, significantly more work and a higher quality of work will be expected from a team than an individual.  You must decide whether or not you will work in a team by 5 p.m. Friday (11/30).  Please e-mail me the name of your teammate by that time.  Feel free to use the class mailing list (cs151) to recruit a partner.

 

You will create the following classes: ChessGame, ChessPiece, ChessRook, ChessBishop, ChessKing, ChessQueen, ChessKnight, and ChessPawn. 

ChessPiece Superclass Details

The class should have instance variables for the position of the piece, its color, and the game board on which the piece sits (see description of the board in ChessGame).  It should also have constants for the colors BLACK and WHITE.

 

The constructor should take: the starting row and column for the piece, its color, and (a reference/pointer to) a game board.  Each subclass constructor should call this superclass constructor then perform any type specific (Rook, Bishop, etc.) initialization after that.

The class should have a move() method that takes two integer parameters: the row and column to which the piece should be moved.  If the move cannot be completed, the move method should throw an error.  Do not print the error message to the screen; just throw an error with a meaningful message about why the move could not be completed.  The more specific the error message the better.  The extra credit user interface method(s) that play the game are responsible for catching and acting upon the exceptions the pieces throw. 

Unlike the move method we implemented in lab this version not only changes the instance variable(s) that record the piece's position,  it also actually moves the piece to the new position on the board.  Also it isn't necessary for the move method to return a boolean.  Since we throw any errors it can probably just return void.  If the move is valid, the piece should assign itself to the new position in the board array and set its former position to null.  The following rules for moving apply to all pieces and should cause an error to be thrown:

á      No piece may move off the board.

á      No piece may move to a position occupied by a piece of the same color as the piece being moved.  It may move there if the piece is of the other color.  The piece being moved captures and removes the previous piece from the board by occupying the position in the board array.

á      With the exception of a knight/horse, no piece may move through another piece to get to a new position.  In other words, the path the piece traverses from its current position to the new position must be clear of any other pieces.

 

ChessGame Details

The class should have at least two instance variables:

1.  A (non-static) final variable that represents an 8 by 8 array of ChessPieces representing the game board.  If having it final cause problem feel free to make it non-final.

2.  A variable that stores whether it's black or white's turn.

 

It should also have the following methods/constructors:

1.  public void move(int row, int column, int newRow, int newColumn) where row and column are the board coordinates of the piece to move and newRow and newColumn are the position to move the piece to.  It there is no piece to move, the method should throw an error with a descriptive message. 

 

2.  public void save(String filename) where filename is the name of a file to which the current state of the game gets written.  It should record whether it's white or black's turn and the position of each piece on the board.  A simple list of each piece's type, color, and position should suffice, but the format is up to you.  You need not worry about specifying a directory for the file (extra credit if you do).

 

3.  public ChessGame(String filename) where filename is the name of a file from which to read the state of a previously saved game.  Set the contents of the board to be empty, read in the information about each piece from the file, create it, and assign it to the on the board in it's specified location.  You need not worry about specifying a directory for the file (extra credit if you do).

 

4.  public ChessGame() which sets the board up with a full complement of white and black chess pieces in their proper initial positions.

 

Extra Credit Ideas (in no particular order)

á      Enforce the constraint that the color of the piece to be moved matches the color of the player whose turn it is.

á      Allow pawns to move either 1 or 2 spaces on their first move.  After the first move they can only move one space.

á      Enforce the constraints that if a player's king is in check (immediate danger of being captured) the player must, if possible, move the king out of check on their turn.  Also enforce the constraint that a player may not move their king into check.

á      A version of the move method that takes a file name as a parameter.  The method reads the file and executes the series of moves it contains.

á      A toString() method for the ChessGame that returns a nice String representation of the contents of the game board that can be used to display it.

á      The ability to castle which involves moving both the rook and king, provided neither has been moved yet.

á      A main method that creates a new ChessGame object then prompts the user to start a new game or load an existing game.  Allow the players to alternate moves.  Exceptions thrown when an invalid move is made should be caught and printed to the screen.  A player's turn only ends when he or she has made a valid move.

á      Have the error message for each piece indicate its allowable moves on the current board.

Strategy and Hints

Try to put as much common code as possible into the ChessPiece superclass and write as little piece specific code as possible.  Start with ChessRook and ChessBishop since they have simple horizontal and diagonal moves.  Save the pawns for last: they can only move forward vertically to an unoccupied position and can only capture an opponent's piece by moving diagonally forward one square.

 

For Knights and Kings you may find the static distance() method in the Point2D class (in java.awt.geom) useful.  Point2D.distance(x1,y1,x2,y2) returns the distance between (x1,y1) and (x2,y2) as a double.  Instead of checking for 8 different possible moves you can do one check based on the distance of the move.

 

Start early and try to make a little progress each day.  Test early and often.  Sections 2.8, 3.6, 5.5, 6.6--6.7, and 7.8 of your book discuss different aspects of testing.  You can use BlueJ to test constructors and simple methods interactively.  Feel free to use the logging capabilities described in Advanced Topic 5.6, the BlueJ debugger (see Lab 7), etc. to help you find errors in your classes.  Make use of the online documentation, the Olin lab TAs, your fellow classmates (to the extent allowed by the Collaboration Policy), and me early and often.  Avoid the temptation to put off starting the project until a day or two before it's due and expect to get it done.

 

Style and Documentation

All constructors, methods, and public instance variables should be commented using @param and @return tags to facilitate the generation of .html documentation similar to the Java Class Libraries on the Web via the Javadoc utility.  BlueJ automatically generates this documentation for your class when you switch from "Implementation" to "Interface" in the class editor window.  We've discussed the use of these tags in class and additional information is in section 3.3 of your textbook. 

 

Your code should adhere to the stylistic guidelines we have been discussing and which are in Appendix A of your textbook as much as possible.  In particular group different constructors, instance variables, and similar methods together.  Use meaningful variable names and internally comment your code liberally (with // or /*   */ pairs).

 

The graders will be instructed to determine approximately 10-15% of your project grade based on style, readability, and documentation.  Please use indentation, white space, meaningful variable names, and other stylistic techniques to make your code easier to read (and grade).  Define and use public constants instead of using so-called "magic numbers" in your code.

 

Project Submission

You will again submit your projects electronically.  To do so

1.    Connect to the Academics volume of the server called fileserver1.  From a Mac choose "Connect to Server ..." from the "Go" menu in the Finder, type afp://fileserver1 in the dialog box, and select Academics from the volume list.  From Windows machine open Windows Explorer, select "Map Network Drive" from the tools menu, pick a free drive letter and type \\fileserver1\Academics or just type it directly into the address filed.  When prompted for your user name and password, type COLBY\username, where username is your Colby username and enter your password.  These instructions are adapted from http://www.colby.edu/administration_cs/its/support/fileserver1.cfm. 

2.    On the Academics volume inside of the COMP/CS151 folder/directory you will see a folder with your Colby e-mail/login name and within that a private folder.  Copy the BlueJ project folder containing your project 7 solution to your private folder.  Change its name to project7 and delete all .class files and any test classes you created during development. Include a brief readme.txt file that explains whatever you think is necessary, including any extra credit attempted. DO NOT submit a class that doesn't compile!  If you can't get something working comment it out (/*  */) so the grader can possibly give you partial credit for your efforts.  DO NOT submit your project late!  As the syllabus states, late projects are graded at my discretion.

 

Acknowledgements

Thanks to Prof. Dale Skrien for his advice and help developing this project.