solver
This module provides the Connect Four AI agent "BitBully" with opening book support.
Classes:
| Name | Description |
|---|---|
BitBully |
A Connect Four AI agent with optional opening book support. |
Attributes:
| Name | Type | Description |
|---|---|---|
OpeningBookName |
TypeAlias
|
Name of the opening book used by the BitBully engine. |
OpeningBookName
module-attribute
Name of the opening book used by the BitBully engine.
Possible values:
- "default": Alias for "12-ply-dist".
- "8-ply": 8-ply opening book (win/loss only).
- "12-ply": 12-ply opening book (win/loss only).
- "12-ply-dist": 12-ply opening book with distance-to-win information.
-
BitBully API Reference
solverBitBullyopening_book_type
BitBully
BitBully(opening_book: OpeningBookName | None = 'default')
A Connect Four AI agent with optional opening book support.
Todo: - We have to describe the scoring scheme (range of values and their meaning).
This class is a high-level Python wrapper around
bitbully_core.BitBullyCore.
It integrates the packaged BitBully Databases opening books and
operates on bitbully.Board objects.
Notes
- If an opening book is enabled, it is used automatically for early-game positions.
- For deeper positions or positions outside the database horizon, the engine falls back to search-based evaluation.
Example
from bitbully import BitBully, Board
agent = BitBully()
board, _ = Board.random_board(14, forbid_direct_win=True)
print(board)
# All three search methods should agree on the score
score_mtdf = agent.mtdf(board)
score_negamax = agent.negamax(board)
score_null_window = agent.null_window(board)
assert score_negamax == score_null_window == score_mtdf
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
OpeningBookName | None
|
Which opening book to load.
|
'default'
|
TODO: Example for initialization with different books.
Methods:
| Name | Description |
|---|---|
__repr__ |
Return a concise string representation of the BitBully agent. |
get_node_counter |
Return the number of nodes visited since the last reset. |
is_book_loaded |
Check whether an opening book is loaded. |
load_book |
Load an opening book from a file path. |
mtdf |
Evaluate a position using the MTD(f) algorithm. |
negamax |
Evaluate a position using negamax search. |
null_window |
Evaluate a position using a null-window search. |
reset_book |
Unload the currently loaded opening book (if any). |
reset_node_counter |
Reset the internal node counter. |
reset_transposition_table |
Clear the internal transposition table. |
score_all_moves |
Score all legal moves for the given board state. |
score_move |
Evaluate a single move for the given board state. |
Attributes:
| Name | Type | Description |
|---|---|---|
opening_book_type |
OpeningBookName | None
|
|
Source code in src/bitbully/solver.py
__repr__
__repr__() -> str
get_node_counter
get_node_counter() -> int
Return the number of nodes visited since the last reset.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Number of visited nodes. |
Example
-
BitBully API Reference
solverBitBullyreset_node_counter
Source code in src/bitbully/solver.py
is_book_loaded
is_book_loaded() -> bool
Check whether an opening book is loaded.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
|
Example
Source code in src/bitbully/solver.py
load_book
Load an opening book from a file path.
This is a thin wrapper around
bitbully_core.BitBullyCore.loadBook.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
Path to the opening book file. If empty or invalid, no book is loaded. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
|
Example
from bitbully import BitBully
from pathlib import Path
import bitbully_databases as bbd
db_path = bbd.BitBullyDatabases.get_database_path("default")
agent = BitBully(opening_book=None) # start without book
assert agent.is_book_loaded() is False
success = agent.load_book(db_path)
assert agent.is_book_loaded() is True
Source code in src/bitbully/solver.py
mtdf
mtdf(board: Board, first_guess: int = 0) -> int
Evaluate a position using the MTD(f) algorithm.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Board
|
The board position to evaluate. |
required |
|
int
|
Initial guess for the score (often 0). |
0
|
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The evaluation score. |
Example
Source code in src/bitbully/solver.py
negamax
Evaluate a position using negamax search.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Board
|
The board position to evaluate. |
required |
|
int
|
Alpha bound. |
-1000
|
|
int
|
Beta bound. |
1000
|
|
int
|
Search depth in plies. |
0
|
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The evaluation score returned by the engine. |
Example
Source code in src/bitbully/solver.py
null_window
Evaluate a position using a null-window search.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Board
|
The board position to evaluate. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The evaluation score. |
Example
Source code in src/bitbully/solver.py
reset_book
Unload the currently loaded opening book (if any).
This resets the engine to search-only mode until another opening book is loaded.
Example
Source code in src/bitbully/solver.py
reset_node_counter
Reset the internal node counter.
Example:
See get_node_counter for usage.
reset_transposition_table
score_all_moves
Score all legal moves for the given board state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Board
|
The current board state. |
required |
Returns:
| Type | Description |
|---|---|
list[int]
|
list[int]: A list of 7 integers, one per column (0-6). Higher values generally indicate better moves for the player to move. |
Example
```python from bitbully import BitBully, Board
agent = BitBully() board = Board() scores = agent.score_all_moves(board) assert scores == [-2, -1, 0, 1, 0, -1, -2] # Center column is best on an empty board
```
Source code in src/bitbully/solver.py
score_move
Evaluate a single move for the given board state.
This is a wrapper around
bitbully_core.BitBullyCore.scoreMove.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Board
|
The current board state. |
required |
|
int
|
Column index (0-6) of the move to evaluate. |
required |
|
int
|
Initial guess for the score (often 0). |
0
|
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The evaluation score of the move. |