Added commands to execute interactively
This commit is contained in:
@@ -6,6 +6,7 @@ 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]]
|
||||
@@ -21,6 +22,56 @@ public class Board: CustomStringConvertible, EventDelegate {
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
|
||||
}
|
||||
@@ -38,10 +89,11 @@ public class Board: CustomStringConvertible, EventDelegate {
|
||||
halfMoveClock / 2
|
||||
}
|
||||
|
||||
internal var threatenedSquares: [Color: Set<Square.Position>] = [
|
||||
.White: [],
|
||||
.Black: [],
|
||||
]
|
||||
public internal(set) var threatenedSquares: [Color: Set<Square.Position>] =
|
||||
[
|
||||
.White: [],
|
||||
.Black: [],
|
||||
]
|
||||
|
||||
public internal(set) var history = History()
|
||||
|
||||
@@ -79,7 +131,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):
|
||||
@@ -106,7 +158,7 @@ public class Board: CustomStringConvertible, EventDelegate {
|
||||
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 {
|
||||
guard let f2 = dst.first, let r2 = dst.last, r2.isWholeNumber else {
|
||||
return
|
||||
}
|
||||
guard
|
||||
@@ -245,7 +297,9 @@ public class Board: CustomStringConvertible, EventDelegate {
|
||||
do {
|
||||
try setBoard()
|
||||
for square in squares where square.piece != nil {
|
||||
square.piece?.getLegalPosition()
|
||||
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)")
|
||||
@@ -254,15 +308,29 @@ 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)"
|
||||
if let c = colors {
|
||||
for (color, s) in c {
|
||||
h1 = def
|
||||
if (s.contains { $0 == square.position }) {
|
||||
h1 = color
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
boardString +=
|
||||
"\(h1) \(p) \(def)\(UnicodeBar.VerticalBar)"
|
||||
}
|
||||
boardString += "\n"
|
||||
_rank -= 1
|
||||
@@ -274,6 +342,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
|
||||
|
||||
Reference in New Issue
Block a user