Skip to content

Commit 5649d00

Browse files
committed
games: Flip chessboard when playing as black
1 parent f207a13 commit 5649d00

3 files changed

Lines changed: 13 additions & 5 deletions

File tree

src/ps/games/chess/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ export class Chess extends Game<State> {
151151
showMoves: side === this.turn ? this.showMoves : [],
152152
selected: side === this.turn ? this.selected : null,
153153
isActive: side === this.turn,
154+
side,
154155
id: this.id,
155156
turn: this.turn!,
156157
theme: this.theme,

src/ps/games/chess/render.tsx

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import { Table } from '@/ps/games/render';
22
import { Button, Form } from '@/utils/components/ps';
33

4-
import type { RenderCtx } from '@/ps/games/chess/types';
4+
import type { RenderCtx, Turn } from '@/ps/games/chess/types';
55
import type { CellRenderer } from '@/ps/games/render';
66
import type { Chess, Square } from 'chess.js';
77
import type { ReactElement } from 'react';
88

99
type This = { msg: string };
1010

11-
function getSquare(x: number, y: number): Square {
12-
return ((y + 1).toLetter().toLowerCase() + (8 - x)) as Square;
11+
function getSquare(x: number, y: number, side: Turn | null): Square {
12+
const flip = side === 'B';
13+
return ((flip ? 8 - y : y + 1).toLetter().toLowerCase() + (flip ? x + 1 : 8 - x)) as Square;
1314
}
1415

1516
type BoardCell = ReturnType<Chess['board']>[number][number];
@@ -29,11 +30,16 @@ const PIECE_IMAGES: Record<string, string> = {
2930
bp: 'https://partbot.partman.dev/public/chess/BP.png',
3031
};
3132

33+
function adaptBoard(board: BoardCell[][], side: Turn | null): BoardCell[][] {
34+
if (side !== 'B') return board;
35+
return board.map(row => row.toReversed()).reverse();
36+
}
37+
3238
export function renderBoard(this: This, ctx: RenderCtx) {
3339
const size = ctx.small ? 30 : 45;
3440

3541
const Cell: CellRenderer<BoardCell> = ({ cell, i, j }) => {
36-
const square = getSquare(i, j);
42+
const square = getSquare(i, j, ctx.side);
3743
const action = ctx.showMoves.find(move => move.to === square);
3844
// Use the form during promotions instead
3945
const clickable =
@@ -64,7 +70,7 @@ export function renderBoard(this: This, ctx: RenderCtx) {
6470
);
6571
};
6672

67-
return <Table<BoardCell> board={ctx.board} labels={{ row: '9-1', col: 'A-Z' }} Cell={Cell} />;
73+
return <Table<BoardCell> board={adaptBoard(ctx.board, ctx.side)} labels={{ row: '9-1', col: 'A-Z' }} Cell={Cell} />;
6874
}
6975

7076
export function render(this: This, ctx: RenderCtx): ReactElement {

src/ps/games/chess/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export type ThemeColours = {
1818

1919
export type RenderCtx = {
2020
id: string;
21+
side: Turn | null;
2122
turn: Turn;
2223
board: ReturnType<Chess['board']>;
2324
selected?: Square | null;

0 commit comments

Comments
 (0)