@react-chess-tools/react-chess-puzzle 1.0.0 → 1.0.2
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 +14 -0
- package/README.md +570 -0
- package/dist/index.cjs +132 -107
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +19 -5
- package/dist/index.d.ts +19 -5
- package/dist/index.js +118 -93
- package/dist/index.js.map +1 -1
- package/package.json +3 -2
- package/src/components/ChessPuzzle/ChessPuzzle.stories.tsx +46 -0
- package/src/components/ChessPuzzle/parts/Hint.tsx +32 -23
- package/src/components/ChessPuzzle/parts/PuzzleBoard.tsx +28 -27
- package/src/components/ChessPuzzle/parts/Reset.tsx +56 -36
- package/src/components/ChessPuzzle/parts/Root.tsx +14 -2
- package/src/components/ChessPuzzle/parts/__tests__/Hint.test.tsx +158 -0
- package/src/components/ChessPuzzle/parts/__tests__/PuzzleBoard.test.tsx +140 -0
- package/src/components/ChessPuzzle/parts/__tests__/Reset.test.tsx +341 -0
- package/src/components/ChessPuzzle/parts/__tests__/Root.test.tsx +42 -0
- package/src/hooks/__tests__/reducer.test.ts +134 -0
- package/src/hooks/reducer.ts +13 -1
- package/src/hooks/useChessPuzzle.ts +2 -0
- package/src/utils/__tests__/index.test.ts +0 -17
- package/src/utils/index.ts +1 -11
- package/README.MD +0 -344
package/README.MD
DELETED
|
@@ -1,344 +0,0 @@
|
|
|
1
|
-
<div align="center">
|
|
2
|
-
<h1>react-chess-puzzle</h1>
|
|
3
|
-
A lightweight, customizable React component library for rendering and interacting with chess puzzles.
|
|
4
|
-
</div>
|
|
5
|
-
|
|
6
|
-
## Project Description
|
|
7
|
-
|
|
8
|
-
This project is a React-based chess puzzle component that allows users to solve chess puzzles online. It is part of the `react-chess-tools` package and is designed to be easy to use and customizable. It is built on top of `react-chess-game` component.
|
|
9
|
-
|
|
10
|
-
## Preview
|
|
11
|
-
|
|
12
|
-
Visit the [demo](https://react-chess-tools.vercel.app/) to see the `react-chess-puzzle` component in action.
|
|
13
|
-
|
|
14
|
-
## Table of Contents
|
|
15
|
-
|
|
16
|
-
- [Installation](#installation)
|
|
17
|
-
- [Quick Start](#quick-start)
|
|
18
|
-
- [Puzzle Solving Flow](#puzzle-solving-flow)
|
|
19
|
-
- [API Reference](#api-reference)
|
|
20
|
-
- [ChessPuzzle.Root](#chesspuzzleroot)
|
|
21
|
-
- [ChessPuzzle.Board](#chesspuzzleboard)
|
|
22
|
-
- [ChessPuzzle.Reset](#chesspuzzlereset)
|
|
23
|
-
- [ChessPuzzle.Hint](#chesspuzzlehint)
|
|
24
|
-
- [Hooks and Context](#hooks-and-context)
|
|
25
|
-
- [useChessPuzzleContext](#usechesspuzzlecontext)
|
|
26
|
-
- [useChessGameContext](#usechessgamecontext)
|
|
27
|
-
- [Integration with react-chess-game](#using-react-chess-game-components)
|
|
28
|
-
- [Complete Example](#complete-example)
|
|
29
|
-
- [License](#-license)
|
|
30
|
-
|
|
31
|
-
## Installation
|
|
32
|
-
|
|
33
|
-
To install the `react-chess-puzzle` package, run the following command:
|
|
34
|
-
|
|
35
|
-
```bash
|
|
36
|
-
$ npm install @react-chess-tools/react-chess-puzzle
|
|
37
|
-
```
|
|
38
|
-
|
|
39
|
-
## Quick Start
|
|
40
|
-
|
|
41
|
-
To use the `react-chess-puzzle` package, you can import the `ChessPuzzle` component and use it as follows:
|
|
42
|
-
|
|
43
|
-
```tsx
|
|
44
|
-
import { ChessPuzzle } from "@react-chess-tools/react-chess-puzzle";
|
|
45
|
-
|
|
46
|
-
const App = () => {
|
|
47
|
-
const puzzle = {
|
|
48
|
-
fen: "r1bqkbnr/pppp1ppp/2n5/4p3/4P3/5N2/PPPP1PPP/RNBQKB1R w KQkq - 2 3",
|
|
49
|
-
moves: ["d2d4", "e5d4", "f3d4"],
|
|
50
|
-
makeFirstMove: false,
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
return (
|
|
54
|
-
<ChessPuzzle.Root puzzle={puzzle}>
|
|
55
|
-
<ChessPuzzle.Board />
|
|
56
|
-
<div className="controls">
|
|
57
|
-
<ChessPuzzle.Reset>Restart Puzzle</ChessPuzzle.Reset>
|
|
58
|
-
<ChessPuzzle.Hint showOn={["in-progress"]}>Get Hint</ChessPuzzle.Hint>
|
|
59
|
-
</div>
|
|
60
|
-
</ChessPuzzle.Root>
|
|
61
|
-
);
|
|
62
|
-
};
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
## Puzzle Solving Flow
|
|
66
|
-
|
|
67
|
-
The puzzle-solving flow follows these steps:
|
|
68
|
-
|
|
69
|
-
1. **Initial Setup**: The board is set up using the provided FEN string
|
|
70
|
-
2. **First Move**: If `makeFirstMove` is `true`, the component automatically makes the first move in the solution sequence
|
|
71
|
-
3. **User Interaction**: The user attempts to solve the puzzle by making the correct moves
|
|
72
|
-
4. **Feedback**: The component validates each move and provides feedback:
|
|
73
|
-
- If the move is correct, the puzzle continues
|
|
74
|
-
- If the move is incorrect, the puzzle is marked as failed
|
|
75
|
-
5. **Completion**: When all correct moves have been made, the puzzle is marked as solved
|
|
76
|
-
|
|
77
|
-
## API Reference
|
|
78
|
-
|
|
79
|
-
The `react-chess-puzzle` package provides a set of components that you can use to build your chess app. The following sections describe the components and their usage.
|
|
80
|
-
|
|
81
|
-
### ChessPuzzle.Root
|
|
82
|
-
|
|
83
|
-
The `ChessPuzzle.Root` component is the root component of the `react-chess-puzzle` package. It is used to provide the `ChessPuzzleContext` to the rest of the components. It accepts a `puzzle` prop that is used to instantiate the puzzle.
|
|
84
|
-
|
|
85
|
-
#### Props
|
|
86
|
-
|
|
87
|
-
The `ChessPuzzle.Root` component accepts the following props:
|
|
88
|
-
|
|
89
|
-
| Name | Type | Default | Description |
|
|
90
|
-
| ----------- | ------------------------------------------------- | ----------- | -------------------------------------------------------------------------------------------------------------------- |
|
|
91
|
-
| `puzzle` | `Puzzle` | | The puzzle to be solved |
|
|
92
|
-
| `onSolve` | `(puzzleContext: ChessPuzzleContextType) => void` | `undefined` | Callback function that is triggered when the puzzle is successfully solved, receives the puzzle context as parameter |
|
|
93
|
-
| `onFail` | `(puzzleContext: ChessPuzzleContextType) => void` | `undefined` | Callback function that is triggered when an incorrect move is played, receives the puzzle context as parameter |
|
|
94
|
-
| `children?` | `ReactNode` | | The children to be rendered |
|
|
95
|
-
|
|
96
|
-
The `puzzle` prop contains the following properties:
|
|
97
|
-
|
|
98
|
-
| Name | Type | Default | Description |
|
|
99
|
-
| --------------- | ---------- | ------- | -------------------------------------------------------------------------- |
|
|
100
|
-
| `fen` | `string` | | The FEN string representing the initial position of the puzzle |
|
|
101
|
-
| `moves` | `string[]` | | The sequence of moves (in algebraic or UCI notation) that solve the puzzle |
|
|
102
|
-
| `makeFirstMove` | `boolean` | `false` | Whether the first move is part of the problem or must be played by the CPU |
|
|
103
|
-
|
|
104
|
-
### ChessPuzzle.Board
|
|
105
|
-
|
|
106
|
-
The `ChessPuzzle.Board` component renders the chess board and delegates to `ChessGame.Board` under the hood.
|
|
107
|
-
|
|
108
|
-
#### Props
|
|
109
|
-
|
|
110
|
-
It accepts the same props as `ChessGame.Board`:
|
|
111
|
-
|
|
112
|
-
| Name | Type | Description |
|
|
113
|
-
| -------- | ------------------- | ---------------------------------------------------------------------- |
|
|
114
|
-
| options? | `ChessboardOptions` | Forwarded to `react-chessboard` v5 via `ChessGame.Board({ options })`. |
|
|
115
|
-
|
|
116
|
-
Any other props are passed through to `ChessGame.Board` unchanged.
|
|
117
|
-
|
|
118
|
-
### ChessPuzzle.Reset
|
|
119
|
-
|
|
120
|
-
A button component that resets the current puzzle or loads a new one.
|
|
121
|
-
|
|
122
|
-
#### Props
|
|
123
|
-
|
|
124
|
-
| Name | Type | Default | Description |
|
|
125
|
-
| --------- | --------------------- | ---------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
126
|
-
| `puzzle` | `Puzzle \| undefined` | `undefined` | The puzzle object for a new puzzle. If not provided, the current puzzle is reset. |
|
|
127
|
-
| `onReset` | `() => void` | `undefined` | A callback function that is called when the puzzle is reset. |
|
|
128
|
-
| `showOn` | `Status[]` | `["not-started", "in-progress", "solved", "failed"]` | The state(s) in which the button is shown. Valid states are: "not-started", "in-progress", "solved", "failed" |
|
|
129
|
-
| `asChild` | `boolean` | `false` | Change the component to the HTML tag or custom component of the only child. This will merge the original component props with the props of the supplied element/component and change the underlying DOM node. |
|
|
130
|
-
|
|
131
|
-
### ChessPuzzle.Hint
|
|
132
|
-
|
|
133
|
-
A button component that provides a hint by highlighting the next move in the solution.
|
|
134
|
-
|
|
135
|
-
#### Props
|
|
136
|
-
|
|
137
|
-
| Name | Type | Default | Description |
|
|
138
|
-
| --------- | ---------- | ----------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
139
|
-
| `showOn` | `Status[]` | `["in-progress"]` | The state(s) in which the button is shown. Valid states are: "not-started", "in-progress", "solved", "failed" |
|
|
140
|
-
| `asChild` | `boolean` | `false` | Change the component to the HTML tag or custom component of the only child. This will merge the original component props with the props of the supplied element/component and change the underlying DOM node. |
|
|
141
|
-
|
|
142
|
-
## Hooks and Context
|
|
143
|
-
|
|
144
|
-
### useChessPuzzleContext
|
|
145
|
-
|
|
146
|
-
A hook that provides access to the puzzle state and methods.
|
|
147
|
-
|
|
148
|
-
```tsx
|
|
149
|
-
import { useChessPuzzleContext } from "@react-chess-tools/react-chess-puzzle";
|
|
150
|
-
|
|
151
|
-
const MyComponent = () => {
|
|
152
|
-
const {
|
|
153
|
-
puzzleState, // "not-started" | "in-progress" | "solved" | "failed"
|
|
154
|
-
resetPuzzle, // Function to reset the current puzzle
|
|
155
|
-
onHint, // Function to show a hint
|
|
156
|
-
movesPlayed, // Number of moves played so far
|
|
157
|
-
totalMoves, // Total number of moves in the solution
|
|
158
|
-
} = useChessPuzzleContext();
|
|
159
|
-
|
|
160
|
-
return (
|
|
161
|
-
<div>
|
|
162
|
-
<p>Puzzle state: {puzzleState}</p>
|
|
163
|
-
<p>
|
|
164
|
-
Progress: {movesPlayed}/{totalMoves} moves
|
|
165
|
-
</p>
|
|
166
|
-
<button onClick={resetPuzzle}>Reset</button>
|
|
167
|
-
<button onClick={onHint}>Show Hint</button>
|
|
168
|
-
</div>
|
|
169
|
-
);
|
|
170
|
-
};
|
|
171
|
-
```
|
|
172
|
-
|
|
173
|
-
### useChessGameContext
|
|
174
|
-
|
|
175
|
-
Since `react-chess-puzzle` is built on top of `react-chess-game`, you can also use the `useChessGameContext` hook to access the underlying game state and methods.
|
|
176
|
-
|
|
177
|
-
```tsx
|
|
178
|
-
import { useChessGameContext } from "@react-chess-tools/react-chess-game";
|
|
179
|
-
|
|
180
|
-
const MyComponent = () => {
|
|
181
|
-
const { currentFen, info, methods } = useChessGameContext();
|
|
182
|
-
|
|
183
|
-
return (
|
|
184
|
-
<div>
|
|
185
|
-
<p>Current FEN: {currentFen}</p>
|
|
186
|
-
<p>Current turn: {info.turn === "w" ? "White" : "Black"}</p>
|
|
187
|
-
<button onClick={() => methods.flipBoard()}>Flip</button>
|
|
188
|
-
</div>
|
|
189
|
-
);
|
|
190
|
-
};
|
|
191
|
-
```
|
|
192
|
-
|
|
193
|
-
## Using react-chess-game Components
|
|
194
|
-
|
|
195
|
-
Since `react-chess-puzzle` is built on top of `react-chess-game`, you can use any of its components within your puzzle interface:
|
|
196
|
-
|
|
197
|
-
### ChessGame.Sounds
|
|
198
|
-
|
|
199
|
-
Add sound effects for moves, captures, and other chess events.
|
|
200
|
-
|
|
201
|
-
```tsx
|
|
202
|
-
import { ChessPuzzle } from "@react-chess-tools/react-chess-puzzle";
|
|
203
|
-
import { ChessGame } from "@react-chess-tools/react-chess-game";
|
|
204
|
-
|
|
205
|
-
const App = () => (
|
|
206
|
-
<ChessPuzzle.Root puzzle={...}>
|
|
207
|
-
<ChessGame.Sounds />
|
|
208
|
-
<ChessPuzzle.Board />
|
|
209
|
-
</ChessPuzzle.Root>
|
|
210
|
-
);
|
|
211
|
-
```
|
|
212
|
-
|
|
213
|
-
### ChessGame.KeyboardControls
|
|
214
|
-
|
|
215
|
-
Add keyboard navigation for accessible play.
|
|
216
|
-
|
|
217
|
-
```tsx
|
|
218
|
-
import { ChessPuzzle } from "@react-chess-tools/react-chess-puzzle";
|
|
219
|
-
import { ChessGame } from "@react-chess-tools/react-chess-game";
|
|
220
|
-
|
|
221
|
-
const App = () => (
|
|
222
|
-
<ChessPuzzle.Root puzzle={...}>
|
|
223
|
-
<ChessGame.KeyboardControls />
|
|
224
|
-
<ChessPuzzle.Board />
|
|
225
|
-
</ChessPuzzle.Root>
|
|
226
|
-
);
|
|
227
|
-
```
|
|
228
|
-
|
|
229
|
-
## Complete Example
|
|
230
|
-
|
|
231
|
-
Here's a complete example of a chess puzzle component with sounds, keyboard controls, and custom styling:
|
|
232
|
-
|
|
233
|
-
```tsx
|
|
234
|
-
import {
|
|
235
|
-
ChessPuzzle,
|
|
236
|
-
type ChessPuzzleContextType,
|
|
237
|
-
} from "@react-chess-tools/react-chess-puzzle";
|
|
238
|
-
import { ChessGame } from "@react-chess-tools/react-chess-game";
|
|
239
|
-
import { useState } from "react";
|
|
240
|
-
import "./ChessPuzzleStyles.css"; // Your custom CSS
|
|
241
|
-
|
|
242
|
-
export const PuzzleSolver = () => {
|
|
243
|
-
// Example puzzles
|
|
244
|
-
const puzzles = [
|
|
245
|
-
{
|
|
246
|
-
fen: "4kb1r/p2r1ppp/4qn2/1B2p1B1/4P3/1Q6/PPP2PPP/2KR4 w k - 0 1",
|
|
247
|
-
moves: ["Bxd7+", "Nxd7", "Qb8+", "Nxb8", "Rd8#"],
|
|
248
|
-
makeFirstMove: false,
|
|
249
|
-
},
|
|
250
|
-
{
|
|
251
|
-
fen: "6k1/5p1p/p1q1p1p1/1pB1P3/1Pr3Pn/P4P1P/4Q3/3R2K1 b - - 0 31",
|
|
252
|
-
moves: ["h4f3", "e2f3", "c4c5", "d1d8", "g8g7", "f3f6"],
|
|
253
|
-
makeFirstMove: true,
|
|
254
|
-
},
|
|
255
|
-
];
|
|
256
|
-
|
|
257
|
-
const [currentPuzzle, setCurrentPuzzle] = useState(0);
|
|
258
|
-
const [score, setScore] = useState(0);
|
|
259
|
-
|
|
260
|
-
const nextPuzzle = () => {
|
|
261
|
-
const nextPuzzle = (currentPuzzle + 1) % puzzles.length;
|
|
262
|
-
setCurrentPuzzle(nextPuzzle);
|
|
263
|
-
};
|
|
264
|
-
|
|
265
|
-
const handleSolve = (puzzleContext: ChessPuzzleContextType) => {
|
|
266
|
-
setScore((prev) => prev + 10);
|
|
267
|
-
console.log(`Puzzle solved in ${puzzleContext.movesPlayed} moves`);
|
|
268
|
-
nextPuzzle();
|
|
269
|
-
};
|
|
270
|
-
|
|
271
|
-
const handleFail = (puzzleContext: ChessPuzzleContextType) => {
|
|
272
|
-
setScore((prev) => Math.max(0, prev - 5));
|
|
273
|
-
console.log(`Puzzle failed in ${puzzleContext.movesPlayed} moves`);
|
|
274
|
-
|
|
275
|
-
nextPuzzle();
|
|
276
|
-
};
|
|
277
|
-
|
|
278
|
-
const handleReset = (puzzleContext: ChessPuzzleContextType) => {
|
|
279
|
-
puzzleContext.resetPuzzle();
|
|
280
|
-
};
|
|
281
|
-
|
|
282
|
-
return (
|
|
283
|
-
<div className="puzzle-container">
|
|
284
|
-
<div className="score">Score: {score}</div>
|
|
285
|
-
<ChessPuzzle.Root
|
|
286
|
-
puzzle={puzzles[currentPuzzle]!}
|
|
287
|
-
onSolve={handleSolve}
|
|
288
|
-
onFail={handleFail}
|
|
289
|
-
>
|
|
290
|
-
<ChessGame.Sounds />
|
|
291
|
-
<ChessGame.KeyboardControls />
|
|
292
|
-
|
|
293
|
-
<div className="board-container">
|
|
294
|
-
<ChessPuzzle.Board />
|
|
295
|
-
</div>
|
|
296
|
-
|
|
297
|
-
<div className="controls">
|
|
298
|
-
<div className="buttons">
|
|
299
|
-
<ChessPuzzle.Reset>Restart</ChessPuzzle.Reset>
|
|
300
|
-
<ChessPuzzle.Hint showOn={["in-progress"]}>Hint</ChessPuzzle.Hint>
|
|
301
|
-
<ChessPuzzle.Reset
|
|
302
|
-
puzzle={puzzles[(currentPuzzle + 1) % puzzles.length]}
|
|
303
|
-
onReset={handleReset}
|
|
304
|
-
>
|
|
305
|
-
Next Puzzle
|
|
306
|
-
</ChessPuzzle.Reset>
|
|
307
|
-
</div>
|
|
308
|
-
</div>
|
|
309
|
-
</ChessPuzzle.Root>
|
|
310
|
-
</div>
|
|
311
|
-
);
|
|
312
|
-
};
|
|
313
|
-
|
|
314
|
-
// Custom component using the context
|
|
315
|
-
const PuzzleStatus = () => {
|
|
316
|
-
const { puzzleState, movesPlayed, totalMoves } = useChessPuzzleContext();
|
|
317
|
-
|
|
318
|
-
let message = "";
|
|
319
|
-
switch (puzzleState) {
|
|
320
|
-
case "not-started":
|
|
321
|
-
message = "Make your move to start the puzzle";
|
|
322
|
-
break;
|
|
323
|
-
case "in-progress":
|
|
324
|
-
message = `Progress: ${movesPlayed}/${totalMoves} moves`;
|
|
325
|
-
break;
|
|
326
|
-
case "solved":
|
|
327
|
-
message = "Puzzle solved! Well done!";
|
|
328
|
-
break;
|
|
329
|
-
case "failed":
|
|
330
|
-
message = "Incorrect move. Try again!";
|
|
331
|
-
break;
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
return <div className={`status ${puzzleState}`}>{message}</div>;
|
|
335
|
-
};
|
|
336
|
-
```
|
|
337
|
-
|
|
338
|
-
## 📝 License
|
|
339
|
-
|
|
340
|
-
This project is [MIT](https://opensource.org/licenses/MIT) licensed.
|
|
341
|
-
|
|
342
|
-
## Show your support
|
|
343
|
-
|
|
344
|
-
Give a ⭐️ if this project helped you!
|