8 Commits

Author SHA1 Message Date
cdricms
b04ecceb9f yes 2024-09-16 15:20:02 +02:00
cdricms
8335db5888 Pawn moves 2024-07-01 17:18:10 +02:00
cdricms
cf42721235 Sliding pieces moves are now illegal if it's after the enemy 2024-07-01 16:14:49 +02:00
cdricms
d3503f1441 Sliding pieces moves are now illegal if after piece of same color 2024-06-30 21:06:03 +02:00
cdricms
02de114d74 Some bug fixes 2024-06-29 23:13:18 +02:00
cdricms
533322c5dd Added commands to execute interactively 2024-06-29 20:32:40 +02:00
cdricms
90f2988a14 Added history — though still need to be checked thoroughly 2024-06-29 17:50:55 +02:00
cdricms
0a220f14f5 Started to work on king threats 2024-06-28 23:58:26 +02:00
17 changed files with 693 additions and 123 deletions

View File

@@ -1,11 +1,15 @@
enum Event {
case kingInCheck(_ by: Piece)
case kingInCheck(_ by: Piece, on: King)
case piecePinned(from: Piece, on: Piece)
}
protocol EventDelegate {
func notify(_ event: Event)
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 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 {
let from = piece.position
history.add(
.pieceMove(
from: squares[from.index!],
to: squares[dst.index!]))
piece.position = dst
squares[dst.index!].piece = piece
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 {
@@ -47,7 +139,7 @@ public class Board: CustomStringConvertible, EventDelegate {
case .destinationSquareUnreachable(let pos):
"The destination square given \(pos.rank) \(pos.file) is unreachable."
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):
"The destination square given \(pos.rank) \(pos.file) is illegal to be moved to."
case .squareANIsTooLong(let an):
@@ -59,38 +151,11 @@ public class Board: CustomStringConvertible, EventDelegate {
}
public func move(src: String, dst: String) throws {
guard src.count < 3 else {
throw MoveFailure.squareANIsTooLong(an: src)
if let from = try Square.Position(with: 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 {
@@ -105,6 +170,15 @@ public class Board: CustomStringConvertible, EventDelegate {
}
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? {
@@ -183,9 +257,6 @@ public class Board: CustomStringConvertible, EventDelegate {
file += 1
index += 1
}
#if DEBUG
print(b)
#endif
squares = b
}
@@ -206,9 +277,14 @@ public class Board: CustomStringConvertible, EventDelegate {
rank -= 1
}
}
// TODO: Handle better
#warning("Handle better.")
do {
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) {
fatalError("Not appropriate length: \(n) on \(column)")
} 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 =
" A B C D E F G H \n \(UnicodeBar.TopHorizontalLine)\n"
var _rank: UInt8 = 8
let def: TerminalColors = .init()
var h1 = def
board.forEach { rank in
boardString += String(_rank) + " \(UnicodeBar.VerticalBar)"
rank.forEach { square in
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"
_rank -= 1
@@ -236,6 +324,10 @@ public class Board: CustomStringConvertible, EventDelegate {
return boardString
}
public var description: String {
text()
}
internal subscript(pos: Square.Position) -> Square? {
guard let i = pos.index else {
return nil

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

View File

@@ -0,0 +1,9 @@
extension Square.Targets {
public mutating func insert(_ piece: Piece) {
guard !self.contains(piece) else {
return
}
self.append(piece)
}
}

View File

@@ -64,8 +64,9 @@ public struct Fen: CustomStringConvertible {
}
internal mutating func set(
from board: Board.Grid, castling ca: CastlingAvailibility,
enPassant: String
from board: Board.Grid, activeColor: Color,
castling ca: CastlingAvailibility,
enPassant: String, halfMoveClock hmc: UInt8, fullMoveClock fmc: UInt8
) {
#warning(
"Determine active color, halfMoveClock, fullMoveClock based on history SAN later passed in arguments"
@@ -95,7 +96,8 @@ public struct Fen: CustomStringConvertible {
rankNr += 1
}
value = placement + " w " + ca.rawValue + " " + enPassant + " 0 " + "1"
value =
"\(placement) \(activeColor) \(ca.rawValue) \(enPassant) \(halfMoveClock) \(fullMoveClock)"
}
public enum FenError: Error {

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

View File

@@ -8,12 +8,48 @@ final class Bishop: Piece, DiagonalMoves {
return getDiagonalMoves(from: position)
}
override var legalPositions: [Square.Position] {
return pseudoLegalPositions.filter { isLegal(on: $0) }
override func getLegalPosition() {
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 {
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) {

View File

@@ -1,30 +1,118 @@
fileprivate func getDirectionalMoves(from pos: Square.Position, with dir: [(Int8, Int8)]) -> [Square.Position] {
var squares = [Square.Position]()
for i: (Int8, Int8) in dir {
var currentSquare = pos + i
while currentSquare.index != nil {
squares.append(currentSquare)
currentSquare += i
}
}
return squares
private func getDirectionalMoves(
from pos: Square.Position, with dir: [(Int8, Int8)]
) -> [Square.Position] {
var squares = [Square.Position]()
for i: (Int8, Int8) in dir {
var currentSquare = pos + i
while currentSquare.index != nil {
squares.append(currentSquare)
currentSquare += i
}
}
return squares
}
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 {
func getDiagonalMoves(from pos: Square.Position) -> [Square.Position] {
getDirectionalMoves(from: pos, with: [(1, -1), (1, 1), (-1, 1), (-1, -1)])
}
func getDiagonalMoves(from pos: Square.Position) -> [Square.Position] {
getDirectionalMoves(
from: pos,
with: [
DiagonalDirection.northWest.values,
DiagonalDirection.northEast.values,
DiagonalDirection.southEast.values,
DiagonalDirection.southWest.values,
])
}
}
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 {
func getLinearMoves(from pos: Square.Position) -> [Square.Position] {
getDirectionalMoves(from: pos, with: [(1, 0), (0, 1), (-1, 0), (0, -1)])
}
}
func getLinearMoves(from pos: Square.Position) -> [Square.Position] {
getDirectionalMoves(
from: pos,
with: [
LinearDirection.north.values,
LinearDirection.east.values,
LinearDirection.south.values,
LinearDirection.west.values,
])
}
}

View File

@@ -1,7 +1,21 @@
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 {
return color == .Black ? "" : ""
return color == .Black ? "" : ""
}
override var pseudoLegalPositions: [Square.Position] {
@@ -17,12 +31,12 @@ final class King: Piece {
].filter { $0.index != nil }
}
override var legalPositions: [Square.Position] {
return pseudoLegalPositions.filter { isLegal(on: $0) }
}
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) {

View File

@@ -16,10 +16,6 @@ final class Knight: Piece {
].filter { $0.index != nil }
}
override var legalPositions: [Square.Position] {
return pseudoLegalPositions.filter { isLegal(on: $0) }
}
override func isLegal(on pos: Square.Position) -> Bool {
super.isLegal(on: pos)
}

View File

@@ -1,6 +1,8 @@
final class Pawn: Piece {
private var sign: Int8 {
color == .Black ? -1 : 1
}
override var pseudoLegalPositions: [Square.Position] {
let sign: Int8 = color == .Black ? -1 : 1
return [
position + (1 * sign, 0),
position + (2 * sign, 0),
@@ -9,16 +11,30 @@ final class Pawn: Piece {
].filter { $0.index != nil }
}
override var legalPositions: [Square.Position] {
pseudoLegalPositions.filter { isLegal(on: $0) }
}
override var unicodeRepresentation: String {
color == .Black ? "" : ""
}
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) {

View File

@@ -53,10 +53,9 @@ public enum Kind: String, CaseIterable {
}
}
public class Piece {
#warning("TODO: TO be removed, handle everything through the delegate")
internal weak var board: Board?
public class Piece: Hashable {
public internal(set) var color: Color
public internal(set) var halfMoveCount: UInt8 = 0
public var unicodeRepresentation: String {
return ""
}
@@ -65,30 +64,34 @@ public class Piece {
internal var pseudoLegalPositions: [Square.Position] {
return []
}
internal var legalPositions: [Square.Position] {
return []
package internal(set) var legalPositions = [Square.Position]()
internal func getLegalPosition() {
legalPositions = pseudoLegalPositions.filter { isLegal(on: $0) }
}
internal var delegate: EventDelegate?
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)
}
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 p.color == color { return false }
if p.kind == .King {
// TODO: Notify board of check
delegate?.notify(.kingInCheck(self))
if let king = p as? King {
delegate?.notify(.kingInCheck(self, on: king))
return false
}
}
}
delegate?.addPieceToTarget(self, target: pos)
return true
}
@@ -97,4 +100,30 @@ public class Piece {
self.position = pos
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)
}
}

View File

@@ -7,12 +7,51 @@ final class Queen: Piece, LinearMoves, DiagonalMoves {
return getDiagonalMoves(from: position) + getLinearMoves(from: position)
}
override var legalPositions: [Square.Position] {
return pseudoLegalPositions.filter { isLegal(on: $0) }
override func getLegalPosition() {
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 {
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) {

View File

@@ -8,12 +8,48 @@ final class Rook: Piece, LinearMoves {
return getLinearMoves(from: position)
}
override var legalPositions: [Square.Position] {
return pseudoLegalPositions.filter { isLegal(on: $0) }
override func getLegalPosition() {
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 {
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) {

View File

@@ -1,6 +1,6 @@
public struct Square: Equatable {
public struct Position: Equatable {
public struct Position: Equatable, Hashable {
public let rank: Int8
public let file: Int8
@@ -16,6 +16,25 @@ public struct Square: Equatable {
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 {
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)) {
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 internal(set) var piece: Piece? = nil
public let color: Color
public typealias Targets = [Piece]
public internal(set) var targetted: Targets = []
public static func == (lhs: Square, rhs: Square) -> Bool {
return lhs.position == rhs.position

View File

@@ -1,4 +1,15 @@
public enum Color: UInt8 {
public enum Color: UInt8, CustomStringConvertible {
case Black = 0
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
}
}

View File

@@ -1,28 +1,109 @@
import Engine
let board = Board(
fen: .init(
fen: "rnbqkb1r/pppppppp/2n5/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"))
// let board: Board = .init()
enum Command: Equatable {
case quit
case noop
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)
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
// 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))

View File

@@ -109,8 +109,11 @@ final class EngineTests: XCTestCase {
let board: Board = .init()
let fen = board.fen.value
board.fen.set(
from: board.board, castling: board.fen.castlingAvailibility,
enPassant: board.fen.enPassant)
from: board.board, activeColor: board.fen.activeColor,
castling: board.fen.castlingAvailibility,
enPassant: board.fen.enPassant,
halfMoveClock:
board.fen.halfMoveClock, fullMoveClock: board.fen.fullMoveClock)
XCTAssertEqual(
fen, board.fen.value, "Expected \(fen) got \(board.fen.value)")
}