Skip to main content

A lightweight chess engine written in Python. It can evaluate chess positions, calculate best moves, and integrate with various interfaces. It is designed to be simple and extensible, offering the flexibility to implement your own chess logic or use it as part of a larger project

Project description

pychess_engine - A Python Chess Engine

pychess_engine is a lightweight chess engine written in Python. It can evaluate chess positions, calculate best moves, and integrate with various interfaces. It is designed to be simple and extensible, offering the flexibility to implement your own chess logic or use it as part of a larger project.

Features

  • Evaluate positions and calculate best moves
  • Supports chess rules and common move generation
  • Easy integration with Python applications
  • Optimized for speed and simplicity

Installation

Install pychess_engine with pip

    pip install pychess-engine

Documentation

Engine Class

The Engine class provides core functionality for a chess engine, including move generation, board management, evaluation, and search for the best move. It is designed to simulate a chess-playing AI, capable of interacting with a chessboard, evaluating positions, and determining optimal moves.

Attributes

  • name (str): Name of the chess engine.
  • author (str): Author of the engine.
  • version (str): Version of the engine.
  • elo (int): ELO rating that adjusts the search depth.
  • controls (EngineControls): Manages search control settings.
  • board (Board): Represents the current board state.
  • search (Search): Manages search algorithms and move evaluation.

Move Format Used by the Engine

Engine supports standard co-ordinate based notation for representing chess moves.

Example

For moving pawn from e2 to e4 use e2e4

For moving Knight from g1 to f3: g1f3 is valid ✅ Nf3 is invalid ❌

Methods

print_board() -> None

Prints the current board to the console.

from pychess_engine import Engine
engine = Engine()
engine.print_board()

load_fen(fen: str) -> bool

Loads a position from a FEN string.

Arguments

  • fen (str): FEN string representing the board state.

Returns

  • bool: True if the FEN string is parsed successfully, False otherwise.
from pychess_engine import Engine
engine = Engine()
WAC1 = "2rr3k/pp3pp1/1nnqbN1p/3pN3/2pP4/2P3Q1/PPB4P/R4RK1 w - -"
engine.load_fen(WAC1)
engine.print_board()

legal_moves() -> list

Generates all legal moves from the current board position.

Returns

  • List of all possible moves in algebraic notation.
from pychess_engine import Engine
engine = Engine()
moves = engine.legal_moves()
print(moves)

# also use load_fen() to load a fen and generate moves for that
WAC1 = "2rr3k/pp3pp1/1nnqbN1p/3pN3/2pP4/2P3Q1/PPB4P/R4RK1 w - -"
engine.load_fen(WAC1)
print(engine.legal_moves())

reset_board() -> None

Resets the board to the initial starting position.

from pychess_engine import Engine
engine = Engine()

WAC1 = "2rr3k/pp3pp1/1nnqbN1p/3pN3/2pP4/2P3Q1/PPB4P/R4RK1 w - -"
engine.load_fen(WAC1)

engine.reset_board()
engine.print_board()

make_move(move: str) -> bool

Attempts to make a move on the board.

Arguments

  • move (str): Move in algebraic notation (e.g., "e2e4").

Returns

  • bool: True if the move is successfully made, False if the move is illegal.
from pychess_engine import Engine
engine = Engine()

islegal = engine.make_move("e2e4")
if(not islegal):
    print("Illegal Move!")
engine.print_board()

is_move_legal(move: str) -> bool

Checks if a move is legal on the current board.

Arguments

  • move (str): Move in algebraic notation (e.g., "e2e4").

Returns

  • bool: True if the move is legal, False otherwise.
from pychess_engine import Engine
engine = Engine()

islegal = engine.is_move_legal("e2e4")
if(islegal):
    print("Legal Move")
else:
    print("Illegal Move")

set_elo(elo: int) -> None

Sets the ELO rating for the engine, influencing search depth.

Arguments

  • elo (int): New ELO rating for the engine.
from pychess_engine import Engine
engine = Engine()

engine.set_elo(1700)

get_elo() -> int

Retrieves the current ELO rating of the engine.

Returns

  • int: The current ELO rating.
from pychess_engine import Engine
engine = Engine()

print(engine.get_elo())

evaluate() -> int

Evaluates the current board position.

Chess Piece Evaluation Scores

  • Pawn: 100
  • Knight: 325
  • Bishop: 325
  • Rook: 550
  • Queen: 1000
  • King: 50000 (INF)

What it means

  • Positive Score means Advantage
  • Negative Score means Disadvantage

Returns

  • int: Evaluation score of the position (based on current situation only, i.e material and placement of pieces).
from pychess_engine import Engine
engine = Engine()

# can load_fen for evaluating specific positions

print(engine.evaluate())

analyze_position(fen: str, depth=4) -> int

Analyzes a given chess position using iterative deepening search.

Arguments

  • fen (str): FEN string representing the chess position.
  • depth (int, optional): The maximum depth for the search. Default is 4.

Returns

  • int: The evaluation score of the position

Example Usage

from pychess_engine import Engine
engine = Engine()

WAC1 = "2rr3k/pp3pp1/1nnqbN1p/3pN3/2pP4/2P3Q1/PPB4P/R4RK1 w - -"
score = engine.analyze_position(fen=WAC1, depth=5)
print(f"Evaluation score: {score}")

best_move(depth=None, movestogo=30, movetime=None, increment=0, time=None, display_calculation=True) -> str

Determines the best move based on search parameters and constraints.

Arguments

  • depth (int, optional): Search depth for the engine.
  • movestogo (int, optional): Number of moves to manage within the available time. By default 30
  • movetime (int, optional): Time allocated for this move only.
  • increment (int, optional): Additional time (increment) per move.
  • time (int, optional): Total time available for the current search phase.
  • display_calculation (bool, optional): Whether to display the calculation part.

Returns

  • str: The best move in algebraic notation. - if there is some problem in generating bestmove (maybe due to invalid position)
from pychess_engine import Engine
engine = Engine()

# can load a fen, or make a move using load_fen, make_move
# can set the elo also

bestmove = engine.best_move()
bestmove = engine.best_move(depth=5) # searching for depth 5
bestmove = engine.best_move(movetime=5000) # specifying the movetime in ms
bestmove = engine.best_move(movestogo=10, time=90000) # 90 seconds divided among 10 moves so each move will get 9 seconds
bestmove = engine.best_move(movetime=3000, increment=1000) # setting the increment

# play around with these controls to get the best out of the engine

print(bestmove)

perft_test(depth=3) -> None

Performs Perft (performance test) to calculate the number of legal positions up to a certain depth.

Only for testing if Move Generation is Accurate or not

Arguments

  • depth (int, optional): Depth for the Perft test. Default is 3.
from pychess_engine import Engine
engine = Engine()

engine.perft_test()

Authors

Acknowledgements

License

This project is licensed under the MIT License.

You are free to use, modify, and distribute this software under the terms of the MIT License. See the LICENSE file for details.

Feedback

If you have any feedback, please reach out at galhotravanshu@gmail.com or 205124107@nitt.edu

GitHub Repo

https://github.com/vanshugalhotra/pychess-engine

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pychess_engine-1.1.0.tar.gz (36.6 kB view details)

Uploaded Source

Built Distribution

pychess_engine-1.1.0-py3-none-any.whl (39.6 kB view details)

Uploaded Python 3

File details

Details for the file pychess_engine-1.1.0.tar.gz.

File metadata

  • Download URL: pychess_engine-1.1.0.tar.gz
  • Upload date:
  • Size: 36.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.5

File hashes

Hashes for pychess_engine-1.1.0.tar.gz
Algorithm Hash digest
SHA256 47084fa8e95ac0a768386f8f1dc3f78810dfb568c16d5ad5aeaa64360b97d2bb
MD5 cd4895649da35a03293036f64244c2f7
BLAKE2b-256 089662a7829f2ef50f36698a1265ba7494b70ff7517a0e82ca29723db54ce85e

See more details on using hashes here.

File details

Details for the file pychess_engine-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: pychess_engine-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 39.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.5

File hashes

Hashes for pychess_engine-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e3a262f0126ad939e353878556ef66b2546251f13be540eaa5b930188ee52a00
MD5 744d94e3fa70f784b8e1e5fb7b743e20
BLAKE2b-256 eeaf925adbdec6c6dabd8398fdc662a52f3ef06621efcbd6fa74c99cd65d7e1a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page