BitBully API Reference
Main
BitBully: A Connect Four AI agent with opening book support.
BitBully
A Connect Four AI agent with opening book support.
Source code in src/bitbully/bitbully.py
__init__(opening_book='default')
Initializes the BitBully agent with the specified opening book.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
opening_book
|
Literal['default', '8-ply', '12-ply', '12-ply-dist'] | None
|
The type of opening book to use. Options are: - "default": Uses the default 12-ply distance-based opening book. - "8-ply": Uses an 8-ply opening book. - "12-ply": Uses a 12-ply opening book. - "12-ply-dist": Uses a 12-ply distance-based opening book. - None: No opening book will be used. |
'default'
|
Source code in src/bitbully/bitbully.py
score_next_moves(board)
Scores all possible moves for the given board state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
board
|
Board
|
The current board state. |
required |
Returns:
| Type | Description |
|---|---|
list[int]
|
list[int]: A list of scores for each column (0-6). |
Source code in src/bitbully/bitbully.py
This module defines the Board class for managing the state of a Connect Four game.
Board
Represents the state of a Connect Four board. Mostly a thin wrapper around BoardCore.
Source code in src/bitbully/board.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 | |
__eq__(value)
Checks equality between two Board instances.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
object
|
The other Board instance to compare against. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if both boards are equal, False otherwise. |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If the other value is not a Board instance. |
Source code in src/bitbully/board.py
__hash__()
Returns a hash of the Board instance for use in hash-based collections.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The hash value of the Board instance. |
__init__(board=None)
Initializes a Board instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
board
|
Sequence[Sequence[int]] | Sequence[int] | str | None
|
Optional initial board state. Accepts: - 2D array (list, tuple, numpy-array) with shape 7x6 or 6x7 - 1D sequence of ints: a move sequence of columns (e.g., [0, 0, 2, 2, 3, 3]) - String: A move sequence of columns as string (e.g., "002233...") - None for an empty board |
None
|
Example
Here we have an example:
It is possible to write some text inbetween the examples.
Source code in src/bitbully/board.py
__ne__(value)
Checks inequality between two Board instances.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
object
|
The other Board instance to compare against. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if both boards are not equal, False otherwise. |
Source code in src/bitbully/board.py
__repr__()
all_positions(up_to_n_ply, exactly_n)
Finds all positions on the board up to a certain ply.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
up_to_n_ply
|
int
|
The maximum ply depth to search. |
required |
exactly_n
|
bool
|
If True, only returns positions at exactly N ply. |
required |
Returns:
| Type | Description |
|---|---|
list[Board]
|
list[Board]: A list of Board instances representing all positions. |
Source code in src/bitbully/board.py
can_win_next(move=None)
Checks if the current player can win in the next move.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
move
|
int | None
|
Optional column to check for an immediate win. If None, checks all columns. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the current player can win next, False otherwise. |
Source code in src/bitbully/board.py
copy()
Creates a copy of the current Board instance.
Returns:
| Name | Type | Description |
|---|---|---|
Board |
Board
|
A new Board instance that is a copy of the current one. |
count_tokens()
Counts the total number of tokens on the board.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The total number of tokens. |
get_legal_moves()
Returns a list of legal moves (columns) that can be played.
Returns:
| Type | Description |
|---|---|
list[int]
|
list[int]: A list of column indices (0-6) where a move can be played. |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If the method is not implemented yet. |
Source code in src/bitbully/board.py
has_win()
Checks if the current player has a winning position.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the current player has a winning position (4-in-a-row), False otherwise. |
is_legal_move(move)
Checks if a move (column) is legal.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
move
|
int
|
The column index (0-6) to check. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the move is legal, False otherwise. |
Source code in src/bitbully/board.py
mirror()
Returns a new Board instance that is the mirror image of the current board.
Returns:
| Name | Type | Description |
|---|---|---|
Board |
Board
|
A new Board instance that is the mirror image. |
Source code in src/bitbully/board.py
moves_left()
Returns the number of moves left until the board is full.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The number of moves left (0-42). |
play_move(move)
Plays a move (column) for the current player.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
move
|
int
|
The column index (0-6) where the token should be placed. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the move was played successfully, False if the move was illegal. |
Source code in src/bitbully/board.py
play_move_on_copy(move)
Plays a move on a copy of the current board and returns the new board.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
move
|
int
|
The column index (0-6) where the token should be placed. |
required |
Returns:
| Type | Description |
|---|---|
Board | None
|
Board | None: A new Board instance with the move played, or None if the move was illegal. |
Source code in src/bitbully/board.py
random_board(n_ply, forbid_direct_win)
staticmethod
Generates a random board state by playing a specified number of random moves.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_ply
|
int
|
The number of random moves to play on the board. |
required |
forbid_direct_win
|
bool
|
If True, the board will have a state that would result in an immediate win. |
required |
Source code in src/bitbully/board.py
reset()
set_board(board)
Sets (overrides) the board to a specific state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
board
|
list[list[int]] | list[int]
|
The new board state. Accepts: - 2D array (list, tuple, numpy-array) with shape 7x6 or 6x7 - 1D sequence of ints: a move sequence of columns (e.g., [0, 0, 2, 2, 3, 3]) - String: A move sequence of columns as string (e.g., "002233...") |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the board was set successfully, False otherwise. |
Source code in src/bitbully/board.py
to_array()
Returns the board state as a 2D array (list of lists).
Returns:
| Type | Description |
|---|---|
list[list[int]]
|
list[list[int]]: A 2D list representing the board state. |
to_string()
Returns a string representation of the board to print on the command line.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A string representing the board (e.g., "002233..."). |
uid()
Returns a unique identifier for the current board state.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
A unique integer identifier for the board state. |
GUI module for the BitBully Connect-4 interactive widget.
GuiC4
A class which allows to create an interactive Connect-4 widget.
GuiC4 is an interactive Connect-4 graphical user interface (GUI) implemented using Matplotlib, IPython widgets, and a backend agent from the BitBully engine. It provides the following main features:
- Interactive Game Board: Presents a dynamic 6-row by 7-column Connect-4 board with clickable board cells.
- Matplotlib Integration: Utilizes Matplotlib figures to render high-quality game visuals directly within Jupyter notebook environments.
- User Interaction: Captures and processes mouse clicks and button events, enabling intuitive gameplay via either direct board interaction or button controls.
- Undo/Redo Moves: Supports undo and redo functionalities, allowing users to navigate through their move history during gameplay.
- Automated Agent Moves: Incorporates BitBully, a Connect-4 backend engine, enabling computer-generated moves and board evaluations.
- Game State Handling: Detects game-over scenarios, including win/draw conditions, and provides immediate user feedback through popup alerts.
Attributes:
| Name | Type | Description |
|---|---|---|
notify_output |
Output
|
Output widget for notifications and popups. |
Examples:
Generally, you should this method to retreive and display the widget.
Source code in src/bitbully/gui_c4.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 | |
__init__()
Init the GuiC4 widget.
Source code in src/bitbully/gui_c4.py
destroy()
get_widget()
Get the widget.
Examples:
Generally, you should this method to retreive and display the widget.
Returns:
| Name | Type | Description |
|---|---|---|
AppLayout |
AppLayout
|
the widget. |