|
|
@ -109,8 +109,7 @@ const
|
|
|
|
(0, 1, 0, 0, 0), # bishop only
|
|
|
|
(0, 1, 0, 0, 0), # bishop only
|
|
|
|
(0, 2, 0, 0, 0) # 2 knights
|
|
|
|
(0, 2, 0, 0, 0) # 2 knights
|
|
|
|
] ## `InsufficientMaterial` describes the pieces where no checkmate can be
|
|
|
|
] ## `InsufficientMaterial` describes the pieces where no checkmate can be
|
|
|
|
## forced.
|
|
|
|
## forced
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let
|
|
|
|
let
|
|
|
|
PieceChar = {
|
|
|
|
PieceChar = {
|
|
|
@ -144,47 +143,65 @@ let
|
|
|
|
# `FileChar` maps the files of the chessboard to numbers for better
|
|
|
|
# `FileChar` maps the files of the chessboard to numbers for better
|
|
|
|
# conversion.
|
|
|
|
# conversion.
|
|
|
|
|
|
|
|
|
|
|
|
proc checkInsufficientMaterial(board: Board): bool =
|
|
|
|
proc fieldToInd*(file: string, line: int): int =
|
|
|
|
## Checks for combinations of pieces on a `board`, where no checkmate can be
|
|
|
|
## Calculate and return board index from `file` and `line` of a chess board.
|
|
|
|
## forced.
|
|
|
|
## Returns -1 if the `field` was not input correct.
|
|
|
|
## Returns true if no player can force a checkmate to the other.
|
|
|
|
try:
|
|
|
|
var wp = 0
|
|
|
|
return 1+(line+1)*10+FileChar[file]
|
|
|
|
var wn = 0
|
|
|
|
except IndexDefect, ValueError:
|
|
|
|
var wb = 0
|
|
|
|
return -1
|
|
|
|
var wr = 0
|
|
|
|
|
|
|
|
var wq = 0
|
|
|
|
proc fieldToInd*(field: string): int =
|
|
|
|
var bp = 0
|
|
|
|
## Calculate and return board index from `field` of a chess board.
|
|
|
|
var bn = 0
|
|
|
|
## Returns -1 if the `field` was not input correct.
|
|
|
|
var bb = 0
|
|
|
|
try:
|
|
|
|
var br = 0
|
|
|
|
return fieldToInd($field[0], parseInt($field[1]))
|
|
|
|
var bq = 0
|
|
|
|
except IndexDefect, ValueError:
|
|
|
|
for field in board.low..board.high:
|
|
|
|
return -1
|
|
|
|
case board[field]:
|
|
|
|
|
|
|
|
of WPawn:
|
|
|
|
proc indToField*(ind: int): string =
|
|
|
|
wp = wp + 1
|
|
|
|
## Calculate and returns field name from board index `ind`.
|
|
|
|
of BPawn:
|
|
|
|
let line = (int)ind/10-1
|
|
|
|
bp = bp + 1
|
|
|
|
let file_ind = (ind)%%10-1
|
|
|
|
of WKnight:
|
|
|
|
for file, i in FileChar:
|
|
|
|
wn = wn + 1
|
|
|
|
if FileChar[file] == file_ind:
|
|
|
|
of BKnight:
|
|
|
|
return $file & $line
|
|
|
|
bn = bn + 1
|
|
|
|
|
|
|
|
of WBishop:
|
|
|
|
proc getMove*(start: int, dest: int, prom: int, color: Color): Move =
|
|
|
|
wb = wb + 1
|
|
|
|
## Get a move object of the `color` player from `start` to `dest` with an
|
|
|
|
of BBishop:
|
|
|
|
## eventual promition to `prom`.
|
|
|
|
bb = bb + 1
|
|
|
|
var move = Move(start: start, dest: dest, prom: prom * ord(color), color: color)
|
|
|
|
of WRook:
|
|
|
|
if (WKnight > prom or WQueen < prom):
|
|
|
|
wr = wr + 1
|
|
|
|
move.prom = WQueen
|
|
|
|
of BRook:
|
|
|
|
return move
|
|
|
|
br = br + 1
|
|
|
|
|
|
|
|
of WQueen:
|
|
|
|
proc getMove*(start: int, dest: int, color: Color): Move =
|
|
|
|
wq = wq + 1
|
|
|
|
## Get a move object of the `color` player from `start` to `dest` with
|
|
|
|
of BQueen:
|
|
|
|
## automatic promition to queen.
|
|
|
|
bq = bq + 1
|
|
|
|
var move = Move(start: start, dest: dest, prom: WQueen * ord(color), color: color)
|
|
|
|
else:
|
|
|
|
return move
|
|
|
|
continue
|
|
|
|
|
|
|
|
let wpieces: PieceAmount = (wp, wn, wb, wr, wq)
|
|
|
|
proc notationToMove*(notation: string, color: Color): Move =
|
|
|
|
let bpieces: PieceAmount = (bp, bn, bb, br, bq)
|
|
|
|
## Convert and return simplified algebraic chess `notation` to a move object,
|
|
|
|
return (wpieces in InsufficientMaterial) and (bpieces in InsufficientMaterial)
|
|
|
|
## color of player is `color`.
|
|
|
|
|
|
|
|
var move: Move
|
|
|
|
|
|
|
|
var start = fieldToInd(notation[0..1])
|
|
|
|
|
|
|
|
var dest = fieldToInd(notation[2..3])
|
|
|
|
|
|
|
|
move = getMove(start, dest, color)
|
|
|
|
|
|
|
|
if (len(notation) > 4):
|
|
|
|
|
|
|
|
var promStr = $notation[4]
|
|
|
|
|
|
|
|
var prom: int
|
|
|
|
|
|
|
|
case promStr:
|
|
|
|
|
|
|
|
of "Q":
|
|
|
|
|
|
|
|
prom = WQueen * ord(color)
|
|
|
|
|
|
|
|
of "R":
|
|
|
|
|
|
|
|
prom = WRook * ord(color)
|
|
|
|
|
|
|
|
of "B":
|
|
|
|
|
|
|
|
prom = WBishop * ord(color)
|
|
|
|
|
|
|
|
of "N":
|
|
|
|
|
|
|
|
prom = WKnight * ord(color)
|
|
|
|
|
|
|
|
move = getMove(start, dest, prom, color)
|
|
|
|
|
|
|
|
return move
|
|
|
|
|
|
|
|
|
|
|
|
proc initBoard(): Board =
|
|
|
|
proc initBoard(): Board =
|
|
|
|
## Create and return a board with pieces in starting position.
|
|
|
|
## Create and return a board with pieces in starting position.
|
|
|
@ -256,20 +273,6 @@ proc initGame*(board: array[0..63, int], color: Color): Game =
|
|
|
|
fiftyMoveCounter: 0)
|
|
|
|
fiftyMoveCounter: 0)
|
|
|
|
return game
|
|
|
|
return game
|
|
|
|
|
|
|
|
|
|
|
|
proc getMove*(start: int, dest: int, prom: int, color: Color): Move =
|
|
|
|
|
|
|
|
## Get a move object of the `color` player from `start` to `dest` with an
|
|
|
|
|
|
|
|
## eventual promition to `prom`.
|
|
|
|
|
|
|
|
var move = Move(start: start, dest: dest, prom: prom * ord(color), color: color)
|
|
|
|
|
|
|
|
if (WKnight > prom or WQueen < prom):
|
|
|
|
|
|
|
|
move.prom = WQueen
|
|
|
|
|
|
|
|
return move
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
proc getMove*(start: int, dest: int, color: Color): Move =
|
|
|
|
|
|
|
|
## Get a move object of the `color` player from `start` to `dest` with
|
|
|
|
|
|
|
|
## automatic promition to queen.
|
|
|
|
|
|
|
|
var move = Move(start: start, dest: dest, prom: WQueen * ord(color), color: color)
|
|
|
|
|
|
|
|
return move
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
proc echoBoard*(game: Game, color: Color) =
|
|
|
|
proc echoBoard*(game: Game, color: Color) =
|
|
|
|
## Prints out the given `board` with its pieces as characters and line
|
|
|
|
## Prints out the given `board` with its pieces as characters and line
|
|
|
|
## indices from perspecive of `color`.
|
|
|
|
## indices from perspecive of `color`.
|
|
|
@ -293,30 +296,6 @@ proc echoBoard*(game: Game, color: Color) =
|
|
|
|
echo line_str
|
|
|
|
echo line_str
|
|
|
|
echo "a b c d e f g h"
|
|
|
|
echo "a b c d e f g h"
|
|
|
|
|
|
|
|
|
|
|
|
proc fieldToInd*(file: string, line: int): int =
|
|
|
|
|
|
|
|
## Calculate and return board index from `file` and `line` of a chess board.
|
|
|
|
|
|
|
|
## Returns -1 if the `field` was not input correct.
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
return 1+(line+1)*10+FileChar[file]
|
|
|
|
|
|
|
|
except IndexDefect, ValueError:
|
|
|
|
|
|
|
|
return -1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
proc fieldToInd*(field: string): int =
|
|
|
|
|
|
|
|
## Calculate and return board index from `field` of a chess board.
|
|
|
|
|
|
|
|
## Returns -1 if the `field` was not input correct.
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
return fieldToInd($field[0], parseInt($field[1]))
|
|
|
|
|
|
|
|
except IndexDefect, ValueError:
|
|
|
|
|
|
|
|
return -1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
proc indToField*(ind: int): string =
|
|
|
|
|
|
|
|
## Calculate and returns field name from board index `ind`.
|
|
|
|
|
|
|
|
let line = (int)ind/10-1
|
|
|
|
|
|
|
|
let file_ind = (ind)%%10-1
|
|
|
|
|
|
|
|
for file, i in FileChar:
|
|
|
|
|
|
|
|
if FileChar[file] == file_ind:
|
|
|
|
|
|
|
|
return $file & $line
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
proc genCastleRights(moved: Moved): CastleRights =
|
|
|
|
proc genCastleRights(moved: Moved): CastleRights =
|
|
|
|
## Generate and return rights to castle from given `moved`
|
|
|
|
## Generate and return rights to castle from given `moved`
|
|
|
|
let wk = not moved[fieldToInd("e1")] and not moved[fieldToInd("h1")]
|
|
|
|
let wk = not moved[fieldToInd("e1")] and not moved[fieldToInd("h1")]
|
|
|
@ -325,27 +304,84 @@ proc genCastleRights(moved: Moved): CastleRights =
|
|
|
|
let bq = not moved[fieldToInd("e8")] and not moved[fieldToInd("a8")]
|
|
|
|
let bq = not moved[fieldToInd("e8")] and not moved[fieldToInd("a8")]
|
|
|
|
return (wk, wq, bk, bq)
|
|
|
|
return (wk, wq, bk, bq)
|
|
|
|
|
|
|
|
|
|
|
|
proc notationToMove*(notation: string, color: Color): Move =
|
|
|
|
proc genPawnAttackDests(game: Game, field: int, color: Color): seq[int] =
|
|
|
|
## Convert and return simplified algebraic chess `notation` to a move object,
|
|
|
|
## Generate possible attack destinations for a pawn with specific `color`
|
|
|
|
## color of player is `color`.
|
|
|
|
## located at index `field` of `game`.
|
|
|
|
var move: Move
|
|
|
|
## Returns a sequence of possible indices to move to.
|
|
|
|
var start = fieldToInd(notation[0..1])
|
|
|
|
if (not field in game.board.low..game.board.high):
|
|
|
|
var dest = fieldToInd(notation[2..3])
|
|
|
|
return @[]
|
|
|
|
move = getMove(start, dest, color)
|
|
|
|
var res = newSeq[int]()
|
|
|
|
if (len(notation) > 4):
|
|
|
|
var dest: int
|
|
|
|
var promStr = $notation[4]
|
|
|
|
var target: int
|
|
|
|
var prom: int
|
|
|
|
for attacks in Pawn_Moves_White_Attack:
|
|
|
|
case promStr:
|
|
|
|
dest = field + (attacks * ord(color))
|
|
|
|
of "Q":
|
|
|
|
if (not dest in game.board.low..game.board.high):
|
|
|
|
prom = WQueen * ord(color)
|
|
|
|
continue
|
|
|
|
of "R":
|
|
|
|
target = game.board[dest]
|
|
|
|
prom = WRook * ord(color)
|
|
|
|
if (target == 999 or ord(color) * target >= 0):
|
|
|
|
of "B":
|
|
|
|
continue
|
|
|
|
prom = WBishop * ord(color)
|
|
|
|
res.add(dest)
|
|
|
|
of "N":
|
|
|
|
return res
|
|
|
|
prom = WKnight * ord(color)
|
|
|
|
|
|
|
|
move = getMove(start, dest, prom, color)
|
|
|
|
proc genPawnDoubleDests(game: Game, field: int, color: Color): seq[int] =
|
|
|
|
return move
|
|
|
|
## Generate possible double destinations for a pawn with specific `color`
|
|
|
|
|
|
|
|
## located at index `field` of `game`.
|
|
|
|
|
|
|
|
## Returns a sequence of possible indices to move to.
|
|
|
|
|
|
|
|
if (not field in game.board.low..game.board.high):
|
|
|
|
|
|
|
|
return @[]
|
|
|
|
|
|
|
|
var res = newSeq[int]()
|
|
|
|
|
|
|
|
var dest: int
|
|
|
|
|
|
|
|
var target: int
|
|
|
|
|
|
|
|
for doubles in Pawn_Moves_White_Double:
|
|
|
|
|
|
|
|
dest = field + doubles * ord(color)
|
|
|
|
|
|
|
|
if (not dest in game.board.low..game.board.high):
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
target = game.board[dest]
|
|
|
|
|
|
|
|
if (game.moved[field] or (target != 0) or (
|
|
|
|
|
|
|
|
game.board[dest+(S*ord(color))] != 0)):
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
res.add(dest)
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
proc genPawnDests(game: Game, field: int, color: Color): seq[int] =
|
|
|
|
|
|
|
|
## Generate possible destinations for a pawn with specific `color` located at
|
|
|
|
|
|
|
|
## index `field` of `game`.
|
|
|
|
|
|
|
|
## Returns a sequence of possible indices to move to.
|
|
|
|
|
|
|
|
if (not field in game.board.low..game.board.high):
|
|
|
|
|
|
|
|
return @[]
|
|
|
|
|
|
|
|
var res = newSeq[int]()
|
|
|
|
|
|
|
|
var dest: int
|
|
|
|
|
|
|
|
var target: int
|
|
|
|
|
|
|
|
for move in Pawn_Moves_White:
|
|
|
|
|
|
|
|
dest = field + move * ord(color)
|
|
|
|
|
|
|
|
if (not dest in game.board.low..game.board.high):
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
target = game.board[dest]
|
|
|
|
|
|
|
|
if (target != 0 and target != ord(color) * WEnPassant):
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
res.add(dest)
|
|
|
|
|
|
|
|
res.add(game.genPawnAttackDests(field, color))
|
|
|
|
|
|
|
|
res.add(game.genPawnDoubleDests(field, color))
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
proc genKnightDests(game: Game, field: int, color: Color): seq[int] =
|
|
|
|
|
|
|
|
## Generate possible destinations for a knight with specific `color` located
|
|
|
|
|
|
|
|
## at index `field` of `game`.
|
|
|
|
|
|
|
|
## Returns a sequence of possible indices to move to.
|
|
|
|
|
|
|
|
if (not field in game.board.low..game.board.high):
|
|
|
|
|
|
|
|
return @[]
|
|
|
|
|
|
|
|
var res = newSeq[int]()
|
|
|
|
|
|
|
|
var dest: int
|
|
|
|
|
|
|
|
var target: int
|
|
|
|
|
|
|
|
for move in Knight_Moves:
|
|
|
|
|
|
|
|
dest = field + move
|
|
|
|
|
|
|
|
if (not dest in game.board.low..game.board.high):
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
target = game.board[dest]
|
|
|
|
|
|
|
|
if (target == 999 or (ord(color) * target > 0 and ord(color) * target != WEnPassant)):
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
res.add(dest)
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
|
|
|
proc genBishopDests(game: Game, field: int, color: Color): seq[int] =
|
|
|
|
proc genBishopDests(game: Game, field: int, color: Color): seq[int] =
|
|
|
|
## Generate possible destinations for a bishop with specific `color` located
|
|
|
|
## Generate possible destinations for a bishop with specific `color` located
|
|
|
@ -461,85 +497,6 @@ proc genKingDests(game: Game, field: int, color: Color): seq[int] =
|
|
|
|
res.add(game.genKingCastleDest(field, color))
|
|
|
|
res.add(game.genKingCastleDest(field, color))
|
|
|
|
return res
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
|
|
|
proc genKnightDests(game: Game, field: int, color: Color): seq[int] =
|
|
|
|
|
|
|
|
## Generate possible destinations for a knight with specific `color` located
|
|
|
|
|
|
|
|
## at index `field` of `game`.
|
|
|
|
|
|
|
|
## Returns a sequence of possible indices to move to.
|
|
|
|
|
|
|
|
if (not field in game.board.low..game.board.high):
|
|
|
|
|
|
|
|
return @[]
|
|
|
|
|
|
|
|
var res = newSeq[int]()
|
|
|
|
|
|
|
|
var dest: int
|
|
|
|
|
|
|
|
var target: int
|
|
|
|
|
|
|
|
for move in Knight_Moves:
|
|
|
|
|
|
|
|
dest = field + move
|
|
|
|
|
|
|
|
if (not dest in game.board.low..game.board.high):
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
target = game.board[dest]
|
|
|
|
|
|
|
|
if (target == 999 or (ord(color) * target > 0 and ord(color) * target != WEnPassant)):
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
res.add(dest)
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
proc genPawnAttackDests(game: Game, field: int, color: Color): seq[int] =
|
|
|
|
|
|
|
|
## Generate possible attack destinations for a pawn with specific `color`
|
|
|
|
|
|
|
|
## located at index `field` of `game`.
|
|
|
|
|
|
|
|
## Returns a sequence of possible indices to move to.
|
|
|
|
|
|
|
|
if (not field in game.board.low..game.board.high):
|
|
|
|
|
|
|
|
return @[]
|
|
|
|
|
|
|
|
var res = newSeq[int]()
|
|
|
|
|
|
|
|
var dest: int
|
|
|
|
|
|
|
|
var target: int
|
|
|
|
|
|
|
|
for attacks in Pawn_Moves_White_Attack:
|
|
|
|
|
|
|
|
dest = field + (attacks * ord(color))
|
|
|
|
|
|
|
|
if (not dest in game.board.low..game.board.high):
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
target = game.board[dest]
|
|
|
|
|
|
|
|
if (target == 999 or ord(color) * target >= 0):
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
res.add(dest)
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
proc genPawnDoubleDests(game: Game, field: int, color: Color): seq[int] =
|
|
|
|
|
|
|
|
## Generate possible double destinations for a pawn with specific `color`
|
|
|
|
|
|
|
|
## located at index `field` of `game`.
|
|
|
|
|
|
|
|
## Returns a sequence of possible indices to move to.
|
|
|
|
|
|
|
|
if (not field in game.board.low..game.board.high):
|
|
|
|
|
|
|
|
return @[]
|
|
|
|
|
|
|
|
var res = newSeq[int]()
|
|
|
|
|
|
|
|
var dest: int
|
|
|
|
|
|
|
|
var target: int
|
|
|
|
|
|
|
|
for doubles in Pawn_Moves_White_Double:
|
|
|
|
|
|
|
|
dest = field + doubles * ord(color)
|
|
|
|
|
|
|
|
if (not dest in game.board.low..game.board.high):
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
target = game.board[dest]
|
|
|
|
|
|
|
|
if (game.moved[field] or (target != 0) or (
|
|
|
|
|
|
|
|
game.board[dest+(S*ord(color))] != 0)):
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
res.add(dest)
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
proc genPawnDests(game: Game, field: int, color: Color): seq[int] =
|
|
|
|
|
|
|
|
## Generate possible destinations for a pawn with specific `color` located at
|
|
|
|
|
|
|
|
## index `field` of `game`.
|
|
|
|
|
|
|
|
## Returns a sequence of possible indices to move to.
|
|
|
|
|
|
|
|
if (not field in game.board.low..game.board.high):
|
|
|
|
|
|
|
|
return @[]
|
|
|
|
|
|
|
|
var res = newSeq[int]()
|
|
|
|
|
|
|
|
var dest: int
|
|
|
|
|
|
|
|
var target: int
|
|
|
|
|
|
|
|
for move in Pawn_Moves_White:
|
|
|
|
|
|
|
|
dest = field + move * ord(color)
|
|
|
|
|
|
|
|
if (not dest in game.board.low..game.board.high):
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
target = game.board[dest]
|
|
|
|
|
|
|
|
if (target != 0 and target != ord(color) * WEnPassant):
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
res.add(dest)
|
|
|
|
|
|
|
|
res.add(game.genPawnAttackDests(field, color))
|
|
|
|
|
|
|
|
res.add(game.genPawnDoubleDests(field, color))
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
proc pieceOn(game: Game, color: Color, sequence: seq[int],
|
|
|
|
proc pieceOn(game: Game, color: Color, sequence: seq[int],
|
|
|
|
pieceID: int): bool =
|
|
|
|
pieceID: int): bool =
|
|
|
|
## Returns true if the `PieceID` of a given `color` is in `sequence` else
|
|
|
|
## Returns true if the `PieceID` of a given `color` is in `sequence` else
|
|
|
@ -593,11 +550,31 @@ proc moveLeadsToCheck(game: Game, start: int, dest: int,
|
|
|
|
check.uncheckedMove(start, dest)
|
|
|
|
check.uncheckedMove(start, dest)
|
|
|
|
return check.isInCheck(color)
|
|
|
|
return check.isInCheck(color)
|
|
|
|
|
|
|
|
|
|
|
|
proc removeEnPassant(board: var Board, color: Color): void =
|
|
|
|
proc genPawnPromotion(move: Move, color: Color): seq[Move] =
|
|
|
|
## Removes every en passant of given `color` from the `board`.
|
|
|
|
## Generate all possible promotions of a `move` by `color`.
|
|
|
|
for field in board.low..board.high:
|
|
|
|
var promotions = newSeq[Move]()
|
|
|
|
if board[field] == ord(color) * WEnPassant:
|
|
|
|
let start = move.start
|
|
|
|
board[field] = 0
|
|
|
|
let dest = move.dest
|
|
|
|
|
|
|
|
if (90 < dest and dest < 99) or (20 < dest and dest < 29):
|
|
|
|
|
|
|
|
for piece in WKnight..WQueen:
|
|
|
|
|
|
|
|
promotions.add(getMove(start, dest, piece, color))
|
|
|
|
|
|
|
|
return promotions
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
proc genLegalPawnMoves(game: Game, field: int, color: Color): seq[Move] =
|
|
|
|
|
|
|
|
## Generates all legal pawn moves in a `game` starting from `field` for a
|
|
|
|
|
|
|
|
## `color`.
|
|
|
|
|
|
|
|
if game.board[field] != WPawn * ord(color):
|
|
|
|
|
|
|
|
return @[]
|
|
|
|
|
|
|
|
var res = newSeq[Move]()
|
|
|
|
|
|
|
|
var moves = game.genPawnDests(field, color)
|
|
|
|
|
|
|
|
for dest in moves:
|
|
|
|
|
|
|
|
if not game.moveLeadsToCheck(field, dest, color):
|
|
|
|
|
|
|
|
var promotions = genPawnPromotion(getMove(field, dest, color), color)
|
|
|
|
|
|
|
|
if promotions != @[]:
|
|
|
|
|
|
|
|
res.add(promotions)
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
res.add(getMove(field, dest, color))
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
|
|
|
proc genLegalKnightMoves(game: Game, field: int, color: Color): seq[Move] =
|
|
|
|
proc genLegalKnightMoves(game: Game, field: int, color: Color): seq[Move] =
|
|
|
|
## Generates all legal knight moves in a `game` starting from `field` for a
|
|
|
|
## Generates all legal knight moves in a `game` starting from `field` for a
|
|
|
@ -663,32 +640,6 @@ proc genLegalKingMoves(game: Game, field: int, color: Color): seq[Move] =
|
|
|
|
res.add(getMove(field, dest, color))
|
|
|
|
res.add(getMove(field, dest, color))
|
|
|
|
return res
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
|
|
|
proc genPawnPromotion(move: Move, color: Color): seq[Move] =
|
|
|
|
|
|
|
|
## Generate all possible promotions of a `move` by `color`.
|
|
|
|
|
|
|
|
var promotions = newSeq[Move]()
|
|
|
|
|
|
|
|
let start = move.start
|
|
|
|
|
|
|
|
let dest = move.dest
|
|
|
|
|
|
|
|
if (90 < dest and dest < 99) or (20 < dest and dest < 29):
|
|
|
|
|
|
|
|
for piece in WKnight..WQueen:
|
|
|
|
|
|
|
|
promotions.add(getMove(start, dest, piece, color))
|
|
|
|
|
|
|
|
return promotions
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
proc genLegalPawnMoves(game: Game, field: int, color: Color): seq[Move] =
|
|
|
|
|
|
|
|
## Generates all legal pawn moves in a `game` starting from `field` for a
|
|
|
|
|
|
|
|
## `color`.
|
|
|
|
|
|
|
|
if game.board[field] != WPawn * ord(color):
|
|
|
|
|
|
|
|
return @[]
|
|
|
|
|
|
|
|
var res = newSeq[Move]()
|
|
|
|
|
|
|
|
var moves = game.genPawnDests(field, color)
|
|
|
|
|
|
|
|
for dest in moves:
|
|
|
|
|
|
|
|
if not game.moveLeadsToCheck(field, dest, color):
|
|
|
|
|
|
|
|
var promotions = genPawnPromotion(getMove(field, dest, color), color)
|
|
|
|
|
|
|
|
if promotions != @[]:
|
|
|
|
|
|
|
|
res.add(promotions)
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
res.add(getMove(field, dest, color))
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
proc genLegalMoves*(game: Game, field: int, color: Color): seq[Move] =
|
|
|
|
proc genLegalMoves*(game: Game, field: int, color: Color): seq[Move] =
|
|
|
|
## Generates all legal moves in a `game` starting from `field` for a `color`.
|
|
|
|
## Generates all legal moves in a `game` starting from `field` for a `color`.
|
|
|
|
var legal_moves = newSeq[Move]()
|
|
|
|
var legal_moves = newSeq[Move]()
|
|
|
@ -756,6 +707,12 @@ proc castling(game: var Game, kstart: int, dest_kingside: bool,
|
|
|
|
return true
|
|
|
|
return true
|
|
|
|
return false
|
|
|
|
return false
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
proc removeEnPassant(board: var Board, color: Color): void =
|
|
|
|
|
|
|
|
## Removes every en passant of given `color` from the `board`.
|
|
|
|
|
|
|
|
for field in board.low..board.high:
|
|
|
|
|
|
|
|
if board[field] == ord(color) * WEnPassant:
|
|
|
|
|
|
|
|
board[field] = 0
|
|
|
|
|
|
|
|
|
|
|
|
proc checkedMove*(game: var Game, move: Move): bool {.discardable.} =
|
|
|
|
proc checkedMove*(game: var Game, move: Move): bool {.discardable.} =
|
|
|
|
## Tries to make a `move` in a given `game``.
|
|
|
|
## Tries to make a `move` in a given `game``.
|
|
|
|
## This process checks for the legality of the move and performs the switch
|
|
|
|
## This process checks for the legality of the move and performs the switch
|
|
|
@ -833,6 +790,48 @@ proc isDrawClaimable*(game: Game): bool =
|
|
|
|
## Returns true if a draw is claimable by either player.
|
|
|
|
## Returns true if a draw is claimable by either player.
|
|
|
|
return game.threeMoveRep() or game.fiftyMoveRule()
|
|
|
|
return game.threeMoveRep() or game.fiftyMoveRule()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
proc checkInsufficientMaterial(board: Board): bool =
|
|
|
|
|
|
|
|
## Checks for combinations of pieces on a `board`, where no checkmate can be
|
|
|
|
|
|
|
|
## forced.
|
|
|
|
|
|
|
|
## Returns true if no player can force a checkmate to the other.
|
|
|
|
|
|
|
|
var wp = 0
|
|
|
|
|
|
|
|
var wn = 0
|
|
|
|
|
|
|
|
var wb = 0
|
|
|
|
|
|
|
|
var wr = 0
|
|
|
|
|
|
|
|
var wq = 0
|
|
|
|
|
|
|
|
var bp = 0
|
|
|
|
|
|
|
|
var bn = 0
|
|
|
|
|
|
|
|
var bb = 0
|
|
|
|
|
|
|
|
var br = 0
|
|
|
|
|
|
|
|
var bq = 0
|
|
|
|
|
|
|
|
for field in board.low..board.high:
|
|
|
|
|
|
|
|
case board[field]:
|
|
|
|
|
|
|
|
of WPawn:
|
|
|
|
|
|
|
|
wp = wp + 1
|
|
|
|
|
|
|
|
of BPawn:
|
|
|
|
|
|
|
|
bp = bp + 1
|
|
|
|
|
|
|
|
of WKnight:
|
|
|
|
|
|
|
|
wn = wn + 1
|
|
|
|
|
|
|
|
of BKnight:
|
|
|
|
|
|
|
|
bn = bn + 1
|
|
|
|
|
|
|
|
of WBishop:
|
|
|
|
|
|
|
|
wb = wb + 1
|
|
|
|
|
|
|
|
of BBishop:
|
|
|
|
|
|
|
|
bb = bb + 1
|
|
|
|
|
|
|
|
of WRook:
|
|
|
|
|
|
|
|
wr = wr + 1
|
|
|
|
|
|
|
|
of BRook:
|
|
|
|
|
|
|
|
br = br + 1
|
|
|
|
|
|
|
|
of WQueen:
|
|
|
|
|
|
|
|
wq = wq + 1
|
|
|
|
|
|
|
|
of BQueen:
|
|
|
|
|
|
|
|
bq = bq + 1
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
let wpieces: PieceAmount = (wp, wn, wb, wr, wq)
|
|
|
|
|
|
|
|
let bpieces: PieceAmount = (bp, bn, bb, br, bq)
|
|
|
|
|
|
|
|
return (wpieces in InsufficientMaterial) and (bpieces in InsufficientMaterial)
|
|
|
|
|
|
|
|
|
|
|
|
proc isStalemate*(game: Game, color: Color): bool =
|
|
|
|
proc isStalemate*(game: Game, color: Color): bool =
|
|
|
|
## Returns true if the `color` player is stalemate in a `game`.
|
|
|
|
## Returns true if the `color` player is stalemate in a `game`.
|
|
|
|
return (game.hasNoMoves(color) and not game.isInCheck(color)) or
|
|
|
|
return (game.hasNoMoves(color) and not game.isInCheck(color)) or
|
|
|
|