BitBully 0.0.56-a6
Loading...
Searching...
No Matches
bitbully.py
1"""BitBully: A Connect Four AI agent with opening book support."""
2
3import importlib
4import importlib.resources
5import pathlib
6from importlib.resources.abc import Traversable
7from typing import Literal
8
9from bitbully import bitbully_core
10
11from .board import Board
12
13
15 """A Connect Four AI agent with opening book support."""
16
17 def __init__(self, opening_book: Literal["default", "8-ply", "12-ply", "12-ply-dist"] | None = "default") -> None:
18 """Initializes the BitBully agent with the specified opening book.
19
20 Args:
21 opening_book (Literal["default", "8-ply", "12-ply", "12-ply-dist"] | None):
22 The type of opening book to use. Options are:
23 - "default": Uses the default 12-ply distance-based opening book.
24 - "8-ply": Uses an 8-ply opening book.
25 - "12-ply": Uses a 12-ply opening book.
26 - "12-ply-dist": Uses a 12-ply distance-based opening book.
27 - None: No opening book will be used.
28 """
29 self.opening_book_type = opening_book
30
31 db_path: Traversable | str | None = None
32 if opening_book == "default":
33 db_path = importlib.resources.files("bitbully").joinpath("assets/book_12ply_distances.dat")
34 elif opening_book == "8-ply":
35 db_path = importlib.resources.files("bitbully").joinpath("assets/book_8ply.dat")
36 elif opening_book == "12-ply":
37 db_path = importlib.resources.files("bitbully").joinpath("assets/book_12ply.dat")
38 elif opening_book == "12-ply-dist":
39 db_path = importlib.resources.files("bitbully").joinpath("assets/book_12ply_distances.dat")
40
41 if db_path:
42 self.bitbully_agent = bitbully_core.BitBullyCore(pathlib.Path(str(db_path)))
43 else:
44 self.bitbully_agent = bitbully_core.BitBullyCore()
45
46 def score_next_moves(self, board: Board) -> list[int]:
47 """Scores all possible moves for the given board state.
48
49 Args:
50 board (Board):
51 The current board state.
52
53 Returns:
54 list[int]: A list of scores for each column (0-6).
55 """
56 return self.bitbully_agent.scoreMoves(board._board)
None __init__(self, Literal["default", "8-ply", "12-ply", "12-ply-dist"]|None opening_book="default")
Definition bitbully.py:17
list[int] score_next_moves(self, Board board)
Definition bitbully.py:46