73 lines
1.7 KiB
Swift
73 lines
1.7 KiB
Swift
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 {
|
|
#warning("Doesn't work properly")
|
|
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))
|
|
}
|
|
}
|