Giter Site home page Giter Site logo

chess-tournament-lib's Introduction

chess-tournament-lib

Some utilities for a simple custom chess engine tournament with the chess package.

Documentation

Documentation can be found here

Installation

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.

Windows

Navigate to the root directory of the repository and execute

install.bat

If the smoke tests passed, the installation is complete.

Linux

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.

Examples

FirstMoveEngine

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()

chess-tournament-lib's People

Contributors

supurcalvinhiggins avatar

Watchers

 avatar

Forkers

ethan-j-carlson

chess-tournament-lib's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.