More UCI ground work

This commit is contained in:
Mattia Giambirtone 2024-04-24 12:38:03 +02:00
parent 7cd16cea88
commit ce960003a2
2 changed files with 72 additions and 3 deletions

View File

@ -175,4 +175,21 @@ func `$`*(self: Move): string =
result &= $flag
if i < flags.high():
result &= ", "
result &= ")"
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

View File

@ -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..<moves.len())].toAlgebraic()}"
of Position:
discard
else: