57 lines
1.2 KiB
Swift
57 lines
1.2 KiB
Swift
final class Pawn: Piece {
|
|
override var pseudoLegalPositions: [Square.Position] {
|
|
let sign: Int8 = color == .Black ? -1 : 1
|
|
return [
|
|
position + (1 * sign, 0),
|
|
position + (2 * sign, 0),
|
|
position + (1 * sign, 1),
|
|
position + (1 * sign, -1),
|
|
].filter { $0.index != nil }
|
|
}
|
|
|
|
override var legalPositions: [Square.Position] {
|
|
pseudoLegalPositions.filter { isLegal(on: $0) }
|
|
}
|
|
|
|
override var unicodeRepresentation: String {
|
|
color == .Black ? "♟" : "♙"
|
|
}
|
|
|
|
override func move(to dst: Square.Position) {
|
|
if !(legalPositions.contains { $0 == dst }) {
|
|
return
|
|
}
|
|
|
|
delegate?.movePiece(self, to: dst)
|
|
|
|
// if let board = board, var s = board[position], var d = board[dst] {
|
|
// s.piece = self
|
|
// d.piece = nil
|
|
// }
|
|
|
|
// position = dst
|
|
|
|
// return true
|
|
}
|
|
|
|
override func isLegal(on pos: Square.Position) -> Bool {
|
|
// TODO: Handle "En-Passant"
|
|
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
|
|
}
|
|
|
|
init(with color: Color, on position: Square.Position) {
|
|
super.init(kind: .Pawn, on: position, with: color)
|
|
}
|
|
}
|