chess: refactoring genDests

genDests procs where copy and pasted, now they
are done by a common function.
Single line functions where also removed, due
to them being to trivial
master
TiynGER 4 years ago
parent 087da7d3f1
commit ee7d5a0a9d

@ -39,41 +39,41 @@ type
Pieces = array[10, int] Pieces = array[10, int]
const const
Block = 999 ## \ Block = 999 ## \
## `Block` is the value assigned to empty blocked fields in a board. ## `Block` is the value assigned to empty blocked fields in a board.
WPawn* = 1 WPawn* = 1
## `WPawn` is the value assigned to a square in a board with a white pawn. ## `WPawn` is the value assigned to a square in a board with a white pawn.
WKnight* = 2 ## \ WKnight* = 2 ## \
## `WKnight` is the value assigned to a square in a board with a white ## `WKnight` is the value assigned to a square in a board with a white
## knight. ## knight.
WBishop* = 3 ## \ WBishop* = 3 ## \
## `WBishop` is the value assigned to a square in a board with a white ## `WBishop` is the value assigned to a square in a board with a white
## bishop. ## bishop.
WRook* = 4 ## \ WRook* = 4 ## \
## `WRook` is the value assigned to a square in a board with a white rook. ## `WRook` is the value assigned to a square in a board with a white rook.
WQueen* = 5 ## \ WQueen* = 5 ## \
## `WQueen` is the value assigned to a square in a board with a white ## `WQueen` is the value assigned to a square in a board with a white
## queen. ## queen.
WKing* = 6 ## \ WKing* = 6 ## \
## `WKing` is the value assigned to a square in a board with a white king. ## `WKing` is the value assigned to a square in a board with a white king.
WEnPassant = 7 ## \ WEnPassant = 7 ## \
## `WEnPassant` is assigned to a square in a board with an invisible white ## `WEnPassant` is assigned to a square in a board with an invisible white
## en passant pawn. ## en passant pawn.
BPawn* = -WPawn ## \ BPawn* = -WPawn ## \
## `BPawn` is the value assigned to a square in a board with a black pawn. ## `BPawn` is the value assigned to a square in a board with a black pawn.
BKnight* = -WKnight ## \ BKnight* = -WKnight ## \
## `BKnight` is the value assigned to a square in a board with a black\ ## `BKnight` is the value assigned to a square in a board with a black\
## knight. ## knight.
BBishop* = -WBishop ## \ BBishop* = -WBishop ## \
## `BBishop` is the value assigned to a square in a board with a black\ ## `BBishop` is the value assigned to a square in a board with a black\
## bishop. ## bishop.
BRook* = -WRook ## \ BRook* = -WRook ## \
## `BRook` is the value assigned to a square in a board with a black rook. ## `BRook` is the value assigned to a square in a board with a black rook.
BQueen* = -WQueen ## \ BQueen* = -WQueen ## \
## `BQueen` is the value assigned to a square in a board with a black queen. ## `BQueen` is the value assigned to a square in a board with a black queen.
BKing* = -WKing ## \ BKing* = -WKing ## \
## `BKing` is the value assigned to a square in a board with a black king. ## `BKing` is the value assigned to a square in a board with a black king.
BEnPassant = -WEnPassant ## \ BEnPassant = -WEnPassant ## \
## `BEnPassant` is assigned to a square in a board with an invisible black ## `BEnPassant` is assigned to a square in a board with an invisible black
## en passant pawn. ## en passant pawn.
N = 10 ## `N` describes a move a field up the board from whites perspective. N = 10 ## `N` describes a move a field up the board from whites perspective.
@ -81,34 +81,34 @@ const
E = 1 ## `E` describes a move a field to the right from whites perspective. E = 1 ## `E` describes a move a field to the right from whites perspective.
W = -E ## `W` describes a move a field to the left from whites perspective. W = -E ## `W` describes a move a field to the left from whites perspective.
# Directions for the pieces. Special moves are in separate arrays. # Directions for the pieces. Special moves are in separate arrays.
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] ## \ 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] ## \
## `Knight_Moves` describes the possible knight moves. ## `Knight_Moves` describes the possible knight moves.
Bishop_Moves = [N+E, N+W, S+E, S+W] ## \ Bishop_Moves = @[N+E, N+W, S+E, S+W] ## \
## `Bishop_Moves` describes the possible 1 field distance bishop moves. ## `Bishop_Moves` describes the possible 1 field distance bishop moves.
Rook_Moves = [N, E, S, W] ## \ Rook_Moves = @[N, E, S, W] ## \
## `Rook_Moves` describes the possible 1 field distance rook moves. ## `Rook_Moves` describes the possible 1 field distance rook moves.
Queen_Moves = [N, E, S, W, N+E, N+W, S+E, S+W] ## \ Queen_Moves = @[N, E, S, W, N+E, N+W, S+E, S+W] ## \
## `Queen_Moves` describes the possible 1 field distance queen moves. ## `Queen_Moves` describes the possible 1 field distance queen moves.
King_Moves = [N, E, S, W, N+E, N+W, S+E, S+W] ## \ King_Moves = @[N, E, S, W, N+E, N+W, S+E, S+W] ## \
## `King_Moves` describes the possible 1 field distance king moves. ## `King_Moves` describes the possible 1 field distance king moves.
King_Moves_White_Castle = [E+E, W+W] ## \ King_Moves_White_Castle = @[E+E, W+W] ## \
## `King_Moves` describes the possible king moves for castling. ## `King_Moves` describes the possible king moves for castling.
Pawn_Moves_White = [N] ## \ Pawn_Moves_White = @[N] ## \
## `Pawn_Moves_White` describes the possible 1 field distance pawn moves ## `Pawn_Moves_White` describes the possible 1 field distance pawn moves
## from whites perspective that are not attacks. ## from whites perspective that are not attacks.
Pawn_Moves_White_Double = [N+N] ## \ Pawn_Moves_White_Double = @[N+N] ## \
## `Pawn_Moves_White_Double` describes the possible pawn 2 field distance ## `Pawn_Moves_White_Double` describes the possible pawn 2 field distance
## moves from whites perspective. ## moves from whites perspective.
Pawn_Moves_White_Attack = [N+E, N+W] ## \ Pawn_Moves_White_Attack = @[N+E, N+W] ## \
## `Pawn_Moves_White` describes the possible 1 field distance pawn moves ## `Pawn_Moves_White` describes the possible 1 field distance pawn moves
## from whites perspective that are ttacks. ## from whites perspective that are ttacks.
InsufficientMaterial: array[4, PieceAmount] = [ InsufficientMaterial: array[4, PieceAmount] = [
(0, 0, 0, 0, 0), # only kings (0, 0, 0, 0, 0), # only kings
(0, 0, 1, 0, 0), # bishop only (0, 0, 1, 0, 0), # bishop only
(0, 1, 0, 0, 0), # knight only (0, 1, 0, 0, 0), # knight 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 = {
@ -264,7 +264,6 @@ proc initChess(board: array[0..63, int], color: Color): Chess =
## `board` describes the pieces, `color` the color that is about to move. ## `board` describes the pieces, `color` the color that is about to move.
let board = initBoard(board) let board = initBoard(board)
let compare = initBoard() let compare = initBoard()
var same_piece: bool
var wk: bool var wk: bool
var wq: bool var wq: bool
var bk: bool var bk: bool
@ -279,8 +278,6 @@ proc initChess(board: array[0..63, int], color: Color): Chess =
bq = true bq = true
if (board[fieldToInd("h8")] == compare[fieldToInd("h8")]): if (board[fieldToInd("h8")] == compare[fieldToInd("h8")]):
bk = true bk = true
for ind in board.low..board.high:
same_piece = (board[ind] != compare[ind])
let chess = Chess(board: board, let chess = Chess(board: board,
to_move: color, castleRights: (wk, wq, bk, bq)) to_move: color, castleRights: (wk, wq, bk, bq))
return chess return chess
@ -288,7 +285,7 @@ proc initChess(board: array[0..63, int], color: Color): Chess =
proc echoBoard*(chess: Chess, color: Color) = proc echoBoard*(chess: Chess, 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`.
var line_str = "" var line_str: string
if (color == Color.Black): if (color == Color.Black):
for i in 0..len(chess.board)-1: for i in 0..len(chess.board)-1:
if (chess.board[i] == Block): if (chess.board[i] == Block):
@ -299,7 +296,7 @@ proc echoBoard*(chess: Chess, color: Color) =
echo line_str echo line_str
echo "h g f e d c b a" echo "h g f e d c b a"
else: else:
for i in len(chess.board)-1..0: for i in countdown(len(chess.board)-1, 0):
if (chess.board[i] == Block): if (chess.board[i] == Block):
continue continue
line_str &= PieceChar[chess.board[i]] & " " line_str &= PieceChar[chess.board[i]] & " "
@ -338,8 +335,6 @@ proc genPawnDoubleDests(chess: Chess, field: int, color: Color): seq[int] =
var target: int var target: int
for doubles in Pawn_Moves_White_Double: for doubles in Pawn_Moves_White_Double:
dest = field + doubles * ord(color) dest = field + doubles * ord(color)
if (not dest in chess.board.low..chess.board.high):
continue
target = chess.board[dest] target = chess.board[dest]
if ((target != 0) or ( if ((target != 0) or (
chess.board[dest + (S * ord(color))] != 0)): chess.board[dest + (S * ord(color))] != 0)):
@ -391,16 +386,16 @@ proc genKnightDests(chess: Chess, field: int, color: Color): seq[int] =
res.add(dest) res.add(dest)
return res return res
proc genBishopDests(chess: Chess, field: int, color: Color): seq[int] = proc genSlidePieceDests(chess: Chess, field: int, color: Color, moves: seq[int]): seq[int] =
## Generate possible destinations for a bishop with specific `color` located ## Generate possible destinations for a piece with `moves` and specific `color`
## at index `field` of `chess`. ## located at index `field` of `chess`.
## Returns a sequence of possible indices to move to. ## Returns a sequence of possible indices to move to.
if (not field in chess.board.low..chess.board.high): if (not field in chess.board.low..chess.board.high):
return @[] return @[]
var res = newSeq[int]() var res = newSeq[int]()
var dest: int var dest: int
var target: int var target: int
for move in Bishop_Moves: for move in moves:
dest = field + move dest = field + move
if (not dest in chess.board.low..chess.board.high): if (not dest in chess.board.low..chess.board.high):
continue continue
@ -414,51 +409,23 @@ proc genBishopDests(chess: Chess, field: int, color: Color): seq[int] =
target = chess.board[dest] target = chess.board[dest]
return res return res
proc genBishopDests(chess: Chess, field: int, color: Color): seq[int] =
## Generate possible destinations for a bishop with specific `color` located
## at index `field` of `chess`.
## Returns a sequence of possible indices to move to.
return genSlidePieceDests(chess, field, color, Bishop_Moves)
proc genRookDests(chess: Chess, field: int, color: Color): seq[int] = proc genRookDests(chess: Chess, field: int, color: Color): seq[int] =
## Generate possible destinations for a rook with specific `color` located at ## Generate possible destinations for a rook with specific `color` located at
## index `field` of `chess`. ## index `field` of `chess`.
## Returns a sequence of possible indices to move to. ## Returns a sequence of possible indices to move to.
if (not field in chess.board.low..chess.board.high): return genSlidePieceDests(chess, field, color, Rook_Moves)
return @[]
var res = newSeq[int]()
var dest: int
var target: int
for move in Rook_Moves:
dest = field + move
if (not dest in chess.board.low..chess.board.high):
continue
target = chess.board[dest]
while (target != Block and (ord(color) * target <= 0) or abs(target) ==
WEnPassant):
res.add(dest)
if (ord(color) * target < 0 and ord(color) * target > BEnPassant):
break
dest = dest + move
target = chess.board[dest]
return res
proc genQueenDests(chess: Chess, field: int, color: Color): seq[int] = proc genQueenDests(chess: Chess, field: int, color: Color): seq[int] =
## Generate possible destinations for a queen with specific `color` located ## Generate possible destinations for a queen with specific `color` located
## at index `field` of `chess`. ## at index `field` of `chess`.
## Returns a sequence of possible indices to move to. ## Returns a sequence of possible indices to move to.
if (not field in chess.board.low..chess.board.high): return genSlidePieceDests(chess, field, color, Queen_Moves)
return @[]
var res = newSeq[int]()
var dest: int
var target: int
for move in Queen_Moves:
dest = field + move
if (not dest in chess.board.low..chess.board.high):
continue
target = chess.board[dest]
while (target != Block and (ord(color) * target <= 0) or abs(target) ==
WEnPassant):
res.add(dest)
if (ord(color) * target < 0 and ord(color) * target > BEnPassant):
break
dest = dest + move
target = chess.board[dest]
return res
proc genKingCastleDest(chess: Chess, field: int, color: Color): seq[int] = proc genKingCastleDest(chess: Chess, field: int, color: Color): seq[int] =
## Generate possible castle destinations for a king with specific `color` ## Generate possible castle destinations for a king with specific `color`
@ -791,13 +758,9 @@ proc checkedMove*(chess: var Chess, move: Move): bool {.discardable.} =
chess.fiftyMoveCounter = 0 chess.fiftyMoveCounter = 0
return true return true
proc hasNoMoves(chess: Chess, color: Color): bool =
## Returns true if the `color` player has no legal moves in a `chess`.
return (chess.genLegalMoves(color) == @[])
proc isCheckmate*(chess: Chess, color: Color): bool = proc isCheckmate*(chess: Chess, color: Color): bool =
## Returns true if the `color` player is checkmate in a `chess`. ## Returns true if the `color` player is checkmate in a `chess`.
return chess.hasNoMoves(color) and chess.isInCheck(color) return chess.genLegalMoves(color) == @[] and chess.isInCheck(color)
proc threeMoveRep(chess: Chess): bool = proc threeMoveRep(chess: Chess): bool =
## Returns true if a 3-fold repitition happened on the last move of the ## Returns true if a 3-fold repitition happened on the last move of the
@ -813,13 +776,9 @@ proc threeMoveRep(chess: Chess): bool =
reps = reps + 1 reps = reps + 1
return reps >= 3 return reps >= 3
proc fiftyMoveRule(chess: Chess): bool =
## Returns true if a draw can be claimed by the 50 move rule in a `chess`.
return chess.fiftyMoveCounter >= 100
proc isDrawClaimable*(chess: Chess): bool = proc isDrawClaimable*(chess: Chess): bool =
## Returns true if a draw is claimable by either player. ## Returns true if a draw is claimable by either player.
return chess.threeMoveRep() or chess.fiftyMoveRule() return chess.threeMoveRep() or chess.fiftyMoveCounter >= 100
proc checkInsufficientMaterial(board: Board): bool = proc checkInsufficientMaterial(board: Board): bool =
## Checks for combinations of pieces on a `board`, where no checkmate can be ## Checks for combinations of pieces on a `board`, where no checkmate can be
@ -843,5 +802,5 @@ proc checkInsufficientMaterial(board: Board): bool =
proc isStalemate*(chess: Chess, color: Color): bool = proc isStalemate*(chess: Chess, color: Color): bool =
## Returns true if the `color` player is stalemate in a `chess`. ## Returns true if the `color` player is stalemate in a `chess`.
return (chess.hasNoMoves(color) and not chess.isInCheck(color)) or return (chess.genLegalMoves(color) == @[] and not chess.isInCheck(color)) or
chess.board.checkInsufficientMaterial() chess.board.checkInsufficientMaterial()

Loading…
Cancel
Save