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