import type { Meta } from "@storybook/react-vite"; import React from "react"; import { RootProps } from "./parts/Root"; import { ChessPuzzle } from "."; import { ChessGame } from "@react-chess-tools/react-chess-game"; import { StoryHeader, StoryContainer, BoardWrapper, Kbd, Button, } from "@story-helpers"; const puzzles = [ { fen: "4kb1r/p2r1ppp/4qn2/1B2p1B1/4P3/1Q6/PPP2PPP/2KR4 w k - 0 1", moves: ["Bxd7+", "Nxd7", "Qb8+", "Nxb8", "Rd8#"], makeFirstMove: false, }, { fen: "6k1/5p1p/p1q1p1p1/1pB1P3/1Pr3Pn/P4P1P/4Q3/3R2K1 b - - 0 31", moves: ["h4f3", "e2f3", "c4c5", "d1d8", "g8g7", "f3f6"], makeFirstMove: true, }, ]; const meta = { title: "Packages/react-chess-puzzle/ChessPuzzle", component: ChessPuzzle.Root, tags: ["components", "puzzle"], argTypes: { onSolve: { action: "onSolve" }, onFail: { action: "onFail" }, }, parameters: { layout: "centered", }, } satisfies Meta; export default meta; export const Example = (args: RootProps) => { const [puzzleIndex, setPuzzleIndex] = React.useState(0); const puzzle = puzzles[puzzleIndex]; return ( Puzzle {puzzleIndex + 1} of {puzzles.length}
setPuzzleIndex((puzzleIndex + 1) % puzzles.length)} >
); }; export const WithOrientation = (args: RootProps) => { const puzzle = { fen: "4kbnr/2p1pp1p/pp4p1/5b2/8/2NB1N2/PP3PPP/RKB4R b k - 0 1", makeFirstMove: false, moves: ["Bxd3"], }; return (
); }; export const Underpromotion = (args: RootProps) => { const puzzle = { fen: "8/8/5R1p/8/3pb1P1/kpKp4/8/8 w - - 0 54", moves: ["c3d4", "d3d2", "d4c3", "d2d1n"], makeFirstMove: true, }; return (
Sometimes promoting to a knight is better than a queen!
); }; export const WithSounds = (args: RootProps) => { return (

Move pieces to hear different sounds

); }; export const WithKeyboardControls = (args: RootProps) => { return ( context.methods.flipBoard(), w: (context) => context.methods.goToStart(), s: (context) => context.methods.goToEnd(), a: (context) => context.methods.goToPreviousMove(), d: (context) => context.methods.goToNextMove(), }} />
W Start
A Previous
F Flip
S End
D Next
); }; const multiMatePuzzle = { fen: "7k/R7/1R6/2Q5/4Q3/8/8/7K w - - 0 1", moves: ["a7a8"], makeFirstMove: false, }; export const MultiMatePuzzle = (args: RootProps) => { return (
solveOnCheckmate=true (default)
Try Qc8#, Qf8#, Rb8#, or the canonical Ra8#
); }; export const MultiMatePuzzleStrict = (args: RootProps) => { return (
solveOnCheckmate=false
Only Ra8# is accepted. Alternative mates like Qc8# will fail!
); };