59 lines
1.4 KiB
Swift
59 lines
1.4 KiB
Swift
final class Queen: Piece, LinearMoves, DiagonalMoves {
|
|
override var unicodeRepresentation: String {
|
|
return color == .Black ? "♛" : "♕"
|
|
}
|
|
|
|
override var pseudoLegalPositions: [Square.Position] {
|
|
return getDiagonalMoves(from: position) + getLinearMoves(from: position)
|
|
}
|
|
|
|
override func getLegalPosition() {
|
|
legalPositions = []
|
|
var last: Square.Position? = nil
|
|
for position in pseudoLegalPositions {
|
|
if !isLegal(on: position) {
|
|
last = position
|
|
continue
|
|
}
|
|
|
|
if !LinearDirection.isLegal(last: &last, current: position)
|
|
&& !DiagonalDirection.isLegal(last: &last, current: position)
|
|
{
|
|
last = nil
|
|
}
|
|
|
|
if last == nil {
|
|
legalPositions.append(position)
|
|
if let square = delegate?.getSquareInfo(on: position),
|
|
let piece = square.piece
|
|
{
|
|
if piece.color != color {
|
|
if let king = piece as? King {
|
|
delegate?.notify(.kingInCheck(self, on: king))
|
|
legalPositions.removeLast()
|
|
} else {
|
|
delegate?.notify(
|
|
.piecePinned(from: self, on: piece))
|
|
}
|
|
last = position
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
override func isLegal(on pos: Square.Position) -> Bool {
|
|
if let s = delegate?.getSquareInfo(on: pos), let p = s.piece,
|
|
p.color == color
|
|
{
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
init(with color: Color, on position: Square.Position) {
|
|
super.init(kind: .Queen, on: position, with: color)
|
|
}
|
|
|
|
}
|