CPG/src/Chess/player.nim

51 lines
1.4 KiB
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
2023-10-17 16:38:43 +02:00
board = newChessboardFromFEN("rnbqkbnr/2p/8/8/8/8/P7/RNBQKBNR w KQkq - 0 1")
2023-10-17 12:08:07 +02:00
data: string
move: Move
echo "\x1Bc"
2023-10-17 12:08:07 +02:00
while true:
echo &"{board.pretty()}"
echo &"Turn: {board.getActiveColor()}"
echo &"Moves: {board.getMoveCount()} full, {board.getHalfMoveCount()} half"
2023-10-17 16:38:43 +02:00
stdout.write(&"En passant target: ")
if board.getEnPassantTarget() != emptyLocation():
echo board.getEnPassantTarget().locationToAlgebraic()
2023-10-17 16:38:43 +02:00
else:
echo "None"
stdout.write(&"Check: ")
2023-10-17 12:08:07 +02:00
if board.inCheck():
2023-10-17 16:38:43 +02:00
echo &"Yes"
else:
echo "No"
stdout.write("\nMove -> ")
2023-10-17 12:08:07 +02:00
try:
data = readLine(stdin).strip(chars={'\0', ' '})
except IOError:
echo ""
break
2023-10-17 15:08:46 +02:00
if data == "undo":
echo &"\x1BcUndo: {board.undoLastMove()}"
2023-10-17 15:08:46 +02:00
continue
2023-10-17 12:08:07 +02:00
if len(data) != 4:
echo "\x1BcError: invalid move"
2023-10-17 12:08:07 +02:00
continue
try:
2023-10-17 15:08:46 +02:00
move = board.makeMove(data[0..1], data[2..3])
2023-10-17 12:08:07 +02:00
except ValueError:
echo &"\x1BcError: {getCurrentExceptionMsg()}"
2023-10-17 12:42:15 +02:00
if move == emptyMove():
echo &"\x1BcError: move is illegal"
else:
echo "\x1Bc"