CPG/src/Chess/player.nim

40 lines
1012 B
Nim
Raw Normal View History

2023-10-13 11:54:57 +02:00
import board as chess
2023-10-16 22:14:58 +02:00
import std/strformat
import std/strutils
2023-03-18 18:14:30 +01:00
2023-10-16 14:55:43 +02:00
2023-10-16 22:14:58 +02:00
2023-10-17 12:08:07 +02:00
when isMainModule:
setControlCHook(proc () {.noconv.} = echo ""; quit(0))
var
board = newChessboardFromFEN("rnbqkbnr/8/8/8/8/8/8/RNBQKBNR w KQkq - 0 1")
startSquare: string
targetSquare: string
data: string
move: Move
while true:
echo &"{board.pretty()}"
echo &"Turn: {board.getActiveColor()}"
if board.inCheck():
echo &"Check!"
stdout.write("Move (from, to) -> ")
try:
data = readLine(stdin).strip(chars={'\0', ' '})
except IOError:
echo ""
break
if len(data) != 4:
continue
startSquare = data[0..1]
targetSquare = data[2..3]
try:
move = board.makeMove(startSquare, targetSquare)
except ValueError:
echo &"Error: {getCurrentExceptionMsg()}"
2023-10-17 12:42:15 +02:00
if move == emptyMove():
echo &"Error: move is illegal"