CS 173: Homework 1
Chess
Due Sunday, February 17
In this assignment, you are to complete Lab 7 to create an almost
complete game of chess. You should implement the standard pieces, alternate
moves between two players, and capture pieces. You do not have to implement (unless you want to)
the castling move, check and checkmate, pawn promotion, or the pawn en passant.
Rules of the assignment
- All variables should be private.
- All methods should have comments above the definition.
- All variables should have comments next to the variable.
- All classes should have comments above the class.
- All loops and complicated code should have comments.
- All comments should be in JavaDoc style.
Complete Lab 7
If you have not done so, complete lab 7.
Adding opponents
We need to specify a direction for each piece. The direction corresponds
to the side for a piece, or the direction the pawns may move. There are two possible
values for the direction, up or down. As a result, you use either a
boolean, and int, or an enum type to represent the direction.
Modify the ChessPiece class to add an instance variable that stores the
piece's direction, add a direction to the constructor parameters, and add a method
getDirection. You will need to make the appropriate changes to KnightPiece.
You should modify ChessBoard to keep track of the current
direction. Change the actionPerformed method so that only a piece
that matches the current direction can be selected and so that once the
piece is moved, the current direction is changed.
Create the chess pieces
First you are to create the chess pieces. In the lab, you created the abstract
ChessPiece as well as the KnightPiece. The other pieces are:
- KingPiece: A king should be labelled with "K". The king can
move one space in any direction (including diagonal) as long as there is not a piece
of the same color in the terminating space.
- RookPiece: A rook, labelled "R", can move any number of
spaces either horzontally or vertically. There must be no piece on a square between
the rook's starting and terminating spaces, and there must not be a piece of the same
color on the rook's terminating space.
- BishopPiece: A bishop, labelled "B", can move any number of
spaces in a straight diagonal. There must be no piece on a square between the
bishop's starting and terminating spaces, and there must not be a piece of the same
color on the bishop's terminating space.
- QueenPiece: A queen, labelled "Q", can move any number of
spaces in a straight line, either horizontally, vertically, or diagonally. There must
not be a piece on a square between the queen's starting and terminating spaces, and
there must not be a piece of the same color on the queen's terminating space.
- PawnPiece: A pawn, labelled "P", has the following
move rules:
- A pawn may move one space in the proper direction (either up or down) if there is no piece on the
terminating square.
- If this is the first time the pawn is being moved, the pawn may move two spaces in the proper
direction (either up or down) if there is no piece on either the space between the starting and
terminating space or in the terminating space.
- The pawn may move one space diagonally in the proper direction (if the pawn's direction is up, the
pawn may move up and to the left or up and to the right) if there is a piece with a different color
in the terminating space.
Note: as mentioned in the lab, the different chess piece classes should not save any value that is saved
in the ChessPiece class. Instead, if you need access to one of these values you should call the
appropriate method from the parent class.
Play the game
You should create a main method that creates a ChessBoard
and populates it with the appropriate pieces of two different colors, each color going
in opposite directions. If you do not know the appropriate starting positions for the
different pieces, you can do a quick internet search for "chess".