Started to work on king threats
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
enum Event {
|
enum Event {
|
||||||
case kingInCheck(_ by: Piece)
|
case kingInCheck(_ by: Piece, on: King)
|
||||||
case piecePinned(from: Piece, on: Piece)
|
case piecePinned(from: Piece, on: Piece)
|
||||||
}
|
}
|
||||||
protocol EventDelegate {
|
protocol EventDelegate {
|
||||||
@@ -24,12 +24,30 @@ public class Board: CustomStringConvertible, EventDelegate {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public private(set) var halfMoveClock: UInt8 = 0
|
||||||
|
|
||||||
|
public var fullMoveClock: UInt8 {
|
||||||
|
halfMoveClock / 2
|
||||||
|
}
|
||||||
|
|
||||||
|
internal var threatenedSquares: [Color: Set<Square.Position>] = [
|
||||||
|
.White: [],
|
||||||
|
.Black: [],
|
||||||
|
]
|
||||||
|
|
||||||
|
public private(set) var turn: Color = .White
|
||||||
|
|
||||||
func movePiece(_ piece: Piece, to dst: Square.Position) throws {
|
func movePiece(_ piece: Piece, to dst: Square.Position) throws {
|
||||||
let from = piece.position
|
let from = piece.position
|
||||||
piece.position = dst
|
piece.position = dst
|
||||||
squares[dst.index!].piece = piece
|
squares[dst.index!].piece = piece
|
||||||
squares[from.index!].piece = nil
|
squares[from.index!].piece = nil
|
||||||
fen.set(from: board, castling: .All, enPassant: fen.enPassant)
|
halfMoveClock += 1
|
||||||
|
turn = !turn
|
||||||
|
fen.set(
|
||||||
|
from: board, activeColor: turn, castling: .All,
|
||||||
|
enPassant: fen.enPassant,
|
||||||
|
halfMoveClock: halfMoveClock, fullMoveClock: fullMoveClock)
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum MoveFailure: Error, CustomStringConvertible {
|
public enum MoveFailure: Error, CustomStringConvertible {
|
||||||
@@ -105,6 +123,12 @@ public class Board: CustomStringConvertible, EventDelegate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try piece.move(to: dst)
|
try piece.move(to: dst)
|
||||||
|
#warning("May need to clear threatenedSquares before hand.")
|
||||||
|
for square in squares where square.piece != nil {
|
||||||
|
let p = square.piece!
|
||||||
|
p.getLegalPosition()
|
||||||
|
threatenedSquares[p.color]! += Set(p.legalPositions)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public func getSquareInfo(on pos: Square.Position) -> Square? {
|
public func getSquareInfo(on pos: Square.Position) -> Square? {
|
||||||
@@ -183,9 +207,6 @@ public class Board: CustomStringConvertible, EventDelegate {
|
|||||||
file += 1
|
file += 1
|
||||||
index += 1
|
index += 1
|
||||||
}
|
}
|
||||||
#if DEBUG
|
|
||||||
print(b)
|
|
||||||
#endif
|
|
||||||
squares = b
|
squares = b
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -206,7 +227,7 @@ public class Board: CustomStringConvertible, EventDelegate {
|
|||||||
rank -= 1
|
rank -= 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// TODO: Handle better
|
#warning("Handle better.")
|
||||||
do {
|
do {
|
||||||
try setBoard()
|
try setBoard()
|
||||||
} catch Fen.FenError.NotAppropriateLength(let n, let column) {
|
} catch Fen.FenError.NotAppropriateLength(let n, let column) {
|
||||||
|
|||||||
9
Sources/Engine/Extensions/Set+Union.swift
Normal file
9
Sources/Engine/Extensions/Set+Union.swift
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
extension Set {
|
||||||
|
static func + (lhs: Set<Element>, rhs: Set<Element>) -> Set<Element> {
|
||||||
|
return lhs.union(rhs)
|
||||||
|
}
|
||||||
|
|
||||||
|
static func += (lhs: inout Set<Element>, rhs: Set<Element>) {
|
||||||
|
lhs.formUnion(rhs)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -64,8 +64,9 @@ public struct Fen: CustomStringConvertible {
|
|||||||
}
|
}
|
||||||
|
|
||||||
internal mutating func set(
|
internal mutating func set(
|
||||||
from board: Board.Grid, castling ca: CastlingAvailibility,
|
from board: Board.Grid, activeColor: Color,
|
||||||
enPassant: String
|
castling ca: CastlingAvailibility,
|
||||||
|
enPassant: String, halfMoveClock hmc: UInt8, fullMoveClock fmc: UInt8
|
||||||
) {
|
) {
|
||||||
#warning(
|
#warning(
|
||||||
"Determine active color, halfMoveClock, fullMoveClock based on history SAN later passed in arguments"
|
"Determine active color, halfMoveClock, fullMoveClock based on history SAN later passed in arguments"
|
||||||
@@ -95,7 +96,8 @@ public struct Fen: CustomStringConvertible {
|
|||||||
rankNr += 1
|
rankNr += 1
|
||||||
}
|
}
|
||||||
|
|
||||||
value = placement + " w " + ca.rawValue + " " + enPassant + " 0 " + "1"
|
value =
|
||||||
|
"\(placement) \(activeColor) \(ca.rawValue) \(enPassant) \(halfMoveClock) \(fullMoveClock)"
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum FenError: Error {
|
public enum FenError: Error {
|
||||||
|
|||||||
@@ -8,10 +8,6 @@ final class Bishop: Piece, DiagonalMoves {
|
|||||||
return getDiagonalMoves(from: position)
|
return getDiagonalMoves(from: position)
|
||||||
}
|
}
|
||||||
|
|
||||||
override var legalPositions: [Square.Position] {
|
|
||||||
return pseudoLegalPositions.filter { isLegal(on: $0) }
|
|
||||||
}
|
|
||||||
|
|
||||||
override func isLegal(on pos: Square.Position) -> Bool {
|
override func isLegal(on pos: Square.Position) -> Bool {
|
||||||
super.isLegal(on: pos)
|
super.isLegal(on: pos)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
final class King: Piece {
|
final class King: Piece {
|
||||||
|
typealias Threats = (Piece?, Piece?)
|
||||||
|
var threats: Threats = (nil, nil)
|
||||||
override var unicodeRepresentation: String {
|
override var unicodeRepresentation: String {
|
||||||
return color == .Black ? "♛" : "♕"
|
return color == .Black ? "♛" : "♕"
|
||||||
}
|
}
|
||||||
@@ -17,12 +18,12 @@ final class King: Piece {
|
|||||||
].filter { $0.index != nil }
|
].filter { $0.index != nil }
|
||||||
}
|
}
|
||||||
|
|
||||||
override var legalPositions: [Square.Position] {
|
|
||||||
return pseudoLegalPositions.filter { isLegal(on: $0) }
|
|
||||||
}
|
|
||||||
|
|
||||||
override func isLegal(on pos: Square.Position) -> Bool {
|
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) {
|
init(with color: Color, on position: Square.Position) {
|
||||||
|
|||||||
@@ -16,10 +16,6 @@ final class Knight: Piece {
|
|||||||
].filter { $0.index != nil }
|
].filter { $0.index != nil }
|
||||||
}
|
}
|
||||||
|
|
||||||
override var legalPositions: [Square.Position] {
|
|
||||||
return pseudoLegalPositions.filter { isLegal(on: $0) }
|
|
||||||
}
|
|
||||||
|
|
||||||
override func isLegal(on pos: Square.Position) -> Bool {
|
override func isLegal(on pos: Square.Position) -> Bool {
|
||||||
super.isLegal(on: pos)
|
super.isLegal(on: pos)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,10 +9,6 @@ final class Pawn: Piece {
|
|||||||
].filter { $0.index != nil }
|
].filter { $0.index != nil }
|
||||||
}
|
}
|
||||||
|
|
||||||
override var legalPositions: [Square.Position] {
|
|
||||||
pseudoLegalPositions.filter { isLegal(on: $0) }
|
|
||||||
}
|
|
||||||
|
|
||||||
override var unicodeRepresentation: String {
|
override var unicodeRepresentation: String {
|
||||||
color == .Black ? "♟" : "♙"
|
color == .Black ? "♟" : "♙"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -54,9 +54,10 @@ public enum Kind: String, CaseIterable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public class Piece {
|
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?
|
internal weak var board: Board?
|
||||||
public internal(set) var color: Color
|
public internal(set) var color: Color
|
||||||
|
public internal(set) var halfMoveCount: UInt8 = 0
|
||||||
public var unicodeRepresentation: String {
|
public var unicodeRepresentation: String {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
@@ -65,25 +66,29 @@ public class Piece {
|
|||||||
internal var pseudoLegalPositions: [Square.Position] {
|
internal var pseudoLegalPositions: [Square.Position] {
|
||||||
return []
|
return []
|
||||||
}
|
}
|
||||||
internal var legalPositions: [Square.Position] {
|
internal var legalPositions = [Square.Position]()
|
||||||
return []
|
internal func getLegalPosition() {
|
||||||
|
legalPositions = pseudoLegalPositions.filter { isLegal(on: $0) }
|
||||||
}
|
}
|
||||||
internal var delegate: EventDelegate?
|
internal var delegate: EventDelegate?
|
||||||
|
|
||||||
internal func move(to dst: Square.Position) throws {
|
internal func move(to dst: Square.Position) throws {
|
||||||
if !(legalPositions.contains { $0 == dst }) {
|
if !(legalPositions.contains { $0 == dst }) {
|
||||||
throw Board.MoveFailure.destinationIsIllegal(pos: dst)
|
throw Board.MoveFailure.destinationIsIllegal(pos: dst)
|
||||||
}
|
}
|
||||||
|
|
||||||
try delegate?.movePiece(self, to: 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 board = board, let s = board[pos] {
|
||||||
if let p = s.piece {
|
if let p = s.piece {
|
||||||
if p.color == color { return false }
|
if p.color == color { return false }
|
||||||
if p.kind == .King {
|
if let king = p as? King {
|
||||||
// TODO: Notify board of check
|
#warning("It could be in check or behind another piece.")
|
||||||
delegate?.notify(.kingInCheck(self))
|
delegate?.notify(.kingInCheck(self, on: king))
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,10 +7,6 @@ final class Queen: Piece, LinearMoves, DiagonalMoves {
|
|||||||
return getDiagonalMoves(from: position) + getLinearMoves(from: position)
|
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 {
|
override func isLegal(on pos: Square.Position) -> Bool {
|
||||||
super.isLegal(on: pos)
|
super.isLegal(on: pos)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,10 +8,6 @@ final class Rook: Piece, LinearMoves {
|
|||||||
return getLinearMoves(from: position)
|
return getLinearMoves(from: position)
|
||||||
}
|
}
|
||||||
|
|
||||||
override var legalPositions: [Square.Position] {
|
|
||||||
return pseudoLegalPositions.filter { isLegal(on: $0) }
|
|
||||||
}
|
|
||||||
|
|
||||||
override func isLegal(on pos: Square.Position) -> Bool {
|
override func isLegal(on pos: Square.Position) -> Bool {
|
||||||
super.isLegal(on: pos)
|
super.isLegal(on: pos)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
public struct Square: Equatable {
|
public struct Square: Equatable {
|
||||||
|
|
||||||
public struct Position: Equatable {
|
public struct Position: Equatable, Hashable {
|
||||||
public let rank: Int8
|
public let rank: Int8
|
||||||
public let file: Int8
|
public let file: Int8
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,15 @@
|
|||||||
public enum Color: UInt8 {
|
public enum Color: UInt8, CustomStringConvertible {
|
||||||
case Black = 0
|
case Black = 0
|
||||||
case White = 1
|
case White = 1
|
||||||
|
|
||||||
|
public var description: String {
|
||||||
|
return switch self {
|
||||||
|
case .Black: "b"
|
||||||
|
case .White: "w"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static prefix func ! (rhs: Self) -> Self {
|
||||||
|
return rhs == .White ? .Black : .White
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -109,8 +109,11 @@ final class EngineTests: XCTestCase {
|
|||||||
let board: Board = .init()
|
let board: Board = .init()
|
||||||
let fen = board.fen.value
|
let fen = board.fen.value
|
||||||
board.fen.set(
|
board.fen.set(
|
||||||
from: board.board, castling: board.fen.castlingAvailibility,
|
from: board.board, activeColor: board.fen.activeColor,
|
||||||
enPassant: board.fen.enPassant)
|
castling: board.fen.castlingAvailibility,
|
||||||
|
enPassant: board.fen.enPassant,
|
||||||
|
halfMoveClock:
|
||||||
|
board.fen.halfMoveClock, fullMoveClock: board.fen.fullMoveClock)
|
||||||
XCTAssertEqual(
|
XCTAssertEqual(
|
||||||
fen, board.fen.value, "Expected \(fen) got \(board.fen.value)")
|
fen, board.fen.value, "Expected \(fen) got \(board.fen.value)")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user