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.

634 lines
22 KiB

  1. import tables
  2. from strutils import parseInt
  3. type
  4. Color* = enum
  5. Black = -1, White = 1
  6. ## Board that saves the pieces
  7. Pieces* = array[0..119, int]
  8. ## Board that checks if pieces moved
  9. Moved* = array[0..119, bool]
  10. ## Game as object of different values
  11. Game* = object
  12. pieces*: Pieces
  13. moved: Moved
  14. to_move*: Color
  15. ## Move as object
  16. Move* = object
  17. start: int
  18. dest: int
  19. color: Color
  20. prom: int
  21. const
  22. # IDs for piece
  23. BlockID* = 999
  24. PawnID* = 1
  25. KnightID* = 2
  26. BishopID* = 3
  27. RookID* = 4
  28. QueenID* = 5
  29. KingID* = 6
  30. EnPassantID* = 7
  31. # IDs that are saved in the array
  32. Block* = BlockID
  33. WPawn* = PawnID
  34. WKnight* = KnightID
  35. WBishop* = BishopID
  36. WRook* = RookID
  37. WQueen* = QueenID
  38. WKing* = KingID
  39. WEnPassant* = EnPassantID
  40. BPawn* = -PawnID
  41. BKnight* = -KnightID
  42. BBishop* = -BishopID
  43. BRook* = -RookID
  44. BQueen* = -QueenID
  45. BKing* = -KingID
  46. BEnPassant* = EnPassantID
  47. # Directions of movement
  48. N = 10
  49. S = -N
  50. W = 1
  51. E = -W
  52. # Movement options for pieces (Bishop/Rook/Queen can repeat in the same direction)
  53. Knight_Moves = [N+N+E, N+N+W, E+E+N, E+E+S, S+S+E, S+S+W, W+W+N, W+W+S]
  54. Bishop_Moves = [N+E, N+W, S+E, S+W]
  55. Rook_Moves = [N, E, S, W]
  56. Queen_Moves = [N, E, S, W, N+E, N+W, S+E, S+W]
  57. King_Moves = [N, E, S, W, N+E, N+W, S+E, S+W]
  58. King_Moves_White_Castle = [E+E, W+W]
  59. Pawn_Moves_White = [N]
  60. Pawn_Moves_White_Double = [N+N]
  61. Pawn_Moves_White_Attack = [N+E, N+W]
  62. var PieceChar = {
  63. 0: " ",
  64. 1: "P",
  65. 2: "N",
  66. 3: "B",
  67. 4: "R",
  68. 5: "Q",
  69. 6: "K",
  70. 7: " ",
  71. -1: "p",
  72. -2: "n",
  73. -3: "b",
  74. -4: "r",
  75. -5: "q",
  76. -6: "k",
  77. -7: " ",
  78. 999: "-"
  79. }.newTable
  80. var FileChar = {
  81. "a": 7,
  82. "b": 6,
  83. "c": 5,
  84. "d": 4,
  85. "e": 3,
  86. "f": 2,
  87. "g": 1,
  88. "h": 0
  89. }.newTable
  90. proc init_board(): Pieces =
  91. ## Create and return a board with pieces in starting position.
  92. let board = [
  93. Block, Block, Block, Block, Block, Block, Block, Block, Block, Block,
  94. Block, Block, Block, Block, Block, Block, Block, Block, Block, Block,
  95. Block, WRook, WKnight, WBishop, WKing, WQueen, WBishop, WKnight, WRook, Block,
  96. Block, WPawn, WPawn, WPawn, WPawn, WPawn, WPawn, WPawn, WPawn, Block,
  97. Block, 0, 0, 0, 0, 0, 0, 0, 0, Block,
  98. Block, 0, 0, 0, 0, 0, 0, 0, 0, Block,
  99. Block, 0, 0, 0, 0, 0, 0, 0, 0, Block,
  100. Block, 0, 0, 0, 0, 0, 0, 0, 0, Block,
  101. Block, BPawn, BPawn, BPawn, BPawn, BPawn, BPawn, BPawn, BPawn, Block,
  102. Block, BRook, BKnight, BBishop, BKing, BQueen, BBishop, BKnight, BRook, Block,
  103. Block, Block, Block, Block, Block, Block, Block, Block, Block, Block,
  104. Block, Block, Block, Block, Block, Block, Block, Block, Block, Block]
  105. return board
  106. proc get_move*(start: int, dest: int, prom: int, color: Color): Move =
  107. ## Get a move object from `start` to `dest` with an eventual promition to `prom`
  108. var move = Move(start: start, dest: dest, prom: prom * ord(color), color: color)
  109. if (KnightID > prom or QueenID < prom):
  110. move.prom = QueenID
  111. return move
  112. proc get_move*(start: int, dest: int, color: Color): Move =
  113. ## Get a move object from `start` to `dest` with automatic promition to `queen`
  114. var move = Move(start: start, dest: dest, prom: QueenID * ord(color), color: color)
  115. return move
  116. proc init_moved(): Moved =
  117. ## Create and return a board of pieces moved.
  118. var moved: Moved
  119. return moved
  120. proc init_game*(): Game =
  121. ## Create and return a Game object.
  122. let game = Game(pieces: init_board(), moved: init_moved(),
  123. to_move: Color.White)
  124. return game
  125. proc get_field(pieces: Pieces, field: int): int =
  126. return pieces[field]
  127. proc set_field(pieces: var Pieces, field: int, val: int): bool {.discardable.} =
  128. if (val in PieceChar):
  129. try:
  130. pieces[field] = val
  131. return true
  132. except Exception:
  133. return false
  134. proc get_field(moved: Moved, field: int): bool =
  135. return moved[field]
  136. proc set_field(moved: var Moved, field: int, val: bool): bool {.discardable.} =
  137. try:
  138. moved[field] = val
  139. return true
  140. except Exception:
  141. return false
  142. proc echo_board*(game: Game, color: Color) =
  143. ## Prints out the given `board` with its pieces as characters and line indices from perspecive of `color`.
  144. var line_str = ""
  145. if (color == Color.Black):
  146. for i in countup(0, len(game.pieces)-1):
  147. if (game.pieces.get_field(i) == 999):
  148. continue
  149. line_str &= PieceChar[game.pieces[i]] & " "
  150. if ((i+2) %% 10 == 0):
  151. line_str &= $((int)((i)/10)-1) & "\n"
  152. echo line_str
  153. echo "h g f e d c b a"
  154. else:
  155. for i in countdown(len(game.pieces)-1, 0):
  156. if (game.pieces.get_field(i) == 999):
  157. continue
  158. line_str &= PieceChar[game.pieces[i]] & " "
  159. if ((i-1) %% 10 == 0):
  160. line_str &= $((int)((i)/10)-1) & "\n"
  161. echo line_str
  162. echo "a b c d e f g h"
  163. proc field_to_ind*(file: string, line: int): int =
  164. ## Calculate board index from `file` and `line` of a chess board.
  165. try:
  166. return 1+(line+1)*10+FileChar[file]
  167. except IndexDefect, ValueError:
  168. return -1
  169. proc field_to_ind*(field: string): int =
  170. ## Calculate board index from `field` of a chess board.
  171. try:
  172. return field_to_ind($field[0], parseInt($field[1]))
  173. except IndexDefect, ValueError:
  174. return -1
  175. proc ind_to_field*(ind: int): string =
  176. ## Calculate field name from board index `ind`.
  177. let line = (int)ind/10-1
  178. let file_ind = (ind)%%10-1
  179. for file, i in FileChar:
  180. if FileChar[file] == file_ind:
  181. return $file & $line
  182. proc gen_bishop_dests(game: Game, field: int, color: Color): seq[int] =
  183. ## Generate possible destinations for a bishop with specific `color` located at index `field` of `game`.
  184. ## Returns a sequence of possible indices to move to.
  185. try:
  186. var res = newSeq[int]()
  187. var dest: int
  188. var target: int
  189. for move in Bishop_Moves:
  190. dest = field+move
  191. target = game.pieces.get_field(dest)
  192. while (target != 999 and (ord(color) * target <= 0) or target == EnPassantID):
  193. res.add(dest)
  194. if (ord(color) * target < 0 and ord(color) * target > -EnPassantID):
  195. break
  196. dest = dest+move
  197. target = game.pieces.get_field(dest)
  198. return res
  199. except IndexDefect:
  200. return @[]
  201. proc gen_rook_dests(game: Game, field: int, color: Color): seq[int] =
  202. ## Generate possible destinations for a rook with specific `color` located at index `field` of `game`.
  203. ## Returns a sequence of possible indices to move to.
  204. try:
  205. var res = newSeq[int]()
  206. var dest: int
  207. var target: int
  208. for move in Rook_Moves:
  209. dest = field+move
  210. target = game.pieces.get_field(dest)
  211. while (target != 999 and (ord(color) * target <= 0) or target == EnPassantID):
  212. res.add(dest)
  213. if (ord(color) * target < 0 and ord(color) * target > -EnPassantID):
  214. break
  215. dest = dest+move
  216. target = game.pieces.get_field(dest)
  217. return res
  218. except IndexDefect:
  219. return @[]
  220. proc gen_queen_dests(game: Game, field: int, color: Color): seq[int] =
  221. ## Generate possible destinations for a queen with specific `color` located at index `field` of `game`.
  222. ## Returns a sequence of possible indices to move to.
  223. try:
  224. var res = newSeq[int]()
  225. var dest: int
  226. var target: int
  227. for move in Queen_Moves:
  228. dest = field+move
  229. target = game.pieces.get_field(dest)
  230. while (target != 999 and (ord(color) * target <= 0) or target == EnPassantID):
  231. res.add(dest)
  232. if (ord(color) * target < 0 and ord(color) * target > -EnPassantID):
  233. break
  234. dest = dest+move
  235. target = game.pieces.get_field(dest)
  236. return res
  237. except IndexDefect:
  238. return @[]
  239. proc gen_king_castle_dest(game: Game, field: int, color: Color): seq[int] =
  240. ## Generate possible castle destinations for a king with specific `color` located at index `field` of `game`
  241. ## Returns a sequence of possible indices to move to.
  242. try:
  243. var res = newSeq[int]()
  244. var dest: int
  245. var target: int
  246. var half_dest: int
  247. var half_target: int
  248. for castle in King_Moves_White_Castle:
  249. dest = field + castle
  250. target = game.pieces.get_field(dest)
  251. half_dest = field + (int) castle/2
  252. half_target = game.pieces.get_field(half_dest)
  253. if (target == 999 or (target != 0)):
  254. continue
  255. if (half_target == 999 or (half_target != 0)):
  256. continue
  257. res.add(dest)
  258. return res
  259. except IndexDefect:
  260. return @[]
  261. proc gen_king_dests(game: Game, field: int, color: Color): seq[int] =
  262. ## Generate possible destinations for a king with specific `color` located at index `field` of `game`.
  263. ## Returns a sequence of possible indices to move to.
  264. try:
  265. var res = newSeq[int]()
  266. var dest: int
  267. var target: int
  268. for move in King_Moves:
  269. dest = field + move
  270. target = game.pieces.get_field(dest)
  271. if (target == 999 or (ord(color) * target > 0 and ord(color) * target != EnPassantID)):
  272. continue
  273. res.add(dest)
  274. res.add(game.gen_king_castle_dest(field, color))
  275. return res
  276. except IndexDefect:
  277. return @[]
  278. proc gen_knight_dests(game: Game, field: int, color: Color): seq[int] =
  279. ## Generate possible destinations for a knight with specific `color` located at index `field` of `game`.
  280. ## Returns a sequence of possible indices to move to.
  281. try:
  282. var res = newSeq[int]()
  283. var dest: int
  284. var target: int
  285. for move in Knight_Moves:
  286. dest = field + move
  287. target = game.pieces.get_field(dest)
  288. if (target == 999 or (ord(color) * target > 0 and ord(color) * target != EnPassantID)):
  289. continue
  290. res.add(dest)
  291. return res
  292. except IndexDefect:
  293. return @[]
  294. proc gen_pawn_attack_dests(game: Game, field: int, color: Color): seq[int] =
  295. ## Generate possible attack destinations for a pawn with specific `color` located at index `field` of `game`.
  296. ## Returns a sequence of possible indices to move to.
  297. try:
  298. var res = newSeq[int]()
  299. var dest: int
  300. var target: int
  301. for attacks in Pawn_Moves_White_Attack:
  302. dest = field + attacks * ord(color)
  303. target = game.pieces.get_field(dest)
  304. if (target == 999 or ord(color) * target >= 0):
  305. continue
  306. res.add(dest)
  307. return res
  308. except IndexDefect:
  309. return @[]
  310. proc gen_pawn_double_dests(game: Game, field: int, color: Color): seq[int] =
  311. ## Generate possible double destinations for a pawn with specific `color` located at index `field` of `game`.
  312. ## Returns a sequence of possible indices to move to.
  313. try:
  314. var res = newSeq[int]()
  315. var dest: int
  316. var target: int
  317. for doubles in Pawn_Moves_White_Double:
  318. dest = field + doubles * ord(color)
  319. target = game.pieces.get_field(dest)
  320. if (game.moved.get_field(field) or (target != 0) or (game.pieces.get_field(dest+(S*ord(color))) != 0)):
  321. continue
  322. res.add(dest)
  323. return res
  324. except IndexDefect:
  325. return @[]
  326. proc gen_pawn_dests(game: Game, field: int, color: Color): seq[int] =
  327. ## Generate possible destinations for a pawn with specific `color` located at index `field` of `game`.
  328. ## Returns a sequence of possible indices to move to.
  329. try:
  330. var res = newSeq[int]()
  331. var dest: int
  332. var target: int
  333. for move in Pawn_Moves_White:
  334. dest = field + move * ord(color)
  335. target = game.pieces.get_field(dest)
  336. if (target != 0 and target != ord(color) * EnPassantID):
  337. continue
  338. res.add(dest)
  339. res.add(game.gen_pawn_attack_dests(field, color))
  340. res.add(game.gen_pawn_double_dests(field, color))
  341. return res
  342. except IndexDefect:
  343. return @[]
  344. proc piece_on(game: Game, color: Color, sequence: seq[int],
  345. pieceID: int): bool =
  346. ## Check if a piece with `pieceID` of a given `color` is in a field described in a `sequence` in a `game`.
  347. for check in sequence:
  348. if game.pieces.get_field(check) == ord(color) * -1 * pieceID:
  349. return true
  350. return false
  351. proc is_attacked(game: Game, position: int, color: Color): bool =
  352. ## Check if a field is attacked by the opposite of `color` in a `game`.
  353. var attacked = false
  354. attacked = attacked or game.piece_on(color, game.gen_pawn_attack_dests(position,
  355. color), PawnID)
  356. attacked = attacked or game.piece_on(color, game.gen_queen_dests(position,
  357. color), QueenID)
  358. attacked = attacked or game.piece_on(color, game.gen_king_dests(position,
  359. color), KingID)
  360. attacked = attacked or game.piece_on(color, game.gen_rook_dests(position,
  361. color), RookID)
  362. attacked = attacked or game.piece_on(color, game.gen_bishop_dests(position,
  363. color), BishopID)
  364. attacked = attacked or game.piece_on(color, game.gen_knight_dests(position,
  365. color), KnightID)
  366. return attacked
  367. proc is_in_check*(game: Game, color: Color): bool =
  368. ## Check if the King of a given `color` is in check in a `game`.
  369. var king_pos: int
  370. for i in countup(0, game.pieces.high):
  371. if game.pieces.get_field(i) == ord(color) * KingID:
  372. king_pos = i
  373. return game.is_attacked(king_pos, color)
  374. proc unchecked_move(game: var Game, start: int, dest: int): bool {.discardable.} =
  375. ## Moves a piece if possible from `start` position to `dest` position.
  376. ## Doesnt check boundaries, checks, movement.
  377. ## returns true if the piece moved, else false
  378. try:
  379. let piece = game.pieces.get_field(start)
  380. if game.pieces.set_field(start, 0):
  381. if game.pieces.set_field(dest, piece):
  382. game.moved.set_field(start, true)
  383. game.moved.set_field(dest, true)
  384. return true
  385. else:
  386. game.pieces.set_field(start, piece)
  387. except IndexDefect, ValueError:
  388. return false
  389. proc move_leads_to_check(game: Game, start: int, dest: int,
  390. color: Color): bool =
  391. ## Checks in a `game` if a move from `start` to `dest` puts the `color` king in check.
  392. var check = game
  393. check.unchecked_move(start, dest)
  394. return check.is_in_check(color)
  395. proc remove_en_passant(pieces: var Pieces, color: Color): void =
  396. ## Removes every en passant of given `color` from the `game`.
  397. for field in pieces.low..pieces.high:
  398. if pieces.get_field(field) == ord(color) * EnPassantID:
  399. pieces.set_field(field,0)
  400. proc gen_legal_knight_moves(game: Game, field: int, color: Color): seq[Move] =
  401. ## Generates all legal knight moves starting from `field` in a `game` for a `color`.
  402. if game.pieces.get_field(field) != KnightID * ord(color):
  403. return @[]
  404. var res = newSeq[Move]()
  405. var moves = game.gen_knight_dests(field, color)
  406. for dest in moves:
  407. if not game.move_leads_to_check(field, dest, color):
  408. res.add(get_move(field, dest, color))
  409. return res
  410. proc gen_legal_bishop_moves(game: Game, field: int, color: Color): seq[Move] =
  411. ## Generates all legal bishop moves starting from `field` in a `game` for a `color`.
  412. if game.pieces.get_field(field) != BishopID * ord(color):
  413. return @[]
  414. var res = newSeq[Move]()
  415. var moves = game.gen_bishop_dests(field, color)
  416. for dest in moves:
  417. if not game.move_leads_to_check(field, dest, color):
  418. res.add(get_move(field, dest, color))
  419. return res
  420. proc gen_legal_rook_moves(game: Game, field: int, color: Color): seq[Move] =
  421. ## Generates all legal rook moves starting from `field` in a `game` for a `color`.
  422. if game.pieces.get_field(field) != RookID * ord(color):
  423. return @[]
  424. var res = newSeq[Move]()
  425. var moves = game.gen_rook_dests(field, color)
  426. for dest in moves:
  427. if not game.move_leads_to_check(field, dest, color):
  428. res.add(get_move(field, dest, color))
  429. return res
  430. proc gen_legal_queen_moves(game: Game, field: int, color: Color): seq[Move] =
  431. ## Generates all legal queen moves starting from `field` in a `game` for a `color`.
  432. if game.pieces.get_field(field) != QueenID * ord(color):
  433. return @[]
  434. var res = newSeq[Move]()
  435. var moves = game.gen_queen_dests(field, color)
  436. for dest in moves:
  437. if not game.move_leads_to_check(field, dest, color):
  438. res.add(get_move(field, dest, color))
  439. return res
  440. proc gen_legal_king_moves(game: Game, field: int, color: Color): seq[Move] =
  441. ## Generates all legal king moves starting from `field` in a `game` for a `color`.
  442. if game.pieces.get_field(field) != KingID * ord(color):
  443. return @[]
  444. var res = newSeq[Move]()
  445. var moves = game.gen_king_dests(field, color)
  446. for dest in moves:
  447. if field - dest == W+W and game.is_attacked(dest+W, color):
  448. continue
  449. if field - dest == E+E and game.is_attacked(dest+E, color):
  450. continue
  451. if not game.move_leads_to_check(field, dest, color):
  452. res.add(get_move(field, dest, color))
  453. return res
  454. proc gen_pawn_promotion(move: Move, color: Color): seq[Move] =
  455. ## Generate all possible promotions of a `move` by `color`.
  456. var promotions = newSeq[Move]()
  457. let start = move.start
  458. let dest = move.dest
  459. if (90 < dest and dest < 99) or (20 < dest and dest < 29):
  460. for piece in KnightID..QueenID:
  461. promotions.add(get_move(start, dest, piece, color))
  462. return promotions
  463. proc gen_legal_pawn_moves(game: Game, field: int, color: Color): seq[Move] =
  464. ## Generates all legal pawn moves starting from `field` in a `game` for a `color`.
  465. if game.pieces.get_field(field) != PawnID * ord(color):
  466. return @[]
  467. var res = newSeq[Move]()
  468. var moves = game.gen_pawn_dests(field, color)
  469. for dest in moves:
  470. if not game.move_leads_to_check(field, dest, color):
  471. var promotions = gen_pawn_promotion(get_move(field, dest, color), color)
  472. if promotions != @[]:
  473. res.add(promotions)
  474. else:
  475. res.add(get_move(field, dest, color))
  476. return res
  477. proc gen_legal_moves*(game: Game, field: int, color: Color): seq[Move] =
  478. ## Generates all legal moves starting from `field` in a `game` for a `color`.
  479. var legal_moves = newSeq[Move]()
  480. var target = ord(color) * game.pieces.get_field(field)
  481. if 0 < target and target < EnPassantID:
  482. legal_moves = case target:
  483. of PawnID:
  484. game.gen_legal_pawn_moves(field, color)
  485. of KnightID:
  486. game.gen_legal_knight_moves(field, color)
  487. of BishopID:
  488. game.gen_legal_bishop_moves(field, color)
  489. of RookID:
  490. game.gen_legal_rook_moves(field, color)
  491. of QueenID:
  492. game.gen_legal_queen_moves(field, color)
  493. of KingID:
  494. game.gen_legal_king_moves(field, color)
  495. else:
  496. @[]
  497. return legal_moves
  498. proc gen_legal_moves*(game: Game, color: Color): seq[Move] =
  499. ## Generates all legal moves in a `game` for a `color`.
  500. var legal_moves = newSeq[Move]()
  501. for field in game.pieces.low..game.pieces.high:
  502. legal_moves.add(game.gen_legal_moves(field, color))
  503. return legal_moves
  504. proc castling(game: var Game, kstart: int, dest_kingside: bool,
  505. color: Color): bool {.discardable.} =
  506. ## Tries to castle in a given `game` with the king of a given `color` from `start`.
  507. ## `dest_kingside` for kingside castling, else castling is queenside.
  508. ## This process checks for the legality of the move and performs the switch of `game.to_move`
  509. try:
  510. if game.to_move != color:
  511. return false
  512. var kdest = kstart
  513. var rstart: int
  514. var rdest: int
  515. if (dest_kingside):
  516. kdest = kstart + (E+E)
  517. rstart = kstart + (E+E+E)
  518. rdest = rstart + (W+W)
  519. else:
  520. rstart = kstart + (W+W+W+W)
  521. rdest = rstart + (E+E+E)
  522. kdest = kstart + (W+W)
  523. if not game.moved.get_field(kstart) and not game.moved.get_field(rstart):
  524. var check = false
  525. if (dest_kingside):
  526. check = check or game.is_attacked(kstart, color)
  527. check = check or game.is_attacked(kstart+(E), color)
  528. check = check or game.is_attacked(kstart+(E+E), color)
  529. else:
  530. check = check or game.is_attacked(kstart, color)
  531. check = check or game.is_attacked(kstart+(W), color)
  532. check = check or game.is_attacked(kstart+(W+W), color)
  533. if check:
  534. return false
  535. game.unchecked_move(kstart, kdest)
  536. game.unchecked_move(rstart, rdest)
  537. return true
  538. return false
  539. except IndexDefect, ValueError:
  540. return false
  541. proc checked_move*(game: var Game, move: Move): bool {.discardable.} =
  542. ## Tries to make a move in a given `game` with the piece of a given `color` from `start` to `dest`.
  543. ## This process checks for the legality of the move and performs the switch of `game.to_move`
  544. try:
  545. let start = move.start
  546. let dest = move.dest
  547. let color = move.color
  548. let prom = move.prom
  549. if game.to_move != color:
  550. return false
  551. var sequence = newSeq[Move]()
  552. let piece = game.pieces.get_field(start)
  553. var create_en_passant = false
  554. var captured_en_passant = false
  555. var move: Move
  556. move = get_move(start, dest, color)
  557. if (piece == PawnID * ord(color)):
  558. create_en_passant = dest in game.gen_pawn_double_dests(start,color)
  559. captured_en_passant = (game.pieces.get_field(dest) == -1 * ord(color) * EnPassantID)
  560. sequence.add(game.gen_legal_moves(start, color))
  561. if (move in sequence):
  562. game.pieces.remove_en_passant(color)
  563. if (piece == KingID * ord(color) and (start - dest == (W+W))):
  564. game.castling(start, true, color)
  565. elif (piece == KingID * ord(color) and (start - dest == (E+E))):
  566. game.castling(start, false, color)
  567. else:
  568. game.unchecked_move(start, dest)
  569. game.to_move = Color(ord(game.to_move)*(-1))
  570. if create_en_passant:
  571. game.pieces.set_field(dest-(N*ord(color)),EnPassantID * ord(color))
  572. if captured_en_passant:
  573. game.pieces.set_field(dest-(N*ord(color)),0)
  574. if ((90 < dest and dest < 99) or (20 < dest and dest < 29)) and game.pieces.get_field(dest) == PawnID * ord(color):
  575. game.pieces.set_field(dest, prom)
  576. return true
  577. except IndexDefect, ValueError:
  578. return false
  579. proc has_no_moves(game: Game, color: Color): bool =
  580. ## Checks if a player of a given `color` has no legal moves in a `game`.
  581. return (game.gen_legal_moves(color) == @[])
  582. proc is_checkmate*(game: Game, color: Color): bool =
  583. ## Checks if a player of a given `color` in a `game` is checkmate.
  584. return game.has_no_moves(color) and game.is_in_check(color)
  585. proc is_stalemate*(game: Game, color: Color): bool =
  586. ## Checks if a player of a given `color` in a `game` is stalemate.
  587. return game.has_no_moves(color) and not game.is_in_check(color)