Can now move pieces

This commit is contained in:
cdricms
2024-06-28 19:35:43 +02:00
parent 501484558a
commit 4f5b51f4ae
11 changed files with 125 additions and 109 deletions

View File

@@ -69,8 +69,28 @@ public class Piece {
return []
}
internal var delegate: EventDelegate?
internal func move(to dst: Square.Position) {}
internal func isLegal(on pos: Square.Position) -> Bool { false }
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)
}
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))
return false
}
}
}
return true
}
internal init(kind: Kind, on pos: Square.Position, with col: Color) {
self.kind = kind