fix : remove piece recapture from great moves
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import { EvaluateGameParams, PositionEval } from "@/types/eval";
|
import { EvaluateGameParams, PositionEval } from "@/types/eval";
|
||||||
import { Game } from "@/types/game";
|
import { Game } from "@/types/game";
|
||||||
import { Chess, PieceSymbol } from "chess.js";
|
import { Chess, PieceSymbol, Square } from "chess.js";
|
||||||
import { getPositionWinPercentage } from "./engine/helpers/winPercentage";
|
import { getPositionWinPercentage } from "./engine/helpers/winPercentage";
|
||||||
import { Color } from "@/types/enums";
|
import { Color } from "@/types/enums";
|
||||||
|
|
||||||
@@ -156,15 +156,30 @@ export const getWhoIsCheckmated = (fen: string): "w" | "b" | null => {
|
|||||||
export const uciMoveParams = (
|
export const uciMoveParams = (
|
||||||
uciMove: string
|
uciMove: string
|
||||||
): {
|
): {
|
||||||
from: string;
|
from: Square;
|
||||||
to: string;
|
to: Square;
|
||||||
promotion?: string | undefined;
|
promotion?: string | undefined;
|
||||||
} => ({
|
} => ({
|
||||||
from: uciMove.slice(0, 2),
|
from: uciMove.slice(0, 2) as Square,
|
||||||
to: uciMove.slice(2, 4),
|
to: uciMove.slice(2, 4) as Square,
|
||||||
promotion: uciMove.slice(4, 5) || undefined,
|
promotion: uciMove.slice(4, 5) || undefined,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export const isSimplePieceRecapture = (
|
||||||
|
fen: string,
|
||||||
|
uciMoves: [string, string]
|
||||||
|
): boolean => {
|
||||||
|
const game = new Chess(fen);
|
||||||
|
const moves = uciMoves.map((uciMove) => uciMoveParams(uciMove));
|
||||||
|
|
||||||
|
if (moves[0].to !== moves[1].to) return false;
|
||||||
|
|
||||||
|
const piece = game.get(moves[0].to);
|
||||||
|
if (piece) return true;
|
||||||
|
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
export const getIsPieceSacrifice = (
|
export const getIsPieceSacrifice = (
|
||||||
fen: string,
|
fen: string,
|
||||||
playedMove: string,
|
playedMove: string,
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import {
|
|||||||
} from "./winPercentage";
|
} from "./winPercentage";
|
||||||
import { MoveClassification } from "@/types/enums";
|
import { MoveClassification } from "@/types/enums";
|
||||||
import { openings } from "@/data/openings";
|
import { openings } from "@/data/openings";
|
||||||
import { getIsPieceSacrifice } from "@/lib/chess";
|
import { getIsPieceSacrifice, isSimplePieceRecapture } from "@/lib/chess";
|
||||||
|
|
||||||
export const getMovesClassification = (
|
export const getMovesClassification = (
|
||||||
rawPositions: PositionEval[],
|
rawPositions: PositionEval[],
|
||||||
@@ -63,12 +63,18 @@ export const getMovesClassification = (
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const fenTwoMovesAgo = index > 1 ? fens[index - 2] : null;
|
||||||
|
const uciNextTwoMoves: [string, string] | null =
|
||||||
|
index > 1 ? [uciMoves[index - 2], uciMoves[index - 1]] : null;
|
||||||
|
|
||||||
if (
|
if (
|
||||||
isGreatMove(
|
isGreatMove(
|
||||||
lastPositionWinPercentage,
|
lastPositionWinPercentage,
|
||||||
positionWinPercentage,
|
positionWinPercentage,
|
||||||
isWhiteMove,
|
isWhiteMove,
|
||||||
lastPositionAlternativeLineWinPercentage
|
lastPositionAlternativeLineWinPercentage,
|
||||||
|
fenTwoMovesAgo,
|
||||||
|
uciNextTwoMoves
|
||||||
)
|
)
|
||||||
) {
|
) {
|
||||||
return {
|
return {
|
||||||
@@ -155,7 +161,9 @@ const isGreatMove = (
|
|||||||
lastPositionWinPercentage: number,
|
lastPositionWinPercentage: number,
|
||||||
positionWinPercentage: number,
|
positionWinPercentage: number,
|
||||||
isWhiteMove: boolean,
|
isWhiteMove: boolean,
|
||||||
lastPositionAlternativeLineWinPercentage: number | undefined
|
lastPositionAlternativeLineWinPercentage: number | undefined,
|
||||||
|
fenTwoMovesAgo: string | null,
|
||||||
|
uciMoves: [string, string] | null
|
||||||
): boolean => {
|
): boolean => {
|
||||||
if (!lastPositionAlternativeLineWinPercentage) return false;
|
if (!lastPositionAlternativeLineWinPercentage) return false;
|
||||||
|
|
||||||
@@ -164,6 +172,13 @@ const isGreatMove = (
|
|||||||
(isWhiteMove ? 1 : -1);
|
(isWhiteMove ? 1 : -1);
|
||||||
if (winPercentageDiff < -2) return false;
|
if (winPercentageDiff < -2) return false;
|
||||||
|
|
||||||
|
if (
|
||||||
|
fenTwoMovesAgo &&
|
||||||
|
uciMoves &&
|
||||||
|
isSimplePieceRecapture(fenTwoMovesAgo, uciMoves)
|
||||||
|
)
|
||||||
|
return false;
|
||||||
|
|
||||||
const hasChangedGameOutcome = getHasChangedGameOutcome(
|
const hasChangedGameOutcome = getHasChangedGameOutcome(
|
||||||
lastPositionWinPercentage,
|
lastPositionWinPercentage,
|
||||||
positionWinPercentage,
|
positionWinPercentage,
|
||||||
|
|||||||
Reference in New Issue
Block a user