BitBully 0.0.78
A fast, perfect-play Connect-4 engine in modern C++
Loading...
Searching...
No Matches
Usage Examples

A handful of recipes for the most common solver tasks.

Evaluating a position

#include "BitBully.h"
(void)b.play("4435"); // 1-based column indices encoded as digits
const int score = solver.mtdf(b, 0);
std::cout << "score = " << score
<< ", plies left = "
Connect-4 search engine that operates on BitBully::Board.
Perfect-play Connect-4 solver.
Definition BitBully.h:45
static int scoreToMovesLeft(const int score, const Board &b) noexcept
Convert a solver score to the number of moves until the game ends.
Definition BitBully.h:115
int mtdf(const Board &b, const int firstGuess, const int maxDepth=-1) noexcept
Solve a position using the MTD(f) driver.
Definition BitBully.h:188
Connect-4 position represented as a pair of 64-bit bitboards.
Definition Board.h:204
bool play(int column)
Drop a stone of the player to move into column.
Definition Board.cpp:137

Ranking all candidate moves

BitBully::Board b; // any position
const auto scores = solver.scoreMoves(b);
for (int c = 0; c < BitBully::Board::N_COLUMNS; ++c) {
std::cout << "column " << c << ": " << scores[c] << '\n';
}
auto scoreMoves(const Board &b, const int maxDepth=-1)
Evaluate every column move from b.
Definition BitBully.h:499
static constexpr int N_COLUMNS
Number of columns in a Connect-4 grid.
Definition Board.h:211

Illegal moves receive the sentinel score -1000.

Loading an opening book

BitBully::BitBully solver{"books/12ply.book"};
if (!solver.isBookLoaded()) {
std::cerr << "could not load book\n";
}
bool isBookLoaded() const
Was an opening book successfully loaded?
Definition BitBully.h:72

BitBully infers the book flavour (8-/12-ply, with or without distance-to-mate) from the file size.

Calling the engine from Python

from bitbully import BitBully, Board
b = Board()
b.play("4435")
solver = BitBully()
print(solver.mtdf(b, 0))
Top-level namespace for the BitBully Connect-4 engine.
Definition BitBully.h:26