Sliding pieces moves are now illegal if after piece of same color

This commit is contained in:
cdricms
2024-06-30 21:06:03 +02:00
parent 02de114d74
commit d3503f1441
8 changed files with 225 additions and 54 deletions

View File

@@ -7,6 +7,7 @@ enum Command: Equatable {
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: " ")
@@ -28,16 +29,25 @@ enum Command: Equatable {
case "history", "h":
self = .history()
case "highlight" where args.count == 2, "hi" where args.count == 2:
let color: Color =
let color: Color? =
switch args[1] {
case "w", "white":
.White
case "b", "black":
.Black
default:
.White
nil
}
self = .highlightThreatened(color: color)
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
}
@@ -81,6 +91,14 @@ while command != .quit {
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]))
}
}
}
}