Started to work on king threats

This commit is contained in:
cdricms
2024-06-28 23:58:26 +02:00
parent 4f5b51f4ae
commit 0a220f14f5
13 changed files with 78 additions and 46 deletions

View File

@@ -54,9 +54,10 @@ public enum Kind: String, CaseIterable {
}
public class Piece {
#warning("TODO: TO be removed, handle everything through the delegate")
#warning("TODO: To be removed, handle everything through the delegate")
internal weak var board: Board?
public internal(set) var color: Color
public internal(set) var halfMoveCount: UInt8 = 0
public var unicodeRepresentation: String {
return ""
}
@@ -65,25 +66,29 @@ public class Piece {
internal var pseudoLegalPositions: [Square.Position] {
return []
}
internal var legalPositions: [Square.Position] {
return []
internal var legalPositions = [Square.Position]()
internal func getLegalPosition() {
legalPositions = pseudoLegalPositions.filter { isLegal(on: $0) }
}
internal var delegate: EventDelegate?
internal func move(to dst: Square.Position) throws {
if !(legalPositions.contains { $0 == dst }) {
throw Board.MoveFailure.destinationIsIllegal(pos: dst)
}
try delegate?.movePiece(self, to: dst)
halfMoveCount += 1
}
internal func isLegal(on pos: Square.Position) -> Bool {
#warning("This method should be better thought out.")
internal func isLegal(on pos: Square.Position) -> Bool {
if let board = board, let s = board[pos] {
if let p = s.piece {
if p.color == color { return false }
if p.kind == .King {
// TODO: Notify board of check
delegate?.notify(.kingInCheck(self))
if let king = p as? King {
#warning("It could be in check or behind another piece.")
delegate?.notify(.kingInCheck(self, on: king))
return false
}
}