chess-rule-engine 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. package/README.md +170 -2
  2. package/dist/board.d.ts +22 -0
  3. package/dist/board.js +107 -0
  4. package/dist/chessRuleSet.d.ts +22 -0
  5. package/dist/chessRuleSet.js +154 -0
  6. package/dist/constants/board.d.ts +2 -0
  7. package/dist/constants/board.js +2 -0
  8. package/dist/constants/castleType.d.ts +7 -0
  9. package/dist/constants/castleType.js +8 -0
  10. package/dist/constants/game.d.ts +24 -0
  11. package/dist/constants/game.js +34 -0
  12. package/dist/constants/index.d.ts +3 -0
  13. package/dist/constants/index.js +3 -0
  14. package/dist/constants/piece.d.ts +34 -0
  15. package/dist/constants/piece.js +6 -0
  16. package/dist/game.d.ts +57 -0
  17. package/dist/game.js +314 -0
  18. package/dist/hashing/zobrist.d.ts +23 -0
  19. package/dist/hashing/zobrist.js +97 -0
  20. package/dist/hashing/zobristUpdater.d.ts +20 -0
  21. package/dist/hashing/zobristUpdater.js +105 -0
  22. package/dist/index.d.ts +6 -0
  23. package/dist/index.js +6 -0
  24. package/dist/pieces/bishop.d.ts +9 -0
  25. package/dist/pieces/bishop.js +17 -0
  26. package/dist/pieces/index.d.ts +8 -0
  27. package/dist/pieces/index.js +7 -0
  28. package/dist/pieces/king.d.ts +12 -0
  29. package/dist/pieces/king.js +90 -0
  30. package/dist/pieces/knight.d.ts +9 -0
  31. package/dist/pieces/knight.js +21 -0
  32. package/dist/pieces/pawn.d.ts +11 -0
  33. package/dist/pieces/pawn.js +90 -0
  34. package/dist/pieces/piece.d.ts +12 -0
  35. package/dist/pieces/piece.js +14 -0
  36. package/dist/pieces/queen.d.ts +9 -0
  37. package/dist/pieces/queen.js +21 -0
  38. package/dist/pieces/rook.d.ts +9 -0
  39. package/dist/pieces/rook.js +17 -0
  40. package/dist/types/board.d.ts +5 -0
  41. package/dist/types/board.js +1 -0
  42. package/dist/types/game.d.ts +38 -0
  43. package/dist/types/game.js +1 -0
  44. package/dist/types/index.d.ts +5 -0
  45. package/dist/types/index.js +5 -0
  46. package/dist/types/move.d.ts +96 -0
  47. package/dist/types/move.js +1 -0
  48. package/dist/types/piece.d.ts +23 -0
  49. package/dist/types/piece.js +1 -0
  50. package/dist/types/util.d.ts +10 -0
  51. package/dist/types/util.js +1 -0
  52. package/dist/utils/color.d.ts +4 -0
  53. package/dist/utils/color.js +7 -0
  54. package/dist/utils/piece.d.ts +6 -0
  55. package/dist/utils/piece.js +7 -0
  56. package/dist/utils/splitMix64.d.ts +6 -0
  57. package/dist/utils/splitMix64.js +15 -0
  58. package/package.json +30 -4
@@ -0,0 +1,90 @@
1
+ import { Piece } from "./piece.js";
2
+ import { ChessRuleSet } from "../chessRuleSet.js";
3
+ import { checkPieceName } from "../utils/piece.js";
4
+ const ShortCastleCondition = (game, pos) => {
5
+ const { board } = game;
6
+ const king = board.getAtPos(pos);
7
+ const rook = board.positionMap[pos.y][board.boardSize - 1];
8
+ if (!checkPieceName(king, "king") || king.isMoved ||
9
+ !checkPieceName(rook, "rook") || rook.isMoved ||
10
+ rook.color !== king.color ||
11
+ ChessRuleSet.isKingInCheck(game, king.color) ||
12
+ !ChessRuleSet.canSeeEachOther(game, pos, { x: board.boardSize - 1, y: pos.y })) {
13
+ return false;
14
+ }
15
+ for (let dx = 1; dx <= 2; ++dx) {
16
+ const isKingInCheck = game.evaluateAndBacktrack({ from: pos, to: { x: pos.x + dx, y: pos.y } }, (game) => ChessRuleSet.isKingInCheck(game, king.color));
17
+ if (isKingInCheck)
18
+ return false;
19
+ }
20
+ return true;
21
+ };
22
+ const LongCastleCondition = (game, pos) => {
23
+ const { board } = game;
24
+ const king = board.getAtPos(pos);
25
+ const rook = board.positionMap[pos.y][0];
26
+ if (!checkPieceName(king, "king") || king.isMoved ||
27
+ !checkPieceName(rook, "rook") || rook.isMoved ||
28
+ rook.color !== king.color ||
29
+ ChessRuleSet.isKingInCheck(game, king.color) ||
30
+ !ChessRuleSet.canSeeEachOther(game, pos, { x: board.boardSize - 1, y: pos.y })) {
31
+ return false;
32
+ }
33
+ for (let dx = -1; dx >= -2; --dx) {
34
+ const isKingInCheck = game.evaluateAndBacktrack({ from: pos, to: { x: pos.x + dx, y: pos.y } }, (game) => ChessRuleSet.isKingInCheck(game, king.color));
35
+ if (isKingInCheck)
36
+ return false;
37
+ }
38
+ return true;
39
+ };
40
+ const OnShortCastle = (game, moveQuery, moveRecord) => {
41
+ const { from: startKingPos, to: endKingPos } = moveQuery;
42
+ game.addMove(moveRecord, {
43
+ from: { x: game.board.boardSize - 1, y: startKingPos.y },
44
+ to: { x: endKingPos.x - 1, y: endKingPos.y }
45
+ });
46
+ };
47
+ const OnLongCastle = (game, moveQuery, moveRecord) => {
48
+ const { from: startKingPos, to: endKingPos } = moveQuery;
49
+ game.addMove(moveRecord, {
50
+ from: { x: 0, y: startKingPos.y },
51
+ to: { x: endKingPos.x + 1, y: endKingPos.y }
52
+ });
53
+ };
54
+ const KingMoves = [
55
+ { vec: { dx: 0, dy: 1 }, isSliding: false, canAttack: true },
56
+ { vec: { dx: 0, dy: -1 }, isSliding: false, canAttack: true },
57
+ { vec: { dx: 1, dy: 0 }, isSliding: false, canAttack: true },
58
+ { vec: { dx: -1, dy: 0 }, isSliding: false, canAttack: true },
59
+ { vec: { dx: 1, dy: 1 }, isSliding: false, canAttack: true },
60
+ { vec: { dx: -1, dy: 1 }, isSliding: false, canAttack: true },
61
+ { vec: { dx: -1, dy: -1 }, isSliding: false, canAttack: true },
62
+ { vec: { dx: 1, dy: -1 }, isSliding: false, canAttack: true },
63
+ { vec: { dx: 2, dy: 0 }, isSliding: false, canAttack: false, condition: ShortCastleCondition, onMove: OnShortCastle }, // Short Castle
64
+ { vec: { dx: -2, dy: 0 }, isSliding: false, canAttack: false, condition: LongCastleCondition, onMove: OnLongCastle }, // Long Castle
65
+ ];
66
+ class King extends Piece {
67
+ static moves = KingMoves;
68
+ constructor(color) {
69
+ super(color, "king");
70
+ }
71
+ getMoves() {
72
+ return King.moves;
73
+ }
74
+ static canCastle(king, rook) {
75
+ return (!king.isMoved && !rook.isMoved && king.color === rook.color);
76
+ }
77
+ static canShortCastle(game, pos) {
78
+ const { board } = game;
79
+ const king = board.getAtPos(pos);
80
+ const rook = board.positionMap[pos.y][board.boardSize - 1];
81
+ return (checkPieceName(king, "king") && checkPieceName(rook, "rook") && King.canCastle(king, rook));
82
+ }
83
+ static canLongCastle(game, pos) {
84
+ const { board } = game;
85
+ const king = board.getAtPos(pos);
86
+ const rook = board.positionMap[pos.y][0];
87
+ return (checkPieceName(king, "king") && checkPieceName(rook, "rook") && King.canCastle(king, rook));
88
+ }
89
+ }
90
+ export { King };
@@ -0,0 +1,9 @@
1
+ import type { Move } from "../types/move.js";
2
+ import type { PieceColor } from "../types/piece.js";
3
+ import { Piece } from "./piece.js";
4
+ declare class Knight extends Piece {
5
+ static readonly moves: Move[];
6
+ constructor(color: PieceColor);
7
+ getMoves(): readonly Move[];
8
+ }
9
+ export { Knight };
@@ -0,0 +1,21 @@
1
+ import { Piece } from "./piece.js";
2
+ const KnightMoves = [
3
+ { vec: { dx: 1, dy: 2 }, isSliding: false, canAttack: true },
4
+ { vec: { dx: -1, dy: 2 }, isSliding: false, canAttack: true },
5
+ { vec: { dx: 1, dy: -2 }, isSliding: false, canAttack: true },
6
+ { vec: { dx: -1, dy: -2 }, isSliding: false, canAttack: true },
7
+ { vec: { dx: 2, dy: 1 }, isSliding: false, canAttack: true },
8
+ { vec: { dx: -2, dy: 1 }, isSliding: false, canAttack: true },
9
+ { vec: { dx: 2, dy: -1 }, isSliding: false, canAttack: true },
10
+ { vec: { dx: -2, dy: -1 }, isSliding: false, canAttack: true }
11
+ ];
12
+ class Knight extends Piece {
13
+ static moves = KnightMoves;
14
+ constructor(color) {
15
+ super(color, "knight");
16
+ }
17
+ getMoves() {
18
+ return Knight.moves;
19
+ }
20
+ }
21
+ export { Knight };
@@ -0,0 +1,11 @@
1
+ import { Piece } from "./piece.js";
2
+ import type { PieceColor } from "../types/piece.js";
3
+ import type { Move, Position, MoveGameContext } from "../types/move.js";
4
+ declare class Pawn extends Piece {
5
+ static readonly moves: Move[];
6
+ constructor(color: PieceColor);
7
+ getMoves(): readonly Move[];
8
+ static isLeftEnPassantLegal(game: MoveGameContext, pos: Position): boolean;
9
+ static isRightEnPassantLegal(game: MoveGameContext, pos: Position): boolean;
10
+ }
11
+ export { Pawn };
@@ -0,0 +1,90 @@
1
+ import { Piece } from "./piece.js";
2
+ import { ChessRuleSet } from "../chessRuleSet.js";
3
+ import { BOARD_SIZE } from "../constants/game.js";
4
+ import { getColorMultiplier } from "../utils/color.js";
5
+ const DoubleStepMoveCondition = (game, pos) => {
6
+ const pawn = game.board.getAtPos(pos);
7
+ if (!pawn || pawn.name !== "pawn" || pawn.isMoved)
8
+ return false;
9
+ const multiplier = getColorMultiplier(pawn.color);
10
+ return ChessRuleSet.isPathClear(game, pos, { x: pos.x, y: pos.y + 2 * multiplier });
11
+ };
12
+ const LeftCaptureCondition = (game, pos) => {
13
+ const pawn = game.board.getAtPos(pos);
14
+ if (!pawn || pawn.name !== "pawn" || pos.x === 0)
15
+ return false;
16
+ const y = pos.y + 1 * getColorMultiplier(pawn.color);
17
+ const x = pos.x - 1;
18
+ const capture = game.board.positionMap[y][x];
19
+ return (capture !== null && capture.color !== pawn.color);
20
+ };
21
+ const RightCaptureCondition = (game, pos) => {
22
+ const pawn = game.board.getAtPos(pos);
23
+ if (!pawn || pawn.name !== "pawn" || pos.x === game.board.boardSize - 1)
24
+ return false;
25
+ const y = pos.y + 1 * getColorMultiplier(pawn.color);
26
+ const x = pos.x + 1;
27
+ const capture = game.board.positionMap[y][x];
28
+ return (capture !== null && capture.color !== pawn.color);
29
+ };
30
+ const LeftSideEnPassantCondition = (game, pos) => {
31
+ const pawn = game.board.getAtPos(pos);
32
+ if (!pawn || pawn.name !== "pawn" || pos.x === 0)
33
+ return false;
34
+ const capturePawn = game.board.positionMap[pos.y][pos.x - 1];
35
+ if (!capturePawn || capturePawn.name !== "pawn" || capturePawn.color === pawn.color)
36
+ return false;
37
+ const lastMove = game.lastMoveRecord?.moves[0];
38
+ return (lastMove !== undefined && lastMove.piece === capturePawn && Math.abs(lastMove.from.y - lastMove.to.y) === 2);
39
+ };
40
+ const RightSideEnPassantCondition = (game, pos) => {
41
+ const pawn = game.board.getAtPos(pos);
42
+ if (!pawn || pawn.name !== "pawn" || pos.x === game.board.boardSize - 1)
43
+ return false;
44
+ const capturePawn = game.board.positionMap[pos.y][pos.x + 1];
45
+ if (!capturePawn || capturePawn.name !== "pawn" || capturePawn.color === pawn.color)
46
+ return false;
47
+ const lastMove = game.lastMoveRecord?.moves[0];
48
+ return (lastMove !== undefined && lastMove.piece === capturePawn && Math.abs(lastMove.from.y - lastMove.to.y) === 2);
49
+ };
50
+ const OnLeftSideEnPassant = (game, moveQuery, moveRecord) => {
51
+ const { from } = moveQuery;
52
+ const capturePawnPos = { x: from.x - 1, y: from.y };
53
+ game.addCapture(moveRecord, capturePawnPos);
54
+ };
55
+ const OnRightSideEnPassant = (game, moveQuery, moveRecord) => {
56
+ const { from } = moveQuery;
57
+ const capturePawnPos = { x: from.x + 1, y: from.y };
58
+ game.addCapture(moveRecord, capturePawnPos);
59
+ };
60
+ const HandlePromotion = (game, moveQuery, moveRecord) => {
61
+ const { from, to } = moveQuery;
62
+ const pawn = game.board.getAtPos(from);
63
+ const promotionRank = pawn.color === "white" ? 0 : BOARD_SIZE - 1;
64
+ if (to.y === promotionRank)
65
+ game.addPromotion(moveRecord, moveQuery.to, pawn.color, moveQuery.promoteTo ?? null);
66
+ };
67
+ const PawnMoves = [
68
+ { vec: { dx: 0, dy: 1 }, isSliding: false, canAttack: false, onMove: HandlePromotion },
69
+ { vec: { dx: 0, dy: 2 }, isSliding: false, canAttack: false, condition: DoubleStepMoveCondition }, // Double Step Move
70
+ { vec: { dx: -1, dy: 1 }, isSliding: false, canAttack: true, condition: LeftCaptureCondition, onMove: HandlePromotion }, // Left Capture
71
+ { vec: { dx: 1, dy: 1 }, isSliding: false, canAttack: true, condition: RightCaptureCondition, onMove: HandlePromotion }, // Right Capture
72
+ { vec: { dx: -1, dy: 1 }, isSliding: false, canAttack: false, condition: LeftSideEnPassantCondition, onMove: OnLeftSideEnPassant }, // Left EnPassant
73
+ { vec: { dx: 1, dy: 1 }, isSliding: false, canAttack: false, condition: RightSideEnPassantCondition, onMove: OnRightSideEnPassant }, // Right EnPassant
74
+ ];
75
+ class Pawn extends Piece {
76
+ static moves = PawnMoves;
77
+ constructor(color) {
78
+ super(color, "pawn");
79
+ }
80
+ getMoves() {
81
+ return Pawn.moves;
82
+ }
83
+ static isLeftEnPassantLegal(game, pos) {
84
+ return LeftSideEnPassantCondition(game, pos);
85
+ }
86
+ static isRightEnPassantLegal(game, pos) {
87
+ return RightSideEnPassantCondition(game, pos);
88
+ }
89
+ }
90
+ export { Pawn };
@@ -0,0 +1,12 @@
1
+ import type { Move } from "../types/move.js";
2
+ import type { PieceColor, PieceName } from "../types/piece.js";
3
+ declare abstract class Piece {
4
+ readonly color: PieceColor;
5
+ readonly name: PieceName;
6
+ movesPlayed: number;
7
+ protected constructor(color: PieceColor, name: PieceName);
8
+ static isSameType(p1: Piece, p2: Piece): boolean;
9
+ get isMoved(): boolean;
10
+ abstract getMoves(): readonly Move[];
11
+ }
12
+ export { Piece };
@@ -0,0 +1,14 @@
1
+ class Piece {
2
+ color;
3
+ name;
4
+ movesPlayed = 0;
5
+ constructor(color, name) {
6
+ this.color = color;
7
+ this.name = name;
8
+ }
9
+ static isSameType(p1, p2) {
10
+ return p1.color === p2.color && p1.name === p2.name;
11
+ }
12
+ get isMoved() { return this.movesPlayed !== 0; }
13
+ }
14
+ export { Piece };
@@ -0,0 +1,9 @@
1
+ import { Piece } from "./piece.js";
2
+ import type { Move } from "../types/move.js";
3
+ import type { PieceColor } from "../types/piece.js";
4
+ declare class Queen extends Piece {
5
+ static readonly moves: Move[];
6
+ constructor(color: PieceColor);
7
+ getMoves(): readonly Move[];
8
+ }
9
+ export { Queen };
@@ -0,0 +1,21 @@
1
+ import { Piece } from "./piece.js";
2
+ const QueenMoves = [
3
+ { vec: { dx: 0, dy: 1 }, isSliding: true, canAttack: true },
4
+ { vec: { dx: 0, dy: -1 }, isSliding: true, canAttack: true },
5
+ { vec: { dx: 1, dy: 0 }, isSliding: true, canAttack: true },
6
+ { vec: { dx: -1, dy: 0 }, isSliding: true, canAttack: true },
7
+ { vec: { dx: 1, dy: 1 }, isSliding: true, canAttack: true },
8
+ { vec: { dx: -1, dy: 1 }, isSliding: true, canAttack: true },
9
+ { vec: { dx: -1, dy: -1 }, isSliding: true, canAttack: true },
10
+ { vec: { dx: 1, dy: -1 }, isSliding: true, canAttack: true }
11
+ ];
12
+ class Queen extends Piece {
13
+ static moves = QueenMoves;
14
+ constructor(color) {
15
+ super(color, "queen");
16
+ }
17
+ getMoves() {
18
+ return Queen.moves;
19
+ }
20
+ }
21
+ export { Queen };
@@ -0,0 +1,9 @@
1
+ import { Piece } from "./piece.js";
2
+ import type { Move } from "../types/move.js";
3
+ import type { PieceColor } from "../types/piece.js";
4
+ declare class Rook extends Piece {
5
+ static readonly moves: Move[];
6
+ constructor(color: PieceColor);
7
+ getMoves(): readonly Move[];
8
+ }
9
+ export { Rook };
@@ -0,0 +1,17 @@
1
+ import { Piece } from "./piece.js";
2
+ const RookMoves = [
3
+ { vec: { dx: 0, dy: 1 }, isSliding: true, canAttack: true },
4
+ { vec: { dx: 0, dy: -1 }, isSliding: true, canAttack: true },
5
+ { vec: { dx: 1, dy: 0 }, isSliding: true, canAttack: true },
6
+ { vec: { dx: -1, dy: 0 }, isSliding: true, canAttack: true }
7
+ ];
8
+ class Rook extends Piece {
9
+ static moves = RookMoves;
10
+ constructor(color) {
11
+ super(color, "rook");
12
+ }
13
+ getMoves() {
14
+ return Rook.moves;
15
+ }
16
+ }
17
+ export { Rook };
@@ -0,0 +1,5 @@
1
+ import type { PieceLayoutCode, PieceLike } from "./piece.js";
2
+ import type { MoveChange, PromotionChange, CaptureChange, BoardChange, MoveRecord } from "./move.js";
3
+ type BoardStringLayout = Array<Array<PieceLayoutCode | null>>;
4
+ type PiecePositionMap = Array<Array<PieceLike | null>>;
5
+ export type { PiecePositionMap, BoardStringLayout, MoveChange, PromotionChange, CaptureChange, BoardChange, MoveRecord };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,38 @@
1
+ import type { GameStatus, GameResult } from "../constants/game.js";
2
+ import type { CastleType } from "../constants/castleType.js";
3
+ import type { PiecePositionMap } from "./board.js";
4
+ import type { Position, LegalPosition, MoveQuery, MoveRecord } from "./move.js";
5
+ import type { PieceColor, PieceName, PieceLike } from "./piece.js";
6
+ interface GameBoardContext {
7
+ readonly boardSize: number;
8
+ readonly positionMap: PiecePositionMap;
9
+ getAtPos(pos: Position): PieceLike | null;
10
+ copyBoard(): GameBoardContext;
11
+ isInBounds(pos: Position): boolean;
12
+ findPiece(color: PieceColor, name: PieceName): Position | null;
13
+ getAllPieces(): PieceLike[];
14
+ }
15
+ interface GameContext {
16
+ readonly board: GameBoardContext;
17
+ readonly currentTurnColor: PieceColor;
18
+ readonly moveHistory: readonly MoveRecord[];
19
+ readonly lastMoveRecord: Readonly<MoveRecord> | null;
20
+ readonly lastPawnMoveOrCaptureIndex: number;
21
+ readonly specialRights: Readonly<GameSpecialRights>;
22
+ readonly zobristUpdater: {
23
+ checkThreeFoldRepetition(game: GameContext): boolean;
24
+ };
25
+ evaluateAndBacktrack<T>(moveQuery: MoveQuery<LegalPosition>, fn: (game: GameContext) => T): T | null;
26
+ evaluateAt<T>(moveHistoryIndexes: number[], evaluate: (game: GameContext) => T): T[];
27
+ isSamePosition(board: GameBoardContext, turnColor: PieceColor, specialRights: GameSpecialRights): boolean;
28
+ updateSpecialRights(): void;
29
+ }
30
+ interface GameOutcome {
31
+ status: GameStatus;
32
+ result: GameResult;
33
+ }
34
+ interface GameSpecialRights {
35
+ castlingRights: Record<CastleType, boolean>;
36
+ enPassantFile: number | null;
37
+ }
38
+ export type { GameOutcome, GameSpecialRights, GameContext, GameBoardContext };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,5 @@
1
+ export * from './game.js';
2
+ export * from './move.js';
3
+ export * from './util.js';
4
+ export * from './board.js';
5
+ export * from './piece.js';
@@ -0,0 +1,5 @@
1
+ export * from './game.js';
2
+ export * from './move.js';
3
+ export * from './util.js';
4
+ export * from './board.js';
5
+ export * from './piece.js';
@@ -0,0 +1,96 @@
1
+ import type { PieceColor, PieceName, PieceNameCode, PieceLike } from "./piece.js";
2
+ interface Position {
3
+ x: number;
4
+ y: number;
5
+ }
6
+ interface MoveBoardContext {
7
+ boardSize: number;
8
+ positionMap: Array<Array<PieceLike | null>>;
9
+ getAtPos(pos: Position): PieceLike | null;
10
+ copyBoard(): MoveBoardContext;
11
+ isInBounds(pos: Position): boolean;
12
+ findPiece(color: PieceColor, name: PieceName): Position | null;
13
+ getAllPieces(): PieceLike[];
14
+ }
15
+ interface MoveGameContext {
16
+ board: MoveBoardContext;
17
+ lastMoveRecord: MoveRecord | null;
18
+ evaluateAndBacktrack<T>(moveQuery: MoveQuery<LegalPosition>, fn: (game: MoveGameContext) => T): T | null;
19
+ }
20
+ interface MoveExecutionContext extends MoveGameContext {
21
+ addMove(moveRecord: MoveRecord, moveQuery: MoveQuery<Position>): void;
22
+ addCapture(moveRecord: MoveRecord, from: Position): void;
23
+ addPromotion(moveRecord: MoveRecord, to: Position, promotionColor: PieceColor, promotionName: PieceNameCode | null): void;
24
+ }
25
+ type MoveConditionFunction = (game: MoveGameContext, pos: Position) => boolean;
26
+ type OnMove = (game: MoveExecutionContext, moveQuery: MoveQuery<Position>, moveRecord: MoveRecord) => void;
27
+ interface MoveChange {
28
+ type: "move";
29
+ piece: PieceLike;
30
+ from: Position;
31
+ to: Position;
32
+ }
33
+ interface PromotionChange {
34
+ type: "promotion";
35
+ piece: PieceLike;
36
+ to: Position;
37
+ }
38
+ interface CaptureChange {
39
+ type: "capture";
40
+ piece: PieceLike;
41
+ from: Position;
42
+ }
43
+ type BoardChange = MoveChange | PromotionChange | CaptureChange;
44
+ interface MoveRecord {
45
+ moves: MoveChange[];
46
+ captures: CaptureChange[];
47
+ promotions: PromotionChange[];
48
+ }
49
+ interface LegalPosition extends Position {
50
+ onMove?: OnMove;
51
+ }
52
+ interface MoveVec2 {
53
+ dx: number;
54
+ dy: number;
55
+ }
56
+ interface MoveQuery<T extends Position> {
57
+ from: T;
58
+ to: T;
59
+ promoteTo?: PieceNameCode;
60
+ }
61
+ interface Move {
62
+ vec: MoveVec2;
63
+ isSliding: boolean;
64
+ canAttack: boolean;
65
+ condition?: MoveConditionFunction;
66
+ onMove?: OnMove;
67
+ }
68
+ interface LegalMoveRemark {
69
+ result: "Legal Move";
70
+ executed: true;
71
+ }
72
+ interface InvalidMoveRemark {
73
+ result: "Invalid Move";
74
+ remark?: string;
75
+ executed: false;
76
+ }
77
+ interface IllegalMoveRemark {
78
+ result: "Illegal Move";
79
+ remark?: string;
80
+ executed: false;
81
+ }
82
+ interface InvalidTurnRemark {
83
+ result: "Invalid Turn";
84
+ remark?: `Wait for ${PieceColor}'s turn!`;
85
+ executed: false;
86
+ }
87
+ type MoveRemark = LegalMoveRemark | InvalidMoveRemark | IllegalMoveRemark | InvalidTurnRemark;
88
+ interface ValidMove {
89
+ valid: false;
90
+ }
91
+ interface InvalidMove {
92
+ valid: true;
93
+ move: LegalPosition;
94
+ }
95
+ type IsValidMove = ValidMove | InvalidMove;
96
+ export type { Position, LegalPosition, MoveVec2, MoveConditionFunction, OnMove, Move, MoveQuery, MoveRecord, MoveChange, PromotionChange, CaptureChange, BoardChange, MoveRemark, ValidMove, InvalidMove, IsValidMove, MoveGameContext, MoveExecutionContext };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,23 @@
1
+ type PieceColor = "white" | "black";
2
+ type PieceName = "pawn" | "knight" | "bishop" | "rook" | "queen" | "king";
3
+ type PieceColorCode = "w" | "b";
4
+ type PieceNameCode = "P" | "N" | "B" | "R" | "Q" | "K";
5
+ type PieceLayoutCode = `${PieceColorCode}${PieceNameCode}`;
6
+ interface PieceLike {
7
+ readonly color: PieceColor;
8
+ readonly name: PieceName;
9
+ movesPlayed: number;
10
+ readonly isMoved: boolean;
11
+ getMoves(): readonly PieceMove[];
12
+ }
13
+ interface PieceMove {
14
+ vec: {
15
+ dx: number;
16
+ dy: number;
17
+ };
18
+ isSliding: boolean;
19
+ canAttack: boolean;
20
+ condition?: (...args: any[]) => boolean;
21
+ onMove?: (...args: any[]) => void;
22
+ }
23
+ export type { PieceColor, PieceName, PieceColorCode, PieceNameCode, PieceLayoutCode, PieceLike, PieceMove };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,10 @@
1
+ type SoftFixedArray<T, N extends number> = T[] & {
2
+ length: N;
3
+ };
4
+ type SoftFixedArrayGrid<T, N extends number> = SoftFixedArray<T, N>[] & {
5
+ length: N;
6
+ };
7
+ type StaticMethodsMatching<T, TargetSignature extends (...args: any[]) => any> = {
8
+ [K in keyof T]: T[K] extends TargetSignature ? T[K] : never;
9
+ }[keyof T];
10
+ export type { SoftFixedArray, SoftFixedArrayGrid, StaticMethodsMatching };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,4 @@
1
+ import type { PieceColor } from "../types/piece.js";
2
+ declare function getColorMultiplier(color: PieceColor): -1 | 1;
3
+ declare function switchColor(color: PieceColor): PieceColor;
4
+ export { getColorMultiplier, switchColor };
@@ -0,0 +1,7 @@
1
+ function getColorMultiplier(color) {
2
+ return color === "white" ? -1 : 1;
3
+ }
4
+ function switchColor(color) {
5
+ return color === "white" ? "black" : "white";
6
+ }
7
+ export { getColorMultiplier, switchColor };
@@ -0,0 +1,6 @@
1
+ import type { PieceColor, PieceName, PieceLike } from "../types/piece.js";
2
+ declare function checkPieceColorAndName(piece: PieceLike | null, color: PieceColor, name: PieceName): boolean;
3
+ declare function checkPieceName<T extends PieceName>(piece: PieceLike | null, name: T): piece is PieceLike & {
4
+ name: T;
5
+ };
6
+ export { checkPieceColorAndName, checkPieceName };
@@ -0,0 +1,7 @@
1
+ function checkPieceColorAndName(piece, color, name) {
2
+ return (piece !== null && piece.color === color && piece.name === name);
3
+ }
4
+ function checkPieceName(piece, name) {
5
+ return (piece !== null && piece.name === name);
6
+ }
7
+ export { checkPieceColorAndName, checkPieceName };
@@ -0,0 +1,6 @@
1
+ declare class SplitMix64 {
2
+ private state;
3
+ constructor(seed: bigint);
4
+ next(): bigint;
5
+ }
6
+ export { SplitMix64 };
@@ -0,0 +1,15 @@
1
+ class SplitMix64 {
2
+ state;
3
+ constructor(seed) {
4
+ this.state = seed;
5
+ }
6
+ next() {
7
+ this.state += 0x9e3779b97f4a7c15n;
8
+ let z = this.state;
9
+ z = (z ^ (z >> 30n)) * 0xbf58476d1ce4e5b9n;
10
+ z = (z ^ (z >> 27n)) * 0x94d049bb133111ebn;
11
+ z ^= z >> 31n;
12
+ return z & 0xffffffffffffffffn;
13
+ }
14
+ }
15
+ export { SplitMix64 };
package/package.json CHANGED
@@ -1,6 +1,32 @@
1
1
  {
2
2
  "name": "chess-rule-engine",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "A TypeScript chess rules engine for legal move validation and game-state management",
5
+ "private": false,
6
+ "type": "module",
7
+ "main": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "scripts": {
19
+ "build": "tsc -p tsconfig.json",
20
+ "dev": "tsc watch src/index.ts"
21
+ },
22
+ "keywords": [
23
+ "chess",
24
+ "chess-rule-engine",
25
+ "typescript"
26
+ ],
27
+ "license": "ISC",
28
+ "devDependencies": {
29
+ "madge": "^8.0.0",
30
+ "typescript": "^7.0.2"
31
+ }
32
+ }