From 07a8a97ef9b13dd14ca1aea21e1cfc3f78d5a490 Mon Sep 17 00:00:00 2001 From: Alexander Perechnev Date: Mon, 22 Mar 2021 12:47:56 +0200 Subject: [PATCH 1/2] Refactoring. --- Sources/ChessKit/Rules/StandardRules.swift | 83 +++++++++++----------- 1 file changed, 43 insertions(+), 40 deletions(-) diff --git a/Sources/ChessKit/Rules/StandardRules.swift b/Sources/ChessKit/Rules/StandardRules.swift index 30ce085..91de0b8 100644 --- a/Sources/ChessKit/Rules/StandardRules.swift +++ b/Sources/ChessKit/Rules/StandardRules.swift @@ -30,48 +30,20 @@ public class StandardRules: Rules { let bitboards = position.board.bitboards - for translation in MovingTranslations.default.diagonal { - for offset in 1..<8 { - let destination = kingSquare.translate(file: translation.0 * offset, rank: translation.1 * offset) - guard destination.isValid else { - break - } - - if (bitboards.white | bitboards.black) & destination.bitboardMask == Int64.zero { - continue - } - if bitboards.bitboard(for: position.state.turn) & destination.bitboardMask != Int64.zero { - break - } - - if (bitboards.queen | bitboards.bishop) & destination.bitboardMask == Int64.zero { - break - } else { - return true - } - } + if self.isLongCheck(kingSquare: kingSquare, + turn: position.state.turn, + translations: MovingTranslations.default.diagonal, + bitboards: bitboards, + pieces: bitboards.queen | bitboards.bishop) { + return true } - for translation in MovingTranslations.default.cross { - for offset in 1..<8 { - let destination = kingSquare.translate(file: translation.0 * offset, rank: translation.1 * offset) - guard destination.isValid else { - break - } - if bitboards.bitboard(for: position.state.turn) & destination.bitboardMask != Int64.zero { - break - } - - if (bitboards.white | bitboards.black) & destination.bitboardMask == Int64.zero { - continue - } - - if (bitboards.queen | bitboards.rook) & destination.bitboardMask != Int64.zero { - return true - } else { - break - } - } + if self.isLongCheck(kingSquare: kingSquare, + turn: position.state.turn, + translations: MovingTranslations.default.cross, + bitboards: bitboards, + pieces: bitboards.queen | bitboards.rook) { + return true } for translation in MovingTranslations.default.knight { @@ -98,6 +70,37 @@ public class StandardRules: Rules { return false } + private func isLongCheck(kingSquare: Square, + turn: PieceColor, + translations: [(Int, Int)], + bitboards: Bitboards, + pieces: Bitboard) -> Bool { + for translation in translations { + for offset in 1..<8 { + let destination = kingSquare.translate(file: translation.0 * offset, + rank: translation.1 * offset) + guard destination.isValid else { + break + } + if bitboards.bitboard(for: turn) & destination.bitboardMask != Int64.zero { + break + } + + if (bitboards.white | bitboards.black) & destination.bitboardMask == Int64.zero { + continue + } + + if pieces & destination.bitboardMask != Int64.zero { + return true + } else { + break + } + } + } + + return false + } + func isMate(in position: Position) -> Bool { guard self.isCheck(in: position) else { return false From 8fe890e882befdeaa6fabb4ece9b040c41510450 Mon Sep 17 00:00:00 2001 From: Alexander Perechnev Date: Mon, 22 Mar 2021 12:49:15 +0200 Subject: [PATCH 2/2] Update ChessKit.podspec --- ChessKit.podspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ChessKit.podspec b/ChessKit.podspec index cde3114..79a1bc3 100644 --- a/ChessKit.podspec +++ b/ChessKit.podspec @@ -8,7 +8,7 @@ Pod::Spec.new do |spec| spec.name = "ChessKit" - spec.version = "1.3.3" + spec.version = "1.3.4" spec.summary = "Lightweight and fast chess framework written in Swift." spec.description = "Lightweight and fast chess framework written in Swift. ChessKit is used as a base freamework for Ladoga chess engine."