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,10 +134,18 @@ 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)
|
||||
@@ -147,12 +155,15 @@ public class Board: CustomStringConvertible {
|
||||
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
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
33
Sources/Engine/Pieces/Bishop.swift
Normal file
33
Sources/Engine/Pieces/Bishop.swift
Normal file
@@ -0,0 +1,33 @@
|
||||
package class Bishop: Piece {
|
||||
package weak var board: Board?
|
||||
package var kind: Kind = .Bishop
|
||||
|
||||
package var unicodeRepresentation: String {
|
||||
return color == .Black ? "♝" : "♗"
|
||||
}
|
||||
package var color: Color
|
||||
|
||||
package var position: Square.Position
|
||||
|
||||
package var pseudoLegalPositions: [Square.Position] {
|
||||
return []
|
||||
}
|
||||
|
||||
package var legalPositions: [Square.Position] {
|
||||
return pseudoLegalPositions.filter { isLegal(pos: $0) }
|
||||
}
|
||||
|
||||
package func move(dst: Square.Position) -> Bool {
|
||||
return false
|
||||
}
|
||||
|
||||
package func isLegal(pos: Square.Position) -> Bool {
|
||||
return false
|
||||
}
|
||||
|
||||
package init(color: Color, on position: Square.Position) {
|
||||
self.color = color
|
||||
self.position = position
|
||||
}
|
||||
|
||||
}
|
||||
33
Sources/Engine/Pieces/King.swift
Normal file
33
Sources/Engine/Pieces/King.swift
Normal file
@@ -0,0 +1,33 @@
|
||||
package class King: Piece {
|
||||
package weak var board: Board?
|
||||
package var kind: Kind = .King
|
||||
|
||||
package var unicodeRepresentation: String {
|
||||
return color == .Black ? "♛" : "♕"
|
||||
}
|
||||
package var color: Color
|
||||
|
||||
package var position: Square.Position
|
||||
|
||||
package var pseudoLegalPositions: [Square.Position] {
|
||||
return []
|
||||
}
|
||||
|
||||
package var legalPositions: [Square.Position] {
|
||||
return pseudoLegalPositions.filter { isLegal(pos: $0) }
|
||||
}
|
||||
|
||||
package func move(dst: Square.Position) -> Bool {
|
||||
return false
|
||||
}
|
||||
|
||||
package func isLegal(pos: Square.Position) -> Bool {
|
||||
return false
|
||||
}
|
||||
|
||||
package init(color: Color, on position: Square.Position) {
|
||||
self.color = color
|
||||
self.position = position
|
||||
}
|
||||
|
||||
}
|
||||
33
Sources/Engine/Pieces/Knight.swift
Normal file
33
Sources/Engine/Pieces/Knight.swift
Normal file
@@ -0,0 +1,33 @@
|
||||
package class Knight: Piece {
|
||||
package weak var board: Board?
|
||||
package var kind: Kind = .Knight
|
||||
|
||||
package var unicodeRepresentation: String {
|
||||
return color == .Black ? "♞" : "♘"
|
||||
}
|
||||
package var color: Color
|
||||
|
||||
package var position: Square.Position
|
||||
|
||||
package var pseudoLegalPositions: [Square.Position] {
|
||||
return []
|
||||
}
|
||||
|
||||
package var legalPositions: [Square.Position] {
|
||||
return pseudoLegalPositions.filter { isLegal(pos: $0) }
|
||||
}
|
||||
|
||||
package func move(dst: Square.Position) -> Bool {
|
||||
return false
|
||||
}
|
||||
|
||||
package func isLegal(pos: Square.Position) -> Bool {
|
||||
return false
|
||||
}
|
||||
|
||||
package init(color: Color, on position: Square.Position) {
|
||||
self.color = color
|
||||
self.position = position
|
||||
}
|
||||
|
||||
}
|
||||
@@ -12,15 +12,18 @@ public enum Kind: String, CaseIterable {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static subscript(_ c: Character) -> (Self, Color)? {
|
||||
let v = c.uppercased()
|
||||
|
||||
guard v == "N" || (Self.allCases.contains { String($0.rawValue.first!) == v}) else {
|
||||
guard
|
||||
v == "N"
|
||||
|| (Self.allCases.contains { String($0.rawValue.first!) == v })
|
||||
else {
|
||||
return nil
|
||||
}
|
||||
|
||||
let kind: Self = switch v {
|
||||
let kind: Self =
|
||||
switch v {
|
||||
case "P": .Pawn
|
||||
case "N": .Knight
|
||||
case "B": .Bishop
|
||||
@@ -28,7 +31,7 @@ public enum Kind: String, CaseIterable {
|
||||
case "Q": .Queen
|
||||
case "K": .King
|
||||
default: .Pawn
|
||||
}
|
||||
}
|
||||
|
||||
let color: Color = c.isUppercase ? .White : .Black
|
||||
|
||||
@@ -37,6 +40,7 @@ public enum Kind: String, CaseIterable {
|
||||
}
|
||||
|
||||
public protocol Piece {
|
||||
var board: Board? { get }
|
||||
var color: Color { get }
|
||||
var unicodeRepresentation: String { get }
|
||||
var kind: Kind { get }
|
||||
|
||||
33
Sources/Engine/Pieces/Queen.swift
Normal file
33
Sources/Engine/Pieces/Queen.swift
Normal file
@@ -0,0 +1,33 @@
|
||||
package class Queen: Piece {
|
||||
package weak var board: Board?
|
||||
package var kind: Kind = .Queen
|
||||
|
||||
package var unicodeRepresentation: String {
|
||||
return color == .Black ? "♛" : "♕"
|
||||
}
|
||||
package var color: Color
|
||||
|
||||
package var position: Square.Position
|
||||
|
||||
package var pseudoLegalPositions: [Square.Position] {
|
||||
return []
|
||||
}
|
||||
|
||||
package var legalPositions: [Square.Position] {
|
||||
return pseudoLegalPositions.filter { isLegal(pos: $0) }
|
||||
}
|
||||
|
||||
package func move(dst: Square.Position) -> Bool {
|
||||
return false
|
||||
}
|
||||
|
||||
package func isLegal(pos: Square.Position) -> Bool {
|
||||
return false
|
||||
}
|
||||
|
||||
package init(color: Color, on position: Square.Position) {
|
||||
self.color = color
|
||||
self.position = position
|
||||
}
|
||||
|
||||
}
|
||||
33
Sources/Engine/Pieces/Rook.swift
Normal file
33
Sources/Engine/Pieces/Rook.swift
Normal file
@@ -0,0 +1,33 @@
|
||||
package class Rook: Piece {
|
||||
package weak var board: Board?
|
||||
package var kind: Kind = .Rook
|
||||
|
||||
package var unicodeRepresentation: String {
|
||||
return color == .Black ? "♜" : "♖"
|
||||
}
|
||||
package var color: Color
|
||||
|
||||
package var position: Square.Position
|
||||
|
||||
package var pseudoLegalPositions: [Square.Position] {
|
||||
return []
|
||||
}
|
||||
|
||||
package var legalPositions: [Square.Position] {
|
||||
return pseudoLegalPositions.filter { isLegal(pos: $0) }
|
||||
}
|
||||
|
||||
package func move(dst: Square.Position) -> Bool {
|
||||
return false
|
||||
}
|
||||
|
||||
package func isLegal(pos: Square.Position) -> Bool {
|
||||
return false
|
||||
}
|
||||
|
||||
package init(color: Color, on position: Square.Position) {
|
||||
self.color = color
|
||||
self.position = position
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import Engine
|
||||
|
||||
let board = Board()
|
||||
print(try! board.setBoard())
|
||||
print(board)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user