Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions ChessKit/Sources/ChessProtocol/UCI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public final class UCIEngine {

// MARK: - Command handlers

/// Parses `position [startpos | fen <6 fields>] [moves <uci>...]`.
/// Parses `position [startpos | fen <fen fields>] [moves <uci>...]`.
private func applyPosition(_ args: [String]) {
var index = 0
var newBoard: Board?
Expand All @@ -78,8 +78,11 @@ public final class UCIEngine {
newBoard = Board()
index = 1
} else if args.first == "fen" {
// A FEN is up to six space-separated fields following "fen".
let fenFields = Array(args.dropFirst().prefix(6))
// The FEN runs until the "moves" keyword, capped at six fields.
// Stopping at "moves" is what lets a short FEN (Board accepts 4–5
// fields, defaulting the clocks) keep its trailing moves instead of
// swallowing the "moves" token and the moves into the FEN string.
let fenFields = Array(args.dropFirst().prefix { $0 != "moves" }.prefix(6))
newBoard = Board(fen: fenFields.joined(separator: " "))
index = 1 + fenFields.count
}
Expand Down
10 changes: 10 additions & 0 deletions ChessKit/Tests/ChessProtocolTests/UCITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ final class UCITests: XCTestCase {
XCTAssertNil(uci.currentBoard[Sq.parse("e2")!])
}

func testPositionShortFENWithMoves() {
// A 4-field FEN (no halfmove/fullmove counters) followed by moves.
// The "moves" keyword must not be absorbed into the FEN, or the move
// is silently dropped and e2e4 is never applied.
let uci = makeAdapter()
uci.process("position fen 4k3/8/8/8/8/8/4P3/4K3 w - - moves e2e4")
XCTAssertEqual(uci.currentBoard[Sq.parse("e4")!], Piece(color: .white, kind: .pawn))
XCTAssertNil(uci.currentBoard[Sq.parse("e2")!])
}

func testUCINewGameResets() {
let uci = makeAdapter()
uci.process("position startpos moves e2e4")
Expand Down
Loading