Socket
Socket
Sign inDemoInstall

chess.js

Package Overview
Dependencies
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

chess.js - npm Package Compare versions

Comparing version 0.12.1 to 0.13.0

.github/workflows/node.js.yml

596

chess.js
/*
* Copyright (c) 2021, Jeff Hlywa (jhlywa@gmail.com)
* Copyright (c) 2022, Jeff Hlywa (jhlywa@gmail.com)
* All rights reserved.

@@ -28,127 +28,292 @@ *

var Chess = function (fen) {
var BLACK = 'b'
var WHITE = 'w'
const SYMBOLS = 'pnbrqkPNBRQK'
var EMPTY = -1
const DEFAULT_POSITION =
'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1'
var PAWN = 'p'
var KNIGHT = 'n'
var BISHOP = 'b'
var ROOK = 'r'
var QUEEN = 'q'
var KING = 'k'
const TERMINATION_MARKERS = ['1-0', '0-1', '1/2-1/2', '*']
var SYMBOLS = 'pnbrqkPNBRQK'
const PAWN_OFFSETS = {
b: [16, 32, 17, 15],
w: [-16, -32, -17, -15],
}
var DEFAULT_POSITION =
'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1'
const PIECE_OFFSETS = {
n: [-18, -33, -31, -14, 18, 33, 31, 14],
b: [-17, -15, 17, 15],
r: [-16, 1, 16, -1],
q: [-17, -16, -15, 1, 17, 16, 15, -1],
k: [-17, -16, -15, 1, 17, 16, 15, -1],
}
var TERMINATION_MARKERS = ['1-0', '0-1', '1/2-1/2', '*']
// prettier-ignore
const ATTACKS = [
20, 0, 0, 0, 0, 0, 0, 24, 0, 0, 0, 0, 0, 0,20, 0,
0,20, 0, 0, 0, 0, 0, 24, 0, 0, 0, 0, 0,20, 0, 0,
0, 0,20, 0, 0, 0, 0, 24, 0, 0, 0, 0,20, 0, 0, 0,
0, 0, 0,20, 0, 0, 0, 24, 0, 0, 0,20, 0, 0, 0, 0,
0, 0, 0, 0,20, 0, 0, 24, 0, 0,20, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0,20, 2, 24, 2,20, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 2,53, 56, 53, 2, 0, 0, 0, 0, 0, 0,
24,24,24,24,24,24,56, 0, 56,24,24,24,24,24,24, 0,
0, 0, 0, 0, 0, 2,53, 56, 53, 2, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0,20, 2, 24, 2,20, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0,20, 0, 0, 24, 0, 0,20, 0, 0, 0, 0, 0,
0, 0, 0,20, 0, 0, 0, 24, 0, 0, 0,20, 0, 0, 0, 0,
0, 0,20, 0, 0, 0, 0, 24, 0, 0, 0, 0,20, 0, 0, 0,
0,20, 0, 0, 0, 0, 0, 24, 0, 0, 0, 0, 0,20, 0, 0,
20, 0, 0, 0, 0, 0, 0, 24, 0, 0, 0, 0, 0, 0,20
];
var PAWN_OFFSETS = {
b: [16, 32, 17, 15],
w: [-16, -32, -17, -15],
// prettier-ignore
const RAYS = [
17, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 15, 0,
0, 17, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 15, 0, 0,
0, 0, 17, 0, 0, 0, 0, 16, 0, 0, 0, 0, 15, 0, 0, 0,
0, 0, 0, 17, 0, 0, 0, 16, 0, 0, 0, 15, 0, 0, 0, 0,
0, 0, 0, 0, 17, 0, 0, 16, 0, 0, 15, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 17, 0, 16, 0, 15, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 17, 16, 15, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1, 0, -1, -1, -1,-1, -1, -1, -1, 0,
0, 0, 0, 0, 0, 0,-15,-16,-17, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0,-15, 0,-16, 0,-17, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0,-15, 0, 0,-16, 0, 0,-17, 0, 0, 0, 0, 0,
0, 0, 0,-15, 0, 0, 0,-16, 0, 0, 0,-17, 0, 0, 0, 0,
0, 0,-15, 0, 0, 0, 0,-16, 0, 0, 0, 0,-17, 0, 0, 0,
0,-15, 0, 0, 0, 0, 0,-16, 0, 0, 0, 0, 0,-17, 0, 0,
-15, 0, 0, 0, 0, 0, 0,-16, 0, 0, 0, 0, 0, 0,-17
];
const SHIFTS = { p: 0, n: 1, b: 2, r: 3, q: 4, k: 5 }
const BITS = {
NORMAL: 1,
CAPTURE: 2,
BIG_PAWN: 4,
EP_CAPTURE: 8,
PROMOTION: 16,
KSIDE_CASTLE: 32,
QSIDE_CASTLE: 64,
}
const RANK_1 = 7
const RANK_2 = 6
const RANK_3 = 5
const RANK_4 = 4
const RANK_5 = 3
const RANK_6 = 2
const RANK_7 = 1
const RANK_8 = 0
// prettier-ignore
const SQUARE_MAP = {
a8: 0, b8: 1, c8: 2, d8: 3, e8: 4, f8: 5, g8: 6, h8: 7,
a7: 16, b7: 17, c7: 18, d7: 19, e7: 20, f7: 21, g7: 22, h7: 23,
a6: 32, b6: 33, c6: 34, d6: 35, e6: 36, f6: 37, g6: 38, h6: 39,
a5: 48, b5: 49, c5: 50, d5: 51, e5: 52, f5: 53, g5: 54, h5: 55,
a4: 64, b4: 65, c4: 66, d4: 67, e4: 68, f4: 69, g4: 70, h4: 71,
a3: 80, b3: 81, c3: 82, d3: 83, e3: 84, f3: 85, g3: 86, h3: 87,
a2: 96, b2: 97, c2: 98, d2: 99, e2: 100, f2: 101, g2: 102, h2: 103,
a1: 112, b1: 113, c1: 114, d1: 115, e1: 116, f1: 117, g1: 118, h1: 119
};
const ROOKS = {
w: [
{ square: SQUARE_MAP.a1, flag: BITS.QSIDE_CASTLE },
{ square: SQUARE_MAP.h1, flag: BITS.KSIDE_CASTLE },
],
b: [
{ square: SQUARE_MAP.a8, flag: BITS.QSIDE_CASTLE },
{ square: SQUARE_MAP.h8, flag: BITS.KSIDE_CASTLE },
],
}
/* this function is used to uniquely identify ambiguous moves */
function get_disambiguator(move, moves) {
var from = move.from
var to = move.to
var piece = move.piece
var ambiguities = 0
var same_rank = 0
var same_file = 0
for (var i = 0, len = moves.length; i < len; i++) {
var ambig_from = moves[i].from
var ambig_to = moves[i].to
var ambig_piece = moves[i].piece
/* if a move of the same piece type ends on the same to square, we'll
* need to add a disambiguator to the algebraic notation
*/
if (piece === ambig_piece && from !== ambig_from && to === ambig_to) {
ambiguities++
if (rank(from) === rank(ambig_from)) {
same_rank++
}
if (file(from) === file(ambig_from)) {
same_file++
}
}
}
var PIECE_OFFSETS = {
n: [-18, -33, -31, -14, 18, 33, 31, 14],
b: [-17, -15, 17, 15],
r: [-16, 1, 16, -1],
q: [-17, -16, -15, 1, 17, 16, 15, -1],
k: [-17, -16, -15, 1, 17, 16, 15, -1],
if (ambiguities > 0) {
/* if there exists a similar moving piece on the same rank and file as
* the move in question, use the square as the disambiguator
*/
if (same_rank > 0 && same_file > 0) {
return algebraic(from)
} else if (same_file > 0) {
/* if the moving piece rests on the same file, use the rank symbol as the
* disambiguator
*/
return algebraic(from).charAt(1)
} else {
/* else use the file symbol */
return algebraic(from).charAt(0)
}
}
// prettier-ignore
var ATTACKS = [
20, 0, 0, 0, 0, 0, 0, 24, 0, 0, 0, 0, 0, 0,20, 0,
0,20, 0, 0, 0, 0, 0, 24, 0, 0, 0, 0, 0,20, 0, 0,
0, 0,20, 0, 0, 0, 0, 24, 0, 0, 0, 0,20, 0, 0, 0,
0, 0, 0,20, 0, 0, 0, 24, 0, 0, 0,20, 0, 0, 0, 0,
0, 0, 0, 0,20, 0, 0, 24, 0, 0,20, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0,20, 2, 24, 2,20, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 2,53, 56, 53, 2, 0, 0, 0, 0, 0, 0,
24,24,24,24,24,24,56, 0, 56,24,24,24,24,24,24, 0,
0, 0, 0, 0, 0, 2,53, 56, 53, 2, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0,20, 2, 24, 2,20, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0,20, 0, 0, 24, 0, 0,20, 0, 0, 0, 0, 0,
0, 0, 0,20, 0, 0, 0, 24, 0, 0, 0,20, 0, 0, 0, 0,
0, 0,20, 0, 0, 0, 0, 24, 0, 0, 0, 0,20, 0, 0, 0,
0,20, 0, 0, 0, 0, 0, 24, 0, 0, 0, 0, 0,20, 0, 0,
20, 0, 0, 0, 0, 0, 0, 24, 0, 0, 0, 0, 0, 0,20
];
return ''
}
// prettier-ignore
var RAYS = [
17, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 15, 0,
0, 17, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 15, 0, 0,
0, 0, 17, 0, 0, 0, 0, 16, 0, 0, 0, 0, 15, 0, 0, 0,
0, 0, 0, 17, 0, 0, 0, 16, 0, 0, 0, 15, 0, 0, 0, 0,
0, 0, 0, 0, 17, 0, 0, 16, 0, 0, 15, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 17, 0, 16, 0, 15, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 17, 16, 15, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1, 0, -1, -1, -1,-1, -1, -1, -1, 0,
0, 0, 0, 0, 0, 0,-15,-16,-17, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0,-15, 0,-16, 0,-17, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0,-15, 0, 0,-16, 0, 0,-17, 0, 0, 0, 0, 0,
0, 0, 0,-15, 0, 0, 0,-16, 0, 0, 0,-17, 0, 0, 0, 0,
0, 0,-15, 0, 0, 0, 0,-16, 0, 0, 0, 0,-17, 0, 0, 0,
0,-15, 0, 0, 0, 0, 0,-16, 0, 0, 0, 0, 0,-17, 0, 0,
-15, 0, 0, 0, 0, 0, 0,-16, 0, 0, 0, 0, 0, 0,-17
];
function infer_piece_type(san) {
var piece_type = san.charAt(0)
if (piece_type >= 'a' && piece_type <= 'h') {
var matches = san.match(/[a-h]\d.*[a-h]\d/)
if (matches) {
return undefined
}
return PAWN
}
piece_type = piece_type.toLowerCase()
if (piece_type === 'o') {
return KING
}
return piece_type
}
function ascii() {
var s = ' +------------------------+\n'
for (var i = SQUARE_MAP.a8; i <= SQUARE_MAP.h1; i++) {
/* display the rank */
if (file(i) === 0) {
s += ' ' + '87654321'[rank(i)] + ' |'
}
var SHIFTS = { p: 0, n: 1, b: 2, r: 3, q: 4, k: 5 }
/* empty piece */
if (board[i] == null) {
s += ' . '
} else {
var piece = board[i].type
var color = board[i].color
var symbol = color === WHITE ? piece.toUpperCase() : piece.toLowerCase()
s += ' ' + symbol + ' '
}
var FLAGS = {
NORMAL: 'n',
CAPTURE: 'c',
BIG_PAWN: 'b',
EP_CAPTURE: 'e',
PROMOTION: 'p',
KSIDE_CASTLE: 'k',
QSIDE_CASTLE: 'q',
if ((i + 1) & 0x88) {
s += '|\n'
i += 8
}
}
s += ' +------------------------+\n'
s += ' a b c d e f g h\n'
var BITS = {
NORMAL: 1,
CAPTURE: 2,
BIG_PAWN: 4,
EP_CAPTURE: 8,
PROMOTION: 16,
KSIDE_CASTLE: 32,
QSIDE_CASTLE: 64,
return s
}
// parses all of the decorators out of a SAN string
function stripped_san(move) {
return move.replace(/=/, '').replace(/[+#]?[?!]*$/, '')
}
/*****************************************************************************
* UTILITY FUNCTIONS
****************************************************************************/
function rank(i) {
return i >> 4
}
function file(i) {
return i & 15
}
function algebraic(i) {
var f = file(i),
r = rank(i)
return 'abcdefgh'.substring(f, f + 1) + '87654321'.substring(r, r + 1)
}
function swap_color(c) {
return c === WHITE ? BLACK : WHITE
}
function is_digit(c) {
return '0123456789'.indexOf(c) !== -1
}
function clone(obj) {
var dupe = obj instanceof Array ? [] : {}
for (var property in obj) {
if (typeof property === 'object') {
dupe[property] = clone(obj[property])
} else {
dupe[property] = obj[property]
}
}
var RANK_1 = 7
var RANK_2 = 6
var RANK_3 = 5
var RANK_4 = 4
var RANK_5 = 3
var RANK_6 = 2
var RANK_7 = 1
var RANK_8 = 0
return dupe
}
// prettier-ignore
var SQUARES = {
a8: 0, b8: 1, c8: 2, d8: 3, e8: 4, f8: 5, g8: 6, h8: 7,
a7: 16, b7: 17, c7: 18, d7: 19, e7: 20, f7: 21, g7: 22, h7: 23,
a6: 32, b6: 33, c6: 34, d6: 35, e6: 36, f6: 37, g6: 38, h6: 39,
a5: 48, b5: 49, c5: 50, d5: 51, e5: 52, f5: 53, g5: 54, h5: 55,
a4: 64, b4: 65, c4: 66, d4: 67, e4: 68, f4: 69, g4: 70, h4: 71,
a3: 80, b3: 81, c3: 82, d3: 83, e3: 84, f3: 85, g3: 86, h3: 87,
a2: 96, b2: 97, c2: 98, d2: 99, e2: 100, f2: 101, g2: 102, h2: 103,
a1: 112, b1: 113, c1: 114, d1: 115, e1: 116, f1: 117, g1: 118, h1: 119
};
function trim(str) {
return str.replace(/^\s+|\s+$/g, '')
}
var ROOKS = {
w: [
{ square: SQUARES.a1, flag: BITS.QSIDE_CASTLE },
{ square: SQUARES.h1, flag: BITS.KSIDE_CASTLE },
],
b: [
{ square: SQUARES.a8, flag: BITS.QSIDE_CASTLE },
{ square: SQUARES.h8, flag: BITS.KSIDE_CASTLE },
],
/***************************************************************************
* PUBLIC CONSTANTS
**************************************************************************/
export const BLACK = 'b'
export const WHITE = 'w'
export const EMPTY = -1
export const PAWN = 'p'
export const KNIGHT = 'n'
export const BISHOP = 'b'
export const ROOK = 'r'
export const QUEEN = 'q'
export const KING = 'k'
export const SQUARES = (function () {
/* from the ECMA-262 spec (section 12.6.4):
* "The mechanics of enumerating the properties ... is
* implementation dependent"
* so: for (var sq in SQUARES) { keys.push(sq); } might not be
* ordered correctly
*/
var keys = []
for (var i = SQUARE_MAP.a8; i <= SQUARE_MAP.h1; i++) {
if (i & 0x88) {
i += 7
continue
}
keys.push(algebraic(i))
}
return keys
})()
export const FLAGS = {
NORMAL: 'n',
CAPTURE: 'c',
BIG_PAWN: 'b',
EP_CAPTURE: 'e',
PROMOTION: 'p',
KSIDE_CASTLE: 'k',
QSIDE_CASTLE: 'q',
}
export const Chess = function (fen) {
var board = new Array(128)

@@ -259,3 +424,3 @@ var kings = { w: EMPTY, b: EMPTY }

ep_square = tokens[3] === '-' ? EMPTY : SQUARES[tokens[3]]
ep_square = tokens[3] === '-' ? EMPTY : SQUARE_MAP[tokens[3]]
half_moves = parseInt(tokens[4], 10)

@@ -368,3 +533,3 @@ move_number = parseInt(tokens[5], 10)

for (var i = SQUARES.a8; i <= SQUARES.h1; i++) {
for (var i = SQUARE_MAP.a8; i <= SQUARE_MAP.h1; i++) {
if (board[i] == null) {

@@ -388,3 +553,3 @@ empty++

if (i !== SQUARES.h1) {
if (i !== SQUARE_MAP.h1) {
fen += '/'

@@ -447,3 +612,3 @@ }

function get(square) {
var piece = board[SQUARES[square]]
var piece = board[SQUARE_MAP[square]]
return piece ? { type: piece.type, color: piece.color } : null

@@ -464,7 +629,7 @@ }

/* check for valid square */
if (!(square in SQUARES)) {
if (!(square in SQUARE_MAP)) {
return false
}
var sq = SQUARES[square]
var sq = SQUARE_MAP[square]

@@ -491,3 +656,3 @@ /* don't let the user place more than one king */

var piece = get(square)
board[SQUARES[square]] = null
board[SQUARE_MAP[square]] = null
if (piece && piece.type === KING) {

@@ -545,4 +710,4 @@ kings[piece.color] = EMPTY

var first_sq = SQUARES.a8
var last_sq = SQUARES.h1
var first_sq = SQUARE_MAP.a8
var last_sq = SQUARE_MAP.h1
var single_square = false

@@ -565,4 +730,4 @@

if (typeof options !== 'undefined' && 'square' in options) {
if (options.square in SQUARES) {
first_sq = last_sq = SQUARES[options.square]
if (options.square in SQUARE_MAP) {
first_sq = last_sq = SQUARE_MAP[options.square]
single_square = true

@@ -744,9 +909,5 @@ } else {

}
// parses all of the decorators out of a SAN string
function stripped_san(move) {
return move.replace(/=/, '').replace(/[+#]?[?!]*$/, '')
}
function attacked(color, square) {
for (var i = SQUARES.a8; i <= SQUARES.h1; i++) {
for (var i = SQUARE_MAP.a8; i <= SQUARE_MAP.h1; i++) {
/* did we run off the end of the board */

@@ -819,3 +980,3 @@ if (i & 0x88) {

for (var i = SQUARES.a8; i <= SQUARES.h1; i++) {
for (var i = SQUARE_MAP.a8; i <= SQUARE_MAP.h1; i++) {
sq_color = (sq_color + 1) % 2

@@ -1054,97 +1215,2 @@ if (i & 0x88) {

/* this function is used to uniquely identify ambiguous moves */
function get_disambiguator(move, moves) {
var from = move.from
var to = move.to
var piece = move.piece
var ambiguities = 0
var same_rank = 0
var same_file = 0
for (var i = 0, len = moves.length; i < len; i++) {
var ambig_from = moves[i].from
var ambig_to = moves[i].to
var ambig_piece = moves[i].piece
/* if a move of the same piece type ends on the same to square, we'll
* need to add a disambiguator to the algebraic notation
*/
if (piece === ambig_piece && from !== ambig_from && to === ambig_to) {
ambiguities++
if (rank(from) === rank(ambig_from)) {
same_rank++
}
if (file(from) === file(ambig_from)) {
same_file++
}
}
}
if (ambiguities > 0) {
/* if there exists a similar moving piece on the same rank and file as
* the move in question, use the square as the disambiguator
*/
if (same_rank > 0 && same_file > 0) {
return algebraic(from)
} else if (same_file > 0) {
/* if the moving piece rests on the same file, use the rank symbol as the
* disambiguator
*/
return algebraic(from).charAt(1)
} else {
/* else use the file symbol */
return algebraic(from).charAt(0)
}
}
return ''
}
function infer_piece_type(san) {
var piece_type = san.charAt(0)
if (piece_type >= 'a' && piece_type <= 'h') {
var matches = san.match(/[a-h]\d.*[a-h]\d/)
if (matches) {
return undefined
}
return PAWN
}
piece_type = piece_type.toLowerCase()
if (piece_type === 'o') {
return KING
}
return piece_type
}
function ascii() {
var s = ' +------------------------+\n'
for (var i = SQUARES.a8; i <= SQUARES.h1; i++) {
/* display the rank */
if (file(i) === 0) {
s += ' ' + '87654321'[rank(i)] + ' |'
}
/* empty piece */
if (board[i] == null) {
s += ' . '
} else {
var piece = board[i].type
var color = board[i].color
var symbol = color === WHITE ? piece.toUpperCase() : piece.toLowerCase()
s += ' ' + symbol + ' '
}
if ((i + 1) & 0x88) {
s += '|\n'
i += 8
}
}
s += ' +------------------------+\n'
s += ' a b c d e f g h\n'
return s
}
// convert a move from Standard Algebraic Notation (SAN) to 0x88 coordinates

@@ -1225,4 +1291,4 @@ function move_from_san(move, sloppy) {

(!piece || piece.toLowerCase() == moves[i].piece) &&
SQUARES[from] == moves[i].from &&
SQUARES[to] == moves[i].to &&
SQUARE_MAP[from] == moves[i].from &&
SQUARE_MAP[to] == moves[i].to &&
(!promotion || promotion.toLowerCase() == moves[i].promotion)

@@ -1237,3 +1303,3 @@ ) {

(!piece || piece.toLowerCase() == moves[i].piece) &&
SQUARES[to] == moves[i].to &&
SQUARE_MAP[to] == moves[i].to &&
(from == square[0] || from == square[1]) &&

@@ -1252,27 +1318,2 @@ (!promotion || promotion.toLowerCase() == moves[i].promotion)

/*****************************************************************************
* UTILITY FUNCTIONS
****************************************************************************/
function rank(i) {
return i >> 4
}
function file(i) {
return i & 15
}
function algebraic(i) {
var f = file(i),
r = rank(i)
return 'abcdefgh'.substring(f, f + 1) + '87654321'.substring(r, r + 1)
}
function swap_color(c) {
return c === WHITE ? BLACK : WHITE
}
function is_digit(c) {
return '0123456789'.indexOf(c) !== -1
}
/* pretty = external move object */

@@ -1297,20 +1338,2 @@ function make_pretty(ugly_move) {

function clone(obj) {
var dupe = obj instanceof Array ? [] : {}
for (var property in obj) {
if (typeof property === 'object') {
dupe[property] = clone(obj[property])
} else {
dupe[property] = obj[property]
}
}
return dupe
}
function trim(str) {
return str.replace(/^\s+|\s+$/g, '')
}
/*****************************************************************************

@@ -1342,32 +1365,2 @@ * DEBUGGING UTILITIES

/***************************************************************************
* PUBLIC CONSTANTS (is there a better way to do this?)
**************************************************************************/
WHITE: WHITE,
BLACK: BLACK,
PAWN: PAWN,
KNIGHT: KNIGHT,
BISHOP: BISHOP,
ROOK: ROOK,
QUEEN: QUEEN,
KING: KING,
SQUARES: (function () {
/* from the ECMA-262 spec (section 12.6.4):
* "The mechanics of enumerating the properties ... is
* implementation dependent"
* so: for (var sq in SQUARES) { keys.push(sq); } might not be
* ordered correctly
*/
var keys = []
for (var i = SQUARES.a8; i <= SQUARES.h1; i++) {
if (i & 0x88) {
i += 7
continue
}
keys.push(algebraic(i))
}
return keys
})(),
FLAGS: FLAGS,
/***************************************************************************
* PUBLIC API

@@ -1464,7 +1457,7 @@ **************************************************************************/

for (var i = SQUARES.a8; i <= SQUARES.h1; i++) {
for (var i = SQUARE_MAP.a8; i <= SQUARE_MAP.h1; i++) {
if (board[i] == null) {
row.push(null)
} else {
row.push({ type: board[i].type, color: board[i].color })
row.push({ square: algebraic(i), type: board[i].type, color: board[i].color })
}

@@ -1916,4 +1909,4 @@ if ((i + 1) & 0x88) {

square_color: function (square) {
if (square in SQUARES) {
var sq_0x88 = SQUARES[square]
if (square in SQUARE_MAP) {
var sq_0x88 = SQUARE_MAP[square]
return (rank(sq_0x88) + file(sq_0x88)) % 2 === 0 ? 'light' : 'dark'

@@ -1981,10 +1974,1 @@ }

}
/* export Chess object if using node or any other CommonJS compatible
* environment */
if (typeof exports !== 'undefined') exports.Chess = Chess
/* export Chess object for any RequireJS compatible environment */
if (typeof define !== 'undefined')
define(function () {
return Chess
})
{
"name": "chess.js",
"version": "0.12.1",
"version": "0.13.0",
"license": "BSD-2-Clause",
"type": "module",
"main": "chess.js",

@@ -15,9 +16,9 @@ "homepage": "https://github.com/jhlywa/chess.js",

"devDependencies": {
"jest": "^27.0.1",
"prettier": "^2.5.1"
"jest": "^27.5.1",
"prettier": "^2.6.0"
},
"scripts": {
"test": "jest",
"format": "prettier --write chess.js"
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
"format": "prettier --write **/*.{js,json}"
}
}
# chess.js
[![Build Status](https://travis-ci.org/jhlywa/chess.js.svg?branch=master)](https://travis-ci.org/jhlywa/chess.js)
![GitHub Workflow Status](https://img.shields.io/github/workflow/status/jhlywa/chess.js/Node.js%20CI)
![npm](https://img.shields.io/npm/v/chess.js?color=blue)
![npm](https://img.shields.io/npm/dm/chess.js)

@@ -28,3 +30,4 @@ chess.js is a Javascript chess library that is used for chess move

```js
const { Chess } = require('./chess.js')
import { Chess } from 'chess.js';
const chess = new Chess()

@@ -100,10 +103,10 @@

chess.board()
// -> [[{type: 'r', color: 'b'},
{type: 'n', color: 'b'},
{type: 'b', color: 'b'},
{type: 'q', color: 'b'},
{type: 'k', color: 'b'},
{type: 'b', color: 'b'},
{type: 'n', color: 'b'},
{type: 'r', color: 'b'}],
// -> [[{square: 'a8', type: 'r', color: 'b'},
{square: 'b8', type: 'n', color: 'b'},
{square: 'c8', type: 'b', color: 'b'},
{square: 'd8', type: 'q', color: 'b'},
{square: 'e8', type: 'k', color: 'b'},
{square: 'f8', type: 'b', color: 'b'},
{square: 'g8', type: 'n', color: 'b'},
{square: 'h8', type: 'r', color: 'b'}],
[...],

@@ -114,10 +117,10 @@ [...],

[...],
[{type: 'r', color: 'w'},
{type: 'n', color: 'w'},
{type: 'b', color: 'w'},
{type: 'q', color: 'w'},
{type: 'k', color: 'w'},
{type: 'b', color: 'w'},
{type: 'n', color: 'w'},
{type: 'r', color: 'w'}]]
[{square: 'a1', type: 'r', color: 'w'},
{square: 'b1', type: 'n', color: 'w'},
{square: 'c1', type: 'b', color: 'w'},
{square: 'd1', type: 'q', color: 'w'},
{square: 'e1', type: 'k', color: 'w'},
{square: 'f1', type: 'b', color: 'w'},
{square: 'g1', type: 'n', color: 'w'},
{square: 'h1', type: 'r', color: 'w'}]]
```

@@ -124,0 +127,0 @@

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc