|
|
@ -6,7 +6,7 @@ type
|
|
|
|
## `Color` describes the possible color of players.
|
|
|
|
## `Color` describes the possible color of players.
|
|
|
|
Black = -1,
|
|
|
|
Black = -1,
|
|
|
|
White = 1
|
|
|
|
White = 1
|
|
|
|
Board* = array[0..119, int] ## \
|
|
|
|
Board = array[0..119, int] ## \
|
|
|
|
## `Board` saves the position of the chess pieces.
|
|
|
|
## `Board` saves the position of the chess pieces.
|
|
|
|
CastleRights = tuple
|
|
|
|
CastleRights = tuple
|
|
|
|
## `CastleRights` contains the rights to castling for each player.
|
|
|
|
## `CastleRights` contains the rights to castling for each player.
|
|
|
@ -24,8 +24,8 @@ type
|
|
|
|
castleRights: CastleRights
|
|
|
|
castleRights: CastleRights
|
|
|
|
Move* = object
|
|
|
|
Move* = object
|
|
|
|
## `Move` stores all important information for a move.
|
|
|
|
## `Move` stores all important information for a move.
|
|
|
|
start*: int
|
|
|
|
start: int
|
|
|
|
dest*: int
|
|
|
|
dest: int
|
|
|
|
color: Color
|
|
|
|
color: Color
|
|
|
|
prom: int
|
|
|
|
prom: int
|
|
|
|
PieceAmount = tuple
|
|
|
|
PieceAmount = tuple
|
|
|
@ -39,7 +39,7 @@ 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.
|
|
|
@ -54,9 +54,9 @@ const
|
|
|
|
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 ## \
|
|
|
@ -71,9 +71,9 @@ const
|
|
|
|
## `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.
|
|
|
@ -142,7 +142,7 @@ 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 fieldToInd*(field: string): int =
|
|
|
|
proc fieldToInd(field: string): int =
|
|
|
|
## Calculate and return board index from `field` of a chess board.
|
|
|
|
## Calculate and return board index from `field` of a chess board.
|
|
|
|
## Returns -1 if the `field` was not input correct.
|
|
|
|
## Returns -1 if the `field` was not input correct.
|
|
|
|
try:
|
|
|
|
try:
|
|
|
@ -152,7 +152,7 @@ proc fieldToInd*(field: string): int =
|
|
|
|
except IndexDefect, ValueError:
|
|
|
|
except IndexDefect, ValueError:
|
|
|
|
return -1
|
|
|
|
return -1
|
|
|
|
|
|
|
|
|
|
|
|
proc indToField*(ind: int): string =
|
|
|
|
proc indToField(ind: int): string =
|
|
|
|
## Calculate and returns field name from board index `ind`.
|
|
|
|
## Calculate and returns field name from board index `ind`.
|
|
|
|
let line = (int)ind/10-1
|
|
|
|
let line = (int)ind/10-1
|
|
|
|
let file_ind = 7-((ind)%%10-1)
|
|
|
|
let file_ind = 7-((ind)%%10-1)
|
|
|
@ -160,7 +160,7 @@ proc indToField*(ind: int): string =
|
|
|
|
if FileChar[file] == file_ind:
|
|
|
|
if FileChar[file] == file_ind:
|
|
|
|
return $file & $line
|
|
|
|
return $file & $line
|
|
|
|
|
|
|
|
|
|
|
|
proc getMove*(start: int, dest: int, prom: int, color: Color): Move =
|
|
|
|
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
|
|
|
|
## Get a move object of the `color` player from `start` to `dest` with an
|
|
|
|
## eventual promition to `prom`.
|
|
|
|
## eventual promition to `prom`.
|
|
|
|
var move = Move(start: start, dest: dest, prom: prom * ord(color), color: color)
|
|
|
|
var move = Move(start: start, dest: dest, prom: prom * ord(color), color: color)
|
|
|
@ -168,7 +168,7 @@ proc getMove*(start: int, dest: int, prom: int, color: Color): Move =
|
|
|
|
move.prom = WQueen
|
|
|
|
move.prom = WQueen
|
|
|
|
return move
|
|
|
|
return move
|
|
|
|
|
|
|
|
|
|
|
|
proc getMove*(start: int, dest: int, color: Color): Move =
|
|
|
|
proc getMove(start: int, dest: int, color: Color): Move =
|
|
|
|
## Get a move object of the `color` player from `start` to `dest` with
|
|
|
|
## Get a move object of the `color` player from `start` to `dest` with
|
|
|
|
## automatic promition to queen.
|
|
|
|
## automatic promition to queen.
|
|
|
|
var move = Move(start: start, dest: dest, prom: WQueen * ord(color), color: color)
|
|
|
|
var move = Move(start: start, dest: dest, prom: WQueen * ord(color), color: color)
|
|
|
@ -259,7 +259,7 @@ proc initChess*(): Chess =
|
|
|
|
fiftyMoveCounter: 0, castleRights: (true, true, true, true))
|
|
|
|
fiftyMoveCounter: 0, castleRights: (true, true, true, true))
|
|
|
|
return chess
|
|
|
|
return chess
|
|
|
|
|
|
|
|
|
|
|
|
proc initChess*(board: array[0..63, int], color: Color): Chess =
|
|
|
|
proc initChess(board: array[0..63, int], color: Color): Chess =
|
|
|
|
## Create and return a Chess object based on a position of choice.
|
|
|
|
## Create and return a Chess object based on a position of choice.
|
|
|
|
## `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)
|
|
|
@ -533,7 +533,7 @@ proc isAttacked(chess: Chess, position: int, color: Color): bool =
|
|
|
|
color), WKnight)
|
|
|
|
color), WKnight)
|
|
|
|
return attacked
|
|
|
|
return attacked
|
|
|
|
|
|
|
|
|
|
|
|
proc isInCheck*(chess: Chess, color: Color): bool =
|
|
|
|
proc isInCheck(chess: Chess, color: Color): bool =
|
|
|
|
## Returns true if the king of a given `color` is in check in a `chess`.
|
|
|
|
## Returns true if the king of a given `color` is in check in a `chess`.
|
|
|
|
var king_pos: int
|
|
|
|
var king_pos: int
|
|
|
|
for i in countup(0, chess.board.high):
|
|
|
|
for i in countup(0, chess.board.high):
|
|
|
@ -663,7 +663,7 @@ proc genLegalKingMoves(chess: Chess, field: int, color: Color): seq[Move] =
|
|
|
|
res.add(getMove(field, dest, color))
|
|
|
|
res.add(getMove(field, dest, color))
|
|
|
|
return res
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
|
|
|
proc genLegalMoves*(chess: Chess, field: int, color: Color): seq[Move] =
|
|
|
|
proc genLegalMoves(chess: Chess, field: int, color: Color): seq[Move] =
|
|
|
|
## Generates all legal moves in a `chess` starting from `field` for a `color`.
|
|
|
|
## Generates all legal moves in a `chess` starting from `field` for a `color`.
|
|
|
|
var legal_moves = newSeq[Move]()
|
|
|
|
var legal_moves = newSeq[Move]()
|
|
|
|
var target = ord(color) * chess.board[field]
|
|
|
|
var target = ord(color) * chess.board[field]
|
|
|
|