Can now initialize board with FEN
This commit is contained in:
@@ -102,15 +102,15 @@ public class Board: CustomStringConvertible {
|
||||
return board
|
||||
}
|
||||
|
||||
public private(set) var fen =
|
||||
Fen(fen: "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")
|
||||
public private(set) var fen: Fen
|
||||
|
||||
public func setBoard() throws -> Grid {
|
||||
public func setBoard() throws {
|
||||
var file: UInt8 = 1
|
||||
var rank: UInt8 = 8
|
||||
var index: UInt8 = 0
|
||||
var b: Grid = board
|
||||
var b: [Square] = squares
|
||||
for c in fen.placement {
|
||||
let r = (8 - Int(rank)) % 8
|
||||
if c == "/" {
|
||||
rank -= 1
|
||||
file = 0
|
||||
@@ -124,7 +124,7 @@ public class Board: CustomStringConvertible {
|
||||
let f = file
|
||||
for i in f..<(f + n) {
|
||||
file += 1
|
||||
b[rank, i-1]?.piece = nil
|
||||
b[8*r+Int(i)].piece = nil
|
||||
}
|
||||
file -= 1
|
||||
} else if c.isASCII {
|
||||
@@ -134,25 +134,36 @@ public class Board: CustomStringConvertible {
|
||||
switch k {
|
||||
case .Pawn:
|
||||
Pawn(color: c, on: .init(file: file, rank: rank))
|
||||
default:
|
||||
Pawn(color: c, on: .init(file: file, rank: rank))
|
||||
case .Knight:
|
||||
Knight(color: c, on: .init(file: file, rank: rank))
|
||||
case .Bishop:
|
||||
Bishop(color: c, on: .init(file: file, rank: rank))
|
||||
case .Rook:
|
||||
Rook(color: c, on: .init(file: file, rank: rank))
|
||||
case .Queen:
|
||||
Queen(color: c, on: .init(file: file, rank: rank))
|
||||
case .King:
|
||||
King(color: c, on: .init(file: file, rank: rank))
|
||||
}
|
||||
b[rank, file-1]?.piece = piece
|
||||
b[8*r+Int(file)-1].piece = piece
|
||||
case .none:
|
||||
throw Fen.FenError.InvalidCharacter(
|
||||
c: String(c), column: index)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
file += 1
|
||||
index += 1
|
||||
}
|
||||
|
||||
return b
|
||||
print(b)
|
||||
squares = b
|
||||
}
|
||||
|
||||
public required init() {
|
||||
public required init(fen: Fen =
|
||||
.init(fen: "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")
|
||||
) {
|
||||
var rank: UInt8 = 8
|
||||
self.fen = fen
|
||||
for i in 0...63 {
|
||||
let index = UInt8(i)
|
||||
let square = Square(
|
||||
@@ -164,6 +175,10 @@ public class Board: CustomStringConvertible {
|
||||
rank -= 1
|
||||
}
|
||||
}
|
||||
do {
|
||||
try setBoard()
|
||||
} catch {
|
||||
}
|
||||
}
|
||||
|
||||
public var description: String {
|
||||
@@ -195,25 +210,26 @@ public class Board: CustomStringConvertible {
|
||||
}
|
||||
}
|
||||
|
||||
extension Board.Grid {
|
||||
public subscript(rank: UInt8, file: UInt8) -> Square? {
|
||||
get {
|
||||
guard 1 > rank && rank < 9 && 1 > file && file < 9 else {
|
||||
return nil
|
||||
}
|
||||
// For now useless, learn how to setup properly
|
||||
// extension Board.Grid {
|
||||
// public subscript(rank: UInt8, file: UInt8) -> Square? {
|
||||
// get {
|
||||
// guard 1 > rank && rank < 9 && 1 > file && file < 9 else {
|
||||
// return nil
|
||||
// }
|
||||
|
||||
return self[(8-Int(rank)) % 8][Int(file) - 1]
|
||||
}
|
||||
set {
|
||||
guard 1 > rank && rank < 9 && 1 > file && file < 9 else {
|
||||
return
|
||||
}
|
||||
// return self[(8 - Int(rank)) % 8][Int(file) - 1]
|
||||
// }
|
||||
// set {
|
||||
// guard 1 > rank && rank < 9 && 1 > file && file < 9 else {
|
||||
// return
|
||||
// }
|
||||
|
||||
guard let n = newValue else {
|
||||
return
|
||||
}
|
||||
// guard let n = newValue else {
|
||||
// return
|
||||
// }
|
||||
|
||||
self[(8-Int(rank)) % 8][Int(file) - 1] = n
|
||||
}
|
||||
}
|
||||
}
|
||||
// self[(8 - Int(rank)) % 8][Int(file) - 1] = n
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
Reference in New Issue
Block a user