@dxos/react-ui-gameboard 0.7.5-labs.071a3e2
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 +8 -0
- package/README.md +21 -0
- package/dist/lib/browser/index.mjs +903 -0
- package/dist/lib/browser/index.mjs.map +7 -0
- package/dist/lib/browser/meta.json +1 -0
- package/dist/lib/node/index.cjs +908 -0
- package/dist/lib/node/index.cjs.map +7 -0
- package/dist/lib/node/meta.json +1 -0
- package/dist/lib/node-esm/index.mjs +904 -0
- package/dist/lib/node-esm/index.mjs.map +7 -0
- package/dist/lib/node-esm/meta.json +1 -0
- package/dist/types/src/Board/Board.d.ts +15 -0
- package/dist/types/src/Board/Board.d.ts.map +1 -0
- package/dist/types/src/Board/Container.d.ts +14 -0
- package/dist/types/src/Board/Container.d.ts.map +1 -0
- package/dist/types/src/Board/Piece.d.ts +13 -0
- package/dist/types/src/Board/Piece.d.ts.map +1 -0
- package/dist/types/src/Board/Square.d.ts +11 -0
- package/dist/types/src/Board/Square.d.ts.map +1 -0
- package/dist/types/src/Board/context.d.ts +10 -0
- package/dist/types/src/Board/context.d.ts.map +1 -0
- package/dist/types/src/Board/index.d.ts +8 -0
- package/dist/types/src/Board/index.d.ts.map +1 -0
- package/dist/types/src/Board/types.d.ts +35 -0
- package/dist/types/src/Board/types.d.ts.map +1 -0
- package/dist/types/src/Board/util.d.ts +3 -0
- package/dist/types/src/Board/util.d.ts.map +1 -0
- package/dist/types/src/Chessboard/Chessboard.d.ts +14 -0
- package/dist/types/src/Chessboard/Chessboard.d.ts.map +1 -0
- package/dist/types/src/Chessboard/Chessboard.stories.d.ts +16 -0
- package/dist/types/src/Chessboard/Chessboard.stories.d.ts.map +1 -0
- package/dist/types/src/Chessboard/chess.d.ts +40 -0
- package/dist/types/src/Chessboard/chess.d.ts.map +1 -0
- package/dist/types/src/Chessboard/index.d.ts +3 -0
- package/dist/types/src/Chessboard/index.d.ts.map +1 -0
- package/dist/types/src/gen/pieces/chess/alpha/bB.d.ts +5 -0
- package/dist/types/src/gen/pieces/chess/alpha/bB.d.ts.map +1 -0
- package/dist/types/src/gen/pieces/chess/alpha/bK.d.ts +5 -0
- package/dist/types/src/gen/pieces/chess/alpha/bK.d.ts.map +1 -0
- package/dist/types/src/gen/pieces/chess/alpha/bN.d.ts +5 -0
- package/dist/types/src/gen/pieces/chess/alpha/bN.d.ts.map +1 -0
- package/dist/types/src/gen/pieces/chess/alpha/bP.d.ts +5 -0
- package/dist/types/src/gen/pieces/chess/alpha/bP.d.ts.map +1 -0
- package/dist/types/src/gen/pieces/chess/alpha/bQ.d.ts +5 -0
- package/dist/types/src/gen/pieces/chess/alpha/bQ.d.ts.map +1 -0
- package/dist/types/src/gen/pieces/chess/alpha/bR.d.ts +5 -0
- package/dist/types/src/gen/pieces/chess/alpha/bR.d.ts.map +1 -0
- package/dist/types/src/gen/pieces/chess/alpha/index.d.ts +13 -0
- package/dist/types/src/gen/pieces/chess/alpha/index.d.ts.map +1 -0
- package/dist/types/src/gen/pieces/chess/alpha/wB.d.ts +5 -0
- package/dist/types/src/gen/pieces/chess/alpha/wB.d.ts.map +1 -0
- package/dist/types/src/gen/pieces/chess/alpha/wK.d.ts +5 -0
- package/dist/types/src/gen/pieces/chess/alpha/wK.d.ts.map +1 -0
- package/dist/types/src/gen/pieces/chess/alpha/wN.d.ts +5 -0
- package/dist/types/src/gen/pieces/chess/alpha/wN.d.ts.map +1 -0
- package/dist/types/src/gen/pieces/chess/alpha/wP.d.ts +5 -0
- package/dist/types/src/gen/pieces/chess/alpha/wP.d.ts.map +1 -0
- package/dist/types/src/gen/pieces/chess/alpha/wQ.d.ts +5 -0
- package/dist/types/src/gen/pieces/chess/alpha/wQ.d.ts.map +1 -0
- package/dist/types/src/gen/pieces/chess/alpha/wR.d.ts +5 -0
- package/dist/types/src/gen/pieces/chess/alpha/wR.d.ts.map +1 -0
- package/dist/types/src/gen/pieces/chess/cburnett/bB.d.ts +5 -0
- package/dist/types/src/gen/pieces/chess/cburnett/bB.d.ts.map +1 -0
- package/dist/types/src/gen/pieces/chess/cburnett/bK.d.ts +5 -0
- package/dist/types/src/gen/pieces/chess/cburnett/bK.d.ts.map +1 -0
- package/dist/types/src/gen/pieces/chess/cburnett/bN.d.ts +5 -0
- package/dist/types/src/gen/pieces/chess/cburnett/bN.d.ts.map +1 -0
- package/dist/types/src/gen/pieces/chess/cburnett/bP.d.ts +5 -0
- package/dist/types/src/gen/pieces/chess/cburnett/bP.d.ts.map +1 -0
- package/dist/types/src/gen/pieces/chess/cburnett/bQ.d.ts +5 -0
- package/dist/types/src/gen/pieces/chess/cburnett/bQ.d.ts.map +1 -0
- package/dist/types/src/gen/pieces/chess/cburnett/bR.d.ts +5 -0
- package/dist/types/src/gen/pieces/chess/cburnett/bR.d.ts.map +1 -0
- package/dist/types/src/gen/pieces/chess/cburnett/index.d.ts +13 -0
- package/dist/types/src/gen/pieces/chess/cburnett/index.d.ts.map +1 -0
- package/dist/types/src/gen/pieces/chess/cburnett/wB.d.ts +5 -0
- package/dist/types/src/gen/pieces/chess/cburnett/wB.d.ts.map +1 -0
- package/dist/types/src/gen/pieces/chess/cburnett/wK.d.ts +5 -0
- package/dist/types/src/gen/pieces/chess/cburnett/wK.d.ts.map +1 -0
- package/dist/types/src/gen/pieces/chess/cburnett/wN.d.ts +5 -0
- package/dist/types/src/gen/pieces/chess/cburnett/wN.d.ts.map +1 -0
- package/dist/types/src/gen/pieces/chess/cburnett/wP.d.ts +5 -0
- package/dist/types/src/gen/pieces/chess/cburnett/wP.d.ts.map +1 -0
- package/dist/types/src/gen/pieces/chess/cburnett/wQ.d.ts +5 -0
- package/dist/types/src/gen/pieces/chess/cburnett/wQ.d.ts.map +1 -0
- package/dist/types/src/gen/pieces/chess/cburnett/wR.d.ts +5 -0
- package/dist/types/src/gen/pieces/chess/cburnett/wR.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +3 -0
- package/dist/types/src/index.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -0
- package/package.json +61 -0
- package/src/Board/Board.tsx +86 -0
- package/src/Board/Container.tsx +25 -0
- package/src/Board/Piece.tsx +135 -0
- package/src/Board/Square.tsx +74 -0
- package/src/Board/context.ts +22 -0
- package/src/Board/index.ts +12 -0
- package/src/Board/types.ts +58 -0
- package/src/Board/util.ts +16 -0
- package/src/Chessboard/Chessboard.stories.tsx +108 -0
- package/src/Chessboard/Chessboard.tsx +190 -0
- package/src/Chessboard/chess.ts +213 -0
- package/src/Chessboard/index.ts +7 -0
- package/src/gen/pieces/chess/alpha/bB.tsx +12 -0
- package/src/gen/pieces/chess/alpha/bK.tsx +15 -0
- package/src/gen/pieces/chess/alpha/bN.tsx +15 -0
- package/src/gen/pieces/chess/alpha/bP.tsx +11 -0
- package/src/gen/pieces/chess/alpha/bQ.tsx +12 -0
- package/src/gen/pieces/chess/alpha/bR.tsx +12 -0
- package/src/gen/pieces/chess/alpha/index.ts +12 -0
- package/src/gen/pieces/chess/alpha/wB.tsx +15 -0
- package/src/gen/pieces/chess/alpha/wK.tsx +15 -0
- package/src/gen/pieces/chess/alpha/wN.tsx +15 -0
- package/src/gen/pieces/chess/alpha/wP.tsx +15 -0
- package/src/gen/pieces/chess/alpha/wQ.tsx +15 -0
- package/src/gen/pieces/chess/alpha/wR.tsx +15 -0
- package/src/gen/pieces/chess/cburnett/bB.tsx +15 -0
- package/src/gen/pieces/chess/cburnett/bK.tsx +26 -0
- package/src/gen/pieces/chess/cburnett/bN.tsx +24 -0
- package/src/gen/pieces/chess/cburnett/bP.tsx +13 -0
- package/src/gen/pieces/chess/cburnett/bQ.tsx +30 -0
- package/src/gen/pieces/chess/cburnett/bR.tsx +19 -0
- package/src/gen/pieces/chess/cburnett/index.ts +12 -0
- package/src/gen/pieces/chess/cburnett/wB.tsx +15 -0
- package/src/gen/pieces/chess/cburnett/wK.tsx +21 -0
- package/src/gen/pieces/chess/cburnett/wN.tsx +18 -0
- package/src/gen/pieces/chess/cburnett/wP.tsx +14 -0
- package/src/gen/pieces/chess/cburnett/wQ.tsx +16 -0
- package/src/gen/pieces/chess/cburnett/wR.tsx +14 -0
- package/src/index.ts +6 -0
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type ReadonlySignal } from '@preact/signals-core';
|
|
6
|
+
|
|
7
|
+
export type Player = 'black' | 'white';
|
|
8
|
+
|
|
9
|
+
export type Location = [number, number];
|
|
10
|
+
|
|
11
|
+
export type PieceType = string;
|
|
12
|
+
|
|
13
|
+
export type PieceRecord<T extends PieceType = PieceType> = {
|
|
14
|
+
id: string;
|
|
15
|
+
type: T;
|
|
16
|
+
side: Player;
|
|
17
|
+
location: Location;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Map of pieces by location.
|
|
22
|
+
*/
|
|
23
|
+
export type PieceMap<T extends PieceType = PieceType> = Record<string, PieceRecord<T>>;
|
|
24
|
+
|
|
25
|
+
export type Move = {
|
|
26
|
+
from: Location;
|
|
27
|
+
to: Location;
|
|
28
|
+
piece: PieceType;
|
|
29
|
+
promotion?: PieceType;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export const locationToString = (location: Location): string => location.join('-');
|
|
33
|
+
export const stringToLocation = (str: string): Location => str.split('-').map(Number) as Location;
|
|
34
|
+
|
|
35
|
+
// Type guard.
|
|
36
|
+
export const isPiece = (piece: unknown): piece is PieceRecord =>
|
|
37
|
+
piece != null &&
|
|
38
|
+
typeof piece === 'object' &&
|
|
39
|
+
'id' in piece &&
|
|
40
|
+
'type' in piece &&
|
|
41
|
+
'location' in piece &&
|
|
42
|
+
isLocation(piece.location);
|
|
43
|
+
|
|
44
|
+
// Type guard.
|
|
45
|
+
export const isLocation = (token: unknown): token is Location =>
|
|
46
|
+
Array.isArray(token) && token.length === 2 && token.every((val) => typeof val === 'number');
|
|
47
|
+
|
|
48
|
+
export const isEqualLocation = (l1: Location, l2: Location): boolean => l1[0] === l2[0] && l1[1] === l2[1];
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Generic board model.
|
|
52
|
+
*/
|
|
53
|
+
export interface BoardModel<T extends PieceType = PieceType> {
|
|
54
|
+
turn: Player;
|
|
55
|
+
pieces: ReadonlySignal<PieceMap<T>>;
|
|
56
|
+
isValidMove: (move: Move) => boolean;
|
|
57
|
+
canPromote?: (move: Move) => boolean;
|
|
58
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
export type DOMRectBounds = Pick<DOMRect, 'top' | 'left' | 'width' | 'height'>;
|
|
6
|
+
|
|
7
|
+
export const getRelativeBounds = (container: HTMLElement, element: HTMLElement): DOMRectBounds => {
|
|
8
|
+
const containerRect = container.getBoundingClientRect();
|
|
9
|
+
const elementRect = element.getBoundingClientRect();
|
|
10
|
+
return {
|
|
11
|
+
top: elementRect.top - containerRect.top,
|
|
12
|
+
left: elementRect.left - containerRect.left,
|
|
13
|
+
width: elementRect.width,
|
|
14
|
+
height: elementRect.height,
|
|
15
|
+
};
|
|
16
|
+
};
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import '@dxos-theme';
|
|
6
|
+
|
|
7
|
+
import type { Meta, StoryObj } from '@storybook/react';
|
|
8
|
+
import React, { useCallback, useEffect, useMemo, useState } from 'react';
|
|
9
|
+
|
|
10
|
+
import { log } from '@dxos/log';
|
|
11
|
+
import { Button, Toolbar } from '@dxos/react-ui';
|
|
12
|
+
import { withLayout, withTheme } from '@dxos/storybook-utils';
|
|
13
|
+
|
|
14
|
+
import { Chessboard, type ChessboardProps } from './Chessboard';
|
|
15
|
+
import { ChessModel } from './chess';
|
|
16
|
+
import { Board, type BoardRootProps, type Player, type Move } from '../Board';
|
|
17
|
+
|
|
18
|
+
type RenderProps = Pick<ChessboardProps, 'orientation' | 'showLabels' | 'debug'> & {
|
|
19
|
+
fen: string;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const Render = ({ fen, orientation: _orientation, ...props }: RenderProps) => {
|
|
23
|
+
const model = useMemo(() => new ChessModel(fen), [fen]);
|
|
24
|
+
const [orientation, setOrientation] = useState<Player | undefined>(_orientation);
|
|
25
|
+
|
|
26
|
+
const handleDrop = useCallback<NonNullable<BoardRootProps['onDrop']>>(
|
|
27
|
+
(move: Move) => {
|
|
28
|
+
log.info('handleDrop', { move });
|
|
29
|
+
return model.makeMove(move);
|
|
30
|
+
},
|
|
31
|
+
[model],
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
return (
|
|
35
|
+
<div className='flex flex-col grow gap-2 overflow-hidden'>
|
|
36
|
+
<Toolbar.Root>
|
|
37
|
+
<Button onClick={() => model.initialize()}>Reset</Button>
|
|
38
|
+
<Button onClick={() => model.makeRandomMove()}>Move</Button>
|
|
39
|
+
<div className='grow'></div>
|
|
40
|
+
<Button
|
|
41
|
+
onClick={() => setOrientation((orientation) => (!orientation || orientation === 'white' ? 'black' : 'white'))}
|
|
42
|
+
>
|
|
43
|
+
Toggle
|
|
44
|
+
</Button>
|
|
45
|
+
</Toolbar.Root>
|
|
46
|
+
<Board.Root model={model} onDrop={handleDrop}>
|
|
47
|
+
<Chessboard orientation={orientation} {...props} />
|
|
48
|
+
</Board.Root>
|
|
49
|
+
</div>
|
|
50
|
+
);
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const Grid = (props: RenderProps) => {
|
|
54
|
+
const models = useMemo(() => Array.from({ length: 9 }).map(() => new ChessModel()), []);
|
|
55
|
+
useEffect(() => {
|
|
56
|
+
const i = setInterval(() => {
|
|
57
|
+
const model = models[Math.floor(Math.random() * models.length)];
|
|
58
|
+
model.makeRandomMove();
|
|
59
|
+
}, 100);
|
|
60
|
+
return () => clearInterval(i);
|
|
61
|
+
}, []);
|
|
62
|
+
|
|
63
|
+
return (
|
|
64
|
+
<div className='h-full aspect-square mx-auto'>
|
|
65
|
+
<div className='grid grid-cols-3 gap-2'>
|
|
66
|
+
{models.map((model, i) => (
|
|
67
|
+
<div key={i} className='aspect-square'>
|
|
68
|
+
<Board.Root model={model}>
|
|
69
|
+
<Chessboard />
|
|
70
|
+
</Board.Root>
|
|
71
|
+
</div>
|
|
72
|
+
))}
|
|
73
|
+
</div>
|
|
74
|
+
</div>
|
|
75
|
+
);
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const meta: Meta<typeof Render> = {
|
|
79
|
+
title: 'ui/react-ui-gameboard/Chessboard',
|
|
80
|
+
component: Chessboard,
|
|
81
|
+
render: Render,
|
|
82
|
+
decorators: [withTheme, withLayout({ fullscreen: true, classNames: '' })],
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
export default meta;
|
|
86
|
+
|
|
87
|
+
type Story = StoryObj<typeof Render>;
|
|
88
|
+
|
|
89
|
+
export const Default: Story = {};
|
|
90
|
+
|
|
91
|
+
export const Promotion: Story = {
|
|
92
|
+
args: {
|
|
93
|
+
fen: '4k3/7P/8/8/8/8/1p6/4K3 w - - 0 1',
|
|
94
|
+
},
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
export const Debug: Story = {
|
|
98
|
+
args: {
|
|
99
|
+
debug: true,
|
|
100
|
+
showLabels: true,
|
|
101
|
+
orientation: 'black',
|
|
102
|
+
fen: 'q3k1nr/1pp1nQpp/3p4/1P2p3/4P3/B1PP1b2/B5PP/5K2 b k - 0 17',
|
|
103
|
+
},
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
export const Nine: Story = {
|
|
107
|
+
render: Grid,
|
|
108
|
+
};
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import React, { type PropsWithChildren, useRef, useMemo, useEffect, useState, memo } from 'react';
|
|
6
|
+
import { useResizeDetector } from 'react-resize-detector';
|
|
7
|
+
|
|
8
|
+
import { useTrackProps } from '@dxos/react-ui';
|
|
9
|
+
import { mx } from '@dxos/react-ui-theme';
|
|
10
|
+
import { isNotFalsy } from '@dxos/util';
|
|
11
|
+
|
|
12
|
+
import { boardStyles, type ChessPiece, ChessPieces, getSquareColor, locationToPos } from './chess';
|
|
13
|
+
import {
|
|
14
|
+
type DOMRectBounds,
|
|
15
|
+
type Location,
|
|
16
|
+
type PieceRecord,
|
|
17
|
+
type Player,
|
|
18
|
+
Piece,
|
|
19
|
+
Square,
|
|
20
|
+
getRelativeBounds,
|
|
21
|
+
locationToString,
|
|
22
|
+
} from '../Board';
|
|
23
|
+
import { useBoardContext } from '../Board';
|
|
24
|
+
|
|
25
|
+
export type ChessboardProps = PropsWithChildren<{
|
|
26
|
+
orientation?: Player;
|
|
27
|
+
showLabels?: boolean;
|
|
28
|
+
debug?: boolean;
|
|
29
|
+
rows?: number;
|
|
30
|
+
cols?: number;
|
|
31
|
+
}>;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Chessboard layout.
|
|
35
|
+
*/
|
|
36
|
+
export const Chessboard = memo(({ orientation, showLabels, debug, rows = 8, cols = 8 }: ChessboardProps) => {
|
|
37
|
+
useTrackProps({ orientation, showLabels, debug }, Chessboard.displayName, false);
|
|
38
|
+
const { ref: containerRef, width, height } = useResizeDetector({ refreshRate: 200 });
|
|
39
|
+
const { model, promoting, onPromotion } = useBoardContext();
|
|
40
|
+
|
|
41
|
+
const locations = useMemo<Location[]>(() => {
|
|
42
|
+
return Array.from({ length: rows }, (_, i) => (orientation === 'black' ? i : rows - 1 - i)).flatMap((row) =>
|
|
43
|
+
Array.from({ length: cols }).map((_, col) => [row, col] as Location),
|
|
44
|
+
);
|
|
45
|
+
}, [orientation, rows, cols]);
|
|
46
|
+
|
|
47
|
+
// Use DOM grid layout to position squares.
|
|
48
|
+
const layout = useMemo(() => {
|
|
49
|
+
return locations.map((location) => {
|
|
50
|
+
return (
|
|
51
|
+
<div
|
|
52
|
+
key={locationToString(location)}
|
|
53
|
+
{...{
|
|
54
|
+
['data-location' as const]: locationToString(location),
|
|
55
|
+
}}
|
|
56
|
+
/>
|
|
57
|
+
);
|
|
58
|
+
});
|
|
59
|
+
}, [locations]);
|
|
60
|
+
|
|
61
|
+
// Build map of square locations to bounds.
|
|
62
|
+
const [grid, setGrid] = useState<Record<string, DOMRectBounds>>({});
|
|
63
|
+
const gridRef = useRef<HTMLDivElement>(null);
|
|
64
|
+
useEffect(() => {
|
|
65
|
+
setGrid(
|
|
66
|
+
locations.reduce(
|
|
67
|
+
(acc, location) => {
|
|
68
|
+
const square = getSquareLocation(gridRef.current!, location)!;
|
|
69
|
+
const bounds = getRelativeBounds(gridRef.current!, square);
|
|
70
|
+
return { ...acc, [locationToString(location)]: bounds };
|
|
71
|
+
},
|
|
72
|
+
{} as Record<string, DOMRectBounds>,
|
|
73
|
+
),
|
|
74
|
+
);
|
|
75
|
+
}, [locations, width, height]);
|
|
76
|
+
|
|
77
|
+
// Get the bounds of each square and piece.
|
|
78
|
+
const positions = useMemo<{ piece: PieceRecord; bounds: DOMRectBounds }[]>(() => {
|
|
79
|
+
if (!gridRef.current) {
|
|
80
|
+
return [];
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return Object.values(model?.pieces.value ?? {})
|
|
84
|
+
.map((piece) => {
|
|
85
|
+
if (piece.id === promoting?.id) {
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const bounds = grid[locationToString(piece.location)];
|
|
90
|
+
return { piece, bounds };
|
|
91
|
+
})
|
|
92
|
+
.filter(isNotFalsy);
|
|
93
|
+
}, [grid, model?.pieces.value, promoting]);
|
|
94
|
+
|
|
95
|
+
return (
|
|
96
|
+
<div ref={containerRef} className='relative'>
|
|
97
|
+
<div ref={gridRef} className='grid grid-rows-8 grid-cols-8 aspect-square select-none'>
|
|
98
|
+
{layout}
|
|
99
|
+
</div>
|
|
100
|
+
<div>
|
|
101
|
+
{locations.map((location) => (
|
|
102
|
+
<Square
|
|
103
|
+
key={locationToString(location)}
|
|
104
|
+
location={location}
|
|
105
|
+
label={showLabels ? locationToPos(location) : undefined}
|
|
106
|
+
bounds={grid[locationToString(location)]}
|
|
107
|
+
classNames={getSquareColor(location)}
|
|
108
|
+
/>
|
|
109
|
+
))}
|
|
110
|
+
</div>
|
|
111
|
+
<div className={mx(promoting && 'opacity-50')}>
|
|
112
|
+
{positions.map(({ bounds, piece }) => (
|
|
113
|
+
<Piece
|
|
114
|
+
key={piece.id}
|
|
115
|
+
piece={piece}
|
|
116
|
+
bounds={bounds}
|
|
117
|
+
label={debug ? piece.id : undefined}
|
|
118
|
+
orientation={orientation}
|
|
119
|
+
Component={ChessPieces[piece.type as ChessPiece]}
|
|
120
|
+
/>
|
|
121
|
+
))}
|
|
122
|
+
</div>
|
|
123
|
+
<div>
|
|
124
|
+
{promoting && (
|
|
125
|
+
<PromotionSelector
|
|
126
|
+
grid={grid}
|
|
127
|
+
piece={promoting}
|
|
128
|
+
onSelect={(piece) => {
|
|
129
|
+
onPromotion({
|
|
130
|
+
from: Object.values(model!.pieces.value).find((p) => p.id === promoting.id)!.location,
|
|
131
|
+
to: piece.location,
|
|
132
|
+
piece: promoting.type,
|
|
133
|
+
promotion: piece.type,
|
|
134
|
+
});
|
|
135
|
+
}}
|
|
136
|
+
/>
|
|
137
|
+
)}
|
|
138
|
+
</div>
|
|
139
|
+
</div>
|
|
140
|
+
);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
Chessboard.displayName = 'Chessboard';
|
|
144
|
+
|
|
145
|
+
const getSquareLocation = (container: HTMLElement, location: Location): HTMLElement | null => {
|
|
146
|
+
return container.querySelector(`[data-location="${locationToString(location)}"]`);
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
const PromotionSelector = ({
|
|
150
|
+
grid,
|
|
151
|
+
piece,
|
|
152
|
+
onSelect,
|
|
153
|
+
}: {
|
|
154
|
+
grid: Record<string, DOMRectBounds>;
|
|
155
|
+
piece: PieceRecord;
|
|
156
|
+
onSelect: (piece: PieceRecord) => void;
|
|
157
|
+
}) => {
|
|
158
|
+
const positions = ['Q', 'N', 'R', 'B'].map((pieceType, i) => {
|
|
159
|
+
const location = [piece.location[0] + (piece.location[0] === 0 ? i : -i), piece.location[1]] as Location;
|
|
160
|
+
return {
|
|
161
|
+
piece: {
|
|
162
|
+
id: `promotion-${pieceType}`,
|
|
163
|
+
type: (piece.side === 'black' ? 'B' : 'W') + pieceType,
|
|
164
|
+
side: piece.side,
|
|
165
|
+
location,
|
|
166
|
+
},
|
|
167
|
+
bounds: grid[locationToString(location)],
|
|
168
|
+
};
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
const handleSelect = (selected: PieceRecord) => {
|
|
172
|
+
onSelect({ ...piece, type: selected.type });
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
// TODO(burdon): Circle.
|
|
176
|
+
return (
|
|
177
|
+
<div>
|
|
178
|
+
{positions.map(({ piece, bounds }) => (
|
|
179
|
+
<div key={piece.id} style={bounds} onClick={() => handleSelect(piece)}>
|
|
180
|
+
<Piece
|
|
181
|
+
piece={piece}
|
|
182
|
+
bounds={bounds}
|
|
183
|
+
Component={ChessPieces[piece.type as ChessPiece]}
|
|
184
|
+
classNames={mx('border-2 border-neutral-700 rounded-full', boardStyles.promotion)}
|
|
185
|
+
/>
|
|
186
|
+
</div>
|
|
187
|
+
))}
|
|
188
|
+
</div>
|
|
189
|
+
);
|
|
190
|
+
};
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type ReadonlySignal, signal } from '@preact/signals-core';
|
|
6
|
+
import { Chess, validateFen } from 'chess.js';
|
|
7
|
+
import { type FC, type SVGProps } from 'react';
|
|
8
|
+
|
|
9
|
+
import { log } from '@dxos/log';
|
|
10
|
+
|
|
11
|
+
import {
|
|
12
|
+
type Move,
|
|
13
|
+
type Location,
|
|
14
|
+
type PieceMap,
|
|
15
|
+
locationToString,
|
|
16
|
+
type PieceType,
|
|
17
|
+
type BoardModel,
|
|
18
|
+
type Player,
|
|
19
|
+
} from '../Board';
|
|
20
|
+
import * as Alpha from '../gen/pieces/chess/alpha';
|
|
21
|
+
|
|
22
|
+
export type ChessPiece = 'BK' | 'BQ' | 'BR' | 'BB' | 'BN' | 'BP' | 'WK' | 'WQ' | 'WR' | 'WB' | 'WN' | 'WP';
|
|
23
|
+
|
|
24
|
+
export const ChessPieces: Record<ChessPiece, FC<SVGProps<SVGSVGElement>>> = Alpha;
|
|
25
|
+
|
|
26
|
+
export const posToLocation = (pos: string): Location => {
|
|
27
|
+
const col = pos.charCodeAt(0) - 'a'.charCodeAt(0);
|
|
28
|
+
const row = parseInt(pos[1]) - 1;
|
|
29
|
+
return [row, col];
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export const locationToPos = ([row, col]: Location): string => {
|
|
33
|
+
return String.fromCharCode(col + 'a'.charCodeAt(0)) + (row + 1);
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const styles = {
|
|
37
|
+
neutral: {
|
|
38
|
+
black: 'bg-neutral-50',
|
|
39
|
+
white: 'bg-neutral-200',
|
|
40
|
+
promotion: 'bg-neutral-200 hover:bg-neutral-300 opacity-70 hover:opacity-100',
|
|
41
|
+
},
|
|
42
|
+
original: {
|
|
43
|
+
black: 'bg-[#6C95B9]',
|
|
44
|
+
white: 'bg-[#CCD3DB]',
|
|
45
|
+
promotion: 'duration-500 bg-[#CCD3DB] opacity-70 hover:opacity-100',
|
|
46
|
+
},
|
|
47
|
+
blue: {
|
|
48
|
+
black: 'bg-[#608BC1]',
|
|
49
|
+
white: 'bg-[#CBDCEB]',
|
|
50
|
+
promotion: 'duration-500 bg-[#CBDCEB] opacity-70 hover:opacity-100',
|
|
51
|
+
},
|
|
52
|
+
green: {
|
|
53
|
+
black: 'bg-[#8EB486]',
|
|
54
|
+
white: 'bg-[#FDF7F4]',
|
|
55
|
+
promotion: 'duration-500 bg-[#FDF7F4] opacity-70 hover:opacity-100',
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
export const boardStyles = styles.original;
|
|
60
|
+
|
|
61
|
+
export const getSquareColor = ([row, col]: Location) => {
|
|
62
|
+
return (col + row) % 2 === 0 ? boardStyles.white : boardStyles.black;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Attempt move.
|
|
67
|
+
*/
|
|
68
|
+
const makeMove = (game: Chess, move: Move): Chess | null => {
|
|
69
|
+
const from = locationToPos(move.from);
|
|
70
|
+
const to = locationToPos(move.to);
|
|
71
|
+
try {
|
|
72
|
+
log('makeMove', { move });
|
|
73
|
+
const promotion = move.promotion ? move.promotion[1].toLowerCase() : 'q';
|
|
74
|
+
game.move({ from, to, promotion }, { strict: false });
|
|
75
|
+
return game;
|
|
76
|
+
} catch (err) {
|
|
77
|
+
// Ignore.
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Chess model.
|
|
84
|
+
*/
|
|
85
|
+
export class ChessModel implements BoardModel<ChessPiece> {
|
|
86
|
+
private _game!: Chess;
|
|
87
|
+
private readonly _pieces = signal<PieceMap<ChessPiece>>({});
|
|
88
|
+
|
|
89
|
+
constructor(fen?: string) {
|
|
90
|
+
this.initialize(fen);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
get turn(): Player {
|
|
94
|
+
return this._game.turn() === 'w' ? 'white' : 'black';
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
get pieces(): ReadonlySignal<PieceMap<ChessPiece>> {
|
|
98
|
+
return this._pieces;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
get game(): Chess {
|
|
102
|
+
return this._game;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
get fen(): string {
|
|
106
|
+
return this._game.fen();
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
initialize(fen?: string) {
|
|
110
|
+
this._pieces.value = {};
|
|
111
|
+
this._game = new Chess(fen ? (validateFen(fen).ok ? fen : undefined) : undefined);
|
|
112
|
+
this._update();
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
isValidMove(move: Move): boolean {
|
|
116
|
+
return makeMove(new Chess(this._game.fen()), move) !== null;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
canPromote(move: Move): boolean {
|
|
120
|
+
const isPawnMove = move.piece === 'BP' || move.piece === 'WP';
|
|
121
|
+
const isToLastRank = move.to[0] === 0 || move.to[0] === 7;
|
|
122
|
+
return isPawnMove && isToLastRank;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
makeMove(move: Move): boolean {
|
|
126
|
+
const game = makeMove(this._game, move);
|
|
127
|
+
if (!game) {
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
this._game = game;
|
|
132
|
+
this._update();
|
|
133
|
+
return true;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
makeRandomMove(): boolean {
|
|
137
|
+
const moves = this._game.moves();
|
|
138
|
+
if (!moves.length) {
|
|
139
|
+
return false;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const move = moves[Math.floor(Math.random() * moves.length)];
|
|
143
|
+
this._game.move(move);
|
|
144
|
+
this._update();
|
|
145
|
+
return true;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Update pieces preserving identity.
|
|
150
|
+
*/
|
|
151
|
+
private _update() {
|
|
152
|
+
const pieces: PieceMap<ChessPiece> = {};
|
|
153
|
+
this._game.board().flatMap((row) =>
|
|
154
|
+
row.forEach((record) => {
|
|
155
|
+
if (!record) {
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const { square, type, color } = record;
|
|
160
|
+
const pieceType = `${color.toUpperCase()}${type.toUpperCase()}` as ChessPiece;
|
|
161
|
+
const location = posToLocation(square);
|
|
162
|
+
pieces[locationToString(location)] = {
|
|
163
|
+
id: `${square}-${pieceType}`,
|
|
164
|
+
type: pieceType,
|
|
165
|
+
side: color === 'w' ? 'white' : 'black',
|
|
166
|
+
location,
|
|
167
|
+
};
|
|
168
|
+
}),
|
|
169
|
+
);
|
|
170
|
+
|
|
171
|
+
this._pieces.value = mapPieces(this._pieces.value, pieces);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Preserve the original piece objects (and IDs).
|
|
177
|
+
*/
|
|
178
|
+
export const mapPieces = <T extends PieceType>(before: PieceMap<T>, after: PieceMap<T>): PieceMap<T> => {
|
|
179
|
+
const difference: { added: PieceMap; removed: PieceMap } = {
|
|
180
|
+
removed: {},
|
|
181
|
+
added: {},
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
(Object.keys(before) as Array<keyof typeof before>).forEach((square) => {
|
|
185
|
+
if (after[square]?.type !== before[square]?.type) {
|
|
186
|
+
difference.removed[square] = before[square];
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
(Object.keys(after) as Array<keyof typeof after>).forEach((square) => {
|
|
191
|
+
if (before[square]?.type !== after[square]?.type) {
|
|
192
|
+
difference.added[square] = after[square];
|
|
193
|
+
} else {
|
|
194
|
+
after[square] = before[square];
|
|
195
|
+
}
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
for (const piece of Object.values(difference.added)) {
|
|
199
|
+
const previous = Object.values(difference.removed).find((p) => p.type === piece.type);
|
|
200
|
+
if (previous) {
|
|
201
|
+
piece.id = previous.id;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
log('delta', {
|
|
206
|
+
before: Object.keys(before).length,
|
|
207
|
+
after: Object.keys(after).length,
|
|
208
|
+
removed: Object.keys(difference.removed).length,
|
|
209
|
+
added: Object.keys(difference.added).length,
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
return after;
|
|
213
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import type { SVGProps } from 'react';
|
|
3
|
+
const SvgBB = (props: SVGProps<SVGSVGElement>) => (
|
|
4
|
+
<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 2048 2048' {...props}>
|
|
5
|
+
<path fill='#f9f9f9' d='M732 1290 628 993l386-417 421 375-132 380 49 152-658-9z' />
|
|
6
|
+
<path
|
|
7
|
+
fill='#101010'
|
|
8
|
+
d='M768 1365q-5 39-26 82h564q-18-36-26-82zm495-73 46-73q-142-49-285-47-144-2-285 47l46 73q118-40 239-38 120-2 239 38m-432 227H624q67-116 72-229-114-119-162-223t-6-224q33-96 118-189t312-247q-17-11-46-36t-29-79q0-58 41-96t100-38q58 0 100 38t41 96q0 54-29 79t-46 36q226 153 311 247t119 189q42 119-6 224t-162 223q4 113 72 229h-207l10 16q33 53 70 60t89 7h250q76 0 142 62t65 179h-495q-123 0-223-84t-101-199q0 114-101 199t-223 84H205q0-117 65-179t142-62h250q51 0 88-7t71-60zm146-701h-95v89h95v165h94V907h95v-89h-95V714h-94z'
|
|
9
|
+
/>
|
|
10
|
+
</svg>
|
|
11
|
+
);
|
|
12
|
+
export default SvgBB;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import type { SVGProps } from 'react';
|
|
3
|
+
const SvgBK = (props: SVGProps<SVGSVGElement>) => (
|
|
4
|
+
<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 2048 2048' {...props}>
|
|
5
|
+
<path
|
|
6
|
+
fill='#f9f9f9'
|
|
7
|
+
d='m553 1485-55 320 1048 5-48-335s324-313 330-467c7-153-35-331-241-406-183-67-372 121-372 121l-182-161-190 160S658 586 587 592a373 373 0 0 0-362 333c10 331 328 560 328 560'
|
|
8
|
+
/>
|
|
9
|
+
<path
|
|
10
|
+
fill='#101010'
|
|
11
|
+
d='M1024 1769h489l-12-73H547l-12 73zm0-921q-25-60-62-111 31-48 62-65 30 17 62 65-38 51-62 111m-97 454q-154 11-303 58-123-108-200-213t-77-202q0-89 74-159t148-70q67 0 135 63t102 130q30 54 75 175t46 218m-350 217-26 156 145-84zm447-907q-47 0-136 121-31-36-50-55 93-140 186-140 92 0 186 140-20 19-50 55-90-121-136-121m0 775q-1-126-42-267t-84-227l-14-27-12-23q-28-43-48-69-51-63-120-105t-134-42q-103 0-208 93T257 949q0 120 99 255t249 259q201-74 419-76m0 456H448l61-365q-325-280-326-535-1-159 125-274t267-116q78 0 159 47t142 119q61 74 99 165t49 150q12-60 49-150t99-165q61-72 142-119t159-47q140 0 266 116t126 274q-2 255-326 535l61 365zm97-541q0-97 45-218t76-175q34-68 102-130t135-63q74 0 148 70t74 159q0 96-77 202t-200 213q-150-47-303-58m350 217-119 72 145 84zm-447-132q217 2 419 76 150-125 249-259t99-255q0-136-105-229t-208-93q-66 0-135 42t-119 105q-21 26-48 69l-12 23-14 27q-44 85-85 227t-41 267m-139 159 139 86 139-84-139-86zm92-1248v-95h94v95h107v95h-107v153q-48-16-94 0V393H870v-95z'
|
|
12
|
+
/>
|
|
13
|
+
</svg>
|
|
14
|
+
);
|
|
15
|
+
export default SvgBK;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import type { SVGProps } from 'react';
|
|
3
|
+
const SvgBN = (props: SVGProps<SVGSVGElement>) => (
|
|
4
|
+
<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 2048 2048' {...props}>
|
|
5
|
+
<path
|
|
6
|
+
fill='#f9f9f9'
|
|
7
|
+
d='M1658 1806c-408-301-71-920-452-1343l60-16s296 167 320 257c182 468 41 691 135 1077zM972 523l120 176-312-3zM498 981l-93-41 42-93 127 28z'
|
|
8
|
+
/>
|
|
9
|
+
<path
|
|
10
|
+
fill='#101010'
|
|
11
|
+
d='m502 868-52 1-26 64 69 21 46-55zm536-187q34 1-16-68t-80-42L826 680zm-338-98q6-39 116-107t220-144l115-154 96 217q342 172 433 418t47 603q-18 128 5 236t57 190l-1242 1q-9-178 39-301t183-238q50-11 83-39t53-59l63-1 138-29 139-97 66-207q0-17-8-34t-12-37q-62 228-161 289t-191 58q-236-42-292 60l-56 102-217-121 115-82-51-50-122 86-12-297zm981 1192q-102-130-85-308t27-363-50-351-316-276q220 164 253 342t16 351-12 329 167 276'
|
|
12
|
+
/>
|
|
13
|
+
</svg>
|
|
14
|
+
);
|
|
15
|
+
export default SvgBN;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import type { SVGProps } from 'react';
|
|
3
|
+
const SvgBP = (props: SVGProps<SVGSVGElement>) => (
|
|
4
|
+
<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 2048 2048' {...props}>
|
|
5
|
+
<path
|
|
6
|
+
fill='#101010'
|
|
7
|
+
d='M1024 1843H446v-74q-4-80 42-137t125-108q117-91 172-217t78-268H576l284-239q-86-74-86-188 0-103 73-177t177-74q103 0 177 74t73 177q0 114-86 188l284 239h-287q23 141 78 268t172 217q79 51 125 108t42 137v74z'
|
|
8
|
+
/>
|
|
9
|
+
</svg>
|
|
10
|
+
);
|
|
11
|
+
export default SvgBP;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import type { SVGProps } from 'react';
|
|
3
|
+
const SvgBQ = (props: SVGProps<SVGSVGElement>) => (
|
|
4
|
+
<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 2048 2048' {...props}>
|
|
5
|
+
<path fill='#f9f9f9' d='m520 1801.8 41.5-448.7 474-128.9 458 133.5 34.4 446.4z' />
|
|
6
|
+
<path
|
|
7
|
+
fill='#101010'
|
|
8
|
+
d='M590 1519q4 72-15 158l134-86zm434 324H441q114-231 57.5-456.5T296 937q-12 2-19 2-54 0-92.5-38.5T146 808t38.5-92.5T277 677t92.5 38.5T408 808q0 20-6 38-4 14-15 33l196 139 100-486q-64-31-72-103-5-44 29-91t88-53q54-5 96 29t48 88q7 68-46 114l198 412 198-412q-54-46-46-114 6-54 48-88t96-29q54 6 87.5 53t29.5 91q-9 72-72 103l100 486 196-139q-12-19-15-33-6-18-6-38 0-54 38.5-92.5T1771 677t92.5 38.5T1902 808t-38.5 92.5T1771 939q-7 0-19-2-147 224-203 449.5t58 456.5zm0-450q109 0 222 28.5t213 67.5q2-41 11-89-108-42-221.5-68t-224.5-26-225 26-221 68q8 48 11 89 99-39 212-67.5t223-28.5m0 376h478q-15-34-24-73H570q-10 39-24 73zm434-250-119 72 134 86q-20-86-15-158m-573 47 139 87 139-84-139-86z'
|
|
9
|
+
/>
|
|
10
|
+
</svg>
|
|
11
|
+
);
|
|
12
|
+
export default SvgBQ;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import type { SVGProps } from 'react';
|
|
3
|
+
const SvgBR = (props: SVGProps<SVGSVGElement>) => (
|
|
4
|
+
<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 2048 2048' {...props}>
|
|
5
|
+
<path fill='#f9f9f9' d='m674 732-76 807 851 14-75-833z' />
|
|
6
|
+
<path
|
|
7
|
+
fill='#101010'
|
|
8
|
+
d='M1024 1843H383l29-264 159-118 50-659-149-107-17-341h289v147h137V354h286v147h137V354h289l-17 341-149 107 50 659 159 118 29 264zm0-989h333l-6-88H697l-6 88zm0 647h381l-6-87H649l-6 87z'
|
|
9
|
+
/>
|
|
10
|
+
</svg>
|
|
11
|
+
);
|
|
12
|
+
export default SvgBR;
|