CSC 270
Homework 5
In this assignment, we'll write the beginnings of a chess program.
In specific, we'll write the part of the program that decides whether a
given move is legal or not.
- Define a class named BoardLocation, which is
simply two ints in the range from 1 to 8 (an x coordinate and a y coordinate).
- Define a class named ChessPiece, with subclasses
for the different kinds of chess pieces (King, Queen, Knight, Bishop, Rook,
Pawn), and a method named isLegal that takes in two BoardLocations and
determines whether the piece can legally move from the first one to the
second one. At any given time, a piece has a location (of type BoardLocation;
this changes every time the piece moves), and a color (either
white
or black
), which never changes.
You may represent color with a string, or a boolean, or an enum,
or a hierarchy of singleton classes, or however you wish (although I
have my preferences as to which representations are safer or clearer
than others).
-
To avoid spending a lot of time on tedious I/O programming, we won't
have the program read in chess positions or anything like that; we'll simply
have a main program that creates a bunch of ChessPieces (at least
one of each kind) and tests the isLegal method as appropriate on each one.
What are the Rules?
For those unfamiliar with the rules of chess, here's a summary of the
parts relevant to this program. There are two players, each with six
kinds of pieces. No piece can move off the board; that is, the X and Y
coordinates of each piece must always be in the range [1...8].
- A King
- can move one space in any direction: forwards, backwards, left,
right, or on any of the four diagonals.
- A Queen
- can move as far as desired in a straight line in any direction:
forwards, backwards, left, right, or on any of the four diagonals.
(If there's a piece in the way along a particular straight line,
she cannot move past it, but that's not relevant to this assignment.)
- A Bishop
- can move as far as desired in a straight line along any of the four
diagonals, but not forwards, backwards, left, or right.
(Again, he cannot move past another piece along the way, but that's not
relevant to this assignment.)
- A Rook
- can move as far as desired in a straight line forwards, backwards,
left, or right, but not diagonally. (See above about
obstructed paths. There's also a special rule about "castling", which
we'll ignore for this assignment.)
- A Knight
- can move in an L-shape, as though it were going two spaces forwards
or backwards and one to either side (or, similarly, two spaces to the
side and one space forwards or backwards. For example,
if a knight were in the space below marked "K", it could move to any of
the spaces marked "x":
- A Pawn
- can move one space forwards. (There are also rules about capturing
and en passant, which are not relevant to this assignment.)
For extra credit, implement the rule that on its first move, a
pawn may move one or two spaces forwards; a pawn that has
already moved at least once, regardless of whether that move was two
spaces, can only move one space forwards.
Note that "forward" means different things for the two players. A
"white" pawn can only move from the top of the board (low Y coordinates)
towards the bottom of the board (high Y coordinates), while a "black"
pawn can only move from the bottom towards the top.
If you want to make the program more interesting,
add a class Board
which keeps track of what piece is in what
location, and have the isLegal
method also detect (for Queen,
Bishop, and Rook) whether a move is blocked by other pieces.
Last modified:
Wed Oct 19 14:43:02 EDT 2005
Stephen Bloch / sbloch@adelphi.edu