Comparing version 1.0.0-beta.5 to 1.0.0-beta.6
{ | ||
"name": "chess.js", | ||
"version": "1.0.0-beta.5", | ||
"version": "1.0.0-beta.6", | ||
"license": "BSD-2-Clause", | ||
"main": "dist/chess.js", | ||
"types": "dist/chess.d.ts", | ||
"main": "dist/cjs/chess.js", | ||
"module": "dist/esm/chess.js", | ||
"types": "dist/types/chess.d.ts", | ||
"homepage": "https://github.com/jhlywa/chess.js", | ||
@@ -12,4 +13,4 @@ "author": "Jeff Hlywa <jhlywa@gmail.com>", | ||
"lint": "eslint src/ --ext .ts", | ||
"build": "tsc --build", | ||
"clean": "tsc --build --clean" | ||
"build": "tsc -b ./tsconfig.cjs.json ./tsconfig.esm.json ./tsconfig.types.json", | ||
"clean": "rm -rf ./dist" | ||
}, | ||
@@ -16,0 +17,0 @@ "repository": { |
@@ -22,2 +22,24 @@ # chess.js | ||
## Import into your project | ||
### Modern way (ESM) | ||
```js | ||
import { Chess } from 'chess.js' | ||
``` | ||
If you want to use it in a browser you can import as module like this: | ||
```html | ||
<script type="module"> | ||
import { Chess } from 'chess.js' | ||
</script> | ||
``` | ||
### Old way (CommonJS) | ||
```js | ||
const { Chess } = require('chess.js') | ||
``` | ||
## Example Code | ||
@@ -27,5 +49,3 @@ | ||
```ts | ||
import { Chess } from 'chess.js' | ||
```js | ||
const chess = new Chess() | ||
@@ -223,2 +243,16 @@ | ||
### .getCastlingRights(color) | ||
Gets the castling rights for the given color. An object is returned which | ||
indicates whether the right is available or not for both kingside and queenside. | ||
Note this does not indicate if such a move is legal or not in the current | ||
position as checks etc. also need to be considered. | ||
```ts | ||
const chess = new Chess() | ||
chess.getCastlingRights(BLACK) // black can castle queenside only | ||
// -> { 'k': false, 'q': true } | ||
``` | ||
### .getComment() | ||
@@ -636,2 +670,12 @@ | ||
### .moveNumber() | ||
Returns the current move number. | ||
```ts | ||
chess.load('4r1k1/p1prnpb1/Pp1pq1pp/3Np2P/2P1P3/R4N2/1PP2PP1/3QR1K1 w - - 2 20') | ||
chess.moveNumber() | ||
// -> 20 | ||
``` | ||
### .moves({ piece?: Piece, square?: Square, verbose?: Boolean }?) | ||
@@ -766,2 +810,14 @@ | ||
### .setCastlingRights(color, rights) | ||
Sets the castling rights for the given color. Returns true if the change was | ||
successfully made. False will be returned when the position doesn't allow the | ||
requested change i.e. if the corresponding king or rook is not on it's starting | ||
square. | ||
```ts | ||
// white can't castle kingside but can castle queenside | ||
chess.setCastlingRights(WHITE, { [chess.KING]: false, [chess.QUEEN]: true }) | ||
``` | ||
### .setComment(comment) | ||
@@ -851,6 +907,1 @@ | ||
``` | ||
## BUGS | ||
- The en passant square and castling flags aren't adjusted when using the | ||
put/remove functions (workaround: use .load() instead) |
@@ -248,2 +248,7 @@ /** | ||
const SIDES = { | ||
[KING]: BITS.KSIDE_CASTLE, | ||
[QUEEN]: BITS.QSIDE_CASTLE | ||
} | ||
const ROOKS = { | ||
@@ -764,2 +769,4 @@ w: [ | ||
this._updateCastlingRights() | ||
this._updateEnPassantSquare() | ||
this._updateSetup(this.fen()) | ||
@@ -777,2 +784,4 @@ | ||
this._updateCastlingRights() | ||
this._updateEnPassantSquare() | ||
this._updateSetup(this.fen()) | ||
@@ -783,2 +792,52 @@ | ||
_updateCastlingRights() { | ||
const whiteKingInPlace = (this._board[Ox88.e1]?.type === KING && this._board[Ox88.e1]?.color === WHITE) | ||
const blackKingInPlace = (this._board[Ox88.e8]?.type === KING && this._board[Ox88.e8]?.color === BLACK) | ||
if (!whiteKingInPlace || this._board[Ox88.a1]?.type !== ROOK || this._board[Ox88.a1]?.color !== WHITE) { | ||
this._castling.w &= ~BITS.QSIDE_CASTLE | ||
} | ||
if (!whiteKingInPlace || this._board[Ox88.h1]?.type !== ROOK || this._board[Ox88.h1]?.color !== WHITE) { | ||
this._castling.w &= ~BITS.KSIDE_CASTLE | ||
} | ||
if (!blackKingInPlace || this._board[Ox88.a8]?.type !== ROOK || this._board[Ox88.a8]?.color !== BLACK) { | ||
this._castling.b &= ~BITS.QSIDE_CASTLE | ||
} | ||
if (!blackKingInPlace || this._board[Ox88.h8]?.type !== ROOK || this._board[Ox88.h8]?.color !== BLACK) { | ||
this._castling.b &= ~BITS.KSIDE_CASTLE | ||
} | ||
} | ||
_updateEnPassantSquare() { | ||
if(this._epSquare === EMPTY) { | ||
return | ||
} | ||
const startSquare = this._epSquare + (this._turn === WHITE ? -16 : 16) | ||
const currentSquare = this._epSquare + (this._turn === WHITE ? 16 : -16) | ||
const attackers = [currentSquare + 1, currentSquare - 1] | ||
if ( | ||
this._board[startSquare] !== null || | ||
this._board[this._epSquare] !== null || | ||
this._board[currentSquare]?.color !== swapColor(this._turn) || | ||
this._board[currentSquare]?.type !== PAWN | ||
) { | ||
this._epSquare = EMPTY | ||
return | ||
} | ||
const canCapture = (square: number) => | ||
!(square & 0x88) && | ||
this._board[square]?.color === this._turn && | ||
this._board[square]?.type === PAWN; | ||
if(!attackers.some(canCapture)) { | ||
this._epSquare = EMPTY | ||
} | ||
} | ||
_attacked(color: Color, square: number) { | ||
@@ -2240,2 +2299,30 @@ for (let i = Ox88.a8; i <= Ox88.h1; i++) { | ||
} | ||
setCastlingRights(color: Color, rights: Partial<Record<typeof KING | typeof QUEEN, boolean>>) { | ||
for (const side of [KING, QUEEN] as const) { | ||
if (rights[side] !== undefined) { | ||
if (rights[side]) { | ||
this._castling[color] |= SIDES[side] | ||
} else { | ||
this._castling[color] &= ~SIDES[side] | ||
} | ||
} | ||
} | ||
this._updateCastlingRights() | ||
const result = this.getCastlingRights(color) | ||
return (rights[KING] === undefined || rights[KING] === result[KING]) && (rights[QUEEN] === undefined || rights[QUEEN] === result[QUEEN]) | ||
} | ||
getCastlingRights(color: Color) { | ||
return { | ||
[KING]: (this._castling[color] & SIDES[KING]) !== 0, | ||
[QUEEN]: (this._castling[color] & SIDES[QUEEN]) !== 0, | ||
} | ||
} | ||
moveNumber() { | ||
return this._moveNumber | ||
} | ||
} |
{ | ||
"compilerOptions": { | ||
"declaration": true, | ||
"esModuleInterop": true, | ||
"module": "commonjs", | ||
"moduleResolution": "node", | ||
"outDir": "./dist", | ||
"removeComments": false, | ||
"sourceMap": true, | ||
"strict": true, | ||
"target": "es2017" | ||
"target": "ESNext" | ||
}, | ||
"include": ["src/**/*"] | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
357887
17
5995
902
1