Skip to content

Commit

Permalink
fix uci castling
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscoBSalgueiro committed Feb 12, 2024
1 parent 4bd7e94 commit 81ee696
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions src/utils/chess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ import { Score, commands } from "@/bindings";
import { MantineColor } from "@mantine/core";
import { invoke } from "@tauri-apps/api";
import { DrawShape } from "chessground/draw";
import { Color, Role, makeSquare, makeUci } from "chessops";
import {
Color,
NormalMove,
Role,
makeSquare,
makeUci,
parseSquare,
} from "chessops";
import { INITIAL_FEN, makeFen, parseFen } from "chessops/fen";
import { isPawns, parseComment } from "chessops/pgn";
import { makeSan, parseSan } from "chessops/san";
Expand Down Expand Up @@ -163,12 +170,33 @@ export function getMainLine(root: TreeNode): string[] {
export function getVariationLine(root: TreeNode, position: number[]): string[] {
const moves = [];
let node = root;
const [chess] = positionFromFen(root.fen);
if (!chess) {
return [];
}
for (const pos of position) {
node = node.children[pos];
if (node.move) {
moves.push(makeUci(node.move));
const move = node.move as NormalMove;
const uci = makeUci(node.move);
const square = parseSquare(uci.substring(0, 2))!;
const kingRole = chess.board.get(square)?.role;

if (kingRole === "king") {
if (uci.endsWith("h1") || uci.endsWith("a1")) {
moves.push(uci.endsWith("h1") ? "e1g1" : "e1c1");
} else if (uci.endsWith("h8") || uci.endsWith("a8")) {
moves.push(uci.endsWith("h8") ? "e8g8" : "e8c8");
} else {
moves.push(uci);
}
} else {
moves.push(uci);
}
chess.play(move);
}
}
console.log(moves);
return moves;
}

Expand Down

3 comments on commit 81ee696

@Disservin
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@franciscoBSalgueiro sorry to bother you, but this patch looks a bit weird, what was the intention behind it?
What if there is a king moving from g1 to h1 ? This patch would convert this to a castling move or am I wrong?

Is this only for internal conversion and not exposed to the engine ? Otherwise engines will be a bit confused if they send e1h1 (for a frc castling move) and get back (e1g1) for the next position which the gui sends.

@franciscoBSalgueiro
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, thanks for catching that.

The issue that I was trying to solve was that stockfish doesn't accept e1h1 as castling, so I was always converting it to e1g1. I'm not considering chess960 yet, because that's not yet working for other reasons as well.

@Disservin
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah you are right. Stockfish (and 99% of engines) castling differs in two ways:

Standard is the from square + the to square
FRC/DFRC is the from square + square of the rook that we are castling with.

Please sign in to comment.