All Projects → goutham → magic-bits

goutham / magic-bits

Licence: other
A C++ header-only library for efficient move generation in Chess using "magic bitboards" technique

Programming Languages

C++
36643 projects - #6 most used programming language

Projects that are alternatives of or similar to magic-bits

ChessLib
C# chess library containing a complete data structure and move generation.
Stars: ✭ 37 (+42.31%)
Mutual labels:  chess, bitboard, magic-bitboards
zahak
A UCI compatible chess AI in Go
Stars: ✭ 20 (-23.08%)
Mutual labels:  chess-engine, chess
rustic
Rustic is a chess engine. It is written from scratch, in the Rust programming language.
Stars: ✭ 68 (+161.54%)
Mutual labels:  chess-engine, chess
Realtime-OpenCV-Chess
♔ Chess-playing with Open-CV [Human vs AI (Stockfish engine)]
Stars: ✭ 18 (-30.77%)
Mutual labels:  chess-engine, chess
renpy-chess
A chess GUI built with Ren'Py, python-chess, and Stockfish. Version 2.0 of https://github.com/RuolinZheng08/renpy-chess-engine
Stars: ✭ 36 (+38.46%)
Mutual labels:  chess-engine, chess
bot-o-tron
Try out lichess' bot interface
Stars: ✭ 36 (+38.46%)
Mutual labels:  chess-engine, chess
elephantfish
elephantfish: 一个只有124行的中国象棋引擎
Stars: ✭ 129 (+396.15%)
Mutual labels:  chess-engine, chess
littlewing
Chess engine written in Rust ♛
Stars: ✭ 27 (+3.85%)
Mutual labels:  chess-engine, chess
Sunfish
Sunfish: a Python Chess Engine in 111 lines of code
Stars: ✭ 2,254 (+8569.23%)
Mutual labels:  chess-engine, chess
Zerofish
An implementation of the AlphaZero algorithm for chess
Stars: ✭ 34 (+30.77%)
Mutual labels:  chess-engine, chess
fastchess
Predicts the best chess move with 27.5% accuracy by a single matrix multiplication
Stars: ✭ 75 (+188.46%)
Mutual labels:  chess-engine, chess
OpenChess
A cross-platform chess game.
Stars: ✭ 18 (-30.77%)
Mutual labels:  chess-engine, chess
liground
A free, open-source and modern Chess Variant Analysis GUI for the 21st century
Stars: ✭ 41 (+57.69%)
Mutual labels:  chess-engine, chess
stockfish-chess-web-gui
Responsive chess web GUI to play against the Stockfish 10 chess engine. Multiple web GUI implementations have also been included.
Stars: ✭ 21 (-19.23%)
Mutual labels:  chess-engine, chess
ConvChess
Convolutional Neural Networks learns to play chess moves
Stars: ✭ 14 (-46.15%)
Mutual labels:  chess-engine, chess
Bit-Genie
UCI chess engine in C++
Stars: ✭ 19 (-26.92%)
Mutual labels:  chess-engine, chess
Walleye
A chess engine written from scratch in Rust ♞
Stars: ✭ 82 (+215.38%)
Mutual labels:  chess-engine, chess
Demolito
UCI Chess Engine
Stars: ✭ 41 (+57.69%)
Mutual labels:  chess-engine, chess
uci
A thin wrapper on a uci chess engine
Stars: ✭ 33 (+26.92%)
Mutual labels:  chess-engine, chess
Batch-First
A JIT compiled chess engine which traverses the search tree in batches in a best-first manner, allowing for neural network batching, asynchronous GPU use, and vectorized CPU computations.
Stars: ✭ 27 (+3.85%)
Mutual labels:  chess

magic-bits

A C++ header-only library to aid in developing super fast move-generation and evaluation routines in chess programs.

Background

Efficient move generation can significantly influence the strength of a chess program since moves are generated at almost all internal nodes and possibly leaf / eval nodes of an alpha-beta search tree. Sliding pieces (queen, rook and bishop) have a lot of mobility and the squares they can occupy is dependent on the occupancy of many other self and opponent pieces on the board. This makes it hard to generate moves for sliding pieces efficiently using naive techniques, and "magic bitboards", as is commonly known in the chess programming literature, comes in handy.

The magic bitboards technique, in short, implements a perfect-hashing scheme where a pre-computed magic number is multiplied with the corresponding occupancy bitboard, which is then right-shifted to obtain an index into a pre-computed attack-bitboards table. More details about magic bitboards can be found at https://www.chessprogramming.org/Magic_Bitboards.

Getting Started

Installation

Requires C++17 (for example, g++ with flag -std=c++17) or higher to compile. The library itself is a self-contained C++ header file to be included in your program:

#include "magic-bits/include/magic_bits.hpp"

Usage

// -- move_generator.cpp --

// Include the header file to use the magic_bits::Attacks class.
#include "magic-bits/include/magic_bits.hpp"

namespace {
...
// Instantiate an object of magic_bits::Attacks class once in your program.
// If it is used in multiple files, instantiate once (for example in main())
// and pass it around.
const magic_bits::Attacks attacks;
...
}

// An example function that generates queen moves.
vector<Move> GenerateQueenMoves(const Board& board, int queen_index) {
  // Get the queen attack bitboard.
  uint64_t queen_attacks = attacks.Queen(board.occupancy_bitboard, queen_index);

  // Discard self-piece captures.
  queen_attacks &= ~board.moving_side_bitboard();

  // Convert the moves to a format appropriate in your program:
  // For each move, queen_index will be the "from" index and each of the set
  // bits in `queen_attacks` bitboard form the "to" indices.
  return CreateMoveVectorFromAttackBitBoard(queen_attacks, queen_index);
}

A real world usage example can be found in https://github.com/goutham/nakshatra.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].