Class ChessSquare
- java.lang.Object
-
- ChessSquare
-
public class ChessSquare extends Object
A class that represents a square on a chessboard. A square has a coordinate and possibly a chesspiece.See the test web page for a description of how the coordinates of a square on a chessboard is defined, as well as the letters used to represent the different chess pieces.
-
-
Constructor Summary
Constructors Constructor Description ChessSquare()
Initializes this square to have the coordinatesa1
and to have no piece occupying the square.ChessSquare(ChessSquare other)
Initializes this square by copying the coordinates and the occupying piece from another square.ChessSquare(String file, String rank, String piece)
Initializes this square to have the coordinates given by the specified file and rank, and to be occupied by the specified piece.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
areConnected(ChessSquare other)
Returns true if and only if this square is connected to the specified square by a straight line path.boolean
equals(Object obj)
Compares this square to an object for equality.String
file()
Returns the file of this square as a string.int
hashCode()
Returns a hash code for this square.boolean
isAdjacentTo(ChessSquare other)
Returns true if and only if this square is adjacent to the specified square.String
piece()
Returns the piece occupying this square.String
rank()
Returns the rank of this square as a string.
-
-
-
Constructor Detail
-
ChessSquare
public ChessSquare()
Initializes this square to have the coordinatesa1
and to have no piece occupying the square.
-
ChessSquare
public ChessSquare(String file, String rank, String piece)
Initializes this square to have the coordinates given by the specified file and rank, and to be occupied by the specified piece.If
piece
is the empty string then no piece is on the square.- Parameters:
file
- the file of the squarerank
- the rank of the squarepiece
- the piece on the square- Throws:
IllegalArgumentException
- if file or rank are not strings of length 1IllegalArgumentException
- if piece is not empty and not a string of length 1IllegalArgumentException
- if file is not a valid fileIllegalArgumentException
- if rank is not a valid rankIllegalArgumentException
- if piece is not a valid piece
-
ChessSquare
public ChessSquare(ChessSquare other)
Initializes this square by copying the coordinates and the occupying piece from another square.- Parameters:
other
- the object to copy
-
-
Method Detail
-
file
public String file()
Returns the file of this square as a string. The returned string is a string of length 1 and is equal to a lowercase letter between a and h, inclusive.- Returns:
- the file of this square
-
rank
public String rank()
Returns the rank of this square as a string. The returned string is a string of length 1 and is equal to a digit between 1 and 8, inclusive.- Returns:
- the rank of this square
-
piece
public String piece()
Returns the piece occupying this square. The returned string is a string of length 1 and is equal to one of "K", "Q", "R", "B", "N", "P", or the empty string. The empty string is used to indicate no piece is on the square.- Returns:
- the piece occupying this square
-
isAdjacentTo
public boolean isAdjacentTo(ChessSquare other)
Returns true if and only if this square is adjacent to the specified square. Two squares are adjacent if they are within one square of each other horizontally, vertically, or diagonally.- Parameters:
other
- the square to compare- Returns:
- true if and only if this square is adjacent to the specified square
-
areConnected
public boolean areConnected(ChessSquare other)
Returns true if and only if this square is connected to the specified square by a straight line path. A straight line path is a linear path that is either horizontal, vertical, or on a +/- 45 degree diagonal.- Parameters:
other
- the square to check- Returns:
- true if and only if this square is connected to the specified square by a straight line path
-
equals
public boolean equals(Object obj)
Compares this square to an object for equality. Returns true if and only ifobj
is aChessSquare
instance having the same rank and file as this square. The occupying piece is not considered in this method.
-
-