Some utilities for a simple custom chess engine tournament with the chess
package.
Documentation can be found here
This library requires Python 3.9 or greater. The correct Python version can be downloaded from here. Other project dependencies will be installed automatically by install.bat.
Navigate to the root directory of the repository and execute
install.bat
If the smoke tests passed, the installation is complete.
Navigate to the root directory of the repository and execute
mv install.bat install.sh
chmod +x install.sh
./install.sh
If the smoke tests passed, the installation is complete.
Let's create an engine that makes the first move on the legal move list. We need to create a FirstMoveEngine
class that inherits from BaseEngine
.
import chess
from chess_tournament.engine import BaseEngine
class FirstMoveEngine(BaseEngine):
pass
Now we need to implement the go
method which takes in a board state and returns the engine's analysis.
import chess
from chess_tournament.engine import BaseEngine, EngineResult
class FirstMoveEngine(BaseEngine):
def go(self, board: chess.Board, time: Optional[float]) -> EngineResult:
move = list(board.legal_moves)[0]
return EngineResult(move=move)
Great! Our engine should be ready analyze games. Let's test it by creating an empty board and asking it to pick a move.
engine = FirstMoveEngine()
board = chess.Board()
result = engine.go(board, None)
print(result)
However, this does not actually make the move on the board. Let's do that now.
board.push(result.move)
Moves can also be undone with board.pop
. See the chess
package documentation here for additional information on how to manipulate the board.
board.pop()
Board manipulation is great to know for implementing a more advanced engine but let's stick with FirstMoveEngine
for now.
Let's try out our engine against RandomEngine
which makes random moves. We will use UnlimitedTimeMatch
from chess_tournament.match
to simplify the game logic.
from chess_tournament.match import UnlimitedTimeMatch
from chess_tournament.engine import RandomEngine
first_move_engine = FirstMoveEngine()
random_engine = RandomEngine()
match = UnlimitedTimeMatch(
white_engine=first_move_engine,
black_engine=random_engine,
)
board = chess.Board()
outcome = match.play(board)
print(outcome)
We got the outcome of the game but we couldn't see anything that happened. Let's include a GUI so we can see the match looks like.
from chess_tournament.gui import TextGui
from chess_tournament.match import UnlimitedTimeMatch
from chess_tournament.engine import RandomEngine
first_move_engine = FirstMoveEngine()
random_engine = RandomEngine()
match = UnlimitedTimeMatch(
white_engine=first_move_engine,
black_engine=random_engine,
)
gui = TextGui(match=match)
gui.start()