diff --git a/Sources/Engine/Board.swift b/Sources/Engine/Board.swift index d067dae..323299f 100644 --- a/Sources/Engine/Board.swift +++ b/Sources/Engine/Board.swift @@ -2,12 +2,14 @@ enum Event { case kingInCheck(_ by: Piece, on: King) case piecePinned(from: Piece, on: Piece) } + protocol EventDelegate { func notify(_ event: Event) func movePiece(_ piece: Piece, to dst: Square.Position) throws func addPieceToTarget(_ piece: Piece, target: Square.Position) func getSquareInfo(on pos: Square.Position) -> Square? } + public class Board: CustomStringConvertible, EventDelegate { public typealias Grid = [[Square]] @@ -77,10 +79,10 @@ public class Board: CustomStringConvertible, EventDelegate { } internal func addPieceToTarget(_ piece: Piece, target: Square.Position) { - guard var square = self[target] else { + guard self[target] != nil else { return } - square.targetted.insert(piece) + squares[target.index!].targetted.insert(piece) } public private(set) var halfMoveClock: UInt8 = 0 @@ -189,7 +191,10 @@ public class Board: CustomStringConvertible, EventDelegate { } try piece.move(to: dst) - #warning("May need to clear threatenedSquares before hand.") + threatenedSquares = [ + .White: [], + .Black: [], + ] for square in squares where square.piece != nil { let p = square.piece! p.getLegalPosition() @@ -320,13 +325,11 @@ public class Board: CustomStringConvertible, EventDelegate { boardString += String(_rank) + " \(UnicodeBar.VerticalBar)" rank.forEach { square in let p = square.piece?.unicodeRepresentation ?? " " + h1 = def if let c = colors { - for (color, s) in c { - h1 = def - if (s.contains { $0 == square.position }) { - h1 = color - break - } + for (color, s) in c + where (s.contains { $0 == square.position }) { + h1 = color } } boardString += diff --git a/Sources/Engine/Extensions/Targets+Unique.swift b/Sources/Engine/Extensions/Targets+Unique.swift new file mode 100644 index 0000000..cd8a78f --- /dev/null +++ b/Sources/Engine/Extensions/Targets+Unique.swift @@ -0,0 +1,9 @@ +extension Square.Targets { + public mutating func insert(_ piece: Piece) { + guard !self.contains(piece) else { + return + } + + self.append(piece) + } +} diff --git a/Sources/Engine/History.swift b/Sources/Engine/History.swift index 4c4f6a2..7ccb0bb 100644 --- a/Sources/Engine/History.swift +++ b/Sources/Engine/History.swift @@ -10,6 +10,7 @@ public struct History { public var value: Value { return switch self { + #warning("Doesn't work properly") case .pieceMove(let from, let to): { guard let piece = from.piece else { diff --git a/Sources/Engine/Pieces/Piece.swift b/Sources/Engine/Pieces/Piece.swift index 7ba3de8..5faebb0 100644 --- a/Sources/Engine/Pieces/Piece.swift +++ b/Sources/Engine/Pieces/Piece.swift @@ -125,7 +125,7 @@ public class Piece: Hashable { public func hash(into hasher: inout Hasher) { hasher.combine(kind) - hasher.combine(position) hasher.combine(color) + hasher.combine(position) } } diff --git a/Sources/Engine/Square.swift b/Sources/Engine/Square.swift index ee018a1..cf5f3af 100644 --- a/Sources/Engine/Square.swift +++ b/Sources/Engine/Square.swift @@ -75,7 +75,8 @@ public struct Square: Equatable { public let position: Position public internal(set) var piece: Piece? = nil public let color: Color - public internal(set) var targetted: Set = [] + public typealias Targets = [Piece] + public internal(set) var targetted: Targets = [] public static func == (lhs: Square, rhs: Square) -> Bool { return lhs.position == rhs.position