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