Can now initialize board with FEN
This commit is contained in:
@@ -102,15 +102,15 @@ public class Board: CustomStringConvertible {
|
|||||||
return board
|
return board
|
||||||
}
|
}
|
||||||
|
|
||||||
public private(set) var fen =
|
public private(set) var fen: Fen
|
||||||
Fen(fen: "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")
|
|
||||||
|
|
||||||
public func setBoard() throws -> Grid {
|
public func setBoard() throws {
|
||||||
var file: UInt8 = 1
|
var file: UInt8 = 1
|
||||||
var rank: UInt8 = 8
|
var rank: UInt8 = 8
|
||||||
var index: UInt8 = 0
|
var index: UInt8 = 0
|
||||||
var b: Grid = board
|
var b: [Square] = squares
|
||||||
for c in fen.placement {
|
for c in fen.placement {
|
||||||
|
let r = (8 - Int(rank)) % 8
|
||||||
if c == "/" {
|
if c == "/" {
|
||||||
rank -= 1
|
rank -= 1
|
||||||
file = 0
|
file = 0
|
||||||
@@ -124,7 +124,7 @@ public class Board: CustomStringConvertible {
|
|||||||
let f = file
|
let f = file
|
||||||
for i in f..<(f + n) {
|
for i in f..<(f + n) {
|
||||||
file += 1
|
file += 1
|
||||||
b[rank, i-1]?.piece = nil
|
b[8*r+Int(i)].piece = nil
|
||||||
}
|
}
|
||||||
file -= 1
|
file -= 1
|
||||||
} else if c.isASCII {
|
} else if c.isASCII {
|
||||||
@@ -134,10 +134,18 @@ public class Board: CustomStringConvertible {
|
|||||||
switch k {
|
switch k {
|
||||||
case .Pawn:
|
case .Pawn:
|
||||||
Pawn(color: c, on: .init(file: file, rank: rank))
|
Pawn(color: c, on: .init(file: file, rank: rank))
|
||||||
default:
|
case .Knight:
|
||||||
Pawn(color: c, on: .init(file: file, rank: rank))
|
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:
|
case .none:
|
||||||
throw Fen.FenError.InvalidCharacter(
|
throw Fen.FenError.InvalidCharacter(
|
||||||
c: String(c), column: index)
|
c: String(c), column: index)
|
||||||
@@ -147,12 +155,15 @@ public class Board: CustomStringConvertible {
|
|||||||
file += 1
|
file += 1
|
||||||
index += 1
|
index += 1
|
||||||
}
|
}
|
||||||
|
print(b)
|
||||||
return 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
|
var rank: UInt8 = 8
|
||||||
|
self.fen = fen
|
||||||
for i in 0...63 {
|
for i in 0...63 {
|
||||||
let index = UInt8(i)
|
let index = UInt8(i)
|
||||||
let square = Square(
|
let square = Square(
|
||||||
@@ -164,6 +175,10 @@ public class Board: CustomStringConvertible {
|
|||||||
rank -= 1
|
rank -= 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
do {
|
||||||
|
try setBoard()
|
||||||
|
} catch {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public var description: String {
|
public var description: String {
|
||||||
@@ -195,25 +210,26 @@ public class Board: CustomStringConvertible {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension Board.Grid {
|
// For now useless, learn how to setup properly
|
||||||
public subscript(rank: UInt8, file: UInt8) -> Square? {
|
// extension Board.Grid {
|
||||||
get {
|
// public subscript(rank: UInt8, file: UInt8) -> Square? {
|
||||||
guard 1 > rank && rank < 9 && 1 > file && file < 9 else {
|
// get {
|
||||||
return nil
|
// guard 1 > rank && rank < 9 && 1 > file && file < 9 else {
|
||||||
}
|
// return nil
|
||||||
|
// }
|
||||||
|
|
||||||
return self[(8-Int(rank)) % 8][Int(file) - 1]
|
// return self[(8 - Int(rank)) % 8][Int(file) - 1]
|
||||||
}
|
// }
|
||||||
set {
|
// set {
|
||||||
guard 1 > rank && rank < 9 && 1 > file && file < 9 else {
|
// guard 1 > rank && rank < 9 && 1 > file && file < 9 else {
|
||||||
return
|
// return
|
||||||
}
|
// }
|
||||||
|
|
||||||
guard let n = newValue else {
|
// guard let n = newValue else {
|
||||||
return
|
// 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)? {
|
public static subscript(_ c: Character) -> (Self, Color)? {
|
||||||
let v = c.uppercased()
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
let kind: Self = switch v {
|
let kind: Self =
|
||||||
|
switch v {
|
||||||
case "P": .Pawn
|
case "P": .Pawn
|
||||||
case "N": .Knight
|
case "N": .Knight
|
||||||
case "B": .Bishop
|
case "B": .Bishop
|
||||||
@@ -37,6 +40,7 @@ public enum Kind: String, CaseIterable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public protocol Piece {
|
public protocol Piece {
|
||||||
|
var board: Board? { get }
|
||||||
var color: Color { get }
|
var color: Color { get }
|
||||||
var unicodeRepresentation: String { get }
|
var unicodeRepresentation: String { get }
|
||||||
var kind: Kind { 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
|
import Engine
|
||||||
|
|
||||||
let board = Board()
|
let board = Board()
|
||||||
print(try! board.setBoard())
|
print(board)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user