From eafa4e0fbb08992710d6cc7a6912d64412eb6006 Mon Sep 17 00:00:00 2001 From: Alex Clink Date: Sat, 25 Sep 2021 00:24:55 -0400 Subject: [PATCH] Fix castling check when converting move to SAN --- src/lx_chess/game.cr | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/lx_chess/game.cr b/src/lx_chess/game.cr index ab93fa4..85ef870 100644 --- a/src/lx_chess/game.cr +++ b/src/lx_chess/game.cr @@ -153,7 +153,20 @@ module LxChess end checkmate = check ? tmp_move(from, to) { checkmate?(next_turn(turn)) } : false - castling = piece.king? ? to - from : nil + + castles_k = false + castles_q = false + + if piece.king? + dist = (to - from) + if dist.abs == 2 + if dist.positive? + castles_k = true + else + castles_q = true + end + end + end Notation.new( square: @board.cord(to), @@ -165,8 +178,8 @@ module LxChess en_passant: en_passant, check: check && !checkmate, checkmate: checkmate, - castles_k: castling && castling.positive? || false, - castles_q: castling && castling.negative? || false + castles_k: castles_k, + castles_q: castles_q ) end