Files
swift-chess/Sources/Engine/Pieces/Knight.swift
2024-06-26 22:46:58 +02:00

57 lines
1.5 KiB
Swift

internal final class Knight: Piece {
internal weak var board: Board?
internal var kind: Kind = .Knight
internal var unicodeRepresentation: String {
return color == .Black ? "" : ""
}
internal var color: Color
internal var position: Square.Position
internal var pseudoLegalPositions: [Square.Position] {
let directions: [Square.Position] = [
.init(file: position.file + 1, rank: position.rank + 2),
.init(file: position.file - 1, rank: position.rank + 2),
.init(file: position.file + 1, rank: position.rank - 2),
.init(file: position.file - 1, rank: position.rank - 2),
.init(file: position.file + 2, rank: position.rank + 1),
.init(file: position.file - 2, rank: position.rank + 1),
.init(file: position.file + 2, rank: position.rank - 1),
.init(file: position.file - 2, rank: position.rank - 1)
]
return directions.filter {
let index = $0.index
return index < 0 || index > 63
}
}
internal var legalPositions: [Square.Position] {
return pseudoLegalPositions.filter { isLegal(on: $0) }
}
internal func move(to dst: Square.Position) -> Bool {
return false
}
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
return false
}
}
}
return true
}
internal init(color: Color, on position: Square.Position) {
self.color = color
self.position = position
}
}