chess.parser.PositionValidator Class
Class used by position setup dialog to validate positions on the board. When the position is valid the "OK" button will be enabled, otherwise it will be disabled.
Item Index
Methods
- getDistance
- getEnPassantSquare
- getFen
- getFromFileByNotation
- getFromRankByNotation
- getKing
- getLongNotationForAMove
- getNewFen
- getNotationForAMove
- getPieceOnSquare
- getPieces
- getPiecesOfAColor
- getPinned
- getSlidingPiecesAttackingKing
- getToSquareByNotation
- getValidMovesAndResult
- hasThreeFoldRepetition
- isCastleMove
- isEnPassantMove
- isOnSameFile
- isOnSameRank
- isValid
- makeMove
- makeMoveByNotation
- parseFen
- setFen
Methods
getEnPassantSquare
()
String | Null
Returns:
Example:
var fen = '5k2/8/8/3pP3/8/8/8/7K w - d6 0 1';
var parser = new chess.parser.FenParser0x88(fen);
alert(parser.getEnPassantSquare()); // alerts 'd6'
getFromFileByNotation
-
notation
Get from rank by notation. 0 is first file(a), 1 is second file(b), 2 is third file etc.
Parameters:
-
notation
String
Returns:
getFromRankByNotation
-
notation
Get from rank by notation, 0 is first rank, 16 is second rank, 32 is third rank etc.
Parameters:
-
notation
String
Returns:
getKing
-
color
Return king of a color
Parameters:
-
color
Object
Returns:
Example:
var fen = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1';
var parser = new chess.parser.FenParser0x88(fen);
console.log(parser.getKing('white'));
returns an object containing the properties s for square and t for type. both are numeric according to the 0x88 board.
getLongNotationForAMove
-
move
-
shortNotation
Return long notation for a move
Returns:
getNotationForAMove
-
move
Return short notation for a move
Parameters:
-
move
Object
Returns:
Example:
alert(parser.getNotationForAMove({from:'g1',to:'f3'});
getPieceOnSquare
-
square
Return information about piece on square in human readable format
Parameters:
-
square
String
Returns:
Example:
var fen = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1';
var parser = new chess.parser.FenParser0x88(fen);
console.log(parser.getPieceOnSquare('e2'));
will return an object like this:
{
"square": "e2",
"type": "pawn",
"color": "white",
"sliding": 0
}
getPiecesOfAColor
-
color
Returns pieces of a color
Parameters:
-
color
Object
Returns:
Example:
var parser = new chess.parser.FenParser0x88('5k2/8/8/3pP3/8/8/8/7K w - d6 0 1');
var pieces = parser.getPiecesOfAColor('white');
console.log(pieces);
each piece is represented by an object like this:
{
s : 112,
t : 14
}
where s is square and type is type. s is numeric according to the 0x88 chess board where a1 is 0, a2 is 16, b2 is 17, a3 is 32, i.e. a 128x64 square board.
t is a numeric representation(4 bits).
P : 0001
N : 0010
K : 0011
B : 0101
R : 0110
Q : 0111
p : 1001
n : 1010
k : 1011
b : 1101
r : 1100
q : 1100
As you can see, black pieces all have the first bit set to 1, and all the sliding pieces (bishop, rook and queen) has the second bit set to 1. This makes it easy to to determine color and sliding pieces using the bitwise & operator.
getPinned
-
color
Return array of the squares where pieces are pinned, i.e. cannot move. Squares are in the 0x88 format. You can use Board0x88Config.numberToSquareMapping to translate to readable format, example: Board0x88Config.numberToSquareMapping[16] will give you 'a2'
Parameters:
-
color
String
Returns:
Example:
var fen = '6k1/Q5n1/4p3/8/8/1B6/B7/5KR1 b - - 0 1';
var parser = new chess.parser.FenParser0x88(fen);
var pinned = parser.getPinned('black');
console.log(pinned);
will output
{
84: { "by": 33, "direction": 17 }, // pawn on e6(84) is pinned by bishop on b3(33).
102 : { "by": "6", "direction": 16 } // knight on g7 is pinned by rook on g1
}
direction is the path to king which can be
15 16 17
-1 1
17 -16 -15
i.e. 1 to the right, -1 to the left, 17 for higher rank and file etc.
getSlidingPiecesAttackingKing
-
color
Returns array of sliding pieces attacking king
Parameters:
-
color
String
Returns:
Example:
fen = '6k1/Q5n1/4p3/8/8/8/B7/5KR1 b - - 0 1';
parser = new chess.parser.FenParser0x88(fen);
pieces = parser.getSlidingPiecesAttackingKing('white');
console.log(pieces);
will return
[ { "s" : 16, "p": 17 }, { "s": 6, "p": 16 }]
where "s" is the 0x88 board position of the piece and "p" is the sliding path to the king of opposite color. A bishop on a1 and a king on h8 will return { "s": "0", "p": 17 } This method returns pieces even when the sliding piece is not checking king.
getToSquareByNotation
-
notation
Return numeric destination square by notation.
Parameters:
-
notation
String
Returns:
getValidMovesAndResult
-
color
Returns valid moves and results for the position according to the 0x88 chess programming algorithm where position on the board is numeric (A1=0,H1=7,A2=16,H2=23,A3=32,A4=48). First rank is numbered 0-7. Second rank starts at first rank + 16, i.e. A2 = 16. Third rank starts at second rank + 16, i.e. A3 = 32 and so on.
Parameters:
-
color
Object
Returns:
Example:
var fen = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1';
var parser = new chess.parser.FenParser0x88(fen)
console.log(parser.getValidMovesAndResult());
returns an object containing information about number of checks(0,1 or 2 for double check), valid moves and result(0 for undecided, .5 for stalemate, -1 for black win and 1 for white win). moves are returend in the following format:
numeric square : [array of valid squares to move]
example for knight on b1:
1 : [32,34]
since it's located on b1(numeric value 1) and can move to either a3 or c3(32 and 34).
hasThreeFoldRepetition
-
fens
Returns true when last position in the game has occured 2 or more times, i.e. 3 fold repetition.(if 2, it will be 3 fold after the next move, a "claimed" draw).
Parameters:
-
fens
Array
Returns:
isCastleMove
-
move
Returns true if a move is a castle move. This method does not validate if the king is allowed to move to the designated square.
Parameters:
-
move
Object
Returns:
isEnPassantMove
-
move
Returns true if a move is an "en passant" move. Move is given in this format:
Parameters:
-
move
Object
Returns:
Example:
var move = {
from: Board0x88Config.mapping['e5'],
to: Board0x88Config.mapping['e6']
}
console.log(parser.isEnPassantMove(move);
Move is an object and requires properties "from" and "to" which is a numeric square(according to a 0x88 board).
isOnSameFile
-
square1
-
square2
Returns true if two squares are on the same file. Squares are in the 0x88 format, i.e. a1=0,a2=16. You can use Board0x88Config.mapping to get a more readable code.
Returns:
Example:
var parser = new chess.parser.FenParser0x88();
console.log(parser.isOnSameFile(0,16)); // a1 and a2 -> true
console.log(parser.isOnSameFile(0,1)); // a1 and b1 -> false
isOnSameRank
-
square1
-
square2
Returns true if two squares are on the same rank. Squares are in the 0x88 format, i.e. a1=0,a2=16. You can use Board0x88Config.mapping to get a more readable code.
Returns:
Example:
var parser = new chess.parser.FenParser0x88();
console.log(parser.isOnSameSquare(0,16)); // a1 and a2 -> false
console.log(parser.isOnSameSquare(0,1)); // a1 and b1 -> true
isValid
-
fenPosition
Returns true if a position is valid.
Parameters:
-
fenPosition
String
Returns:
makeMove
-
move
Make a move by an object
Parameters:
-
move
Object
Example:
var parser = new chess.parser.FenParser0x88('rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1');
parser.makeMove({from:'e2',to:'e4'});
console.log(parser.getFen());
makeMoveByNotation
-
notation
Make a move by notation
Parameters:
-
notation
String
Returns:
Example:
var parser = new chess.parser.FenParser0x88('rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1');
parser.makeMoveByNotation('e4');
console.log(parser.getFen());
parseFen
()
Parses current fen and stores board information internally