Added history — though still need to be checked thoroughly

This commit is contained in:
cdricms
2024-06-29 17:50:55 +02:00
parent 0a220f14f5
commit 90f2988a14
5 changed files with 118 additions and 1 deletions

View File

@@ -5,6 +5,7 @@ enum Event {
protocol EventDelegate {
func notify(_ event: Event)
func movePiece(_ piece: Piece, to dst: Square.Position) throws
func addPieceToTarget(_ piece: Piece, target: Square.Position)
}
public class Board: CustomStringConvertible, EventDelegate {
public typealias Grid = [[Square]]
@@ -24,6 +25,13 @@ public class Board: CustomStringConvertible, EventDelegate {
}
internal func addPieceToTarget(_ piece: Piece, target: Square.Position) {
guard var square = self[target] else {
return
}
square.targetted.insert(piece)
}
public private(set) var halfMoveClock: UInt8 = 0
public var fullMoveClock: UInt8 {
@@ -35,10 +43,16 @@ public class Board: CustomStringConvertible, EventDelegate {
.Black: [],
]
public internal(set) var history = History()
public private(set) var turn: Color = .White
func movePiece(_ piece: Piece, to dst: Square.Position) throws {
let from = piece.position
history.add(
.pieceMove(
from: squares[from.index!],
to: squares[dst.index!]))
piece.position = dst
squares[dst.index!].piece = piece
squares[from.index!].piece = nil
@@ -230,6 +244,9 @@ public class Board: CustomStringConvertible, EventDelegate {
#warning("Handle better.")
do {
try setBoard()
for square in squares where square.piece != nil {
square.piece?.getLegalPosition()
}
} catch Fen.FenError.NotAppropriateLength(let n, let column) {
fatalError("Not appropriate length: \(n) on \(column)")
} catch {