CompSci 251: Piece Positioning
1 Overview
This assignment will add a handful of classes, allowing the board to track the position of chess pieces. A test
driver is supplied which allows two players (on one computer) to take turns moving one of their own pieces
to another place on the board. For the next assignment we will begin implementing chess piece movement
logic so that pieces cannot move arbitrarily around the board (which makes for a very boring game).
2 Implementation Requirements
Tile You will be creating a class called Tile which will represent a position on the board. It should have
a pair of integer variables denoting the rank and fle (row and column, repectively). You should also make
simple accessors for each variable.
Tile
- rank : int
- fle : int
+ Tile(rank : int, fle : int)
+ getRank() : int
+ getFile() : int
+ toString() : String
+ isValid() : boolean
+ equals(other : Object) : boolean
The toString method should return a two-character string in the form A1, B2, : : :, H8. Rember that the
rank of the board is backwards, so B1 is fle and rank (7; 1), and G8 is fle and rank (0; 6).
The isValid method should return true if the tile refers to a square on the board and false otherwise. For
example, the rank and tile (0; 6) is valid while (8; 2) is not.
The equals method should take some object (of any type) as a parameter and return true if the object
represents a logical ly equivalent tile (has the same rank and the same fle). The frst step to determining if
two objedcts represent the same tile is to determine if the unknown object represents some tile (as it could
be an instance of any object).
We can determine if actual type of the object at runtime is a specifc class using the instanceof operator.
Once you’ve determine if a variable is an instance of a particular class, it is safe to cast that variable. If you
perform the cast without the instance-of check, the cast may fail at runtime and crash your program.
if (other instanceof Tile) {
Tile o = (Tile) other;
// "other" is a Tile instance
} else { /* "other" is not a Tile instance */ }
1Assignment 2 - Piece Positing Due 9/28, 2015, 10:00am
Player The Player enumeration is provided for you.
Piece You will also be creating a class called Piece that will replace the strings in your board. A piece is
owned by a single player and has a name. You should create simple accessors for the player variable, and
create a toString method which returns the piece’s name.
Piece
- player : Player
- name : String
+ Piece(player : Player, name : String)
+ getPlayer() : Player
+ toString() : String
Board Finally, you will be modifying some parts of the Board class from the previous assignment. First,
you should change the declaration of the array to hold instances of the Piece class instead of strings. This
will also require you to change the initialize method (be sure to use the same strings from the original board,
but as the value of the piece’s name variable).
Board
- board : Piece[8][8]
+ getPieceAt(tile : Tile) : Piece
+ setPieceAt(tile : Tile, piece : Piece)
+ isOccupied(tile : Tile) : boolean
+ isOccupiedByPlayer(tile : Tile, player : Player) : boolean
+ move(from : Tile, to : Tile) : boolean
You will also be adding a handful of helper methods. Each method should have a very simple implementation.
The isOccupied method should return true if there is a piece on the board at the given tile and false otherwise.
The isOccupiedByPlayer method should return true if there is a piece on the board at the given tile and the
piece on the board at the given tile belongs to the given player, and false otherwise. You should also return
false from either method if the given tile is not valid.
The getPieceAt method should return the piece on the board at the given tile, or nul l if the tile is empty (or
the tile is invalid). The setPieceAt method should add the piece to the board at the given tile. If the tile is
not valid, you should not attemp to do anything.
Finally, the move method will attempt to move the piece at the from tile to the to tile and return true on
success and false otherwise. If this method reutrns false then it is assumed the board has not changed. This
method should return false if either tile is invalid. Remember that you are moving a piece and not copying
a piece from one tile to another, so the from tile should be unoccupied after a successful move.
Many of the board methods share some similar functionality. Instead of duplicating logic in two methods, you
should call one method from another where appropriate (for example, the move method could call setPieceAt
instead of manipulating the board directly).
TextDriver A text driver is supplied for you, but will not compile until you add the necessary methods
described above.
Page 2Assignment 2 - Piece Positing Due 9/28, 2015, 10:00am
3 Solution Complexity
Listed below are some number-of-line trivia regarding the solution. You may use this as a guide (to de-
termine if your solution is far too verbose, or far too trivial), but realize that many equivalent solutions
will have difering line counts. Starting from the solution for Assignment 1, 19 signifcant (non-blank and
non-comment) lines of code must be modifed and 110 signifcant lines must be added to get to my solution
for Assignment 2 (your solution may require fewer or greater number of lines).
File Blank Comment Code
Board.java 32 56 106
Tile.java 7 14 26
Piece.java 3 4 14
Total 55 82 198
4 Submission
Submit all fve Java fles (including those supplied) in a zip fle to the D2L dropbox before the posted
deadline.
Page 3