Internet Chess ToolKit
v0.2.0

ictk.boardgame.chess
Class ChessBoard

java.lang.Object
  extended byictk.boardgame.chess.ChessBoard
All Implemented Interfaces:
Board

public class ChessBoard
extends java.lang.Object
implements Board

This is the playing board. It is a matrix of Squares with Pieces on those Squares. The Pieces themselves determine their legal moves. ChessBoard facilitates this but does not assess move legality by itself. Whenever specifying coordinates on a ChessBoard it should be done by using the range 1-MAX_FILE, or 1-8 for normal chess.


Field Summary
static long DEBUG
          used as a mask for Log.debug()
static char[][] DEFAULT_POSITION
          the default setup of pieces in a traditional chess board
static byte MAX_FILE
          the max number of files on a traditional chess board
static byte MAX_RANK
          the max number of ranks on a traditional chess board
static byte NO_ENPASSANT
          the file that indicates no file is specified
static byte NULL_FILE
          the file that indicates no file is specified
static byte NULL_RANK
          the file that indicates no file is specified
 
Constructor Summary
ChessBoard()
          calls ChessBoard(true)
ChessBoard(boolean defaultBoard)
           
ChessBoard(char[][] matrix)
           
ChessBoard(char[][] matrix, boolean isBlackMove, boolean castleWK, boolean castleWQ, boolean castleBK, boolean castleBQ, char enpassantFile, int plyCount, int moveNum)
           
 
Method Summary
 void addBishop(int file, int rank, boolean isBlack)
          adds a Bishop to a Square on the board and to the team of pieces of the correct color.
 void addBoardListener(BoardListener bl)
          add a listener for board updates
 void addKing(int file, int rank, boolean isBlack)
          adds a King to a Square on the board and to the team of pieces of the correct color.
 void addKnight(int file, int rank, boolean isBlack)
          adds a Knight to a Square on the board and to the team of pieces of the correct color.
 void addPawn(int file, int rank, boolean isBlack)
          adds a Pawn to a Square on the board and to the team of pieces of the correct color.
 void addQueen(int file, int rank, boolean isBlack)
          adds a Queen to a Square on the board and to the team of pieces of the correct color.
 void addRook(int file, int rank, boolean isBlack)
          adds a Rook to a Square on the board and to the team of pieces of the correct color.
 java.lang.String dump()
          full dump of everything
 java.lang.String dumpLegalMoves()
           
 java.lang.String dumpLegalMoves(boolean blacksMoves)
           
 java.lang.String dumpOpposingMoves()
           
 boolean equals(java.lang.Object o)
          standard override of the equals(Object) function.
 void fireBoardEvent(int event)
          this is used by controllers (such as History) to fire board events to BoardListeners.
 int get50MoveRulePlyCount()
          returns how many ply (half-moves) the board is into the 50 move rule.
 BoardListener[] getBoardListeners()
           
 ChessPiece[] getCapturedPieces(boolean isBlack)
          get the pieces that have been captured.
 int getCurrentMoveNumber()
          get move number for the last move executed
 byte getEnPassantFile()
          returns the file index (1-8) of the file where enpassant is legal
 ChessPiece[] getGuards(ChessPiece piece)
          returns an array of the pieces of the team specified who can guard the square (a friendly piece) specified.
 ChessPiece[] getGuards(Square sq, boolean isBlack)
          returns an array of the pieces of the team specified who can guard the square (a friendly piece) specified.
 int getLegalMoveCount()
          how many legal moves on the current board.
 java.util.List getLegalMoves()
          returns a list of moves that are legal on the current board.
 int getMaterialCount(boolean isBlack)
          gets the material count evaluation for the side specified.
 int getMaxFile()
          what is the maxium file allowed.
 int getMaxRank()
          what is the maxium rank allowed.
 Square getOrigin(byte piece_index, int file, int rank, Square dest)
          determines where the origin of the move that goes to this destination is.
 Square getOrigin(byte piece_index, Square dest)
          determines where the origin of the move that goes to this destination is.
 int getPlayerToMove()
           
 Square getSquare(char file, char rank)
          cx and cy should subscribe to the default locale of boardgame.chess.io.SAN.
 Square getSquare(int x, int y)
          x and y should be in the range 1-8
 ChessPiece[] getThreats(ChessPiece piece)
          return the ChessPieces that threaten to take this piece.
 ChessPiece[] getThreats(Square sq, boolean isBlack)
          returns an array of the pieces of the team specified who can attack the square specified.
 ChessPiece[] getUnCapturedPieces(boolean isBlack)
          get the pieces that haven't been captured.
 int hashCode()
           
 boolean is50MoveRuleApplicible()
          tests to see if the 50 move rule can be applied to this game, thus indicating a legal Draw may be called.
 boolean isBlackCastleableKingside()
           
 boolean isBlackCastleableQueenside()
           
 boolean isBlackMove()
          who's move is it?
 boolean isCheck()
          returns true if the King in the side to move is in check or double-check.
 boolean isCheckmate()
          returns true if the current board is in checkmate
 boolean isDoubleCheck()
          returns true if the King on the side to move is threatened by two pieces.
 boolean isEnPassantFile(char f)
          tests to see if the file (a-h) is the enpassant file.
 boolean isEnPassantFile(int f)
          tests to see if the file (1-8) is the enpassant file.
 boolean isFileValid(int f)
          is this a valid file for the board (in the proper range)
 boolean isGuarded(ChessPiece piece)
          does a fellow team member recapture this square if the piece is taken
 boolean isGuarded(Square sq, boolean isBlack)
          does a fellow team member recapture this square if the piece is taken
 boolean isInitialPositionDefault()
          was the initial position of the board (before any move) the default position of traditional chess?
 boolean isLegalMove(Move m)
          Checks to see if the move is legal on the current board.
 boolean isRankValid(int r)
          is this a valid rank for the board (in the proper range)
 boolean isStalemate()
          returns true if there are no legal moves and the King is not in check
 boolean isThreatened(ChessPiece piece)
          is a ChessPiece attacking this piece.
 boolean isThreatened(Square sq, boolean isBlack)
          is a ChessPiece attacking this square?
 boolean isWhiteCastleableKingside()
           
 boolean isWhiteCastleableQueenside()
           
 void playMove(Move move)
          plays the move on the board.
 void removeBoardListener(BoardListener bl)
           
 void set50MoveRulePlyCount(int i)
          sets many ply (half-moves) the board is into the 50 move rule.
 void setBlackCastleableKingside(boolean t)
           
 void setBlackCastleableQueenside(boolean t)
           
 void setBlackMove(boolean t)
          sets who's turn it is to move.
 void setEnPassantFile(char f)
          sets the file (a-h) of the file where enpassant is legal
 void setEnPassantFile(int f)
          sets the file index (1-8) of the file where enpassant is legal
 void setPosition(char[][] matrix)
          set the position from an 8x8 matrix using the FEN characters to represent piece positions.
 void setPositionClear()
          removes all pieces from the board
 void setPositionDefault()
          sets the board position to the default chess board setup.
 void setWhiteCastleableKingside(boolean t)
           
 void setWhiteCastleableQueenside(boolean t)
           
 char[][] toCharArray()
          returns the character matrix array of the current board.
 java.lang.String toString()
          a diagnostic tool which shows the board state.
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

DEBUG

public static final long DEBUG
used as a mask for Log.debug()


NULL_FILE

public static final byte NULL_FILE
the file that indicates no file is specified

See Also:
Constant Field Values

NULL_RANK

public static final byte NULL_RANK
the file that indicates no file is specified

See Also:
Constant Field Values

NO_ENPASSANT

public static final byte NO_ENPASSANT
the file that indicates no file is specified

See Also:
Constant Field Values

DEFAULT_POSITION

public static final char[][] DEFAULT_POSITION
the default setup of pieces in a traditional chess board


MAX_FILE

public static final byte MAX_FILE
the max number of files on a traditional chess board

See Also:
Constant Field Values

MAX_RANK

public static final byte MAX_RANK
the max number of ranks on a traditional chess board

See Also:
Constant Field Values
Constructor Detail

ChessBoard

public ChessBoard()
calls ChessBoard(true)


ChessBoard

public ChessBoard(boolean defaultBoard)
Parameters:
defaultBoard - if true the default chess position will be used. if false the board will be cleared and w/o pieces.

ChessBoard

public ChessBoard(char[][] matrix)
Parameters:
matrix - the position using English SAN notation for the pieces, (PNBRQK) where uppercase is used for White pieces and lowercase for Black.

ChessBoard

public ChessBoard(char[][] matrix,
                  boolean isBlackMove,
                  boolean castleWK,
                  boolean castleWQ,
                  boolean castleBK,
                  boolean castleBQ,
                  char enpassantFile,
                  int plyCount,
                  int moveNum)
Parameters:
matrix - the position using English SAN notation for the pieces, (PNBRQK) where uppercase is used for White pieces and lowercase for Black.
isBlackMove - who's move is it?
castleWK - can white castle Kingside?
castleWQ - can white castle Queenside?
castleBK - can white castle Kingside?
castleBQ - can white castle Queenside?
enpassantFile - the file (A-H) that is vulnerable to enpassant
plyCount - the ply count for the 50 move rule.
moveNum - which move number are we on?
Method Detail

isRankValid

public boolean isRankValid(int r)
is this a valid rank for the board (in the proper range)


isFileValid

public boolean isFileValid(int f)
is this a valid file for the board (in the proper range)


getMaxRank

public int getMaxRank()
what is the maxium rank allowed.


getMaxFile

public int getMaxFile()
what is the maxium file allowed.


playMove

public void playMove(Move move)
              throws IllegalMoveException,
                     OutOfTurnException
plays the move on the board. Note: if a move is played on the board this way it is NOT added to the History. This could seriously FUBAR the state of the board if you use the two methods on the same board.

Specified by:
playMove in interface Board
Throws:
IllegalMoveException
OutOfTurnException

getPlayerToMove

public int getPlayerToMove()
Specified by:
getPlayerToMove in interface Board
Returns:
0 for White and 1 for Black

isLegalMove

public boolean isLegalMove(Move m)
Checks to see if the move is legal on the current board. This will not affect the History of this game in any way.

Specified by:
isLegalMove in interface Board
Returns:
false if the move is null

getThreats

public ChessPiece[] getThreats(Square sq,
                               boolean isBlack)
returns an array of the pieces of the team specified who can attack the square specified.

Parameters:
sq - square to do threat analysis for
isBlack - attackers must be of this team
Returns:
ChessPiece[] - set of all pieces which have this square in their legalDests variable

getThreats

public ChessPiece[] getThreats(ChessPiece piece)
return the ChessPieces that threaten to take this piece.


isThreatened

public boolean isThreatened(Square sq,
                            boolean isBlack)
is a ChessPiece attacking this square?

Parameters:
sq - the square in question.
isBlack - which side do you suppose attacks this square?

isThreatened

public boolean isThreatened(ChessPiece piece)
is a ChessPiece attacking this piece.

Parameters:
piece - the piece under possible attack

getGuards

public ChessPiece[] getGuards(Square sq,
                              boolean isBlack)
returns an array of the pieces of the team specified who can guard the square (a friendly piece) specified. this is used to determine which pieces the King could take this method is extremely similar to getThreats

Parameters:
sq - square to do threat analysis for
isBlack - attackers must be of this team
Returns:
ChessPiece[] - set of all pieces which have this square in their guardSquares variable

getGuards

public ChessPiece[] getGuards(ChessPiece piece)
returns an array of the pieces of the team specified who can guard the square (a friendly piece) specified. this is used to determine which pieces the King could take this method is extremely similar to getThreats

Parameters:
piece - the piece to be guarded
Returns:
ChessPiece[] set of all pieces which have this square in their guardSquares variable

isGuarded

public boolean isGuarded(Square sq,
                         boolean isBlack)
does a fellow team member recapture this square if the piece is taken


isGuarded

public boolean isGuarded(ChessPiece piece)
does a fellow team member recapture this square if the piece is taken


getLegalMoveCount

public int getLegalMoveCount()
how many legal moves on the current board.

Specified by:
getLegalMoveCount in interface Board

getLegalMoves

public java.util.List getLegalMoves()
returns a list of moves that are legal on the current board.

Specified by:
getLegalMoves in interface Board

getOrigin

public Square getOrigin(byte piece_index,
                        Square dest)
                 throws AmbiguousChessMoveException,
                        IllegalMoveException
determines where the origin of the move that goes to this destination is. This is the recipercal method for what isDestUniqueForClass allows

Throws:
AmbiguousChessMoveException
IllegalMoveException

getOrigin

public Square getOrigin(byte piece_index,
                        int file,
                        int rank,
                        Square dest)
                 throws AmbiguousChessMoveException,
                        IllegalMoveException
determines where the origin of the move that goes to this destination is. This is the recipercal method for what isDestUniqueForClass allows

Throws:
AmbiguousChessMoveException
IllegalMoveException

isCheckmate

public boolean isCheckmate()
returns true if the current board is in checkmate


isCheck

public boolean isCheck()
returns true if the King in the side to move is in check or double-check.


isDoubleCheck

public boolean isDoubleCheck()
returns true if the King on the side to move is threatened by two pieces.


isStalemate

public boolean isStalemate()
returns true if there are no legal moves and the King is not in check


toCharArray

public char[][] toCharArray()
returns the character matrix array of the current board. This will contain characters to represent the pieces. The characters are of the English SAN set PNBRQK (for white) and pnbrqk (for black)

Returns:
char[file][rank]

getCapturedPieces

public ChessPiece[] getCapturedPieces(boolean isBlack)
get the pieces that have been captured.

Parameters:
isBlack - if true returns Black pieces that have been captured.

getUnCapturedPieces

public ChessPiece[] getUnCapturedPieces(boolean isBlack)
get the pieces that haven't been captured.

Parameters:
isBlack - if true returns Black pieces that haven't been captured.

getMaterialCount

public int getMaterialCount(boolean isBlack)
gets the material count evaluation for the side specified. The evaluation is based the following values. Pawn = 1, Knight = 3, Bishop = 3, Rook = 5, Queen = 9. The King is not taken into account as it has an infinite value.

Parameters:
isBlack - the color of the side you wish to evaluate.

setBlackMove

public void setBlackMove(boolean t)
sets who's turn it is to move. Setting this midway through throw a game will throw an exception.

Throws:
java.lang.IllegalStateException - if a move has already been executed.

isBlackMove

public boolean isBlackMove()
who's move is it?


getSquare

public Square getSquare(char file,
                        char rank)
cx and cy should subscribe to the default locale of boardgame.chess.io.SAN. That is, [a-h] & [1-8]


getSquare

public Square getSquare(int x,
                        int y)
x and y should be in the range 1-8


toString

public java.lang.String toString()
a diagnostic tool which shows the board state.


setPositionDefault

public void setPositionDefault()
sets the board position to the default chess board setup.

Specified by:
setPositionDefault in interface Board
Throws:
java.lang.IllegalStateException - if this move is used on a board that already has moves played on it.

setPositionClear

public void setPositionClear()
removes all pieces from the board

Specified by:
setPositionClear in interface Board
Throws:
java.lang.IllegalStateException - if this move is used on a board that already has moves played on it.

setPosition

public void setPosition(char[][] matrix)
set the position from an 8x8 matrix using the FEN characters to represent piece positions. Lowercase is black. P=Pawn, N=Knight, B=Bishop, R= Rook, Q=Queen, K=King. Better is to use setPostion(byte[][]).
Note: if rooks and kings are placed on traditional chess default squares the appropriate castleable bit will be set true. If not, then it will eb set to false.

Throws:
java.lang.IllegalStateException - if this move is used on a board that already has moves played on it.

addPawn

public void addPawn(int file,
                    int rank,
                    boolean isBlack)
adds a Pawn to a Square on the board and to the team of pieces of the correct color.
NOTE: if the game is already is progress it might not be wise to add a piece. the results are undefined.

Parameters:
file - the X coordinate (1-8)
rank - the Y coordinate (1-8)

addKnight

public void addKnight(int file,
                      int rank,
                      boolean isBlack)
adds a Knight to a Square on the board and to the team of pieces of the correct color.
NOTE: if the game is already is progress it might not be wise to add a piece. the results are undefined.

Parameters:
file - the X coordinate (1-8)
rank - the Y coordinate (1-8)

addBishop

public void addBishop(int file,
                      int rank,
                      boolean isBlack)
adds a Bishop to a Square on the board and to the team of pieces of the correct color.
NOTE: if the game is already is progress it might not be wise to add a piece. the results are undefined.

Parameters:
file - the X coordinate (1-8)
rank - the Y coordinate (1-8)

addRook

public void addRook(int file,
                    int rank,
                    boolean isBlack)
adds a Rook to a Square on the board and to the team of pieces of the correct color.
NOTE: if the game is already is progress it might not be wise to add a piece. the results are undefined.

Parameters:
file - the X coordinate (1-8)
rank - the Y coordinate (1-8)

addQueen

public void addQueen(int file,
                     int rank,
                     boolean isBlack)
adds a Queen to a Square on the board and to the team of pieces of the correct color.
NOTE: if the game is already is progress it might not be wise to add a piece. the results are undefined.

Parameters:
file - the X coordinate (1-8)
rank - the Y coordinate (1-8)

addKing

public void addKing(int file,
                    int rank,
                    boolean isBlack)
adds a King to a Square on the board and to the team of pieces of the correct color. You cannot add more than one King to the board. The old king will be over written.
NOTE: if the game is already is progress it might not be wise to add a piece. the results are undefined.

Parameters:
file - the X coordinate (1-8)
rank - the Y coordinate (1-8)

isWhiteCastleableKingside

public boolean isWhiteCastleableKingside()

isWhiteCastleableQueenside

public boolean isWhiteCastleableQueenside()

isBlackCastleableKingside

public boolean isBlackCastleableKingside()

isBlackCastleableQueenside

public boolean isBlackCastleableQueenside()

setWhiteCastleableKingside

public void setWhiteCastleableKingside(boolean t)

setWhiteCastleableQueenside

public void setWhiteCastleableQueenside(boolean t)

setBlackCastleableKingside

public void setBlackCastleableKingside(boolean t)

setBlackCastleableQueenside

public void setBlackCastleableQueenside(boolean t)

getEnPassantFile

public byte getEnPassantFile()
returns the file index (1-8) of the file where enpassant is legal

Returns:
NO_ENPASSANT if the file is not set

setEnPassantFile

public void setEnPassantFile(int f)
sets the file index (1-8) of the file where enpassant is legal

Parameters:
f - NO_ENPASSANT if the file is not set

setEnPassantFile

public void setEnPassantFile(char f)
sets the file (a-h) of the file where enpassant is legal

Parameters:
f - '-' if the file is not set

isEnPassantFile

public boolean isEnPassantFile(char f)
tests to see if the file (a-h) is the enpassant file.


isEnPassantFile

public boolean isEnPassantFile(int f)
tests to see if the file (1-8) is the enpassant file.


get50MoveRulePlyCount

public int get50MoveRulePlyCount()
returns how many ply (half-moves) the board is into the 50 move rule.


set50MoveRulePlyCount

public void set50MoveRulePlyCount(int i)
sets many ply (half-moves) the board is into the 50 move rule.


is50MoveRuleApplicible

public boolean is50MoveRuleApplicible()
tests to see if the 50 move rule can be applied to this game, thus indicating a legal Draw may be called.


getCurrentMoveNumber

public int getCurrentMoveNumber()
get move number for the last move executed


isInitialPositionDefault

public boolean isInitialPositionDefault()
was the initial position of the board (before any move) the default position of traditional chess?

Specified by:
isInitialPositionDefault in interface Board

addBoardListener

public void addBoardListener(BoardListener bl)
Description copied from interface: Board
add a listener for board updates

Specified by:
addBoardListener in interface Board

getBoardListeners

public BoardListener[] getBoardListeners()
Specified by:
getBoardListeners in interface Board

removeBoardListener

public void removeBoardListener(BoardListener bl)
Specified by:
removeBoardListener in interface Board

fireBoardEvent

public void fireBoardEvent(int event)
Description copied from interface: Board
this is used by controllers (such as History) to fire board events to BoardListeners. It also good to use this function to update all BoardListeners on things like a position change, instead of updating the displays individually.

Specified by:
fireBoardEvent in interface Board
Parameters:
event - the BoardEvent. you wish to send to listeners

equals

public boolean equals(java.lang.Object o)
standard override of the equals(Object) function. This tests if the positions of the two board are exactly equal.

Some things checked are:
1) who's move
2) pieces on the same squares
3) castling rights
4) enpassant file

Things not checked, but you might think they were:
1) last move played
2) plyCount (for 50 move rule determination
3) the move number
4) is the initial position the default for both boards


hashCode

public int hashCode()

dumpLegalMoves

public java.lang.String dumpLegalMoves()

dumpOpposingMoves

public java.lang.String dumpOpposingMoves()

dumpLegalMoves

public java.lang.String dumpLegalMoves(boolean blacksMoves)

dump

public java.lang.String dump()
full dump of everything


Submit a bug or feature
Visit the Website
Internet Chess ToolKit is licensed under the GPL v2 .