batman
This commit is contained in:
53
Sources/Engine/Pieces/Pawn.swift
Normal file
53
Sources/Engine/Pieces/Pawn.swift
Normal file
@@ -0,0 +1,53 @@
|
||||
package class Pawn: Piece {
|
||||
package var kind: Kind = .Pawn
|
||||
package weak var board: Board?
|
||||
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 var unicodeRepresentation: String {
|
||||
return color == .Black ? "♟" : "♙"
|
||||
}
|
||||
|
||||
package func move(dst: Square.Position) -> Bool {
|
||||
guard board != nil else {
|
||||
return false
|
||||
}
|
||||
|
||||
if !(legalPositions.contains { $0 == dst }) {
|
||||
return false
|
||||
}
|
||||
|
||||
if let board = board, var s = board[position], var d = board[dst] {
|
||||
s.piece = self
|
||||
d.piece = nil
|
||||
}
|
||||
|
||||
position = dst
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
package func isLegal(pos: Square.Position) -> Bool {
|
||||
// TODO: Handle "En-Passant"
|
||||
if let board = board, let s = board[pos] {
|
||||
if let p = s.piece {
|
||||
if p.color == color { return false }
|
||||
}
|
||||
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
package init(
|
||||
color: Color, on position: Square.Position
|
||||
) {
|
||||
self.color = color
|
||||
self.position = position
|
||||
}
|
||||
}
|
||||
48
Sources/Engine/Pieces/Piece.swift
Normal file
48
Sources/Engine/Pieces/Piece.swift
Normal file
@@ -0,0 +1,48 @@
|
||||
public enum Kind: String, CaseIterable {
|
||||
case Pawn, Knight, Bishop, Rook, Queen, King
|
||||
|
||||
public var value: Int8 {
|
||||
switch self {
|
||||
case .Pawn: 1
|
||||
case .Bishop: 3
|
||||
case .Knight: 3
|
||||
case .Rook: 5
|
||||
case .Queen: 9
|
||||
case .King: -1
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public 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)
|
||||
}
|
||||
}
|
||||
|
||||
public protocol Piece {
|
||||
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(dst: Square.Position) -> Bool
|
||||
func isLegal(pos: Square.Position) -> Bool
|
||||
}
|
||||
Reference in New Issue
Block a user