Can now initialize board with FEN

This commit is contained in:
cdricms
2024-06-26 00:18:22 +02:00
parent c36319d4d7
commit 0ea0ec1bc6
8 changed files with 221 additions and 36 deletions

View 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
}
}

View 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
}
}

View 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
}
}

View File

@@ -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 }

View 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
}
}

View 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
}
}