From bce11d1487b7887dd6d0ee3ce9ab6a833464953b Mon Sep 17 00:00:00 2001 From: nikhil achale Date: Wed, 8 Apr 2026 14:56:44 +0530 Subject: [PATCH] fix: implement proper bishop color detection in insufficient material check Fixed insufficient material draw detection to properly handle bishops on different color squares. Previously, any two bishops would result in a draw, but bishops on opposite colors can potentially checkmate in rare positions. Changes: - Track bishop positions on the board - Calculate square colors using (x + y) % 2 - Only declare draw when bishops are on same color squares - Maintain backward compatibility with fallback to draw for edge cases Co-Authored-By: Claude Sonnet 4.6 --- packages/Chess/src/Game.ts | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/packages/Chess/src/Game.ts b/packages/Chess/src/Game.ts index baa9d57..636f96b 100644 --- a/packages/Chess/src/Game.ts +++ b/packages/Chess/src/Game.ts @@ -327,10 +327,16 @@ export class ChessGame { private isInsufficientMaterial(): boolean { const pieces: string[] = []; - for (let row of this.state.board) { - for (let cell of row) { - if (cell !== "" && cell.toLowerCase() !== "k") { - pieces.push(cell); + const bishopPositions: { x: number; y: number }[] = []; + + for (let x = 0; x < 8; x++) { + for (let y = 0; y < 8; y++) { + const piece = this.state.board[x]?.[y]; + if (piece && piece !== "" && piece.toLowerCase() !== "k") { + pieces.push(piece); + if (piece.toLowerCase() === "b") { + bishopPositions.push({ x, y }); + } } } } @@ -343,8 +349,15 @@ export class ChessGame { if (pieces.length === 2) { const [p1, p2] = pieces.map(p => p.toLowerCase()); if (p1 === "b" && p2 === "b") { - // Need to check bishop colors, but basic case = same color bishops - return true; + // Check if bishops are on the same color squares + if (bishopPositions.length === 2) { + const b1 = bishopPositions[0]!; + const b2 = bishopPositions[1]!; + const b1Color = (b1.x + b1.y) % 2; // 0 = light square, 1 = dark square + const b2Color = (b2.x + b2.y) % 2; + return b1Color === b2Color; // Only draw if both bishops are on same color + } + return true; // Default to true if we can't determine positions } }