AbChess

CLASS

The AbChess class creates an instance of an AbChess object. It provides an access to several methods. This class is mainly used to draw a chessboard and interact with it. The chess data are handled in a chessgame directly linked to the chessboard.

Constructor

new AbChess(containerId, options)

Create an instance of the AbChess class. Define the id of the HTML element to contain the desired chessboard. An options object can be also used to configure the instance.

Parameter
containerId String
The id of the HTML container.
options Optional
Object
A configuration object. See options properties below.
options
animated Optional
Boolean
If set to false, disable the animations of the pieces. The default is true.
animationSpeed Optional
Number
The speed of the animated pieces. The default is 20.
clickable Optional
Boolean
If set to true, the chess pieces will be movable with a two-clicks method. The default is true.
coordinates Optional
Boolean
If set to true, the chessboard will have a coordinates border. The default is true.
draggable Optional
Boolean
If set to true, the chess pieces will be movable with a drag-and-drop method. The default is true.
imagesExtension Optional
String
The extension of the chess pieces images. The default is ".png".
imagesPath Optional
String
The path of the chess pieces images. The default is "images/wikipedia/".
legalMarksColor Optional
String
The CSS color of the legal moves indications. The default is "cornflowerblue".
markCheck Optional
Boolean
If set to true, highlight the square of the king when it is attacked. The default is true.
markLastMove Optional
Boolean
If set to true, highlight the start and arrival squares of the last piece movement. The default is true.
markLegalSquares Optional
Boolean
If set to true, highlight the legal squares after a piece has been selected. The default is true.
markOverflownSquare Optional
Boolean
If set to true, highlight the overflown square during a drag and drop operation. The default is true.
markStartSquare Optional
Boolean
If set to true, highlight the square of a piece when it is selected. The default is true.
reversed Optional
Boolean
If set to true, the chessboard will be oriented from black's point of view. The default is false.
width Optional
Number
The width of the chessboard in pixel. Multiples of 8 are recommended. The default is 400.

Methods

flip()

Change the orientation of the chessboard.

See the flip example.

getActiveColor(index)

Return a character ("w" for white or "b" for black) indicating the active color in a position.

Parameter
index Number
The index of the desired position. It should be a positive integer.

Return type : String

See the onMovePlayed example.

getFEN(index)

Return the FEN string of a position in the game.

Parameter
index Number
The index of the desired position. It should be a positive integer.

Return type : String

See the getFEN example.

getInfo(name)

Return an information of the game. Seven informations should be present in a game : Event, Site, Date, Round, White, Black, Result. Some other ones can be added.

Parameter
name String
The name of the desired information.

Return type : String

getLegalMoves(index)

Return an array of the legal moves in a position.

Parameter
index Number
The index of the desired position. It should be a positive integer.

Return type : Object[]

See the random moves example.

getMovesPGN(symbols)

Return an array of the moves stored in the game in PGN notation.

Parameter
symbols Optional
Boolean
When set to true, the function will replace the pieces letters with HTML pieces symbols. The default is false.

Return type : String[]

See the PGN viewer example.

getPGN()

Return the PGN string of the current game.

Return type : String

is50Moves(index)

Return true if a position is a draw because of the 50 moves rule or false if it's not.

Parameter
index Number
The index of the desired position. It should be a positive integer.

Return type : Boolean

See the random moves example.

isCheck(index)

Return true if the king of the active color is in check in a position or false if it's not.

Parameter
index Number
The index of the desired position. It should be a positive integer.

Return type : Boolean

See the onMovePlayed example.

isCheckmate(index)

Return true if a position is a checkmate or false if it's not.

Parameter
index Number
The index of the desired position. It should be a positive integer.

Return type : Boolean

See the random moves example.

isInsufficientMaterial(index)

Return true if the remaining material is insufficient to win a position or false if it's not.

Parameter
index Number
The index of the desired position. It should be a positive integer.

Return type : Boolean

See the random moves example.

isLegalMove(index, start, destination)

Return true if a move is legal in a position or false if it's not.

Parameter
index Number
The index of the desired position. It should be a positive integer.
start String
The start coordinate of the move to check.
destination String
The destination coordinate of the move to check.

Return type : Boolean

isStalemate(index)

Return true if a position is a stalemate or false if it's not.

Parameter
index Number
The index of the desired position. It should be a positive integer.

Return type : Boolean

See the random moves example.

isValidFEN(fen)

Return true if a FEN string is valid or false if it's not.

Parameter
fen String
The Forsyth-Edwards Notation string to validate.

Return type : Boolean

See the setFEN example.

isValidPGN(pgn)

Return true if a PGN string is valid or false if it's not.

Parameter
pgn String
The Portable Game Notation string to validate.

Return type : Boolean

See the PGN viewer example.

onMovePlayed(callback)

Set a function to call each time after a move is played in the game.

Parameter
callback Function
The function to call after a move has been played.

See the onMovePlayed example.

play(start, destination, promotion)

Play a move on the board.

Parameter
start String
The start coordinate of the move to play.
destination String
The destination coordinate of the move to play.
promotion Optional
String
The promotion choice if needed. It will be ignored if the move is not a promotion.

See the play example.

reset()

Create a new game and set the chessboard to the initial starting position.

setFEN(fen)

Load a position on the chessboard from a FEN string. This does not affect the game in progress.

Parameter
fen Optional
String
The Forsyth-Edwards Notation to import. If omitted, the classical starting position will be set.

See the setFEN example.

setInfo(name, value)

Set an information and its value in the game. Replace the information if it already exists. Create a new one if not.

Parameter
name String
The name of the information to set.
value String
The value of the information to set.

setPGN(pgn)

Load a chessgame from a PGN string. This will replace the game in progress.

Parameter
pgn String
The Portable Game Notation to import.

See the PGN viewer example.

view(index)

Update the chessboard to a position in the game.

Parameter
index Number
The index of the desired position. It should be a positive integer.

See the PGN viewer example.