@react-chess-tools/react-chess-puzzle 0.1.4 → 0.2.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.
- package/CHANGELOG.md +13 -0
- package/package.json +2 -3
- package/src/components/ChessPuzzle/ChessPuzzle.stories.tsx +85 -0
- package/src/components/ChessPuzzle/index.ts +11 -0
- package/src/components/ChessPuzzle/parts/Hint.tsx +46 -0
- package/src/components/ChessPuzzle/parts/PuzzleBoard.tsx +37 -0
- package/src/components/ChessPuzzle/parts/Reset.tsx +51 -0
- package/src/components/ChessPuzzle/parts/Root.tsx +41 -0
- package/src/hooks/reducer.ts +160 -0
- package/src/hooks/useChessPuzzle.ts +85 -0
- package/src/hooks/useChessPuzzleContext.ts +16 -0
- package/src/index.ts +2 -0
- package/src/utils/index.ts +103 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# @react-chess-tools/react-chess-puzzle
|
|
2
|
+
|
|
3
|
+
## 0.2.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- ea0eafb: Add changesets versioning
|
|
8
|
+
setup `changesets` and created the first changeset for the available packages. Removed release-it
|
|
9
|
+
|
|
10
|
+
### Patch Changes
|
|
11
|
+
|
|
12
|
+
- Updated dependencies [ea0eafb]
|
|
13
|
+
- @react-chess-tools/react-chess-game@0.2.0
|
package/package.json
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@react-chess-tools/react-chess-puzzle",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "A lightweight, customizable React component library for rendering and interacting with chess puzzles.",
|
|
5
5
|
"main": "dist/index.mjs",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
7
7
|
"types": "dist/index.d.mts",
|
|
8
8
|
"scripts": {
|
|
9
9
|
"build": "tsup src/index.ts",
|
|
10
|
-
"release": "npm run build && release-it --npm.publish=true",
|
|
11
10
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
12
11
|
},
|
|
13
12
|
"keywords": [
|
|
@@ -33,7 +32,7 @@
|
|
|
33
32
|
"author": "Daniele Cammareri <daniele.cammareri@gmail.com>",
|
|
34
33
|
"license": "MIT",
|
|
35
34
|
"dependencies": {
|
|
36
|
-
"@react-chess-tools/react-chess-game": "0.
|
|
35
|
+
"@react-chess-tools/react-chess-game": "0.2.0",
|
|
37
36
|
"chess.js": "^1.0.0-beta.6",
|
|
38
37
|
"lodash": "^4.17.21"
|
|
39
38
|
},
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import type { Meta } from "@storybook/react";
|
|
2
|
+
|
|
3
|
+
import React from "react";
|
|
4
|
+
import { RootProps } from "./parts/Root";
|
|
5
|
+
import { ChessPuzzle } from ".";
|
|
6
|
+
|
|
7
|
+
const puzzles = [
|
|
8
|
+
{
|
|
9
|
+
fen: "4kb1r/p2r1ppp/4qn2/1B2p1B1/4P3/1Q6/PPP2PPP/2KR4 w k - 0 1",
|
|
10
|
+
moves: ["Bxd7+", "Nxd7", "Qb8+", "Nxb8", "Rd8#"],
|
|
11
|
+
makeFirstMove: false,
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
fen: "6k1/5p1p/p1q1p1p1/1pB1P3/1Pr3Pn/P4P1P/4Q3/3R2K1 b - - 0 31",
|
|
15
|
+
moves: ["h4f3", "e2f3", "c4c5", "d1d8", "g8g7", "f3f6"],
|
|
16
|
+
makeFirstMove: true,
|
|
17
|
+
},
|
|
18
|
+
];
|
|
19
|
+
|
|
20
|
+
// More on how to set up stories at: https://storybook.js.org/docs/react/writing-stories/introduction
|
|
21
|
+
const meta = {
|
|
22
|
+
title: "react-chess-puzzle/Components/Puzzle",
|
|
23
|
+
component: ChessPuzzle.Root,
|
|
24
|
+
tags: ["components", "puzzle"],
|
|
25
|
+
argTypes: {
|
|
26
|
+
onSolve: { action: "onSolve" },
|
|
27
|
+
onFail: { action: "onFail" },
|
|
28
|
+
},
|
|
29
|
+
parameters: {
|
|
30
|
+
actions: { argTypesRegex: "^_on.*" },
|
|
31
|
+
},
|
|
32
|
+
decorators: [
|
|
33
|
+
(Story) => (
|
|
34
|
+
<div style={{ width: "400px" }}>
|
|
35
|
+
{/* 👇 Decorators in Storybook also accept a function. Replace <Story/> with Story() to enable it */}
|
|
36
|
+
<Story />
|
|
37
|
+
</div>
|
|
38
|
+
),
|
|
39
|
+
],
|
|
40
|
+
} satisfies Meta<typeof ChessPuzzle.Root>;
|
|
41
|
+
|
|
42
|
+
export default meta;
|
|
43
|
+
|
|
44
|
+
// More on writing stories with args: https://storybook.js.org/docs/react/writing-stories/args
|
|
45
|
+
|
|
46
|
+
export const Example = (args: RootProps) => {
|
|
47
|
+
const [puzzleIndex, setPuzzleIndex] = React.useState(0);
|
|
48
|
+
const puzzle = puzzles[puzzleIndex];
|
|
49
|
+
return (
|
|
50
|
+
<div>
|
|
51
|
+
<ChessPuzzle.Root {...args} puzzle={puzzle}>
|
|
52
|
+
<ChessPuzzle.Board />
|
|
53
|
+
<ChessPuzzle.Reset asChild>
|
|
54
|
+
<button>restart</button>
|
|
55
|
+
</ChessPuzzle.Reset>
|
|
56
|
+
<ChessPuzzle.Reset
|
|
57
|
+
asChild
|
|
58
|
+
puzzle={puzzles[(puzzleIndex + 1) % puzzles.length]}
|
|
59
|
+
onReset={() => setPuzzleIndex((puzzleIndex + 1) % puzzles.length)}
|
|
60
|
+
>
|
|
61
|
+
<button>next</button>
|
|
62
|
+
</ChessPuzzle.Reset>
|
|
63
|
+
<ChessPuzzle.Hint>hint</ChessPuzzle.Hint>
|
|
64
|
+
</ChessPuzzle.Root>
|
|
65
|
+
</div>
|
|
66
|
+
);
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
export const Underpromotion = (args: RootProps) => {
|
|
70
|
+
const puzzle = {
|
|
71
|
+
fen: "8/8/5R1p/8/3pb1P1/kpKp4/8/8 w - - 0 54",
|
|
72
|
+
moves: ["c3d4", "d3d2", "d4c3", "d2d1n"],
|
|
73
|
+
makeFirstMove: true,
|
|
74
|
+
};
|
|
75
|
+
return (
|
|
76
|
+
<div>
|
|
77
|
+
<ChessPuzzle.Root {...args} puzzle={puzzle}>
|
|
78
|
+
<ChessPuzzle.Board />
|
|
79
|
+
<ChessPuzzle.Reset asChild>
|
|
80
|
+
<button>done! Restart</button>
|
|
81
|
+
</ChessPuzzle.Reset>
|
|
82
|
+
</ChessPuzzle.Root>
|
|
83
|
+
</div>
|
|
84
|
+
);
|
|
85
|
+
};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { Status, isClickableElement } from "../../../utils";
|
|
3
|
+
import { useChessPuzzleContext } from "../../..";
|
|
4
|
+
|
|
5
|
+
export interface HintProps {
|
|
6
|
+
asChild?: boolean;
|
|
7
|
+
showOn?: Status[];
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const defaultShowOn: Status[] = ["not-started", "in-progress"];
|
|
11
|
+
|
|
12
|
+
export const Hint: React.FC<React.PropsWithChildren<HintProps>> = ({
|
|
13
|
+
children,
|
|
14
|
+
asChild,
|
|
15
|
+
showOn = defaultShowOn,
|
|
16
|
+
}) => {
|
|
17
|
+
const puzzleContext = useChessPuzzleContext();
|
|
18
|
+
if (!puzzleContext) {
|
|
19
|
+
throw new Error("PuzzleContext not found");
|
|
20
|
+
}
|
|
21
|
+
const { onHint, status } = puzzleContext;
|
|
22
|
+
const handleClick = () => {
|
|
23
|
+
onHint();
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
if (!showOn.includes(status)) {
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (asChild) {
|
|
31
|
+
const child = React.Children.only(children);
|
|
32
|
+
if (isClickableElement(child)) {
|
|
33
|
+
return React.cloneElement(child, {
|
|
34
|
+
onClick: handleClick,
|
|
35
|
+
});
|
|
36
|
+
} else {
|
|
37
|
+
throw new Error("Change child must be a clickable element");
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return (
|
|
42
|
+
<button type="button" onClick={handleClick}>
|
|
43
|
+
{children}
|
|
44
|
+
</button>
|
|
45
|
+
);
|
|
46
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import {
|
|
3
|
+
ChessGame,
|
|
4
|
+
useChessGameContext,
|
|
5
|
+
} from "@react-chess-tools/react-chess-game";
|
|
6
|
+
import { getCustomSquareStyles, stringToMove } from "../../../utils";
|
|
7
|
+
import { useChessPuzzleContext } from "../../..";
|
|
8
|
+
|
|
9
|
+
export interface PuzzleBoardProps
|
|
10
|
+
extends React.ComponentProps<typeof ChessGame.Board> {}
|
|
11
|
+
export const PuzzleBoard: React.FC<PuzzleBoardProps> = ({ ...rest }) => {
|
|
12
|
+
const puzzleContext = useChessPuzzleContext();
|
|
13
|
+
const gameContext = useChessGameContext();
|
|
14
|
+
|
|
15
|
+
if (!puzzleContext) {
|
|
16
|
+
throw new Error("PuzzleContext not found");
|
|
17
|
+
}
|
|
18
|
+
if (!gameContext) {
|
|
19
|
+
throw new Error("ChessGameContext not found");
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const { game } = gameContext;
|
|
23
|
+
const { status, hint, isPlayerTurn, nextMove } = puzzleContext;
|
|
24
|
+
|
|
25
|
+
return (
|
|
26
|
+
<ChessGame.Board
|
|
27
|
+
customSquareStyles={getCustomSquareStyles(
|
|
28
|
+
status,
|
|
29
|
+
hint,
|
|
30
|
+
isPlayerTurn,
|
|
31
|
+
game,
|
|
32
|
+
stringToMove(game, nextMove),
|
|
33
|
+
)}
|
|
34
|
+
{...rest}
|
|
35
|
+
/>
|
|
36
|
+
);
|
|
37
|
+
};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { isClickableElement, type Puzzle, type Status } from "../../../utils";
|
|
3
|
+
import { useChessPuzzleContext } from "../../..";
|
|
4
|
+
|
|
5
|
+
export interface ResetProps {
|
|
6
|
+
asChild?: boolean;
|
|
7
|
+
puzzle?: Puzzle;
|
|
8
|
+
onReset?: () => void;
|
|
9
|
+
showOn?: Status[];
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const defaultShowOn: Status[] = ["failed", "solved"];
|
|
13
|
+
|
|
14
|
+
export const Reset: React.FC<React.PropsWithChildren<ResetProps>> = ({
|
|
15
|
+
children,
|
|
16
|
+
asChild,
|
|
17
|
+
puzzle,
|
|
18
|
+
onReset,
|
|
19
|
+
showOn = defaultShowOn,
|
|
20
|
+
}) => {
|
|
21
|
+
const puzzleContext = useChessPuzzleContext();
|
|
22
|
+
if (!puzzleContext) {
|
|
23
|
+
throw new Error("PuzzleContext not found");
|
|
24
|
+
}
|
|
25
|
+
const { changePuzzle, status } = puzzleContext;
|
|
26
|
+
const handleClick = () => {
|
|
27
|
+
changePuzzle(puzzle || puzzleContext.puzzle);
|
|
28
|
+
onReset?.();
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
if (!showOn.includes(status)) {
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (asChild) {
|
|
36
|
+
const child = React.Children.only(children);
|
|
37
|
+
if (isClickableElement(child)) {
|
|
38
|
+
return React.cloneElement(child, {
|
|
39
|
+
onClick: handleClick,
|
|
40
|
+
});
|
|
41
|
+
} else {
|
|
42
|
+
throw new Error("Change child must be a clickable element");
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return (
|
|
47
|
+
<button type="button" onClick={handleClick}>
|
|
48
|
+
{children}
|
|
49
|
+
</button>
|
|
50
|
+
);
|
|
51
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { Puzzle, getOrientation } from "../../../utils";
|
|
3
|
+
import { useChessPuzzle } from "../../../hooks/useChessPuzzle";
|
|
4
|
+
import { ChessGame } from "@react-chess-tools/react-chess-game";
|
|
5
|
+
import { ChessPuzzleContext } from "../../../hooks/useChessPuzzleContext";
|
|
6
|
+
|
|
7
|
+
export interface RootProps {
|
|
8
|
+
puzzle: Puzzle;
|
|
9
|
+
onSolve?: (changePuzzle: (puzzle: Puzzle) => void) => void;
|
|
10
|
+
onFail?: (changePuzzle: (puzzle: Puzzle) => void) => void;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const PuzzleRoot: React.FC<React.PropsWithChildren<RootProps>> = ({
|
|
14
|
+
puzzle,
|
|
15
|
+
onSolve,
|
|
16
|
+
onFail,
|
|
17
|
+
children,
|
|
18
|
+
}) => {
|
|
19
|
+
const context = useChessPuzzle(puzzle, onSolve, onFail);
|
|
20
|
+
|
|
21
|
+
return (
|
|
22
|
+
<ChessPuzzleContext.Provider value={context}>
|
|
23
|
+
{children}
|
|
24
|
+
</ChessPuzzleContext.Provider>
|
|
25
|
+
);
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export const Root: React.FC<React.PropsWithChildren<RootProps>> = ({
|
|
29
|
+
puzzle,
|
|
30
|
+
onSolve,
|
|
31
|
+
onFail,
|
|
32
|
+
children,
|
|
33
|
+
}) => {
|
|
34
|
+
return (
|
|
35
|
+
<ChessGame.Root fen={puzzle.fen} orientation={getOrientation(puzzle)}>
|
|
36
|
+
<PuzzleRoot puzzle={puzzle} onSolve={onSolve} onFail={onFail}>
|
|
37
|
+
{children}
|
|
38
|
+
</PuzzleRoot>
|
|
39
|
+
</ChessGame.Root>
|
|
40
|
+
);
|
|
41
|
+
};
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { Chess, Move } from "chess.js";
|
|
2
|
+
import { useChessGame } from "@react-chess-tools/react-chess-game";
|
|
3
|
+
import { getOrientation, type Puzzle, type Hint, type Status } from "../utils";
|
|
4
|
+
|
|
5
|
+
export type State = {
|
|
6
|
+
puzzle: Puzzle;
|
|
7
|
+
currentMoveIndex: number;
|
|
8
|
+
status: Status;
|
|
9
|
+
nextMove?: string | null;
|
|
10
|
+
hint: Hint;
|
|
11
|
+
needCpuMove: boolean;
|
|
12
|
+
isPlayerTurn: boolean;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export type Action =
|
|
16
|
+
| {
|
|
17
|
+
type: "INITIALIZE";
|
|
18
|
+
payload: {
|
|
19
|
+
puzzle: Puzzle;
|
|
20
|
+
setPosition: ReturnType<typeof useChessGame>["methods"]["setPosition"];
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
| {
|
|
24
|
+
type: "RESET";
|
|
25
|
+
payload: {
|
|
26
|
+
setPosition: ReturnType<typeof useChessGame>["methods"]["setPosition"];
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
| { type: "TOGGLE_HINT" }
|
|
30
|
+
| {
|
|
31
|
+
type: "CPU_MOVE";
|
|
32
|
+
payload: {
|
|
33
|
+
makeMove?: ReturnType<typeof useChessGame>["methods"]["makeMove"];
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
| {
|
|
37
|
+
type: "PLAYER_MOVE";
|
|
38
|
+
payload: {
|
|
39
|
+
move?: Move | null;
|
|
40
|
+
onSolve?: (changePuzzle: (puzzle: Puzzle) => void) => void;
|
|
41
|
+
onFail?: (changePuzzle: (puzzle: Puzzle) => void) => void;
|
|
42
|
+
changePuzzle: (puzzle: Puzzle) => void;
|
|
43
|
+
game: Chess;
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export const initializePuzzle = ({
|
|
48
|
+
puzzle,
|
|
49
|
+
setPosition,
|
|
50
|
+
}: {
|
|
51
|
+
puzzle: Puzzle;
|
|
52
|
+
setPosition: ReturnType<typeof useChessGame>["methods"]["setPosition"];
|
|
53
|
+
}): State => {
|
|
54
|
+
setPosition(puzzle.fen, getOrientation(puzzle));
|
|
55
|
+
return {
|
|
56
|
+
puzzle,
|
|
57
|
+
currentMoveIndex: 0,
|
|
58
|
+
status: "not-started",
|
|
59
|
+
nextMove: puzzle.moves[0],
|
|
60
|
+
hint: "none",
|
|
61
|
+
needCpuMove: !!puzzle.makeFirstMove,
|
|
62
|
+
isPlayerTurn: !puzzle.makeFirstMove,
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
export const reducer = (state: State, action: Action): State => {
|
|
67
|
+
switch (action.type) {
|
|
68
|
+
case "INITIALIZE":
|
|
69
|
+
return {
|
|
70
|
+
...state,
|
|
71
|
+
...initializePuzzle(action.payload),
|
|
72
|
+
};
|
|
73
|
+
case "RESET":
|
|
74
|
+
return {
|
|
75
|
+
...state,
|
|
76
|
+
...initializePuzzle({
|
|
77
|
+
puzzle: state.puzzle,
|
|
78
|
+
setPosition: action.payload.setPosition,
|
|
79
|
+
}),
|
|
80
|
+
};
|
|
81
|
+
case "TOGGLE_HINT":
|
|
82
|
+
if (state.hint === "none") {
|
|
83
|
+
return { ...state, hint: "piece" };
|
|
84
|
+
}
|
|
85
|
+
return { ...state, hint: "move" };
|
|
86
|
+
case "CPU_MOVE":
|
|
87
|
+
if (state.isPlayerTurn) {
|
|
88
|
+
return state;
|
|
89
|
+
}
|
|
90
|
+
if (["solved", "failed"].includes(state.status)) {
|
|
91
|
+
return state;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (state.nextMove) {
|
|
95
|
+
action.payload.makeMove?.(state.nextMove);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return {
|
|
99
|
+
...state,
|
|
100
|
+
currentMoveIndex: state.currentMoveIndex + 1,
|
|
101
|
+
nextMove:
|
|
102
|
+
state.currentMoveIndex < state.puzzle.moves.length - 1
|
|
103
|
+
? state.puzzle.moves[state.currentMoveIndex + 1]
|
|
104
|
+
: null,
|
|
105
|
+
needCpuMove: false,
|
|
106
|
+
isPlayerTurn: true,
|
|
107
|
+
status: "in-progress",
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
case "PLAYER_MOVE": {
|
|
111
|
+
const { move, onSolve, onFail, changePuzzle } = action.payload;
|
|
112
|
+
|
|
113
|
+
const isMoveRight = [move?.san, move?.lan].includes(
|
|
114
|
+
state?.nextMove || "",
|
|
115
|
+
);
|
|
116
|
+
const isPuzzleSolved =
|
|
117
|
+
state.currentMoveIndex === state.puzzle.moves.length - 1;
|
|
118
|
+
|
|
119
|
+
if (!isMoveRight) {
|
|
120
|
+
if (onFail) {
|
|
121
|
+
onFail(changePuzzle);
|
|
122
|
+
}
|
|
123
|
+
return {
|
|
124
|
+
...state,
|
|
125
|
+
status: "failed",
|
|
126
|
+
nextMove: null,
|
|
127
|
+
hint: "none",
|
|
128
|
+
isPlayerTurn: false,
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (isPuzzleSolved) {
|
|
133
|
+
if (onSolve) {
|
|
134
|
+
onSolve(changePuzzle);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
return {
|
|
138
|
+
...state,
|
|
139
|
+
status: "solved",
|
|
140
|
+
nextMove: null,
|
|
141
|
+
hint: "none",
|
|
142
|
+
isPlayerTurn: false,
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return {
|
|
147
|
+
...state,
|
|
148
|
+
hint: "none",
|
|
149
|
+
currentMoveIndex: state.currentMoveIndex + 1,
|
|
150
|
+
nextMove: state.puzzle.moves[state.currentMoveIndex + 1],
|
|
151
|
+
status: "in-progress",
|
|
152
|
+
needCpuMove: true,
|
|
153
|
+
isPlayerTurn: false,
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
default:
|
|
158
|
+
return state;
|
|
159
|
+
}
|
|
160
|
+
};
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { useEffect, useReducer } from "react";
|
|
2
|
+
import { initializePuzzle, reducer } from "./reducer";
|
|
3
|
+
import { type Puzzle } from "../utils";
|
|
4
|
+
import { useChessGameContext } from "@react-chess-tools/react-chess-game";
|
|
5
|
+
|
|
6
|
+
export const useChessPuzzle = (
|
|
7
|
+
puzzle: Puzzle,
|
|
8
|
+
onSolve?: (changePuzzle: (puzzle: Puzzle) => void) => void,
|
|
9
|
+
onFail?: (changePuzzle: (puzzle: Puzzle) => void) => void,
|
|
10
|
+
) => {
|
|
11
|
+
const gameContext = useChessGameContext();
|
|
12
|
+
|
|
13
|
+
const [state, dispatch] = useReducer(
|
|
14
|
+
reducer,
|
|
15
|
+
{ puzzle, setPosition: gameContext?.methods.setPosition ?? (() => {}) },
|
|
16
|
+
initializePuzzle,
|
|
17
|
+
);
|
|
18
|
+
|
|
19
|
+
const {
|
|
20
|
+
game,
|
|
21
|
+
methods: { makeMove, setPosition },
|
|
22
|
+
} = gameContext;
|
|
23
|
+
|
|
24
|
+
useEffect(() => {
|
|
25
|
+
if (gameContext && game.fen() === puzzle.fen && state.needCpuMove) {
|
|
26
|
+
setTimeout(
|
|
27
|
+
() =>
|
|
28
|
+
dispatch({
|
|
29
|
+
type: "CPU_MOVE",
|
|
30
|
+
payload: {
|
|
31
|
+
makeMove,
|
|
32
|
+
},
|
|
33
|
+
}),
|
|
34
|
+
0,
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
}, [gameContext, state.needCpuMove]);
|
|
38
|
+
|
|
39
|
+
if (!gameContext) {
|
|
40
|
+
throw new Error("useChessPuzzle must be used within a ChessGameContext");
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const changePuzzle = (puzzle: Puzzle) => {
|
|
44
|
+
dispatch({ type: "INITIALIZE", payload: { puzzle, setPosition } });
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
useEffect(() => {
|
|
48
|
+
if (game?.history()?.length <= 0 + (puzzle.makeFirstMove ? 1 : 0)) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
if (game.history().length % 2 === (puzzle.makeFirstMove ? 0 : 1)) {
|
|
52
|
+
dispatch({
|
|
53
|
+
type: "PLAYER_MOVE",
|
|
54
|
+
payload: {
|
|
55
|
+
move: gameContext?.game?.history({ verbose: true })?.pop() ?? null,
|
|
56
|
+
onSolve,
|
|
57
|
+
onFail,
|
|
58
|
+
changePuzzle,
|
|
59
|
+
game: game,
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
dispatch({
|
|
64
|
+
type: "CPU_MOVE",
|
|
65
|
+
payload: {
|
|
66
|
+
makeMove,
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
}, [game?.history()?.length]);
|
|
71
|
+
|
|
72
|
+
const onHint = () => {
|
|
73
|
+
dispatch({ type: "TOGGLE_HINT" });
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
return {
|
|
77
|
+
status: state.status,
|
|
78
|
+
changePuzzle,
|
|
79
|
+
puzzle,
|
|
80
|
+
hint: state.hint,
|
|
81
|
+
onHint,
|
|
82
|
+
nextMove: state.nextMove,
|
|
83
|
+
isPlayerTurn: state.isPlayerTurn,
|
|
84
|
+
};
|
|
85
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { useChessPuzzle } from "./useChessPuzzle";
|
|
3
|
+
|
|
4
|
+
export const ChessPuzzleContext = React.createContext<ReturnType<
|
|
5
|
+
typeof useChessPuzzle
|
|
6
|
+
> | null>(null);
|
|
7
|
+
|
|
8
|
+
export const useChessPuzzleContext = () => {
|
|
9
|
+
const context = React.useContext(ChessPuzzleContext);
|
|
10
|
+
if (!context) {
|
|
11
|
+
throw new Error(
|
|
12
|
+
"useChessGameContext must be used within a ChessGameProvider",
|
|
13
|
+
);
|
|
14
|
+
}
|
|
15
|
+
return context;
|
|
16
|
+
};
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { type Color, Chess, Move } from "chess.js";
|
|
2
|
+
import React, { CSSProperties, ReactElement, ReactNode } from "react";
|
|
3
|
+
import _ from "lodash";
|
|
4
|
+
|
|
5
|
+
export type Status = "not-started" | "in-progress" | "solved" | "failed";
|
|
6
|
+
|
|
7
|
+
export type Hint = "none" | "piece" | "move";
|
|
8
|
+
|
|
9
|
+
export type Puzzle = {
|
|
10
|
+
fen: string;
|
|
11
|
+
moves: string[];
|
|
12
|
+
// if the first move of the puzzle has to be made by the cpu, as in chess.com puzzles
|
|
13
|
+
makeFirstMove?: boolean;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const FAIL_COLOR = "rgba(201, 52, 48, 0.5)";
|
|
17
|
+
const SUCCESS_COLOR = "rgba(172, 206, 89, 0.5)";
|
|
18
|
+
const HINT_COLOR = "rgba(27, 172, 166, 0.5)";
|
|
19
|
+
|
|
20
|
+
export const getOrientation = (puzzle: Puzzle): Color => {
|
|
21
|
+
const fen = puzzle.fen;
|
|
22
|
+
const game = new Chess(fen);
|
|
23
|
+
if (puzzle.makeFirstMove) {
|
|
24
|
+
game.move(puzzle.moves[0]);
|
|
25
|
+
}
|
|
26
|
+
return game.turn();
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
interface ClickableElement extends ReactElement {
|
|
30
|
+
props: {
|
|
31
|
+
onClick?: () => void;
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export const isClickableElement = (
|
|
36
|
+
element: ReactNode,
|
|
37
|
+
): element is ClickableElement => React.isValidElement(element);
|
|
38
|
+
|
|
39
|
+
export const getCustomSquareStyles = (
|
|
40
|
+
status: Status,
|
|
41
|
+
hint: Hint,
|
|
42
|
+
isPlayerTurn: boolean,
|
|
43
|
+
game: Chess,
|
|
44
|
+
nextMove?: Move | null,
|
|
45
|
+
) => {
|
|
46
|
+
const customSquareStyles: Record<string, CSSProperties> = {};
|
|
47
|
+
|
|
48
|
+
const lastMove = _.last(game.history({ verbose: true }));
|
|
49
|
+
|
|
50
|
+
if (status === "failed" && lastMove) {
|
|
51
|
+
customSquareStyles[lastMove.from] = {
|
|
52
|
+
backgroundColor: FAIL_COLOR,
|
|
53
|
+
};
|
|
54
|
+
customSquareStyles[lastMove.to] = {
|
|
55
|
+
backgroundColor: FAIL_COLOR,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (
|
|
60
|
+
lastMove &&
|
|
61
|
+
(status === "solved" || (status !== "failed" && !isPlayerTurn))
|
|
62
|
+
) {
|
|
63
|
+
customSquareStyles[lastMove.from] = {
|
|
64
|
+
backgroundColor: SUCCESS_COLOR,
|
|
65
|
+
};
|
|
66
|
+
customSquareStyles[lastMove.to] = {
|
|
67
|
+
backgroundColor: SUCCESS_COLOR,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (hint === "piece") {
|
|
72
|
+
if (nextMove) {
|
|
73
|
+
customSquareStyles[nextMove.from] = {
|
|
74
|
+
backgroundColor: HINT_COLOR,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (hint === "move") {
|
|
80
|
+
if (nextMove) {
|
|
81
|
+
customSquareStyles[nextMove.from] = {
|
|
82
|
+
backgroundColor: HINT_COLOR,
|
|
83
|
+
};
|
|
84
|
+
customSquareStyles[nextMove.to] = {
|
|
85
|
+
backgroundColor: HINT_COLOR,
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return customSquareStyles;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
export const stringToMove = (game: Chess, move: string | null | undefined) => {
|
|
94
|
+
const copy = new Chess(game.fen());
|
|
95
|
+
if (move === null || move === undefined) {
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
try {
|
|
99
|
+
return copy.move(move);
|
|
100
|
+
} catch (e) {
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
};
|