diff --git a/Chess/nimfish/nimfishpkg/moves.nim b/Chess/nimfish/nimfishpkg/moves.nim index aea9d41..b99828e 100644 --- a/Chess/nimfish/nimfishpkg/moves.nim +++ b/Chess/nimfish/nimfishpkg/moves.nim @@ -175,4 +175,21 @@ func `$`*(self: Move): string = result &= $flag if i < flags.high(): result &= ", " - result &= ")" \ No newline at end of file + result &= ")" + + + +func toAlgebraic*(self: Move): string = + result &= &"{self.startSquare}{self.targetSquare}" + if self.isPromotion(): + case self.getPromotionType(): + of PromoteToBishop: + result &= "b" + of PromoteToKnight: + result &= "n" + of PromoteToQueen: + result &= "q" + of PromoteToRook: + result &= "r" + else: + discard \ No newline at end of file diff --git a/Chess/nimfish/nimfishpkg/uci.nim b/Chess/nimfish/nimfishpkg/uci.nim index 4383296..686c861 100644 --- a/Chess/nimfish/nimfishpkg/uci.nim +++ b/Chess/nimfish/nimfishpkg/uci.nim @@ -15,7 +15,9 @@ ## Implementation of a UCI compatible server import std/strutils import std/strformat +import std/random +randomize() import board @@ -33,7 +35,8 @@ type NewGame Quit, Debug, - Position + Position, + Go UCICommand = object case kind: UCICommandType @@ -44,10 +47,50 @@ type moves: seq[string] of Unknown: reason: string + of Go: + wtime: int + btime: int + winc: int + binc: int + movesToGo: int + depth: int + moveTime: int else: discard +proc handleUCIGoCommand(session: UCISession, command: seq[string]): UCICommand = + result = UCICommand(kind: Go) + result.wtime = -1 + result.btime = -1 + result.winc = 0 + result.binc = 0 + result.movesToGo = 0 + result.depth = 0 + result.moveTime = -1 + var + current = 0 + while current < command.len(): + case command[current]: + of "infinite": + discard + of "wtime": + result.wtime = command[current + 1].parseInt() + of "btime": + result.btime = command[current + 1].parseInt() + of "winc": + result.winc = command[current + 1].parseInt() + of "binc": + result.binc = command[current + 1].parseInt() + of "result.movestogo": + result.movesToGo = command[current + 1].parseInt() + of "depth": + result.depth = command[current + 1].parseInt() + of "movetime": + result.moveTime = command[current + 1].parseInt() + else: + return + proc handleUCIMove(session: UCISession, move: string): tuple[move: Move, cmd: UCICommand] {.discardable.} = var @@ -55,7 +98,7 @@ proc handleUCIMove(session: UCISession, move: string): tuple[move: Move, cmd: UC targetSquare: Square flags: seq[MoveFlag] if len(move) notin 4..5: - return (nullMove(), UCICommand(kind: Unknown, reason: &"invalid move syntax")) + return (nullMove(), UCICommand(kind: Unknown, reason: "invalid move syntax")) try: startSquare = move[0..1].toSquare() except ValueError: @@ -186,6 +229,8 @@ proc parseUCICommand(session: UCISession, command: string): UCICommand = return of "position": return session.handleUCIPositionCommand(cmd) + of "go": + return session.handleUCIGoCommand(cmd) else: # Unknown UCI commands should be ignored. Attempt # to make sense of the input regardless @@ -224,6 +269,13 @@ proc startUCISession* = session.debug = cmd.value of NewGame: session.board = newDefaultChessboard() + of Go: + var moves = MoveList() + session.board.generateMoves(moves) + if session.debug: + echo &"info string generated {len(moves)} moves" + if moves.len() > 0: + echo &"bestmove {moves[rand(0..