mirror of
https://github.com/tiyn/yeschess.git
synced 2025-10-14 04:01:14 +02:00
refactoring: renaming classes and functions
Renamed the 'game' type to 'chess' to make more sense of the existing filestructure. Doubled functions where removed.
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
import ./chess
|
||||
import ./chess.nim
|
||||
|
||||
type
|
||||
MoveTree* = object
|
||||
## `Movetree` is a visualization for possible moves.
|
||||
game*: Game
|
||||
chess*: Chess
|
||||
evaluation: float
|
||||
children*: seq[Movetree]
|
||||
|
||||
@@ -17,10 +17,10 @@ const
|
||||
DrawVal = 0 ## `DrawVal` is the engines value for a draw.
|
||||
LoVal = -1000000 ## `LoVal` is a value always lower than any evaluation.
|
||||
|
||||
proc pieceEval*(game: Game): int =
|
||||
proc pieceEval*(chess: Chess): int =
|
||||
## Returns the evaluation of existing pieces on the `board`
|
||||
var evaluation = DrawVal
|
||||
for square in game.board:
|
||||
for square in chess.board:
|
||||
case square:
|
||||
of WPawn:
|
||||
evaluation += ord(Color.White) * PawnVal
|
||||
@@ -46,56 +46,56 @@ proc pieceEval*(game: Game): int =
|
||||
continue
|
||||
return evaluation
|
||||
|
||||
proc evaluate(game: Game): int =
|
||||
## Returns a complete evaluation of a `game` with player `toMove` about to make
|
||||
proc evaluate(chess: Chess): int =
|
||||
## Returns a complete evaluation of a `chess` with player `toMove` about to make
|
||||
## a move.
|
||||
var evaluation: int
|
||||
if game.isCheckmate(game.toMove):
|
||||
evaluation = ord(game.toMove) * CheckmateVal
|
||||
if game.isStalemate(game.toMove):
|
||||
if chess.isCheckmate(chess.toMove):
|
||||
evaluation = ord(chess.toMove) * CheckmateVal
|
||||
if chess.isStalemate(chess.toMove):
|
||||
evaluation = DrawVal
|
||||
else:
|
||||
evaluation = game.pieceEval()
|
||||
if game.isDrawClaimable():
|
||||
if game.toMove == Color.White:
|
||||
evaluation = chess.pieceEval()
|
||||
if chess.isDrawClaimable():
|
||||
if chess.toMove == Color.White:
|
||||
evaluation = max(DrawVal, evaluation)
|
||||
else:
|
||||
evaluation = min(DrawVal, evaluation)
|
||||
return evaluation
|
||||
|
||||
proc spanMoveTree*(game: Game, depth: int): MoveTree =
|
||||
## Create and return a Movetree of a given `game` with a given maximum `depth`.
|
||||
proc spanMoveTree*(chess: Chess, depth: int): MoveTree =
|
||||
## Create and return a Movetree of a given `chess` with a given maximum `depth`.
|
||||
var mTree: MoveTree
|
||||
mTree.game = game
|
||||
if depth != 0 and not game.isCheckmate(game.toMove) and not game.isStalemate(game.toMove):
|
||||
let possibleMoves = game.genLegalMoves(game.toMove)
|
||||
mTree.chess = chess
|
||||
if depth != 0 and not chess.isCheckmate(chess.toMove) and not chess.isStalemate(chess.toMove):
|
||||
let possibleMoves = chess.genLegalMoves(chess.toMove)
|
||||
for move in possibleMoves:
|
||||
var tmpGame = game
|
||||
tmpGame.checkedMove(move)
|
||||
mTree.children.add(spanMoveTree(tmpGame, depth-1))
|
||||
var tmpChess = chess
|
||||
tmpChess.checkedMove(move)
|
||||
mTree.children.add(spanMoveTree(tmpChess, depth-1))
|
||||
return mTree
|
||||
|
||||
proc negaMax*(mTree: MoveTree): int =
|
||||
## Return the value of the root node of a given `MoveTree`
|
||||
if mTree.children == []:
|
||||
return mTree.game.evaluate()
|
||||
return mTree.chess.evaluate()
|
||||
var bestVal = LoVal
|
||||
for child in mTree.children:
|
||||
var tmpVal = -negaMax(child)
|
||||
bestVal = max(bestVal, tmpVal)
|
||||
return bestVal
|
||||
|
||||
proc bestMove*(game: Game, depth: int): Move =
|
||||
## Generate a MoveTree of a `game` with a given `depth`, run negaMax and return
|
||||
proc bestMove*(chess: Chess, depth: int): Move =
|
||||
## Generate a MoveTree of a `chess` with a given `depth`, run negaMax and return
|
||||
## the best evaluated move.
|
||||
var moves = game.genLegalMoves(game.toMove)
|
||||
var moves = chess.genLegalMoves(chess.toMove)
|
||||
var bestMove: Move
|
||||
var bestEval: int
|
||||
bestEval = LoVal
|
||||
for move in moves:
|
||||
var tmpGame = game
|
||||
tmpGame.checkedMove(move)
|
||||
var tmpMTree = tmpGame.spanMoveTree(depth)
|
||||
var tmpChess = chess
|
||||
tmpChess.checkedMove(move)
|
||||
var tmpMTree = tmpChess.spanMoveTree(depth)
|
||||
var tmpEval = -tmpMTree.negaMax()
|
||||
echo("move:", moveToNotation(move),"; eval:", tmpEval)
|
||||
if tmpEval > bestEval:
|
||||
|
Reference in New Issue
Block a user