A fully function chess engine written in python
Project description
Plazma Chess Engine
In development!
This is a basic chess engine based off of the python-chess library
The engine has a client which supports multiplayer, singleplayer and bot chess matches!
Documentation
Setting up the board
import engine # Import the plazma-chess module
chessEngine = engine.Engine() # Create instance of the Engine class
This will initialize the engine class which contains all of the information of the board. All pieces will be set to normal chess starting positions.
Acessing the board
import engine # Import the plazma-chess module
chessEngine = engine.Engine() # Create instance of the Engine class
print(chessEngine.board.board) # Access the board class stored in the engine class and get the board list
# Example output of starting positions
"""[[10, 8, 9, 11, 12, 9, 8, 10],
[7, 7, 7, 7, 7, 7, 7, 7],
[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],
[1, 1, 1, 1, 1, 1, 1, 1],
[4, 2, 3, 5, 6, 3, 2, 4]]"""
To Access the board class, and the board list within that you need to use the engine class. This is because when the engine class is initialized it also initializes the board.
Generating moves
import engine # Import the plazma-chess module
chessEngine = engine.Engine() # Create instance of the Engine class
legalMoves = chessEngine.generateMoves((4, 6)) # Get the legal moves for the kings pawn
To get the legal moves for a piece you just need to specify the coordinates of the piece and call the generateMoves
function in the engine class, passing in those coordinates.
Finding pieces
import engine # Import the plazma-chess module
chessEngine = engine.Engine() # Create instance of the Engine class
pos = any_pieces_position
print(chessEngine.board.pieceAt(pos)) # Call the 'pieceAt' function in the board
# Prints:
# bool -> Found a piece
# int -> Piece value (0 if no piece)
To check if a piece is on a specific part of the board you can either access the board list manually or use the pieceAt
function in the board class.
Moving pieces
import engine # Import the plazma-chess module
chessEngine = engine.Engine() # Create instance of the Engine class
pos = current_piece_position
newPos = new_piece_position
status = chessEngine.move(pos, newPos) # Call the move function in the engine
# If status = 0 the move was successfull.
# If status = 1 the move ended in checkmate.
# If an Illegal Move error surfaces the move was unsuccessfull.
To move pieces simply call the move function in the engine class. It will not allow illegal moves to be played and it uses the same generateMoves
function to validate moves.
Neither the generateMoves
function or the move
function will return moves that may result in a check.
Bots
Bot support is not fully implemented yet so creating bots requires a little more work than expected.
Generating moves is a little easier thanks to the individual move generation functions.
These include:
generatePawnMoves
generateKnightMoves
generateSlidingMoves
generateDiagonalMoves
generateKingMoves
This allows you to generate moves with more speciallity and presicion.
These functions all take the same argument, pos
which defines the piece position.
These functions do not take checks into account so illegal move errors are a risk.
You can remedy this by using the inCheck
function which takes a turn
argument and and optional square
argument which is used to check for a specific piece. It returns True
or False
if the king is in check.
To bypass the check detection there is a function called __moveWithoutCheck
. This function is dangerous as it defies the rules of chess. It should only be used for move generation in bots which will garrentee faster move generation. chess. It should only be used for move generation in bots which will garrentee faster move generation.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
File details
Details for the file plazma_chess-1.2.1-py3-none-any.whl
.
File metadata
- Download URL: plazma_chess-1.2.1-py3-none-any.whl
- Upload date:
- Size: 6.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 colorama/0.4.4 importlib-metadata/4.6.4 keyring/23.5.0 pkginfo/1.8.2 readme-renderer/34.0 requests-toolbelt/0.9.1 requests/2.25.1 rfc3986/1.5.0 tqdm/4.57.0 urllib3/1.26.5 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b00f6da4bcd550ce1501fb4821eb0d50233c655d7ac51a6d7096eae25e764df4 |
|
MD5 | bc7ecc4ccf7a1dc8dd1365bc673caa03 |
|
BLAKE2b-256 | e4ef0ded81992ee7e1297b6de3f489077360f2eb46e0e70b9da8a4fbc4a8c3e8 |