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

@@ -1,5 +1,5 @@
enum Event {
case kingInCheck(_ by: Piece)
case kingInCheck(_ by: Piece, on: King)
case piecePinned(from: Piece, on: Piece)
}
protocol EventDelegate {
@@ -24,12 +24,30 @@ public class Board: CustomStringConvertible, EventDelegate {
}
public private(set) var halfMoveClock: UInt8 = 0
public var fullMoveClock: UInt8 {
halfMoveClock / 2
}
internal var threatenedSquares: [Color: Set<Square.Position>] = [
.White: [],
.Black: [],
]
public private(set) var turn: Color = .White
func movePiece(_ piece: Piece, to dst: Square.Position) throws {
let from = piece.position
piece.position = dst
squares[dst.index!].piece = piece
squares[from.index!].piece = nil
fen.set(from: board, castling: .All, enPassant: fen.enPassant)
halfMoveClock += 1
turn = !turn
fen.set(
from: board, activeColor: turn, castling: .All,
enPassant: fen.enPassant,
halfMoveClock: halfMoveClock, fullMoveClock: fullMoveClock)
}
public enum MoveFailure: Error, CustomStringConvertible {
@@ -105,6 +123,12 @@ public class Board: CustomStringConvertible, EventDelegate {
}
try piece.move(to: dst)
#warning("May need to clear threatenedSquares before hand.")
for square in squares where square.piece != nil {
let p = square.piece!
p.getLegalPosition()
threatenedSquares[p.color]! += Set(p.legalPositions)
}
}
public func getSquareInfo(on pos: Square.Position) -> Square? {
@@ -183,9 +207,6 @@ public class Board: CustomStringConvertible, EventDelegate {
file += 1
index += 1
}
#if DEBUG
print(b)
#endif
squares = b
}
@@ -206,7 +227,7 @@ public class Board: CustomStringConvertible, EventDelegate {
rank -= 1
}
}
// TODO: Handle better
#warning("Handle better.")
do {
try setBoard()
} catch Fen.FenError.NotAppropriateLength(let n, let column) {