Compare commits
8 Commits
master
...
features/m
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b04ecceb9f | ||
|
|
8335db5888 | ||
|
|
cf42721235 | ||
|
|
d3503f1441 | ||
|
|
02de114d74 | ||
|
|
533322c5dd | ||
|
|
90f2988a14 | ||
|
|
0a220f14f5 |
@@ -1,11 +1,15 @@
|
|||||||
enum Event {
|
enum Event {
|
||||||
case kingInCheck(_ by: Piece)
|
case kingInCheck(_ by: Piece, on: King)
|
||||||
case piecePinned(from: Piece, on: Piece)
|
case piecePinned(from: Piece, on: Piece)
|
||||||
}
|
}
|
||||||
|
|
||||||
protocol EventDelegate {
|
protocol EventDelegate {
|
||||||
func notify(_ event: Event)
|
func notify(_ event: Event)
|
||||||
func movePiece(_ piece: Piece, to dst: Square.Position) throws
|
func movePiece(_ piece: Piece, to dst: Square.Position) throws
|
||||||
|
func addPieceToTarget(_ piece: Piece, target: Square.Position)
|
||||||
|
func getSquareInfo(on pos: Square.Position) -> Square?
|
||||||
}
|
}
|
||||||
|
|
||||||
public class Board: CustomStringConvertible, EventDelegate {
|
public class Board: CustomStringConvertible, EventDelegate {
|
||||||
public typealias Grid = [[Square]]
|
public typealias Grid = [[Square]]
|
||||||
|
|
||||||
@@ -20,16 +24,104 @@ public class Board: CustomStringConvertible, EventDelegate {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func notify(_ event: Event) {
|
public struct TerminalColors: CustomStringConvertible, Hashable {
|
||||||
|
public enum Foreground: String {
|
||||||
|
case def = "39"
|
||||||
|
case black = "30"
|
||||||
|
case red = "31"
|
||||||
|
case green = "32"
|
||||||
|
case yellow = "33"
|
||||||
|
case blue = "34"
|
||||||
|
case magenta = "35"
|
||||||
|
case cyan = "36"
|
||||||
|
case white = "37"
|
||||||
|
}
|
||||||
|
public enum Background: String {
|
||||||
|
case def = "49"
|
||||||
|
case black = "40"
|
||||||
|
case red = "41"
|
||||||
|
case green = "42"
|
||||||
|
case yellow = "43"
|
||||||
|
case blue = "44"
|
||||||
|
case magenta = "45"
|
||||||
|
case cyan = "46"
|
||||||
|
case white = "47"
|
||||||
|
}
|
||||||
|
|
||||||
|
public var foregroundColor: Foreground? = .def
|
||||||
|
public var backgroundColor: Background? = .def
|
||||||
|
|
||||||
|
public init() {}
|
||||||
|
|
||||||
|
public init(fg: Foreground?, bg: Background) {
|
||||||
|
foregroundColor = fg
|
||||||
|
backgroundColor = bg
|
||||||
|
}
|
||||||
|
|
||||||
|
public var description: String {
|
||||||
|
var res = "\u{001B}["
|
||||||
|
if let fc = foregroundColor {
|
||||||
|
res += fc.rawValue
|
||||||
|
if backgroundColor != nil {
|
||||||
|
res += ";"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if let bc = backgroundColor {
|
||||||
|
res += bc.rawValue
|
||||||
|
}
|
||||||
|
return res + "m"
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func notify(_ event: Event) {
|
||||||
|
#warning("Not implemented")
|
||||||
|
switch event {
|
||||||
|
case .kingInCheck(let piece, let king):
|
||||||
|
king.insertThreat(piece)
|
||||||
|
case .piecePinned(let p1, let p2):
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal func addPieceToTarget(_ piece: Piece, target: Square.Position) {
|
||||||
|
guard self[target] != nil else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
squares[target.index!].targetted.insert(piece)
|
||||||
|
}
|
||||||
|
|
||||||
|
public private(set) var halfMoveClock: UInt8 = 0
|
||||||
|
|
||||||
|
public var fullMoveClock: UInt8 {
|
||||||
|
halfMoveClock / 2
|
||||||
|
}
|
||||||
|
|
||||||
|
public internal(set) var threatenedSquares: [Color: Set<Square.Position>] =
|
||||||
|
[
|
||||||
|
.White: [],
|
||||||
|
.Black: [],
|
||||||
|
]
|
||||||
|
|
||||||
|
public internal(set) var history = History()
|
||||||
|
|
||||||
|
public private(set) var turn: Color = .White
|
||||||
|
|
||||||
func movePiece(_ piece: Piece, to dst: Square.Position) throws {
|
func movePiece(_ piece: Piece, to dst: Square.Position) throws {
|
||||||
let from = piece.position
|
let from = piece.position
|
||||||
|
history.add(
|
||||||
|
.pieceMove(
|
||||||
|
from: squares[from.index!],
|
||||||
|
to: squares[dst.index!]))
|
||||||
piece.position = dst
|
piece.position = dst
|
||||||
squares[dst.index!].piece = piece
|
squares[dst.index!].piece = piece
|
||||||
squares[from.index!].piece = nil
|
squares[from.index!].piece = nil
|
||||||
fen.set(from: board, castling: .All, enPassant: fen.enPassant)
|
halfMoveClock += 1
|
||||||
|
turn = !turn
|
||||||
|
fen.set(
|
||||||
|
from: board, activeColor: turn, castling: .All,
|
||||||
|
enPassant: fen.enPassant,
|
||||||
|
halfMoveClock: halfMoveClock, fullMoveClock: fullMoveClock)
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum MoveFailure: Error, CustomStringConvertible {
|
public enum MoveFailure: Error, CustomStringConvertible {
|
||||||
@@ -47,7 +139,7 @@ public class Board: CustomStringConvertible, EventDelegate {
|
|||||||
case .destinationSquareUnreachable(let pos):
|
case .destinationSquareUnreachable(let pos):
|
||||||
"The destination square given \(pos.rank) \(pos.file) is unreachable."
|
"The destination square given \(pos.rank) \(pos.file) is unreachable."
|
||||||
case .squareHasNoPiece(let pos):
|
case .squareHasNoPiece(let pos):
|
||||||
"The square \(pos.rank) \(pos.file) doesn't have any piece to be moved."
|
"The square \(pos.rank) \(pos.file) doesn't have any piece."
|
||||||
case .destinationIsIllegal(let pos):
|
case .destinationIsIllegal(let pos):
|
||||||
"The destination square given \(pos.rank) \(pos.file) is illegal to be moved to."
|
"The destination square given \(pos.rank) \(pos.file) is illegal to be moved to."
|
||||||
case .squareANIsTooLong(let an):
|
case .squareANIsTooLong(let an):
|
||||||
@@ -59,38 +151,11 @@ public class Board: CustomStringConvertible, EventDelegate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public func move(src: String, dst: String) throws {
|
public func move(src: String, dst: String) throws {
|
||||||
guard src.count < 3 else {
|
if let from = try Square.Position(with: src),
|
||||||
throw MoveFailure.squareANIsTooLong(an: src)
|
let to = try Square.Position(with: dst)
|
||||||
|
{
|
||||||
|
try move(src: from, dst: to)
|
||||||
}
|
}
|
||||||
guard src.count > 1 else {
|
|
||||||
throw MoveFailure.squareANIsTooShort(an: src)
|
|
||||||
}
|
|
||||||
guard dst.count < 3 else {
|
|
||||||
throw MoveFailure.squareANIsTooLong(an: dst)
|
|
||||||
}
|
|
||||||
guard dst.count > 1 else {
|
|
||||||
throw MoveFailure.squareANIsTooShort(an: dst)
|
|
||||||
}
|
|
||||||
guard let f1 = src.first, let r1 = src.last, r1.isWholeNumber else {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
guard let f2 = dst.first, let r2 = src.last, r2.isWholeNumber else {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
guard
|
|
||||||
let srcFile = Square.Position.File.init(rawValue: String(f1))?.value
|
|
||||||
else {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
guard
|
|
||||||
let dstFile = Square.Position.File.init(rawValue: String(f2))?.value
|
|
||||||
else {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
try move(
|
|
||||||
src: Square.Position(rank: Int8(String(r1))!, file: srcFile),
|
|
||||||
dst: Square.Position(rank: Int8(String(r2))!, file: dstFile))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public func move(src: Square.Position, dst: Square.Position) throws {
|
public func move(src: Square.Position, dst: Square.Position) throws {
|
||||||
@@ -105,6 +170,15 @@ public class Board: CustomStringConvertible, EventDelegate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try piece.move(to: dst)
|
try piece.move(to: dst)
|
||||||
|
threatenedSquares = [
|
||||||
|
.White: [],
|
||||||
|
.Black: [],
|
||||||
|
]
|
||||||
|
for square in squares where square.piece != nil {
|
||||||
|
let p = square.piece!
|
||||||
|
p.getLegalPosition()
|
||||||
|
threatenedSquares[p.color]! += Set(p.legalPositions)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public func getSquareInfo(on pos: Square.Position) -> Square? {
|
public func getSquareInfo(on pos: Square.Position) -> Square? {
|
||||||
@@ -183,9 +257,6 @@ public class Board: CustomStringConvertible, EventDelegate {
|
|||||||
file += 1
|
file += 1
|
||||||
index += 1
|
index += 1
|
||||||
}
|
}
|
||||||
#if DEBUG
|
|
||||||
print(b)
|
|
||||||
#endif
|
|
||||||
squares = b
|
squares = b
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -206,9 +277,14 @@ public class Board: CustomStringConvertible, EventDelegate {
|
|||||||
rank -= 1
|
rank -= 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// TODO: Handle better
|
#warning("Handle better.")
|
||||||
do {
|
do {
|
||||||
try setBoard()
|
try setBoard()
|
||||||
|
for square in squares where square.piece != nil {
|
||||||
|
let p = square.piece!
|
||||||
|
p.getLegalPosition()
|
||||||
|
threatenedSquares[p.color]! += Set(p.legalPositions)
|
||||||
|
}
|
||||||
} catch Fen.FenError.NotAppropriateLength(let n, let column) {
|
} catch Fen.FenError.NotAppropriateLength(let n, let column) {
|
||||||
fatalError("Not appropriate length: \(n) on \(column)")
|
fatalError("Not appropriate length: \(n) on \(column)")
|
||||||
} catch {
|
} catch {
|
||||||
@@ -216,15 +292,27 @@ public class Board: CustomStringConvertible, EventDelegate {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public var description: String {
|
public func text(with colors: [TerminalColors: [Square.Position]]? = nil)
|
||||||
|
-> String
|
||||||
|
{
|
||||||
var boardString =
|
var boardString =
|
||||||
" A B C D E F G H \n \(UnicodeBar.TopHorizontalLine)\n"
|
" A B C D E F G H \n \(UnicodeBar.TopHorizontalLine)\n"
|
||||||
var _rank: UInt8 = 8
|
var _rank: UInt8 = 8
|
||||||
|
let def: TerminalColors = .init()
|
||||||
|
var h1 = def
|
||||||
board.forEach { rank in
|
board.forEach { rank in
|
||||||
boardString += String(_rank) + " \(UnicodeBar.VerticalBar)"
|
boardString += String(_rank) + " \(UnicodeBar.VerticalBar)"
|
||||||
rank.forEach { square in
|
rank.forEach { square in
|
||||||
let p = square.piece?.unicodeRepresentation ?? " "
|
let p = square.piece?.unicodeRepresentation ?? " "
|
||||||
boardString += " \(p) \(UnicodeBar.VerticalBar)"
|
h1 = def
|
||||||
|
if let c = colors {
|
||||||
|
for (color, s) in c
|
||||||
|
where (s.contains { $0 == square.position }) {
|
||||||
|
h1 = color
|
||||||
|
}
|
||||||
|
}
|
||||||
|
boardString +=
|
||||||
|
"\(h1) \(p) \(def)\(UnicodeBar.VerticalBar)"
|
||||||
}
|
}
|
||||||
boardString += "\n"
|
boardString += "\n"
|
||||||
_rank -= 1
|
_rank -= 1
|
||||||
@@ -236,6 +324,10 @@ public class Board: CustomStringConvertible, EventDelegate {
|
|||||||
return boardString
|
return boardString
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public var description: String {
|
||||||
|
text()
|
||||||
|
}
|
||||||
|
|
||||||
internal subscript(pos: Square.Position) -> Square? {
|
internal subscript(pos: Square.Position) -> Square? {
|
||||||
guard let i = pos.index else {
|
guard let i = pos.index else {
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
9
Sources/Engine/Extensions/Set+Union.swift
Normal file
9
Sources/Engine/Extensions/Set+Union.swift
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
extension Set {
|
||||||
|
static func + (lhs: Set<Element>, rhs: Set<Element>) -> Set<Element> {
|
||||||
|
return lhs.union(rhs)
|
||||||
|
}
|
||||||
|
|
||||||
|
static func += (lhs: inout Set<Element>, rhs: Set<Element>) {
|
||||||
|
lhs.formUnion(rhs)
|
||||||
|
}
|
||||||
|
}
|
||||||
9
Sources/Engine/Extensions/Targets+Unique.swift
Normal file
9
Sources/Engine/Extensions/Targets+Unique.swift
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
extension Square.Targets {
|
||||||
|
public mutating func insert(_ piece: Piece) {
|
||||||
|
guard !self.contains(piece) else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
self.append(piece)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -64,8 +64,9 @@ public struct Fen: CustomStringConvertible {
|
|||||||
}
|
}
|
||||||
|
|
||||||
internal mutating func set(
|
internal mutating func set(
|
||||||
from board: Board.Grid, castling ca: CastlingAvailibility,
|
from board: Board.Grid, activeColor: Color,
|
||||||
enPassant: String
|
castling ca: CastlingAvailibility,
|
||||||
|
enPassant: String, halfMoveClock hmc: UInt8, fullMoveClock fmc: UInt8
|
||||||
) {
|
) {
|
||||||
#warning(
|
#warning(
|
||||||
"Determine active color, halfMoveClock, fullMoveClock based on history SAN later passed in arguments"
|
"Determine active color, halfMoveClock, fullMoveClock based on history SAN later passed in arguments"
|
||||||
@@ -95,7 +96,8 @@ public struct Fen: CustomStringConvertible {
|
|||||||
rankNr += 1
|
rankNr += 1
|
||||||
}
|
}
|
||||||
|
|
||||||
value = placement + " w " + ca.rawValue + " " + enPassant + " 0 " + "1"
|
value =
|
||||||
|
"\(placement) \(activeColor) \(ca.rawValue) \(enPassant) \(halfMoveClock) \(fullMoveClock)"
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum FenError: Error {
|
public enum FenError: Error {
|
||||||
|
|||||||
72
Sources/Engine/History.swift
Normal file
72
Sources/Engine/History.swift
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
public struct History {
|
||||||
|
public typealias Array = [(
|
||||||
|
SANMoveDescriptor.Value?, SANMoveDescriptor.Value?
|
||||||
|
)]
|
||||||
|
|
||||||
|
public enum SANMoveDescriptor {
|
||||||
|
public typealias Value = String
|
||||||
|
case pieceMove(from: Square, to: Square)
|
||||||
|
case pawnPush(to: Square, promotion: Piece?)
|
||||||
|
|
||||||
|
public var value: Value {
|
||||||
|
return switch self {
|
||||||
|
#warning("Doesn't work properly")
|
||||||
|
case .pieceMove(let from, let to):
|
||||||
|
{
|
||||||
|
guard let piece = from.piece else {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
#warning("Ugly ass code 🤢")
|
||||||
|
var result =
|
||||||
|
piece.kind == .Pawn
|
||||||
|
? ""
|
||||||
|
: "\(piece.kind.fenRepresentation(with: piece.color))"
|
||||||
|
if let ambiguity = (to.targetted.first { $0 ~= piece }) {
|
||||||
|
let file =
|
||||||
|
ambiguity.position.file != from.position.file
|
||||||
|
? Square.Position.File[from.position.file] : nil
|
||||||
|
var rank: Int8? {
|
||||||
|
if file == nil {
|
||||||
|
return ambiguity.position.rank
|
||||||
|
!= from.position.rank
|
||||||
|
? from.position.rank : nil
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if let f = file {
|
||||||
|
result += f.rawValue
|
||||||
|
} else if let r = rank {
|
||||||
|
result += String(r)
|
||||||
|
} else {
|
||||||
|
result +=
|
||||||
|
Square.Position.File[from.position.file]!
|
||||||
|
.rawValue
|
||||||
|
+ String(from.position.rank)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if to.piece != nil {
|
||||||
|
result += "x"
|
||||||
|
}
|
||||||
|
result +=
|
||||||
|
"\(Square.Position.File[to.position.file]!.rawValue)\(String(to.position.rank))"
|
||||||
|
return result
|
||||||
|
}()
|
||||||
|
case .pawnPush(let to, let promotion):
|
||||||
|
""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public private(set) var values: Array = []
|
||||||
|
|
||||||
|
internal mutating func add(_ san: SANMoveDescriptor) {
|
||||||
|
#warning("To be tested")
|
||||||
|
if let last = values.last {
|
||||||
|
if last.1 == nil {
|
||||||
|
values[values.count - 1].1 = san.value
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
values.append((san.value, nil))
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,12 +8,48 @@ final class Bishop: Piece, DiagonalMoves {
|
|||||||
return getDiagonalMoves(from: position)
|
return getDiagonalMoves(from: position)
|
||||||
}
|
}
|
||||||
|
|
||||||
override var legalPositions: [Square.Position] {
|
override func getLegalPosition() {
|
||||||
return pseudoLegalPositions.filter { isLegal(on: $0) }
|
legalPositions = []
|
||||||
|
var last: Square.Position? = nil
|
||||||
|
for position in pseudoLegalPositions {
|
||||||
|
if !isLegal(on: position) {
|
||||||
|
last = position
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if !DiagonalDirection.isLegal(last: &last, current: position) {
|
||||||
|
last = nil
|
||||||
|
}
|
||||||
|
if last == nil {
|
||||||
|
legalPositions.append(position)
|
||||||
|
if let square = delegate?.getSquareInfo(on: position),
|
||||||
|
let piece = square.piece
|
||||||
|
{
|
||||||
|
if piece.color != color {
|
||||||
|
if let king = piece as? King {
|
||||||
|
delegate?.notify(.kingInCheck(self, on: king))
|
||||||
|
legalPositions.removeLast()
|
||||||
|
last = position
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
delegate?.notify(
|
||||||
|
.piecePinned(from: self, on: piece))
|
||||||
|
last = position
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
delegate?.addPieceToTarget(self, target: position)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override func isLegal(on pos: Square.Position) -> Bool {
|
override func isLegal(on pos: Square.Position) -> Bool {
|
||||||
super.isLegal(on: pos)
|
if let s = delegate?.getSquareInfo(on: pos), let p = s.piece,
|
||||||
|
p.color == color
|
||||||
|
{
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
init(with color: Color, on position: Square.Position) {
|
init(with color: Color, on position: Square.Position) {
|
||||||
|
|||||||
@@ -1,30 +1,118 @@
|
|||||||
fileprivate func getDirectionalMoves(from pos: Square.Position, with dir: [(Int8, Int8)]) -> [Square.Position] {
|
private func getDirectionalMoves(
|
||||||
var squares = [Square.Position]()
|
from pos: Square.Position, with dir: [(Int8, Int8)]
|
||||||
for i: (Int8, Int8) in dir {
|
) -> [Square.Position] {
|
||||||
var currentSquare = pos + i
|
var squares = [Square.Position]()
|
||||||
while currentSquare.index != nil {
|
for i: (Int8, Int8) in dir {
|
||||||
squares.append(currentSquare)
|
var currentSquare = pos + i
|
||||||
currentSquare += i
|
while currentSquare.index != nil {
|
||||||
}
|
squares.append(currentSquare)
|
||||||
}
|
currentSquare += i
|
||||||
return squares
|
}
|
||||||
|
}
|
||||||
|
return squares
|
||||||
}
|
}
|
||||||
|
|
||||||
protocol DiagonalMoves {
|
protocol DiagonalMoves {
|
||||||
func getDiagonalMoves(from pos: Square.Position) -> [Square.Position]
|
func getDiagonalMoves(from pos: Square.Position) -> [Square.Position]
|
||||||
|
}
|
||||||
|
|
||||||
|
protocol Direction: CaseIterable {
|
||||||
|
var values: (Int8, Int8) { get }
|
||||||
|
static func isLegal(
|
||||||
|
last: inout Square.Position?,
|
||||||
|
current: Square.Position
|
||||||
|
) -> Bool
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum DiagonalDirection: Direction {
|
||||||
|
case northWest
|
||||||
|
case northEast
|
||||||
|
case southEast
|
||||||
|
case southWest
|
||||||
|
|
||||||
|
public var values: (Int8, Int8) {
|
||||||
|
return switch self {
|
||||||
|
case .northWest: (1, -1)
|
||||||
|
case .northEast: (1, 1)
|
||||||
|
case .southEast: (-1, 1)
|
||||||
|
case .southWest: (-1, -1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public static func isLegal(
|
||||||
|
last: inout Square.Position?,
|
||||||
|
current:
|
||||||
|
Square.Position
|
||||||
|
) -> Bool {
|
||||||
|
var isPred = false
|
||||||
|
for dir in DiagonalDirection.allCases {
|
||||||
|
isPred = last == current - dir.values
|
||||||
|
if isPred {
|
||||||
|
last = current
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return isPred
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension DiagonalMoves {
|
extension DiagonalMoves {
|
||||||
func getDiagonalMoves(from pos: Square.Position) -> [Square.Position] {
|
func getDiagonalMoves(from pos: Square.Position) -> [Square.Position] {
|
||||||
getDirectionalMoves(from: pos, with: [(1, -1), (1, 1), (-1, 1), (-1, -1)])
|
getDirectionalMoves(
|
||||||
}
|
from: pos,
|
||||||
|
with: [
|
||||||
|
DiagonalDirection.northWest.values,
|
||||||
|
DiagonalDirection.northEast.values,
|
||||||
|
DiagonalDirection.southEast.values,
|
||||||
|
DiagonalDirection.southWest.values,
|
||||||
|
])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protocol LinearMoves {
|
protocol LinearMoves {
|
||||||
func getLinearMoves(from pos: Square.Position) -> [Square.Position]
|
func getLinearMoves(from pos: Square.Position) -> [Square.Position]
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum LinearDirection: Direction {
|
||||||
|
case north
|
||||||
|
case south
|
||||||
|
case east
|
||||||
|
case west
|
||||||
|
|
||||||
|
public var values: (Int8, Int8) {
|
||||||
|
return switch self {
|
||||||
|
case .north: (1, 0)
|
||||||
|
case .south: (-1, 0)
|
||||||
|
case .east: (0, 1)
|
||||||
|
case .west: (0, -1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static func isLegal(
|
||||||
|
last: inout Square.Position?, current: Square.Position
|
||||||
|
) -> Bool {
|
||||||
|
var isPred = false
|
||||||
|
for dir in LinearDirection.allCases {
|
||||||
|
isPred = last == current - dir.values
|
||||||
|
if isPred {
|
||||||
|
last = current
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return isPred
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension LinearMoves {
|
extension LinearMoves {
|
||||||
func getLinearMoves(from pos: Square.Position) -> [Square.Position] {
|
func getLinearMoves(from pos: Square.Position) -> [Square.Position] {
|
||||||
getDirectionalMoves(from: pos, with: [(1, 0), (0, 1), (-1, 0), (0, -1)])
|
getDirectionalMoves(
|
||||||
}
|
from: pos,
|
||||||
}
|
with: [
|
||||||
|
LinearDirection.north.values,
|
||||||
|
LinearDirection.east.values,
|
||||||
|
LinearDirection.south.values,
|
||||||
|
LinearDirection.west.values,
|
||||||
|
])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,7 +1,21 @@
|
|||||||
final class King: Piece {
|
final class King: Piece {
|
||||||
|
typealias Threats = (Piece?, Piece?)
|
||||||
|
internal private(set) var threats: Threats = (nil, nil)
|
||||||
|
|
||||||
|
func insertThreat(_ piece: Piece) {
|
||||||
|
if threats.0 == nil {
|
||||||
|
threats.0 = piece
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if threats.1 == nil {
|
||||||
|
threats.1 = piece
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override var unicodeRepresentation: String {
|
override var unicodeRepresentation: String {
|
||||||
return color == .Black ? "♛" : "♕"
|
return color == .Black ? "♚" : "♔"
|
||||||
}
|
}
|
||||||
|
|
||||||
override var pseudoLegalPositions: [Square.Position] {
|
override var pseudoLegalPositions: [Square.Position] {
|
||||||
@@ -17,12 +31,12 @@ final class King: Piece {
|
|||||||
].filter { $0.index != nil }
|
].filter { $0.index != nil }
|
||||||
}
|
}
|
||||||
|
|
||||||
override var legalPositions: [Square.Position] {
|
|
||||||
return pseudoLegalPositions.filter { isLegal(on: $0) }
|
|
||||||
}
|
|
||||||
|
|
||||||
override func isLegal(on pos: Square.Position) -> Bool {
|
override func isLegal(on pos: Square.Position) -> Bool {
|
||||||
super.isLegal(on: pos)
|
guard super.isLegal(on: pos) else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
// Do stuff
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
init(with color: Color, on position: Square.Position) {
|
init(with color: Color, on position: Square.Position) {
|
||||||
|
|||||||
@@ -16,10 +16,6 @@ final class Knight: Piece {
|
|||||||
].filter { $0.index != nil }
|
].filter { $0.index != nil }
|
||||||
}
|
}
|
||||||
|
|
||||||
override var legalPositions: [Square.Position] {
|
|
||||||
return pseudoLegalPositions.filter { isLegal(on: $0) }
|
|
||||||
}
|
|
||||||
|
|
||||||
override func isLegal(on pos: Square.Position) -> Bool {
|
override func isLegal(on pos: Square.Position) -> Bool {
|
||||||
super.isLegal(on: pos)
|
super.isLegal(on: pos)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
final class Pawn: Piece {
|
final class Pawn: Piece {
|
||||||
|
private var sign: Int8 {
|
||||||
|
color == .Black ? -1 : 1
|
||||||
|
}
|
||||||
override var pseudoLegalPositions: [Square.Position] {
|
override var pseudoLegalPositions: [Square.Position] {
|
||||||
let sign: Int8 = color == .Black ? -1 : 1
|
|
||||||
return [
|
return [
|
||||||
position + (1 * sign, 0),
|
position + (1 * sign, 0),
|
||||||
position + (2 * sign, 0),
|
position + (2 * sign, 0),
|
||||||
@@ -9,16 +11,30 @@ final class Pawn: Piece {
|
|||||||
].filter { $0.index != nil }
|
].filter { $0.index != nil }
|
||||||
}
|
}
|
||||||
|
|
||||||
override var legalPositions: [Square.Position] {
|
|
||||||
pseudoLegalPositions.filter { isLegal(on: $0) }
|
|
||||||
}
|
|
||||||
|
|
||||||
override var unicodeRepresentation: String {
|
override var unicodeRepresentation: String {
|
||||||
color == .Black ? "♟" : "♙"
|
color == .Black ? "♟" : "♙"
|
||||||
}
|
}
|
||||||
|
|
||||||
override func isLegal(on pos: Square.Position) -> Bool {
|
override func isLegal(on pos: Square.Position) -> Bool {
|
||||||
super.isLegal(on: pos)
|
#warning("Handle en-passant")
|
||||||
|
if pos == position + (2 * sign, 0) && halfMoveCount > 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if (pos == position + (1 * sign, 0) || pos == position + (2 * sign, 0))
|
||||||
|
&& delegate?.getSquareInfo(on: pos)?.piece != nil
|
||||||
|
{
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
guard super.isLegal(on: pos) else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if (pos == position + (1 * sign, 1) || pos == position + (1 * sign, -1))
|
||||||
|
&& delegate?.getSquareInfo(on: pos)?.piece == nil
|
||||||
|
{
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
init(with color: Color, on position: Square.Position) {
|
init(with color: Color, on position: Square.Position) {
|
||||||
|
|||||||
@@ -53,10 +53,9 @@ public enum Kind: String, CaseIterable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class Piece {
|
public class Piece: Hashable {
|
||||||
#warning("TODO: TO be removed, handle everything through the delegate")
|
|
||||||
internal weak var board: Board?
|
|
||||||
public internal(set) var color: Color
|
public internal(set) var color: Color
|
||||||
|
public internal(set) var halfMoveCount: UInt8 = 0
|
||||||
public var unicodeRepresentation: String {
|
public var unicodeRepresentation: String {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
@@ -65,30 +64,34 @@ public class Piece {
|
|||||||
internal var pseudoLegalPositions: [Square.Position] {
|
internal var pseudoLegalPositions: [Square.Position] {
|
||||||
return []
|
return []
|
||||||
}
|
}
|
||||||
internal var legalPositions: [Square.Position] {
|
package internal(set) var legalPositions = [Square.Position]()
|
||||||
return []
|
internal func getLegalPosition() {
|
||||||
|
legalPositions = pseudoLegalPositions.filter { isLegal(on: $0) }
|
||||||
}
|
}
|
||||||
internal var delegate: EventDelegate?
|
internal var delegate: EventDelegate?
|
||||||
|
|
||||||
internal func move(to dst: Square.Position) throws {
|
internal func move(to dst: Square.Position) throws {
|
||||||
if !(legalPositions.contains { $0 == dst }) {
|
guard (legalPositions.contains { $0 == dst }) else {
|
||||||
throw Board.MoveFailure.destinationIsIllegal(pos: dst)
|
throw Board.MoveFailure.destinationIsIllegal(pos: dst)
|
||||||
}
|
}
|
||||||
|
|
||||||
try delegate?.movePiece(self, to: dst)
|
try delegate?.movePiece(self, to: dst)
|
||||||
|
halfMoveCount += 1
|
||||||
}
|
}
|
||||||
internal func isLegal(on pos: Square.Position) -> Bool {
|
|
||||||
|
|
||||||
if let board = board, let s = board[pos] {
|
#warning("This method should be better thought out.")
|
||||||
|
internal func isLegal(on pos: Square.Position) -> Bool {
|
||||||
|
if let s = delegate?.getSquareInfo(on: pos) {
|
||||||
if let p = s.piece {
|
if let p = s.piece {
|
||||||
if p.color == color { return false }
|
if p.color == color { return false }
|
||||||
if p.kind == .King {
|
if let king = p as? King {
|
||||||
// TODO: Notify board of check
|
delegate?.notify(.kingInCheck(self, on: king))
|
||||||
delegate?.notify(.kingInCheck(self))
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
delegate?.addPieceToTarget(self, target: pos)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -97,4 +100,30 @@ public class Piece {
|
|||||||
self.position = pos
|
self.position = pos
|
||||||
self.color = col
|
self.color = col
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Two pieces are equal when they share the same kind, color and position
|
||||||
|
public static func == (lhs: Piece, rhs: Piece) -> Bool {
|
||||||
|
return lhs.kind == rhs.kind && lhs.color == rhs.color
|
||||||
|
&& lhs.position == rhs.position
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Two pieces are not equal when they either do not share the same kind,
|
||||||
|
/// color or position
|
||||||
|
public static func != (lhs: Piece, rhs: Piece) -> Bool {
|
||||||
|
return !(lhs == rhs)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Checks if tho pieces are similar yet not equal.
|
||||||
|
/// They are considered simmilar if they share the same color and the same kind,
|
||||||
|
/// but differ in position.
|
||||||
|
public static func ~= (lhs: Piece, rhs: Piece) -> Bool {
|
||||||
|
return lhs.kind == rhs.kind && lhs.color == rhs.color
|
||||||
|
&& lhs.position != rhs.position
|
||||||
|
}
|
||||||
|
|
||||||
|
public func hash(into hasher: inout Hasher) {
|
||||||
|
hasher.combine(kind)
|
||||||
|
hasher.combine(color)
|
||||||
|
hasher.combine(position)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,12 +7,51 @@ final class Queen: Piece, LinearMoves, DiagonalMoves {
|
|||||||
return getDiagonalMoves(from: position) + getLinearMoves(from: position)
|
return getDiagonalMoves(from: position) + getLinearMoves(from: position)
|
||||||
}
|
}
|
||||||
|
|
||||||
override var legalPositions: [Square.Position] {
|
override func getLegalPosition() {
|
||||||
return pseudoLegalPositions.filter { isLegal(on: $0) }
|
legalPositions = []
|
||||||
|
var last: Square.Position? = nil
|
||||||
|
for position in pseudoLegalPositions {
|
||||||
|
if !isLegal(on: position) {
|
||||||
|
last = position
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if !LinearDirection.isLegal(last: &last, current: position)
|
||||||
|
&& !DiagonalDirection.isLegal(last: &last, current: position)
|
||||||
|
{
|
||||||
|
last = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if last == nil {
|
||||||
|
legalPositions.append(position)
|
||||||
|
if let square = delegate?.getSquareInfo(on: position),
|
||||||
|
let piece = square.piece
|
||||||
|
{
|
||||||
|
if piece.color != color {
|
||||||
|
if let king = piece as? King {
|
||||||
|
delegate?.notify(.kingInCheck(self, on: king))
|
||||||
|
legalPositions.removeLast()
|
||||||
|
last = position
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
delegate?.notify(
|
||||||
|
.piecePinned(from: self, on: piece))
|
||||||
|
last = position
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
delegate?.addPieceToTarget(self, target: position)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override func isLegal(on pos: Square.Position) -> Bool {
|
override func isLegal(on pos: Square.Position) -> Bool {
|
||||||
super.isLegal(on: pos)
|
if let s = delegate?.getSquareInfo(on: pos), let p = s.piece,
|
||||||
|
p.color == color
|
||||||
|
{
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
init(with color: Color, on position: Square.Position) {
|
init(with color: Color, on position: Square.Position) {
|
||||||
|
|||||||
@@ -8,12 +8,48 @@ final class Rook: Piece, LinearMoves {
|
|||||||
return getLinearMoves(from: position)
|
return getLinearMoves(from: position)
|
||||||
}
|
}
|
||||||
|
|
||||||
override var legalPositions: [Square.Position] {
|
override func getLegalPosition() {
|
||||||
return pseudoLegalPositions.filter { isLegal(on: $0) }
|
legalPositions = []
|
||||||
|
var last: Square.Position? = nil
|
||||||
|
for position in pseudoLegalPositions {
|
||||||
|
if !isLegal(on: position) {
|
||||||
|
last = position
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if !LinearDirection.isLegal(last: &last, current: position) {
|
||||||
|
last = nil
|
||||||
|
}
|
||||||
|
if last == nil {
|
||||||
|
legalPositions.append(position)
|
||||||
|
if let square = delegate?.getSquareInfo(on: position),
|
||||||
|
let piece = square.piece
|
||||||
|
{
|
||||||
|
if piece.color != color {
|
||||||
|
if let king = piece as? King {
|
||||||
|
delegate?.notify(.kingInCheck(self, on: king))
|
||||||
|
legalPositions.removeLast()
|
||||||
|
last = position
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
delegate?.notify(
|
||||||
|
.piecePinned(from: self, on: piece))
|
||||||
|
last = position
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
delegate?.addPieceToTarget(self, target: position)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override func isLegal(on pos: Square.Position) -> Bool {
|
override func isLegal(on pos: Square.Position) -> Bool {
|
||||||
super.isLegal(on: pos)
|
if let s = delegate?.getSquareInfo(on: pos), let p = s.piece,
|
||||||
|
p.color == color
|
||||||
|
{
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
init(with color: Color, on position: Square.Position) {
|
init(with color: Color, on position: Square.Position) {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
public struct Square: Equatable {
|
public struct Square: Equatable {
|
||||||
|
|
||||||
public struct Position: Equatable {
|
public struct Position: Equatable, Hashable {
|
||||||
public let rank: Int8
|
public let rank: Int8
|
||||||
public let file: Int8
|
public let file: Int8
|
||||||
|
|
||||||
@@ -16,6 +16,25 @@ public struct Square: Equatable {
|
|||||||
file = f
|
file = f
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public init?(with rep: String) throws {
|
||||||
|
guard rep.count < 3 else {
|
||||||
|
throw Board.MoveFailure.squareANIsTooLong(an: rep)
|
||||||
|
}
|
||||||
|
guard rep.count > 1 else {
|
||||||
|
throw Board.MoveFailure.squareANIsTooShort(an: rep)
|
||||||
|
}
|
||||||
|
guard let f1 = rep.first, let r1 = rep.last, r1.isWholeNumber else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
guard
|
||||||
|
let srcFile = File.init(rawValue: String(f1))?.value
|
||||||
|
else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
rank = Int8(String(r1))!
|
||||||
|
file = srcFile
|
||||||
|
}
|
||||||
|
|
||||||
public enum File: String, CustomStringConvertible {
|
public enum File: String, CustomStringConvertible {
|
||||||
case a, b, c, d, e, f, g, h
|
case a, b, c, d, e, f, g, h
|
||||||
|
|
||||||
@@ -70,11 +89,29 @@ public struct Square: Equatable {
|
|||||||
public static func += (lhs: inout Position, rhs: (Int8, Int8)) {
|
public static func += (lhs: inout Position, rhs: (Int8, Int8)) {
|
||||||
lhs = lhs + rhs
|
lhs = lhs + rhs
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static func - (lhs: Position, rhs: Position) -> Position {
|
||||||
|
.init(rank: lhs.rank - rhs.rank, file: lhs.file - rhs.file)
|
||||||
|
}
|
||||||
|
|
||||||
|
public static func - (lhs: Position, rhs: (Int8, Int8)) -> Position {
|
||||||
|
.init(rank: lhs.rank - rhs.0, file: lhs.file - rhs.1)
|
||||||
|
}
|
||||||
|
|
||||||
|
public static func -= (lhs: inout Position, rhs: Position) {
|
||||||
|
lhs = lhs - rhs
|
||||||
|
}
|
||||||
|
|
||||||
|
public static func -= (lhs: inout Position, rhs: (Int8, Int8)) {
|
||||||
|
lhs = lhs - rhs
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public let position: Position
|
public let position: Position
|
||||||
public internal(set) var piece: Piece? = nil
|
public internal(set) var piece: Piece? = nil
|
||||||
public let color: Color
|
public let color: Color
|
||||||
|
public typealias Targets = [Piece]
|
||||||
|
public internal(set) var targetted: Targets = []
|
||||||
|
|
||||||
public static func == (lhs: Square, rhs: Square) -> Bool {
|
public static func == (lhs: Square, rhs: Square) -> Bool {
|
||||||
return lhs.position == rhs.position
|
return lhs.position == rhs.position
|
||||||
|
|||||||
@@ -1,4 +1,15 @@
|
|||||||
public enum Color: UInt8 {
|
public enum Color: UInt8, CustomStringConvertible {
|
||||||
case Black = 0
|
case Black = 0
|
||||||
case White = 1
|
case White = 1
|
||||||
|
|
||||||
|
public var description: String {
|
||||||
|
return switch self {
|
||||||
|
case .Black: "b"
|
||||||
|
case .White: "w"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static prefix func ! (rhs: Self) -> Self {
|
||||||
|
return rhs == .White ? .Black : .White
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,28 +1,109 @@
|
|||||||
import Engine
|
import Engine
|
||||||
|
|
||||||
let board = Board(
|
enum Command: Equatable {
|
||||||
fen: .init(
|
case quit
|
||||||
fen: "rnbqkb1r/pppppppp/2n5/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"))
|
case noop
|
||||||
// let board: Board = .init()
|
case print
|
||||||
|
case move(from: String, to: String)
|
||||||
|
case history(command: String? = nil)
|
||||||
|
case highlightThreatened(color: Color)
|
||||||
|
case highlightPositions(of: Square.Position)
|
||||||
|
|
||||||
|
init?(rawValue: String) {
|
||||||
|
let args = rawValue.lowercased().split(separator: " ")
|
||||||
|
guard !args.isEmpty else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
switch args[0] {
|
||||||
|
case "quit", "q":
|
||||||
|
self = .quit
|
||||||
|
case "noop", "n":
|
||||||
|
self = .noop
|
||||||
|
case "print", "p":
|
||||||
|
self = .print
|
||||||
|
case "move" where args.count == 3, "m" where args.count == 3:
|
||||||
|
self = .move(from: String(args[1]), to: String(args[2]))
|
||||||
|
case "history" where args.count == 2, "h" where args.count == 2:
|
||||||
|
self = .history(command: String(args[1]))
|
||||||
|
case "history", "h":
|
||||||
|
self = .history()
|
||||||
|
case "highlight" where args.count == 2, "hi" where args.count == 2:
|
||||||
|
let color: Color? =
|
||||||
|
switch args[1] {
|
||||||
|
case "w", "white":
|
||||||
|
.White
|
||||||
|
case "b", "black":
|
||||||
|
.Black
|
||||||
|
default:
|
||||||
|
nil
|
||||||
|
}
|
||||||
|
if let c = color {
|
||||||
|
self = .highlightThreatened(color: c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if let position = try? Square.Position(with: String(args[1])) {
|
||||||
|
self = .highlightPositions(of: position)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
default:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let board: Board = .init()
|
||||||
print(board)
|
print(board)
|
||||||
|
|
||||||
|
var command: Command = .noop
|
||||||
|
|
||||||
|
while command != .quit {
|
||||||
|
if let line = readLine(), let c = Command(rawValue: line.lowercased()) {
|
||||||
|
command = c
|
||||||
|
switch command {
|
||||||
|
case .print: print(board)
|
||||||
|
case .noop, .quit: continue
|
||||||
|
case .move(let from, let to):
|
||||||
|
do {
|
||||||
|
try board.move(src: from, dst: to)
|
||||||
|
} catch {
|
||||||
|
print(error)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
case .history(let command):
|
||||||
|
if let c = command {
|
||||||
|
if let fmc = Int(c),
|
||||||
|
fmc > -1 && fmc < board.history.values.count
|
||||||
|
{
|
||||||
|
print(board.history.values[fmc])
|
||||||
|
} else if c == "count" {
|
||||||
|
print(board.history.values.count)
|
||||||
|
} else {
|
||||||
|
print(board.history.values)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
print(board.history.values)
|
||||||
|
}
|
||||||
|
case .highlightThreatened(let color):
|
||||||
|
if let ts = board.threatenedSquares[color] {
|
||||||
|
let color = Board.TerminalColors(fg: .def, bg: .red)
|
||||||
|
print(ts.count)
|
||||||
|
print(board.text(with: [color: Array(ts)]))
|
||||||
|
}
|
||||||
|
case .highlightPositions(let pos):
|
||||||
|
if let square = board.getSquareInfo(on: pos),
|
||||||
|
let piece =
|
||||||
|
square.piece
|
||||||
|
{
|
||||||
|
let color = Board.TerminalColors(fg: .def, bg: .green)
|
||||||
|
print(board.text(with: [color: piece.legalPositions]))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Execute every time the timer changes
|
// Execute every time the timer changes
|
||||||
// board.on(.timer) { time in
|
// board.on(.timer) { time in
|
||||||
|
|
||||||
// }
|
// }
|
||||||
|
|
||||||
do {
|
|
||||||
try board.move(src: "e4", dst: "f6")
|
|
||||||
} catch {
|
|
||||||
print(error)
|
|
||||||
}
|
|
||||||
do {
|
|
||||||
try board.move(src: .init(rank: 2, file: 5), dst: .init(rank: 4, file: 5))
|
|
||||||
} catch {
|
|
||||||
print(error)
|
|
||||||
}
|
|
||||||
|
|
||||||
print(board)
|
|
||||||
print(board.fen)
|
|
||||||
|
|
||||||
// let square = board.getSquareInfo(on: .init(rank: 2, file: 7))
|
|
||||||
|
|||||||
@@ -109,8 +109,11 @@ final class EngineTests: XCTestCase {
|
|||||||
let board: Board = .init()
|
let board: Board = .init()
|
||||||
let fen = board.fen.value
|
let fen = board.fen.value
|
||||||
board.fen.set(
|
board.fen.set(
|
||||||
from: board.board, castling: board.fen.castlingAvailibility,
|
from: board.board, activeColor: board.fen.activeColor,
|
||||||
enPassant: board.fen.enPassant)
|
castling: board.fen.castlingAvailibility,
|
||||||
|
enPassant: board.fen.enPassant,
|
||||||
|
halfMoveClock:
|
||||||
|
board.fen.halfMoveClock, fullMoveClock: board.fen.fullMoveClock)
|
||||||
XCTAssertEqual(
|
XCTAssertEqual(
|
||||||
fen, board.fen.value, "Expected \(fen) got \(board.fen.value)")
|
fen, board.fen.value, "Expected \(fen) got \(board.fen.value)")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user