More UCI ground work
This commit is contained in:
parent
b4d2c5cbef
commit
51ef1ff9db
|
@ -176,3 +176,20 @@ func `$`*(self: Move): string =
|
||||||
if i < flags.high():
|
if i < flags.high():
|
||||||
result &= ", "
|
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
|
|
@ -15,7 +15,9 @@
|
||||||
## Implementation of a UCI compatible server
|
## Implementation of a UCI compatible server
|
||||||
import std/strutils
|
import std/strutils
|
||||||
import std/strformat
|
import std/strformat
|
||||||
|
import std/random
|
||||||
|
|
||||||
|
randomize()
|
||||||
|
|
||||||
|
|
||||||
import board
|
import board
|
||||||
|
@ -33,7 +35,8 @@ type
|
||||||
NewGame
|
NewGame
|
||||||
Quit,
|
Quit,
|
||||||
Debug,
|
Debug,
|
||||||
Position
|
Position,
|
||||||
|
Go
|
||||||
|
|
||||||
UCICommand = object
|
UCICommand = object
|
||||||
case kind: UCICommandType
|
case kind: UCICommandType
|
||||||
|
@ -44,10 +47,50 @@ type
|
||||||
moves: seq[string]
|
moves: seq[string]
|
||||||
of Unknown:
|
of Unknown:
|
||||||
reason: string
|
reason: string
|
||||||
|
of Go:
|
||||||
|
wtime: int
|
||||||
|
btime: int
|
||||||
|
winc: int
|
||||||
|
binc: int
|
||||||
|
movesToGo: int
|
||||||
|
depth: int
|
||||||
|
moveTime: int
|
||||||
else:
|
else:
|
||||||
discard
|
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.} =
|
proc handleUCIMove(session: UCISession, move: string): tuple[move: Move, cmd: UCICommand] {.discardable.} =
|
||||||
var
|
var
|
||||||
|
@ -55,7 +98,7 @@ proc handleUCIMove(session: UCISession, move: string): tuple[move: Move, cmd: UC
|
||||||
targetSquare: Square
|
targetSquare: Square
|
||||||
flags: seq[MoveFlag]
|
flags: seq[MoveFlag]
|
||||||
if len(move) notin 4..5:
|
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:
|
try:
|
||||||
startSquare = move[0..1].toSquare()
|
startSquare = move[0..1].toSquare()
|
||||||
except ValueError:
|
except ValueError:
|
||||||
|
@ -186,6 +229,8 @@ proc parseUCICommand(session: UCISession, command: string): UCICommand =
|
||||||
return
|
return
|
||||||
of "position":
|
of "position":
|
||||||
return session.handleUCIPositionCommand(cmd)
|
return session.handleUCIPositionCommand(cmd)
|
||||||
|
of "go":
|
||||||
|
return session.handleUCIGoCommand(cmd)
|
||||||
else:
|
else:
|
||||||
# Unknown UCI commands should be ignored. Attempt
|
# Unknown UCI commands should be ignored. Attempt
|
||||||
# to make sense of the input regardless
|
# to make sense of the input regardless
|
||||||
|
@ -224,6 +269,13 @@ proc startUCISession* =
|
||||||
session.debug = cmd.value
|
session.debug = cmd.value
|
||||||
of NewGame:
|
of NewGame:
|
||||||
session.board = newDefaultChessboard()
|
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:
|
of Position:
|
||||||
discard
|
discard
|
||||||
else:
|
else:
|
||||||
|
|
Loading…
Reference in New Issue