1
0
mirror of https://github.com/tiyn/yeschess.git synced 2025-10-19 06:11:22 +02:00

engine: first push for engine

I created an engine, that uses a basic version of the minimax algorithm
to evaluate a position.
It then evaluates all the possible next moves in a given position and picks
the one that has the best evaluation.
Basic test cases were also added.
This commit is contained in:
TiynGER
2021-04-27 00:49:39 +02:00
parent 0e27d0a6b5
commit 4ba3e06d93
3 changed files with 189 additions and 3 deletions

75
src/engineTest.nim Normal file
View File

@@ -0,0 +1,75 @@
import einheit
import algorithm
import ./chess
import ./engine
testSuite GameTest of TestSuite:
var
game: Game
method setup() =
self.game = initGame()
method testPieceEval() =
self.game = initGame([
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, WKing, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, BKing, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0
], Color.Black)
var pieceEvaluation = self.game.pieceEval()
self.check(pieceEvaluation == 0)
method testSpanMoveTree() =
self.game = initGame([
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, WKing, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, BKing, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0
], Color.Black)
var mTree = self.game.spanMoveTree(2)
self.check(mTree.children == [])
method testManualMiniMaxEval() =
self.game = initGame([
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, WKing, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, WPawn, BKing, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0
], Color.Black)
var mTree = self.game.spanMoveTree(2)
var evaluation = mTree.minimax()
self.check(evaluation == 0)
method testBestMove() =
var testGame = initGame([
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, WKing, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, WPawn, BKing, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0
], Color.Black)
var testBestMove = testGame.bestMove(2)
self.check(indToField(testBestMove.start) == "c7")
self.check(indToField(testBestMove.dest) == "d7")
when isMainModule:
einheit.runTests()