Started to work on king threats

This commit is contained in:
cdricms
2024-06-28 23:58:26 +02:00
parent 4f5b51f4ae
commit 0a220f14f5
13 changed files with 78 additions and 46 deletions

View File

@@ -8,10 +8,6 @@ final class Bishop: Piece, DiagonalMoves {
return getDiagonalMoves(from: position)
}
override var legalPositions: [Square.Position] {
return pseudoLegalPositions.filter { isLegal(on: $0) }
}
override func isLegal(on pos: Square.Position) -> Bool {
super.isLegal(on: pos)
}

View File

@@ -1,5 +1,6 @@
final class King: Piece {
typealias Threats = (Piece?, Piece?)
var threats: Threats = (nil, nil)
override var unicodeRepresentation: String {
return color == .Black ? "" : ""
}
@@ -17,12 +18,12 @@ final class King: Piece {
].filter { $0.index != nil }
}
override var legalPositions: [Square.Position] {
return pseudoLegalPositions.filter { isLegal(on: $0) }
}
override func isLegal(on pos: Square.Position) -> Bool {
super.isLegal(on: pos)
guard super.isLegal(on: pos) else {
return false
}
// Do stuff
return true
}
init(with color: Color, on position: Square.Position) {

View File

@@ -16,10 +16,6 @@ final class Knight: Piece {
].filter { $0.index != nil }
}
override var legalPositions: [Square.Position] {
return pseudoLegalPositions.filter { isLegal(on: $0) }
}
override func isLegal(on pos: Square.Position) -> Bool {
super.isLegal(on: pos)
}

View File

@@ -9,10 +9,6 @@ final class Pawn: Piece {
].filter { $0.index != nil }
}
override var legalPositions: [Square.Position] {
pseudoLegalPositions.filter { isLegal(on: $0) }
}
override var unicodeRepresentation: String {
color == .Black ? "" : ""
}

View File

@@ -54,9 +54,10 @@ public enum Kind: String, CaseIterable {
}
public class Piece {
#warning("TODO: TO be removed, handle everything through the delegate")
#warning("TODO: To be removed, handle everything through the delegate")
internal weak var board: Board?
public internal(set) var color: Color
public internal(set) var halfMoveCount: UInt8 = 0
public var unicodeRepresentation: String {
return ""
}
@@ -65,25 +66,29 @@ public class Piece {
internal var pseudoLegalPositions: [Square.Position] {
return []
}
internal var legalPositions: [Square.Position] {
return []
internal var legalPositions = [Square.Position]()
internal func getLegalPosition() {
legalPositions = pseudoLegalPositions.filter { isLegal(on: $0) }
}
internal var delegate: EventDelegate?
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)
halfMoveCount += 1
}
internal func isLegal(on pos: Square.Position) -> Bool {
#warning("This method should be better thought out.")
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))
if let king = p as? King {
#warning("It could be in check or behind another piece.")
delegate?.notify(.kingInCheck(self, on: king))
return false
}
}

View File

@@ -7,10 +7,6 @@ final class Queen: Piece, LinearMoves, DiagonalMoves {
return getDiagonalMoves(from: position) + getLinearMoves(from: position)
}
override var legalPositions: [Square.Position] {
return pseudoLegalPositions.filter { isLegal(on: $0) }
}
override func isLegal(on pos: Square.Position) -> Bool {
super.isLegal(on: pos)
}

View File

@@ -8,10 +8,6 @@ final class Rook: Piece, LinearMoves {
return getLinearMoves(from: position)
}
override var legalPositions: [Square.Position] {
return pseudoLegalPositions.filter { isLegal(on: $0) }
}
override func isLegal(on pos: Square.Position) -> Bool {
super.isLegal(on: pos)
}