U06 HW – Chess Pieces
Given an abstract class of ChessPiece, implement concrete subclasses of Pawn, Knight, Bishop, Rook, Queen and King
which have their own getMoves() method to reflect the different ways in which the pieces move. The following
descriptions are taken from http:
www.chesscoachonline.com/chess-articles/chess-rules
The initial position setup
The chessboard is made up of eight rows and eight columns for a total of 64 squares of alternating colors. Each
square of the chessboard is identified with a unique pair of a letter and a number. The vertical files are labeled a
through h, from White´s left (i.e. the queenside) to White´s right. Similarly, the horizontal ranks are numbered from 1
to 8, starting from the one nearest White´s side of the board. Each square of the board, then, is uniquely identified
y its file letter and rank number.
In the initial position setup, the light queen is positioned on a light square and the dark queen is situated on a dark
square.
The diagram below shows how the pieces should be initially situated.
Chess moves
• King can move exactly one square horizontally, vertically, or diagonally.
• Queen can move any number of vacant squares diagonally, horizontally, or vertically.
• Rook can move any number of vacant squares vertically or horizontally. It also is moved while
castling.
• Bishop can move any number of vacant squares in any diagonal direction.
• Knight can move one square along any rank or file and then at an angle. The knight´s movement
can also be viewed as an “L” or “7″ laid out at any horizontal or vertical angle.
• Pawns can move forward one square, if that square is unoccupied. If it has not yet moved, the pawn
has the option of moving two squares forward provided both squares in front of the pawn are
unoccupied. A pawn cannot move backward. Pawns are the only pieces that capture differently
from how they move. They can capture an enemy piece on either of the two spaces adjacent to the
space in front of them (i.e., the two squares diagonally in front of them) but cannot move to these
spaces if they are vacant.
http:
www.chesscoachonline.com/chess-articles/chess-rules
For our purposes we will treat capturing the same as moving for pawns. The white pawns only move towards the top of
the board while the black pawns move towards bottom. The two square advance rules apply only to white pawns on
the second rank and black pawns on the seventh rank.
We will use alge
aic notation which uses the form of column (file) a-h followed by row (rank XXXXXXXXXXTo help you to
convert back and forth there is a static method provided called getSquare() which converts a given file and rank to its
character representation. It will be easier to work with the rank and file as distinct integer values and convert back at
the end to alge
aic.
Each kind of piece has value:
Pawn 1
Knight 3
Bishop 3.5
Rook 5
Queen 9
King (Infinite) – but 1000 for our purposes
Write the classes for Pawn, Knight, Bishop, Rook, Queen and King and implement the appropriate constructors for them
as well as implementing the getMoves() method. getMoves() should return a string a
ay of all the legal moves of a
piece expressed alge
aically. The list ignores if the square is obstructed or occupied by another piece.
In addition, implement the Comparable interface for the ChessPiece using the value field to order them.
Grading Criteria:
Pawn class 0.75
Knight class 0.75
Bishop class 0.75
Rook class 0.75
Queen class 0.75
King class 0.75
Implement
Comparable interface
1.5
Documentation 2
Test code:
import java.util.A
ays;
public class ChessTest {
public static void main(String s[]) {
XXXXXXXXXXBishop bishop = new Bishop(ChessPiece.Player.WHITE, "a2");
XXXXXXXXXXSystem.out.println(bishop);
XXXXXXXXXXSystem.out.printf("Bishop Moves from %s: %s\n",
ishop.getLocation(),A
ays.toString( bishop.getMoves()));
XXXXXXXXXXChessPiece [] testSet = {
XXXXXXXXXXnew Bishop(ChessPiece.Player.BLACK,"d4"),
XXXXXXXXXXnew Pawn(ChessPiece.Player.WHITE,"e2"),
XXXXXXXXXXnew Pawn(ChessPiece.Player.WHITE,"f4"),
XXXXXXXXXXnew Pawn(ChessPiece.Player.BLACK,"h7"),
XXXXXXXXXXnew Pawn(ChessPiece.Player.BLACK,"d5"),
XXXXXXXXXXnew King(ChessPiece.Player.BLACK,"d6"),
XXXXXXXXXXnew King(ChessPiece.Player.WHITE,"e1"),
XXXXXXXXXXnew Knight(ChessPiece.Player.WHITE,"a1"),
XXXXXXXXXXnew Knight(ChessPiece.Player.BLACK,"d4"),
XXXXXXXXXXnew Queen(ChessPiece.Player.WHITE,"c4"),
XXXXXXXXXXnew Queen(ChessPiece.Player.BLACK,"b7"),
XXXXXXXXXXnew Rook(ChessPiece.Player.BLACK,"g3"),
XXXXXXXXXXnew Rook(ChessPiece.Player.WHITE,"a1")
};
XXXXXXXXXXdouble sum=0;
XXXXXXXXXXfor (ChessPiece piece:testSet) {
XXXXXXXXXXSystem.out.printf(" %s moves %s\n", piece,A
ays.toString(piece.getMoves()));
XXXXXXXXXXif (piece.getPlayer()== ChessPiece.Player.BLACK)
XXXXXXXXXXsum+=piece.getValue();
}
XXXXXXXXXXSystem.out.printf("The sum of the value of the Black pieces is %.1f\n",sum);
}
}
Bishop[WHITE] @ a2
Bishop Moves from a2: [b1, b3, c4, d5, e6, f7, g8]
Bishop[BLACK] @ d4 moves [a1, a7, b2, b6, c3, c5, e3, e5, f2, f6, g1, g7, h8]
Pawn[WHITE] @ e2 moves [d3, e3, e4, f3]
Pawn[WHITE] @ f4 moves [e5, f5, g5]
Pawn[BLACK] @ h7 moves [g6, h5, h6]
Pawn[BLACK] @ d5 moves [c4, d4, e4]
King[BLACK] @ d6 moves [c5, c6, c7, d5, d7, e5, e6, e7]
King[WHITE] @ e1 moves [d1, d2, e2, f1, f2]
Knight[WHITE] @ a1 moves [b3, c2]
Knight[BLACK] @ d4 moves [b3, b5, c2, c6, e2, e6, f3, f5]
Queen[WHITE] @ c4 moves [a2, a4, a6, b3, b4, b5, c1, c2, c3, c5, c6, c7, c8, d3, d4, d5, e2, e4,
e6, f1, f4, f7, g4, g8, h4]
Queen[BLACK] @ b7 moves [a6, a7, a8, b1, b2, b3, b4, b5, b6, b8, c6, c7, c8, d5, d7, e4, e7, f3,
f7, g2, g7, h1, h7]
Rook[BLACK] @ g3 moves [a3, b3, c3, d3, e3, f3, g1, g2, g4, g5, g6, g7, g8, h3]
Rook[WHITE] @ a1 moves [a2, a3, a4, a5, a6, a7, a8, b1, c1, d1, e1, f1, g1, h1]
The sum of the value of the Black pieces is 1022.5