@connectorvol/chessops 0.15.1
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.
- package/LICENSE.txt +674 -0
- package/README.md +61 -0
- package/dist/cjs/attacks.js +152 -0
- package/dist/cjs/attacks.js.map +1 -0
- package/dist/cjs/board.js +143 -0
- package/dist/cjs/board.js.map +1 -0
- package/dist/cjs/chess.js +638 -0
- package/dist/cjs/chess.js.map +1 -0
- package/dist/cjs/compat.js +89 -0
- package/dist/cjs/compat.js.map +1 -0
- package/dist/cjs/debug.js +103 -0
- package/dist/cjs/debug.js.map +1 -0
- package/dist/cjs/fen.js +325 -0
- package/dist/cjs/fen.js.map +1 -0
- package/dist/cjs/index.js +94 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/pgn.js +796 -0
- package/dist/cjs/pgn.js.map +1 -0
- package/dist/cjs/san.js +174 -0
- package/dist/cjs/san.js.map +1 -0
- package/dist/cjs/setup.js +167 -0
- package/dist/cjs/setup.js.map +1 -0
- package/dist/cjs/squareSet.js +206 -0
- package/dist/cjs/squareSet.js.map +1 -0
- package/dist/cjs/transform.js +57 -0
- package/dist/cjs/transform.js.map +1 -0
- package/dist/cjs/types.js +24 -0
- package/dist/cjs/types.js.map +1 -0
- package/dist/cjs/util.js +104 -0
- package/dist/cjs/util.js.map +1 -0
- package/dist/cjs/variant.js +833 -0
- package/dist/cjs/variant.js.map +1 -0
- package/dist/esm/attacks.js +140 -0
- package/dist/esm/attacks.js.map +1 -0
- package/dist/esm/board.js +138 -0
- package/dist/esm/board.js.map +1 -0
- package/dist/esm/chess.js +624 -0
- package/dist/esm/chess.js.map +1 -0
- package/dist/esm/compat.js +81 -0
- package/dist/esm/compat.js.map +1 -0
- package/dist/esm/debug.js +94 -0
- package/dist/esm/debug.js.map +1 -0
- package/dist/esm/fen.js +308 -0
- package/dist/esm/fen.js.map +1 -0
- package/dist/esm/index.js +15 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/pgn.js +769 -0
- package/dist/esm/pgn.js.map +1 -0
- package/dist/esm/san.js +167 -0
- package/dist/esm/san.js.map +1 -0
- package/dist/esm/setup.js +157 -0
- package/dist/esm/setup.js.map +1 -0
- package/dist/esm/squareSet.js +202 -0
- package/dist/esm/squareSet.js.map +1 -0
- package/dist/esm/transform.js +48 -0
- package/dist/esm/transform.js.map +1 -0
- package/dist/esm/types.js +19 -0
- package/dist/esm/types.js.map +1 -0
- package/dist/esm/util.js +87 -0
- package/dist/esm/util.js.map +1 -0
- package/dist/esm/variant.js +812 -0
- package/dist/esm/variant.js.map +1 -0
- package/dist/types/attacks.d.ts +58 -0
- package/dist/types/board.d.ts +62 -0
- package/dist/types/chess.d.ts +82 -0
- package/dist/types/compat.d.ts +26 -0
- package/dist/types/debug.d.ts +10 -0
- package/dist/types/fen.d.ts +40 -0
- package/dist/types/index.d.ts +14 -0
- package/dist/types/pgn.d.ts +203 -0
- package/dist/types/san.d.ts +6 -0
- package/dist/types/setup.d.ts +65 -0
- package/dist/types/squareSet.d.ts +50 -0
- package/dist/types/transform.d.ts +9 -0
- package/dist/types/types.d.ts +58 -0
- package/dist/types/util.d.ts +21 -0
- package/dist/types/variant.d.ts +92 -0
- package/package.json +86 -0
- package/src/attacks.ts +160 -0
- package/src/board.ts +168 -0
- package/src/chess.ts +687 -0
- package/src/compat.ts +120 -0
- package/src/debug.ts +100 -0
- package/src/fen.ts +328 -0
- package/src/index.ts +85 -0
- package/src/pgn.ts +876 -0
- package/src/san.ts +190 -0
- package/src/setup.ts +203 -0
- package/src/squareSet.ts +243 -0
- package/src/transform.ts +49 -0
- package/src/types.ts +93 -0
- package/src/util.ts +116 -0
- package/src/variant.ts +939 -0
package/src/types.ts
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
export const FILE_NAMES = ["a", "b", "c", "d", "e", "f", "g", "h"] as const;
|
|
2
|
+
|
|
3
|
+
export type FileName = (typeof FILE_NAMES)[number];
|
|
4
|
+
|
|
5
|
+
export const RANK_NAMES = ["1", "2", "3", "4", "5", "6", "7", "8"] as const;
|
|
6
|
+
|
|
7
|
+
export type RankName = (typeof RANK_NAMES)[number];
|
|
8
|
+
|
|
9
|
+
export type Square = number;
|
|
10
|
+
|
|
11
|
+
export type SquareName = `${FileName}${RankName}`;
|
|
12
|
+
|
|
13
|
+
export const ROLE_CHARS = ["q", "n", "r", "b", "p", "k"] as const;
|
|
14
|
+
|
|
15
|
+
export type RoleChar = (typeof ROLE_CHARS)[number];
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Indexable by square indices.
|
|
19
|
+
*/
|
|
20
|
+
export type BySquare<T> = T[];
|
|
21
|
+
|
|
22
|
+
export const COLORS = ["white", "black"] as const;
|
|
23
|
+
|
|
24
|
+
export type Color = (typeof COLORS)[number];
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Indexable by `white` and `black`.
|
|
28
|
+
*/
|
|
29
|
+
export type ByColor<T> = {
|
|
30
|
+
[color in Color]: T;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export const ROLES = ["pawn", "knight", "bishop", "rook", "queen", "king"] as const;
|
|
34
|
+
|
|
35
|
+
export type Role = (typeof ROLES)[number];
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Indexable by `pawn`, `knight`, `bishop`, `rook`, `queen`, and `king`.
|
|
39
|
+
*/
|
|
40
|
+
export type ByRole<T> = {
|
|
41
|
+
[role in Role]: T;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export const CASTLING_SIDES = ["a", "h"] as const;
|
|
45
|
+
|
|
46
|
+
export type CastlingSide = (typeof CASTLING_SIDES)[number];
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Indexable by `a` and `h`.
|
|
50
|
+
*/
|
|
51
|
+
export type ByCastlingSide<T> = {
|
|
52
|
+
[side in CastlingSide]: T;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export interface Piece {
|
|
56
|
+
role: Role;
|
|
57
|
+
color: Color;
|
|
58
|
+
promoted?: boolean;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export interface NormalMove {
|
|
62
|
+
from: Square;
|
|
63
|
+
to: Square;
|
|
64
|
+
promotion?: Role;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export interface DropMove {
|
|
68
|
+
role: Role;
|
|
69
|
+
to: Square;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export type Move = NormalMove | DropMove;
|
|
73
|
+
|
|
74
|
+
export const isDrop = (v: Move): v is DropMove => "role" in v;
|
|
75
|
+
|
|
76
|
+
export const isNormal = (v: Move): v is NormalMove => "from" in v;
|
|
77
|
+
|
|
78
|
+
export const RULES = [
|
|
79
|
+
"chess",
|
|
80
|
+
"antichess",
|
|
81
|
+
"kingofthehill",
|
|
82
|
+
"3check",
|
|
83
|
+
"atomic",
|
|
84
|
+
"horde",
|
|
85
|
+
"racingkings",
|
|
86
|
+
"crazyhouse",
|
|
87
|
+
] as const;
|
|
88
|
+
|
|
89
|
+
export type Rules = (typeof RULES)[number];
|
|
90
|
+
|
|
91
|
+
export interface Outcome {
|
|
92
|
+
winner: Color | undefined;
|
|
93
|
+
}
|
package/src/util.ts
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CastlingSide,
|
|
3
|
+
Color,
|
|
4
|
+
FILE_NAMES,
|
|
5
|
+
isDrop,
|
|
6
|
+
isNormal,
|
|
7
|
+
Move,
|
|
8
|
+
RANK_NAMES,
|
|
9
|
+
Role,
|
|
10
|
+
type RoleChar,
|
|
11
|
+
Square,
|
|
12
|
+
SquareName,
|
|
13
|
+
} from "./types.js";
|
|
14
|
+
|
|
15
|
+
export const defined = <A>(v: A | undefined): v is A => v !== undefined;
|
|
16
|
+
|
|
17
|
+
export const opposite = (color: Color): Color => (color === "white" ? "black" : "white");
|
|
18
|
+
|
|
19
|
+
export const squareRank = (square: Square): number => square >> 3;
|
|
20
|
+
|
|
21
|
+
export const squareFile = (square: Square): number => square & 0x7;
|
|
22
|
+
|
|
23
|
+
export const squareFromCoords = (file: number, rank: number): Square | undefined =>
|
|
24
|
+
0 <= file && file < 8 && 0 <= rank && rank < 8 ? file + 8 * rank : undefined;
|
|
25
|
+
|
|
26
|
+
export const roleToChar = (role: Role): RoleChar => {
|
|
27
|
+
switch (role) {
|
|
28
|
+
case "pawn":
|
|
29
|
+
return "p";
|
|
30
|
+
case "knight":
|
|
31
|
+
return "n";
|
|
32
|
+
case "bishop":
|
|
33
|
+
return "b";
|
|
34
|
+
case "rook":
|
|
35
|
+
return "r";
|
|
36
|
+
case "queen":
|
|
37
|
+
return "q";
|
|
38
|
+
case "king":
|
|
39
|
+
return "k";
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export function charToRole(ch: RoleChar | Uppercase<RoleChar>): Role;
|
|
44
|
+
export function charToRole(ch: string): Role | undefined;
|
|
45
|
+
export function charToRole(ch: string): Role | undefined {
|
|
46
|
+
switch (ch.toLowerCase()) {
|
|
47
|
+
case "p":
|
|
48
|
+
return "pawn";
|
|
49
|
+
case "n":
|
|
50
|
+
return "knight";
|
|
51
|
+
case "b":
|
|
52
|
+
return "bishop";
|
|
53
|
+
case "r":
|
|
54
|
+
return "rook";
|
|
55
|
+
case "q":
|
|
56
|
+
return "queen";
|
|
57
|
+
case "k":
|
|
58
|
+
return "king";
|
|
59
|
+
default:
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function parseSquare(str: SquareName): Square;
|
|
65
|
+
export function parseSquare(str: string): Square | undefined;
|
|
66
|
+
export function parseSquare(str: string): Square | undefined {
|
|
67
|
+
if (str.length !== 2) return;
|
|
68
|
+
return squareFromCoords(
|
|
69
|
+
str.charCodeAt(0) - "a".charCodeAt(0),
|
|
70
|
+
str.charCodeAt(1) - "1".charCodeAt(0),
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export const makeSquare = (square: Square): SquareName =>
|
|
75
|
+
(FILE_NAMES[squareFile(square)] + RANK_NAMES[squareRank(square)]) as SquareName;
|
|
76
|
+
|
|
77
|
+
export const parseUci = (str: string): Move | undefined => {
|
|
78
|
+
if (str[1] === "@" && str.length === 4) {
|
|
79
|
+
const role = charToRole(str[0]);
|
|
80
|
+
const to = parseSquare(str.slice(2));
|
|
81
|
+
if (role && defined(to)) return { role, to };
|
|
82
|
+
} else if (str.length === 4 || str.length === 5) {
|
|
83
|
+
const from = parseSquare(str.slice(0, 2));
|
|
84
|
+
const to = parseSquare(str.slice(2, 4));
|
|
85
|
+
let promotion: Role | undefined;
|
|
86
|
+
if (str.length === 5) {
|
|
87
|
+
promotion = charToRole(str[4]);
|
|
88
|
+
if (!promotion) return;
|
|
89
|
+
}
|
|
90
|
+
if (defined(from) && defined(to)) return { from, to, promotion };
|
|
91
|
+
}
|
|
92
|
+
return;
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
export const moveEquals = (left: Move, right: Move): boolean => {
|
|
96
|
+
if (left.to !== right.to) return false;
|
|
97
|
+
if (isDrop(left)) return isDrop(right) && left.role === right.role;
|
|
98
|
+
else return isNormal(right) && left.from === right.from && left.promotion === right.promotion;
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Converts a move to UCI notation, like `g1f3` for a normal move,
|
|
103
|
+
* `a7a8q` for promotion to a queen, and `Q@f7` for a Crazyhouse drop.
|
|
104
|
+
*/
|
|
105
|
+
export const makeUci = (move: Move): string =>
|
|
106
|
+
isDrop(move)
|
|
107
|
+
? `${roleToChar(move.role).toUpperCase()}@${makeSquare(move.to)}`
|
|
108
|
+
: makeSquare(move.from) +
|
|
109
|
+
makeSquare(move.to) +
|
|
110
|
+
(move.promotion ? roleToChar(move.promotion) : "");
|
|
111
|
+
|
|
112
|
+
export const kingCastlesTo = (color: Color, side: CastlingSide): Square =>
|
|
113
|
+
color === "white" ? (side === "a" ? 2 : 6) : side === "a" ? 58 : 62;
|
|
114
|
+
|
|
115
|
+
export const rookCastlesTo = (color: Color, side: CastlingSide): Square =>
|
|
116
|
+
color === "white" ? (side === "a" ? 3 : 5) : side === "a" ? 59 : 61;
|