From 151277524bf64453cd3107f32e5aa8c96cfb462b Mon Sep 17 00:00:00 2001 From: GuillaumeSD Date: Tue, 7 Jan 2025 00:03:00 +0100 Subject: [PATCH] fix : analysis with stalemate game --- src/lib/chess.ts | 5 +++++ src/lib/engine/uciEngine.ts | 18 +++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/lib/chess.ts b/src/lib/chess.ts index 1722e05..945d0d4 100644 --- a/src/lib/chess.ts +++ b/src/lib/chess.ts @@ -147,6 +147,11 @@ export const getEvaluationBarValue = ( return { whiteBarPercentage, label }; }; +export const getIsStalemate = (fen: string): boolean => { + const game = new Chess(fen); + return game.isStalemate(); +}; + export const getWhoIsCheckmated = (fen: string): "w" | "b" | null => { const game = new Chess(fen); if (!game.isCheckmate()) return null; diff --git a/src/lib/engine/uciEngine.ts b/src/lib/engine/uciEngine.ts index 7c2a41c..b3dc409 100644 --- a/src/lib/engine/uciEngine.ts +++ b/src/lib/engine/uciEngine.ts @@ -10,7 +10,7 @@ import { parseEvaluationResults, } from "./helpers/parseResults"; import { computeAccuracy } from "./helpers/accuracy"; -import { getWhoIsCheckmated } from "../chess"; +import { getIsStalemate, getWhoIsCheckmated } from "../chess"; import { getLichessEval } from "../lichess"; import { getMovesClassification } from "./helpers/moveClassification"; import { EngineWorker } from "@/types/engine"; @@ -158,6 +158,22 @@ export class UciEngine { }); continue; } + + const isStalemate = getIsStalemate(fen); + if (isStalemate) { + positions.push({ + lines: [ + { + pv: [], + depth: 0, + multiPv: 1, + cp: 0, + }, + ], + }); + continue; + } + const result = await this.evaluatePosition(fen, depth); positions.push(result); setEvaluationProgress?.(