Written in Rust🦀💖
Why write a Chess engine?
Above all, this video by Tom7 is my inspiration for this project. He's absolutely brilliant and I implore you to watch his content.
I love chess a lot. It's definitely one of my favorite games ever. However, I've always been disappointed when trying to write programs that play chess digitally (particularly in a compiled language). Although several amazing engines exist, it's near impossible to find a neat library for chess-related-programming that runs on everything.
chess-engine is a solution to my problem. If you want a chess engine that runs on embedded devices, the terminal, the desktop (with a gui), and the web, this is probably your best bet.
How does it work?
This particular AI (along with most other chess AIs) works using the Minimax algorithm, along with Alpha-Beta pruning for optimization.
Now, let's unpack that.
The Minimax algorithm essentially iterates through all possible moves recursively, and evaluates all of the boards after the moves are played. If the board is more favorable, it will encourage playing its parent move, but if a board is less favorable, then it will select against playing a given move.
Additionally, when the AI attempts to see past just the current board, it will assume the human always responds with the best moves. As a result, the computer almost never blunders. This allows the computer to almost always play objectively better moves than the player.
Embedded in the Web
Because it has zero dependencies, it's extremely simple to embed in the web browser using wasm. Try playing it yourself!
Usage
The Board
structure has a few different methods that allow users to generate moves from a given position, including get_best_next_move
, get_worst_next_move
, and get_legal_moves
. These are particularly handy for writing chess AIs to play against.
To add some variation or more advanced play, consider writing an AI that plays known openings that build better positions before using the get_best_next_move
method!
Custom Boards
Additionally, users can create their own custom Board
objects other than the default one. This is done using the BoardBuilder
structure. The BoardBuilder
structure supports enabling and disabling castling, placing rows and columns of pieces, and placing individual pieces.
Keep in mind when using a BoardBuilder
that castling is disabled by default!