• Stars
    star
    379
  • Rank 113,004 (Top 3 %)
  • Language
    Rust
  • License
    MIT License
  • Created almost 4 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A dependency-free chess engine♟️ library built to run anywhere.

♔chess-engine♚

A dependency-free chess engine library built to run anywhere.

Donate Open Source

Demo | Docs | Contact Me

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.

Minimax

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!

Average AI Setting

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.

fn main() {
    let board = Board::default();

    // Get the best move with 4 moves of lookahead
    let best_move = board.get_best_next_move(4);
    // Get the worst move with 3 moves of lookahead
    let worst_move = board.get_worst_next_move(3);

    // Get all of the possible legal moves for the given player
    let legal_moves = board.get_legal_moves();
    // Print the board
    println!("{}", board);

    print!("CPU chose to ");
    match best_move {
        Move::Piece(from, to) => println!("move {} to {}", from, to),
        Move::KingSideCastle => println!("castle kingside"),
        Move::QueenSideCastle => println!("castle queenside"),
        Move::Resign => println!("resign")
    }
}

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!

Play the Horde Chess Variant

Play the Horde Chess Variant
fn main() {
    // `BoardBuilder::new()` returns an empty board
    // with castling disabled.
    // Creating a board builder from another board
    // structure will preserve
    // all settings from the board (such as castling
    // and the last en-passant move).

    // This BoardBuilder constructs the "Horde" chess variant!
    let board = BoardBuilder::from(Board::default())
            .row(Piece::Pawn(WHITE, A1))
            .row(Piece::Pawn(WHITE, A2))
            .row(Piece::Pawn(WHITE, A3))
            .row(Piece::Pawn(WHITE, A4))
            .piece(Piece::Pawn(WHITE, F5))
            .piece(Piece::Pawn(WHITE, G5))
            .piece(Piece::Pawn(WHITE, B5))
            .piece(Piece::Pawn(WHITE, C5))
            .build();

    // The CPU can also play variants!
    let cpu_move = board.get_best_next_move(3);
    
    match board.play_move(cpu_move) {
        GameResult::Continuing(next_board) => {
            println!("{}", next_board);
        }

        GameResult::Victory(winner) => {
            // You can use the ! operator on a player's
            // color to invert.
            println!("{} loses. {} is victorious.",
              !winner, winner
            );
        }

        GameResult::IllegalMove(x) => {
            eprintln!("{} is an illegal move.", x);
        }

        GameResult::Stalemate => {
            println!("Drawn game.");
        }
    }
}

About the Author

Website | Blog | GitHub

I'm a freshman in college, mainly working on side projects like these in the ~30 minute breaks between classes. If you enjoy my projects, consider supporting me by buying me a coffee!

Buy Me A Coffee

More Repositories

1

oakc

A portable programming language with a compact intermediate representation
Rust
697
star
2

dune

A shell🐚 by the beach🏖️!
Rust
682
star
3

free

An esoteric programming language with an unusual compiler backend
Rust
320
star
4

atom

Shell scripting that will knock your socks off
Rust
258
star
5

wisp

A lisp👽 written in C++
C++
172
star
6

harbor

A language that ports⚓: examining the limits of compilation⚙️.
Rust
80
star
7

sage

A programming language that's wise beyond its bytes!🌱🌿🪴
Rust
32
star
8

smpl

A superset of brainfuck with dynamic memory management.
C++
23
star
9

maroon-lang

A (nearly) pure functional programming language.
Rust
22
star
10

tsar

Tsar programming language
Rust
13
star
11

honeycomb

A portable parser combinator library that does not require a runtime
Rust
10
star
12

game-of-life

A game of life🔬 simulator on an infinite♾️ plane
Rust
8
star
13

qalc

A very unstable and terrible language for quickly writing calculator programs
Rust
5
star
14

xasm

A cross platform, compiled and dynamically typed programming / intermediate language
Rust
5
star
15

sight

A library for Vision Processing
Python
5
star
16

lasm

A tiny and portable assembly language for complex compilers
Rust
4
star
17

program-evolution

Evolutionary algorithms applied to SKI combinator calclus and a Turing-machine-based architecture
Python
3
star
18

rusty-ci

A tool to generate buildbot projects from a YAML file
Rust
3
star
19

wisp-bootstrap

A bootstrapped lisp compiler
Common Lisp
3
star
20

battaliants

A roguelike ant-catching game written for a hackathon
GDScript
3
star
21

calendar

A tiny calendar program
Nim
2
star
22

pink-lang

A high level programming language that compiles to native
C++
2
star
23

blame-tracker

Accuse guilty developers!
Python
2
star
24

cable-lang-cxx

A fast dynamically typed language interoperable with C++
C++
2
star
25

adam-mcdaniel.github.io

My site📖
HTML
2
star
26

hlvm

A high level virtual machine library that can be used to easily make powerful programming languages
Rust
2
star
27

finn-assembler

The assembler for the Finn IR implemented using my HLVM in Rust
Rust
2
star
28

sage-chess

A chess engine written in my programming language, Sage🌱
C
2
star
29

xasm-vm

All of the implementations of virtual machines for the Xasm intermediate representation
1
star
30

Octaculus

Octaculus - 8-way directional gesture detection using machine learning and light dependent resistors
Jupyter Notebook
1
star
31

toki-pona-dataset

Dataset for Toki Pona constructed language
Jupyter Notebook
1
star
32

periwinkle

Language inspired by Python, Rust, and C++
Python
1
star
33

hlvm_runtime

The runtime library for HLVM
Rust
1
star
34

xassembler

The assembler for XASM
Rust
1
star
35

Dynamite

An interpreted programming language with speed and extensibility in mind.
Go
1
star
36

dune-old

A cross platform shell built with my ideals in mind
Rust
1
star
37

comment

Strip multiple types of comments
Rust
1
star
38

ski

SKI combinator calculus implemented in rust
Rust
1
star
39

xmachine

A fast, cross platform virtual machine implemented in Rust without the Standard Library.
Rust
1
star
40

adams-bot

A mini me for my Discord server!
C
1
star
41

adam-mcdaniel

My profile📝
1
star
42

rapture

A library to write installation programs / a tool to install programs. Meant to be to the future backend of rusty-ci.
Rust
1
star
43

xgopher

Golang Xasm virtual machine implementation
Go
1
star
44

lite

A tiny text editor📝 with syntax highlighting🎨, undo/redo🧰, and a language for customizing the editor🏗️
Rust
1
star
45

resume

My Resume
1
star
46

vpn

A VPN implemented over TLS/SSL written in C
C
1
star
47

gc-rust

A rust library to implement reference counting garbage collection
Rust
1
star
48

ramify

A functional programming language with builtin parallelism
Rust
1
star
49

music-generator

A tool to generate a lead sheet in a specified key given a certain number of choruses and verses.
Rust
1
star