Skip to main content

A chess board model that will import and export FEN, generate valid moves, and provide game status

Project description

Chessir

Chessir is a fork of Chessnut, which is a chess board model written in Python that is intentionally simple.

Like Chessnut, Chessir is not written for speed, but it is about twenty times faster than Chessnut at generating valid chess moves from a given position on a chess board.

Chessir is not as simple as Chessnut: it has about 30% more lines of code.

Chessir is an alternative to Chessnut--it is faster, but a bit more complicated.

What is the Same and what is Different from Chessnut

Chessir is used in much the same way as Chessnut. As with Chessnut, only the Game class needs to be imported. Methods and properties of Chessnut's Game class are available in Chessir, as described below.

Chessir creates a move list differently than Chessnut. Specifically, the differences relate to the evaluation of moves to exclude check for the active player, moves when the active player's king is in check, and how potential castling moves are evaluated.

Chessir will determine draws via three-fold repetition, insufficient material, and the 50-move rule, which Chessnut does not do.

Installation

PIP

pip is the easiest way to install Chessir. It can be installed directly from the pypi package:

pip install chessir

As a Module

Chessir can be a standalone module, so if you place the Chessir directory within your project folder, you don't need to install the package, you can just import the module as usual.

from Chessir import Game

Testing

Unit tests can be run with the pytest package.

Additionally the GitHub repository for Chessir includes scripts for comparing Chessir to Chessnut for both moves generated and for the speed at which moves are generated. These scripts are in the files chessnut_moves_comparison.py and chessnut_speed_comparison.py, respectively.

Using Chessir

There are only two real classes in the Chessir package: Board and Game. Board is only used internally by Game to keep track of pieces and perform string formatting to and from FEN notation, so Game should be the only class you need to import. After installing the Chessir package, you can import and use it as you would expect:

from Chessir import Game

chessgame = Game()
print(chessgame)  # 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1'

The get_moves method of Game will provide a List of valid moves:

print(chessgame.get_moves())
"""
['a2a3', 'a2a4', 'b2b3', 'b2b4', 'c2c3', 'c2c4', 'd2d3', 'd2d4', 'e2e3',
 'e2e4', 'f2f3', 'f2f4', 'g2g3', 'g2g4', 'h2h3', 'h2h4', 'b1c3', 'b1a3',
 'g1h3', 'g1f3']
"""

Note that Chessir uses simple algebraic notation, which has four characters identifying the starting and ending squares. In the case of castling, the starting and ending squares of the king are used (e.g., 'e1g1' for white kingside castle). In the case of a pawn promotion, a fifth character is provided to describe the type of promotion (e.g., 'e7e8q').

The apply_moves method will update the state of the chess game.

If the move could be invalid, the argument validate=True should be supplied. In this case, the method can be used in a try-except block, and it will raise an exception if the move is invalid.

chessgame.apply_move('e2e4')
print(chessgame)  # 'rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1'

try:
    chessgame.apply_move('d8e4', validate=True)
except Exception as e:
    print(e) # 'Illegal move: d8e4'

The reset method updates the game state, either to the standard starting position or, if a FEN argument is supplied, to a specific state.

chessgame.reset()
print(chessgame)  # 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1'

chessgame.reset('8/8/8/8/8/k7/7p/K7 b - - 1 40')
print(chessgame)  # '8/8/8/8/8/k7/7p/K7 b - - 1 40'

Game has a status property, which can be used to determine whether checkmate has been achieved. It also will detect check, stalemate, and draws due to three-fold repetition, insufficient material, and the 50-move rule. The statuses have values as follows: NORMAL = 0, CHECK = 1, CHECKMATE = 2, STALEMATE = 3, and DRAW = 4.

chessgame.reset('r1bk3r/p2pBpNp/n4n2/1p1NP2P/6P1/3P4/P1P1K3/q5b1 b - - 1 23')
print(chessgame.status) # 2
print(chessgame.status == chessgame.CHECKMATE) # True

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

chessir-0.1.1.tar.gz (18.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

chessir-0.1.1-py3-none-any.whl (13.7 kB view details)

Uploaded Python 3

File details

Details for the file chessir-0.1.1.tar.gz.

File metadata

  • Download URL: chessir-0.1.1.tar.gz
  • Upload date:
  • Size: 18.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.2

File hashes

Hashes for chessir-0.1.1.tar.gz
Algorithm Hash digest
SHA256 392093e4bd0a71911a58f7f52396545686dec9af5e6836107dd57014f657e94e
MD5 e3dfafce60b9a6d6123eb1856509167a
BLAKE2b-256 8c254f8ccf87f13aa72b0e20661246f82772a8927132c8d61ab36f7ce3dc549e

See more details on using hashes here.

File details

Details for the file chessir-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: chessir-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 13.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.2

File hashes

Hashes for chessir-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 60cd62a0697ce0754eb4ed4e4197b43e81fa80b8608e485af492753fa20c709e
MD5 8b87e7c8d7c0ad6a4fc36d8c3be60bf3
BLAKE2b-256 281d9c1e645bc4a29fb47d94e7d0007059f5fdb691a508db839215b8bdd82f65

See more details on using hashes here.

Supported by

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