Skip to content

Commit

Permalink
Merge branch 'release/1.3.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
aperechnev committed Mar 22, 2021
2 parents eb94ecc + 8fe890e commit cba45ab
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 41 deletions.
2 changes: 1 addition & 1 deletion ChessKit.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
83 changes: 43 additions & 40 deletions Sources/ChessKit/Rules/StandardRules.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down

0 comments on commit cba45ab

Please sign in to comment.