Initial work for UCI mode

This commit is contained in:
Mattia Giambirtone 2024-04-09 17:55:12 +02:00
parent 57353c0994
commit c1ac5ea5c3
1 changed files with 23 additions and 5 deletions

View File

@ -2079,6 +2079,17 @@ proc handlePositionCommand(board: var ChessBoard, command: seq[string]) =
echo board.pretty() echo board.pretty()
proc handleUCICommand(board: var ChessBoard, command: seq[string]): bool =
if len(command) != 1:
echo "error: uci: invalid number of arguments"
return false
echo "id name Nimfish 0.1"
echo "id author Nocturn9x & Contributors (see LICENSE)"
# TODO
echo "uciok"
return true
const HELP_TEXT = """Nimfish help menu: const HELP_TEXT = """Nimfish help menu:
- go: Begin a search - go: Begin a search
Subcommands: Subcommands:
@ -2112,26 +2123,33 @@ const HELP_TEXT = """Nimfish help menu:
- pretty: Shorthand for "position pretty" - pretty: Shorthand for "position pretty"
- print: Shorthand for "position print" - print: Shorthand for "position print"
- get <square>: Get the piece on the given square - get <square>: Get the piece on the given square
- uci: enter UCI mode (WIP)
""" """
proc main: int = proc main: int =
## Nimfish's control interface ## Nimfish's control interface
echo "Nimfish by nocturn9x (see LICENSE)" echo "Nimfish by nocturn9x (see LICENSE)"
var board = newDefaultChessboard() var
board = newDefaultChessboard()
uciMode = false
while true: while true:
var var
cmd: seq[string] cmd: seq[string]
cmdStr: string cmdStr: string
try: try:
stdout.write(">>> ") if not uciMode:
stdout.flushFile() stdout.write(">>> ")
stdout.flushFile()
cmdStr = readLine(stdin).strip(leading=true, trailing=true, chars={'\t', ' '}) cmdStr = readLine(stdin).strip(leading=true, trailing=true, chars={'\t', ' '})
if cmdStr.len() == 0: if cmdStr.len() == 0:
continue continue
cmd = cmdStr.splitWhitespace(maxsplit=2) cmd = cmdStr.splitWhitespace(maxsplit=2)
case cmd[0]: case cmd[0]:
of "uci":
if handleUCICommand(board, cmd):
uciMode = true
of "clear": of "clear":
echo "\x1Bc" echo "\x1Bc"
of "help": of "help":
@ -2156,12 +2174,12 @@ proc main: int =
echo "En passant target: None" echo "En passant target: None"
of "get": of "get":
if len(cmd) != 2: if len(cmd) != 2:
echo "get: invalid syntax" echo "error: get: invalid number of arguments"
continue continue
try: try:
echo board.getPiece(cmd[1]) echo board.getPiece(cmd[1])
except ValueError: except ValueError:
echo "get: invalid square" echo "error: get: invalid square"
continue continue
of "castle": of "castle":
let canCastle = board.canCastle() let canCastle = board.canCastle()