diff --git a/Sources/Engine/Board.swift b/Sources/Engine/Board.swift index fe28a0c..02b3a1e 100644 --- a/Sources/Engine/Board.swift +++ b/Sources/Engine/Board.swift @@ -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 - } - } -} \ No newline at end of file +// self[(8 - Int(rank)) % 8][Int(file) - 1] = n +// } +// } +// } diff --git a/Sources/Engine/Pieces/Bishop.swift b/Sources/Engine/Pieces/Bishop.swift new file mode 100644 index 0000000..932ff01 --- /dev/null +++ b/Sources/Engine/Pieces/Bishop.swift @@ -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 + } + +} diff --git a/Sources/Engine/Pieces/King.swift b/Sources/Engine/Pieces/King.swift new file mode 100644 index 0000000..11ba2b4 --- /dev/null +++ b/Sources/Engine/Pieces/King.swift @@ -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 + } + +} diff --git a/Sources/Engine/Pieces/Knight.swift b/Sources/Engine/Pieces/Knight.swift new file mode 100644 index 0000000..d9c6566 --- /dev/null +++ b/Sources/Engine/Pieces/Knight.swift @@ -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 + } + +} diff --git a/Sources/Engine/Pieces/Piece.swift b/Sources/Engine/Pieces/Piece.swift index 12df6fa..bf91e4e 100644 --- a/Sources/Engine/Pieces/Piece.swift +++ b/Sources/Engine/Pieces/Piece.swift @@ -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 } diff --git a/Sources/Engine/Pieces/Queen.swift b/Sources/Engine/Pieces/Queen.swift new file mode 100644 index 0000000..3bcd537 --- /dev/null +++ b/Sources/Engine/Pieces/Queen.swift @@ -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 + } + +} diff --git a/Sources/Engine/Pieces/Rook.swift b/Sources/Engine/Pieces/Rook.swift new file mode 100644 index 0000000..0fa7e57 --- /dev/null +++ b/Sources/Engine/Pieces/Rook.swift @@ -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 + } + +} diff --git a/Sources/exe/main.swift b/Sources/exe/main.swift index 718102c..beb476f 100644 --- a/Sources/exe/main.swift +++ b/Sources/exe/main.swift @@ -1,5 +1,5 @@ import Engine let board = Board() -print(try! board.setBoard()) +print(board)