ychess is a chess implementation written in nim.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.3 KiB

  1. from strutils import parseInt
  2. import rdstdin
  3. import ./chess
  4. proc notation_to_move(notation: string, color: Color): Move =
  5. ## Convert simplified algebraic chess `notation` to a move object, color of player is `color`.
  6. try:
  7. var move: Move
  8. var start = field_to_ind(notation[0..1])
  9. var dest = field_to_ind(notation[2..3])
  10. move = get_move(start, dest, color)
  11. if (len(notation) > 4):
  12. var prom_str = $notation[4]
  13. var prom: int
  14. case prom_str:
  15. of "Q":
  16. prom = QueenID * ord(color)
  17. of "R":
  18. prom = RookID * ord(color)
  19. of "B":
  20. prom = BishopID * ord(color)
  21. of "N":
  22. prom = KnightID * ord(color)
  23. move = get_move(start, dest, prom, color)
  24. return move
  25. except IndexError:
  26. var move: Move
  27. return move
  28. proc run_game(): void =
  29. var game = init_game()
  30. game.echo_board(game.to_move)
  31. while not game.is_checkmate(game.to_move):
  32. echo "Make a move"
  33. echo game.to_move
  34. var move = readLine(stdin)
  35. while not game.checked_move(notation_to_move(move, game.to_move)):
  36. move = readLine(stdin)
  37. game.echo_board(game.to_move)
  38. if game.is_checkmate(game.to_move):
  39. echo $game.to_move & " was checkmated"
  40. if game.is_stalemate(game.to_move):
  41. echo "Stalemate"
  42. run_game()