Test + bug fixes
This commit is contained in:
76
Sources/Engine/FEN.swift
Normal file
76
Sources/Engine/FEN.swift
Normal file
@@ -0,0 +1,76 @@
|
||||
public struct Fen: CustomStringConvertible {
|
||||
public enum CastlingAvailibility: String {
|
||||
case Neither = "-"
|
||||
case WhiteKingSide = "K"
|
||||
case WhiteQueenSide = "Q"
|
||||
case BlackKingSide = "k"
|
||||
case BlackQueenSide = "q"
|
||||
case WhiteSide = "KQ"
|
||||
case BlackSide = "kq"
|
||||
case Kings = "Kk"
|
||||
case Queens = "Qq"
|
||||
case WKingBQueen = "Kq"
|
||||
case WQueenBKing = "Qk"
|
||||
case All = "KQkq"
|
||||
}
|
||||
private var _fen: String = ""
|
||||
private var value: String {
|
||||
get {
|
||||
return _fen
|
||||
}
|
||||
set {
|
||||
_fen = newValue
|
||||
let splitted = _fen.split(separator: " ")
|
||||
guard splitted.count == 6 else {
|
||||
return
|
||||
}
|
||||
placement = String(splitted[0])
|
||||
activeColor =
|
||||
switch splitted[1] {
|
||||
case "w": .White
|
||||
case "b": .Black
|
||||
default: .White
|
||||
}
|
||||
castlingAvailibility =
|
||||
CastlingAvailibility(
|
||||
rawValue:
|
||||
String(splitted[2])) ?? .Neither
|
||||
enPassant = String(splitted[3])
|
||||
halfMoveClock =
|
||||
if let c = splitted[4].first, c.isWholeNumber {
|
||||
UInt8(String(c)) ?? 0
|
||||
} else {
|
||||
0
|
||||
}
|
||||
fullMoveClock =
|
||||
if let c = splitted[5].first, c.isWholeNumber {
|
||||
UInt8(String(c)) ?? 1
|
||||
} else {
|
||||
1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public private(set) var placement: String = "" // 70 chars
|
||||
public private(set) var activeColor: Color = .White // 1 char
|
||||
public private(set) var castlingAvailibility: CastlingAvailibility = .All
|
||||
// 1 to 4 chars
|
||||
public private(set) var enPassant: String = "-" // 1 or 2 chars
|
||||
public private(set) var halfMoveClock: UInt8 = 0
|
||||
public package(set) var fullMoveClock: UInt8 = 1
|
||||
|
||||
public init(fen value: String) {
|
||||
self.value = value
|
||||
}
|
||||
|
||||
public enum FenError: Error {
|
||||
case InvalidCharacter(c: String, column: UInt8)
|
||||
case NumberTooBig(n: UInt8, column: UInt8)
|
||||
case NumberTooSmall(n: UInt8, column: UInt8)
|
||||
case NotAppropriateLength(n: UInt8, column: UInt8)
|
||||
}
|
||||
|
||||
public var description: String {
|
||||
return value
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user