Some bug fixes

This commit is contained in:
cdricms
2024-06-29 23:13:18 +02:00
parent 533322c5dd
commit 02de114d74
5 changed files with 25 additions and 11 deletions

View File

@@ -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 +=

View File

@@ -0,0 +1,9 @@
extension Square.Targets {
public mutating func insert(_ piece: Piece) {
guard !self.contains(piece) else {
return
}
self.append(piece)
}
}

View File

@@ -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 {

View File

@@ -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)
} }
} }

View File

@@ -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