Added history — though still need to be checked thoroughly
This commit is contained in:
71
Sources/Engine/History.swift
Normal file
71
Sources/Engine/History.swift
Normal file
@@ -0,0 +1,71 @@
|
||||
public struct History {
|
||||
public typealias Array = [(
|
||||
SANMoveDescriptor.Value?, SANMoveDescriptor.Value?
|
||||
)]
|
||||
|
||||
public enum SANMoveDescriptor {
|
||||
public typealias Value = String
|
||||
case pieceMove(from: Square, to: Square)
|
||||
case pawnPush(to: Square, promotion: Piece?)
|
||||
|
||||
public var value: Value {
|
||||
return switch self {
|
||||
case .pieceMove(let from, let to):
|
||||
{
|
||||
guard let piece = from.piece else {
|
||||
return ""
|
||||
}
|
||||
#warning("Ugly ass code 🤢")
|
||||
var result =
|
||||
piece.kind == .Pawn
|
||||
? ""
|
||||
: "\(piece.kind.fenRepresentation(with: piece.color))"
|
||||
if let ambiguity = (to.targetted.first { $0 ~= piece }) {
|
||||
let file =
|
||||
ambiguity.position.file != from.position.file
|
||||
? Square.Position.File[from.position.file] : nil
|
||||
var rank: Int8? {
|
||||
if file == nil {
|
||||
return ambiguity.position.rank
|
||||
!= from.position.rank
|
||||
? from.position.rank : nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if let f = file {
|
||||
result += f.rawValue
|
||||
} else if let r = rank {
|
||||
result += String(r)
|
||||
} else {
|
||||
result +=
|
||||
Square.Position.File[from.position.file]!
|
||||
.rawValue
|
||||
+ String(from.position.rank)
|
||||
}
|
||||
}
|
||||
if to.piece != nil {
|
||||
result += "x"
|
||||
}
|
||||
result +=
|
||||
"\(Square.Position.File[to.position.file]!.rawValue)\(String(to.position.rank))"
|
||||
return result
|
||||
}()
|
||||
case .pawnPush(let to, let promotion):
|
||||
""
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public private(set) var values: Array = []
|
||||
|
||||
internal mutating func add(_ san: SANMoveDescriptor) {
|
||||
#warning("To be tested")
|
||||
if let last = values.last {
|
||||
if last.1 == nil {
|
||||
values[values.count - 1].1 = san.value
|
||||
return
|
||||
}
|
||||
}
|
||||
values.append((san.value, nil))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user