mirror of
https://github.com/tiyn/yeschess.git
synced 2025-04-02 23:17:47 +02:00
visibility: use include in tests and only set needed functions as public
This commit is contained in:
parent
10098da82f
commit
b3c76fd2c1
@ -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]
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import einheit
|
import einheit
|
||||||
import algorithm
|
import algorithm
|
||||||
|
|
||||||
import ./chess.nim
|
include ./chess.nim
|
||||||
|
|
||||||
testSuite ChessTest of TestSuite:
|
testSuite ChessTest of TestSuite:
|
||||||
|
|
||||||
|
@ -17,7 +17,7 @@ const
|
|||||||
DrawVal = 0 ## `DrawVal` is the engines value for a draw.
|
DrawVal = 0 ## `DrawVal` is the engines value for a draw.
|
||||||
LoVal = -1000000 ## `LoVal` is a value always lower than any evaluation.
|
LoVal = -1000000 ## `LoVal` is a value always lower than any evaluation.
|
||||||
|
|
||||||
proc pieceEval*(chess: Chess): int =
|
proc pieceEval(chess: Chess): int =
|
||||||
## Returns the evaluation of existing pieces on the `board`
|
## Returns the evaluation of existing pieces on the `board`
|
||||||
var evaluation = DrawVal
|
var evaluation = DrawVal
|
||||||
for square in chess.board:
|
for square in chess.board:
|
||||||
@ -63,7 +63,7 @@ proc evaluate(chess: Chess): int =
|
|||||||
evaluation = min(DrawVal, evaluation)
|
evaluation = min(DrawVal, evaluation)
|
||||||
return evaluation
|
return evaluation
|
||||||
|
|
||||||
proc spanMoveTree*(chess: Chess, depth: int): MoveTree =
|
proc spanMoveTree(chess: Chess, depth: int): MoveTree =
|
||||||
## Create and return a Movetree of a given `chess` with a given maximum `depth`.
|
## Create and return a Movetree of a given `chess` with a given maximum `depth`.
|
||||||
var mTree: MoveTree
|
var mTree: MoveTree
|
||||||
mTree.chess = chess
|
mTree.chess = chess
|
||||||
@ -75,7 +75,7 @@ proc spanMoveTree*(chess: Chess, depth: int): MoveTree =
|
|||||||
mTree.children.add(spanMoveTree(tmpChess, depth-1))
|
mTree.children.add(spanMoveTree(tmpChess, depth-1))
|
||||||
return mTree
|
return mTree
|
||||||
|
|
||||||
proc negaMax*(mTree: MoveTree): int =
|
proc negaMax(mTree: MoveTree): int =
|
||||||
## Return the value of the root node of a given `MoveTree`
|
## Return the value of the root node of a given `MoveTree`
|
||||||
if mTree.children == []:
|
if mTree.children == []:
|
||||||
return mTree.chess.evaluate()
|
return mTree.chess.evaluate()
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import einheit
|
import einheit
|
||||||
|
|
||||||
import ./chess.nim
|
include ./chess.nim
|
||||||
import ./engine.nim
|
include ./engine.nim
|
||||||
|
|
||||||
testSuite ChessTest of TestSuite:
|
testSuite ChessTest of TestSuite:
|
||||||
|
|
||||||
|
@ -15,7 +15,7 @@ proc runGameHotseat*(): void =
|
|||||||
while not chess.checkedMove(notationToMove(move, chess.toMove)):
|
while not chess.checkedMove(notationToMove(move, chess.toMove)):
|
||||||
move = readLine(stdin)
|
move = readLine(stdin)
|
||||||
chess.echoBoard(chess.toMove)
|
chess.echoBoard(chess.toMove)
|
||||||
if (chess.isDrawClaimable):
|
if (chess.isDrawClaimable()):
|
||||||
echo "Do you want to claim a draw? (y/N)"
|
echo "Do you want to claim a draw? (y/N)"
|
||||||
draw = readLine(stdin)
|
draw = readLine(stdin)
|
||||||
if (draw == "y"):
|
if (draw == "y"):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user