Files
swift-chess/Sources/Engine/Pieces/Piece.swift
2024-06-27 18:09:01 +02:00

53 lines
1.0 KiB
Swift

enum Kind: String, CaseIterable {
case Pawn, Knight, Bishop, Rook, Queen, King
var value: Int8 {
switch self {
case .Pawn: 1
case .Bishop: 3
case .Knight: 3
case .Rook: 5
case .Queen: 9
case .King: -1
}
}
static subscript(_ c: Character) -> (Self, Color)? {
let v = c.uppercased()
guard
v == "N"
|| (Self.allCases.contains { String($0.rawValue.first!) == v })
else {
return nil
}
let kind: Self =
switch v {
case "P": .Pawn
case "N": .Knight
case "B": .Bishop
case "R": .Rook
case "Q": .Queen
case "K": .King
default: .Pawn
}
let color: Color = c.isUppercase ? .White : .Black
return (kind, color)
}
}
protocol Piece {
var board: Board? { get }
var color: Color { get }
var unicodeRepresentation: String { get }
var kind: Kind { get }
var position: Square.Position { get }
var pseudoLegalPositions: [Square.Position] { get }
var legalPositions: [Square.Position] { get }
func move(to dst: Square.Position) -> Bool
func isLegal(on pos: Square.Position) -> Bool
}