BitBully 0.0.74
Loading...
Searching...
No Matches
bitbully.gui_c4.GuiC4 Class Reference

Public Member Functions

None __init__ (self, dict[str, Connect4Agent]|Sequence[Connect4Agent]|None agents=None, *, bool autoplay=False)
AppLayout get_widget (self)
None destroy (self)

Public Attributes

 m_logger = logging.getLogger(self.__class__.__name__)
dict m_png
 m_n_row
int m_n_col = 6, 7
 m_height = np.zeros(7, dtype=np.int32)
float m_board_size = 3.5
bool is_busy = False
 autoplay = bool(autoplay)
dict agents = {}
str yellow_player = "human"
str red_player = self._agent_names[0] if self._agent_names else "human"
str eval_agent_choice = "auto"
list m_movelist = []
list m_redolist = []
bool m_gameover = False
 dd_yellow
 dd_red
 cb_autoplay
 dd_eval_agent
 player_select_row
 m_status_label
 m_active_player_label
 m_time_row
 ta_moves
 btn_copy_pos
 btn_copy_moves_ag
 move_list_row
list ims
dict m_control_buttons = {}
list m_eval_labels
 m_eval_row
 output = Output()
 m_fig = fig
 m_axs = axs
list m_insert_buttons

Static Public Attributes

widgets notify_output = widgets.Output()

Protected Member Functions

None _create_player_selectors (self)
None _create_status_bar (self)
None _create_move_list_ui (self)
str _position_string (self)
str _moves_ag_string (self)
None _update_move_list_ui (self)
None _copy_to_clipboard (self, str text)
None _copy_position_string (self)
None _copy_moves_ag (self)
int _current_player (self)
str _controller_for_player (self, int player)
Connect4Agent|None _agent_for_player (self, int player)
Connect4Agent|None _agent_for_evaluation (self)
None _reset (self)
npt.NDArray[np.float64] _get_fig_size_px (self)
None _create_control_buttons (self)
None _create_eval_row (self)
None _clear_eval_row (self)
None _evaluate_board (self)
None _computer_move (self)
None _create_board (self)
None _popup (self, str text)
Board _board_from_history (self)
None _insert_token (self, int col, bool reset_redo_list=True)
None _redo_move (self)
None _undo_move (self)
None _update_insert_buttons (self)
int _get_img_idx (self, int col, int row)
None _paint_token (self)
None _create_buttons (self)
HBox _create_column_labels (self)
None _on_field_click (self, mpl_backend_bases.Event event)
None _maybe_autoplay (self)
None _check_winner (self, Board board)

Protected Attributes

list[str] _agent_names = list(self.agents.keys())

Detailed Description

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:
    notify_output (widgets.Output): Output widget for notifications and popups.

Examples:
        Generally, you should this method to retreive and display the widget.

        ```pycon
        >>> %matplotlib ipympl
        >>> c4gui = GuiC4()
        >>> display(c4gui.get_widget())
        ```

Definition at line 21 of file gui_c4.py.

Constructor & Destructor Documentation

◆ __init__()

None bitbully.gui_c4.GuiC4.__init__ ( self,
dict[str, Connect4Agent] | Sequence[Connect4Agent] | None agents = None,
* ,
bool autoplay = False )
Init the GuiC4 widget.

Definition at line 55 of file gui_c4.py.

Member Function Documentation

◆ _agent_for_evaluation()

Connect4Agent | None bitbully.gui_c4.GuiC4._agent_for_evaluation ( self)
protected
Return the agent used for the Evaluate button based on dropdown selection.

Definition at line 384 of file gui_c4.py.

◆ _agent_for_player()

Connect4Agent | None bitbully.gui_c4.GuiC4._agent_for_player ( self,
int player )
protected

Definition at line 378 of file gui_c4.py.

◆ _board_from_history()

Board bitbully.gui_c4.GuiC4._board_from_history ( self)
protected

Definition at line 615 of file gui_c4.py.

◆ _check_winner()

None bitbully.gui_c4.GuiC4._check_winner ( self,
Board board )
protected
Check for Win or draw.

Definition at line 948 of file gui_c4.py.

◆ _clear_eval_row()

None bitbully.gui_c4.GuiC4._clear_eval_row ( self)
protected
Clear all evaluation score labels.

Definition at line 482 of file gui_c4.py.

◆ _computer_move()

None bitbully.gui_c4.GuiC4._computer_move ( self)
protected

Definition at line 540 of file gui_c4.py.

◆ _controller_for_player()

str bitbully.gui_c4.GuiC4._controller_for_player ( self,
int player )
protected

Definition at line 375 of file gui_c4.py.

◆ _copy_moves_ag()

None bitbully.gui_c4.GuiC4._copy_moves_ag ( self)
protected

Definition at line 366 of file gui_c4.py.

◆ _copy_position_string()

None bitbully.gui_c4.GuiC4._copy_position_string ( self)
protected

Definition at line 362 of file gui_c4.py.

◆ _copy_to_clipboard()

None bitbully.gui_c4.GuiC4._copy_to_clipboard ( self,
str text )
protected
Copy text to clipboard in Jupyter (best-effort).

Definition at line 340 of file gui_c4.py.

◆ _create_board()

None bitbully.gui_c4.GuiC4._create_board ( self)
protected

Definition at line 572 of file gui_c4.py.

◆ _create_buttons()

None bitbully.gui_c4.GuiC4._create_buttons ( self)
protected

Definition at line 793 of file gui_c4.py.

◆ _create_column_labels()

HBox bitbully.gui_c4.GuiC4._create_column_labels ( self)
protected
Creates a row with the column labels 'a' to 'g'.

Returns:
    HBox: A row of textboxes containing the columns labels 'a' to 'g'.

Definition at line 808 of file gui_c4.py.

◆ _create_control_buttons()

None bitbully.gui_c4.GuiC4._create_control_buttons ( self)
protected

Definition at line 426 of file gui_c4.py.

◆ _create_eval_row()

None bitbully.gui_c4.GuiC4._create_eval_row ( self)
protected
Create a row of 7 labels to display per-column evaluation scores.

Definition at line 460 of file gui_c4.py.

◆ _create_move_list_ui()

None bitbully.gui_c4.GuiC4._create_move_list_ui ( self)
protected
Create the move list display and clipboard buttons.

Definition at line 266 of file gui_c4.py.

◆ _create_player_selectors()

None bitbully.gui_c4.GuiC4._create_player_selectors ( self)
protected
Create UI controls for player assignment, autoplay, and evaluation agent.

Definition at line 158 of file gui_c4.py.

◆ _create_status_bar()

None bitbully.gui_c4.GuiC4._create_status_bar ( self)
protected
Create a row that shows the computation time of the last agent move.

Definition at line 245 of file gui_c4.py.

◆ _current_player()

int bitbully.gui_c4.GuiC4._current_player ( self)
protected
Return player to move: 1 (Yellow) starts, then alternates.

Definition at line 371 of file gui_c4.py.

◆ _evaluate_board()

None bitbully.gui_c4.GuiC4._evaluate_board ( self)
protected
Compute and display per-column evaluation scores for the current position.

Definition at line 487 of file gui_c4.py.

◆ _get_fig_size_px()

npt.NDArray[np.float64] bitbully.gui_c4.GuiC4._get_fig_size_px ( self)
protected

Definition at line 414 of file gui_c4.py.

◆ _get_img_idx()

int bitbully.gui_c4.GuiC4._get_img_idx ( self,
int col,
int row )
protected
Translates a column and row ID into the corresponding image ID.

Args:
    col (int): column (0-6) of the considered board cell.
    row (int): row (0-5) of the considered board cell.

Returns:
    int: The corresponding image id (0-41).

Definition at line 743 of file gui_c4.py.

◆ _insert_token()

None bitbully.gui_c4.GuiC4._insert_token ( self,
int col,
bool reset_redo_list = True )
protected

Definition at line 618 of file gui_c4.py.

◆ _maybe_autoplay()

None bitbully.gui_c4.GuiC4._maybe_autoplay ( self)
protected
If it's an agent-controlled turn, immediately play its move.

Definition at line 940 of file gui_c4.py.

◆ _moves_ag_string()

str bitbully.gui_c4.GuiC4._moves_ag_string ( self)
protected
Return board as ascii string..

Definition at line 318 of file gui_c4.py.

◆ _on_field_click()

None bitbully.gui_c4.GuiC4._on_field_click ( self,
mpl_backend_bases.Event event )
protected
Based on the column where the click was detected, insert a token.

Args:
    event (mpl_backend_bases.Event): A matplotlib mouse event.

Definition at line 833 of file gui_c4.py.

◆ _paint_token()

None bitbully.gui_c4.GuiC4._paint_token ( self)
protected

Definition at line 757 of file gui_c4.py.

◆ _popup()

None bitbully.gui_c4.GuiC4._popup ( self,
str text )
protected

Definition at line 611 of file gui_c4.py.

◆ _position_string()

str bitbully.gui_c4.GuiC4._position_string ( self)
protected
Return the position encoding compatible with Board(...).

BitBully's Board examples use strings like "341" (columns as digits),
so we follow the same convention.

Definition at line 310 of file gui_c4.py.

◆ _redo_move()

None bitbully.gui_c4.GuiC4._redo_move ( self)
protected

Definition at line 660 of file gui_c4.py.

◆ _reset()

None bitbully.gui_c4.GuiC4._reset ( self)
protected

Definition at line 398 of file gui_c4.py.

◆ _undo_move()

None bitbully.gui_c4.GuiC4._undo_move ( self)
protected

Definition at line 666 of file gui_c4.py.

◆ _update_insert_buttons()

None bitbully.gui_c4.GuiC4._update_insert_buttons ( self)
protected

Definition at line 708 of file gui_c4.py.

◆ _update_move_list_ui()

None bitbully.gui_c4.GuiC4._update_move_list_ui ( self)
protected
Refresh the move list textarea.

Definition at line 324 of file gui_c4.py.

◆ destroy()

None bitbully.gui_c4.GuiC4.destroy ( self)
Destroy and release the acquired resources.

Definition at line 962 of file gui_c4.py.

◆ get_widget()

AppLayout bitbully.gui_c4.GuiC4.get_widget ( self)
Get the widget.

Examples:
    Generally, you should this method to retreive and display the widget.

    ```pycon
    >>> %matplotlib ipympl
    >>> c4gui = GuiC4()
    >>> display(c4gui.get_widget())
    ```

Returns:
    AppLayout: the widget.

Definition at line 852 of file gui_c4.py.

Member Data Documentation

◆ _agent_names

list[str] bitbully.gui_c4.GuiC4._agent_names = list(self.agents.keys())
protected

Definition at line 114 of file gui_c4.py.

◆ agents

dict bitbully.gui_c4.GuiC4.agents = {}

Definition at line 108 of file gui_c4.py.

◆ autoplay

bitbully.gui_c4.GuiC4.autoplay = bool(autoplay)

Definition at line 104 of file gui_c4.py.

◆ btn_copy_moves_ag

bitbully.gui_c4.GuiC4.btn_copy_moves_ag
Initial value:
= Button(
description="📋 Copy ASCII board",
tooltip="Copy the ascii representation of the board",
layout=Layout(width="100%"),
)

Definition at line 280 of file gui_c4.py.

◆ btn_copy_pos

bitbully.gui_c4.GuiC4.btn_copy_pos
Initial value:
= Button(
description="📋 Copy move sequence",
tooltip="Copy the position string used by Board(...), e.g. '3431'",
layout=Layout(width="100%"),
)

Definition at line 275 of file gui_c4.py.

◆ cb_autoplay

bitbully.gui_c4.GuiC4.cb_autoplay
Initial value:
= widgets.Checkbox(
value=bool(self.autoplay),
description="Autoplay",
indent=False,
layout=Layout(width="auto"), # width="200px"
)

Definition at line 179 of file gui_c4.py.

◆ dd_eval_agent

bitbully.gui_c4.GuiC4.dd_eval_agent
Initial value:
= widgets.Dropdown(
options=eval_options,
value=self.eval_agent_choice if self.eval_agent_choice in eval_options else "auto",
description="Eval:",
layout=Layout(width="200px"),
)

Definition at line 187 of file gui_c4.py.

◆ dd_red

bitbully.gui_c4.GuiC4.dd_red
Initial value:
= widgets.Dropdown(
options=player_options,
value=self.red_player if self.red_player in player_options else "human",
description="Red:",
layout=Layout(width="200px"),
)

Definition at line 171 of file gui_c4.py.

◆ dd_yellow

bitbully.gui_c4.GuiC4.dd_yellow
Initial value:
= widgets.Dropdown(
options=player_options,
value=self.yellow_player if self.yellow_player in player_options else "human",
description="Yellow:",
layout=Layout(width="200px"),
)

Definition at line 165 of file gui_c4.py.

◆ eval_agent_choice

str bitbully.gui_c4.GuiC4.eval_agent_choice = "auto"

Definition at line 123 of file gui_c4.py.

◆ ims

list bitbully.gui_c4.GuiC4.ims

Definition at line 404 of file gui_c4.py.

◆ is_busy

bool bitbully.gui_c4.GuiC4.is_busy = False

Definition at line 101 of file gui_c4.py.

◆ m_active_player_label

bitbully.gui_c4.GuiC4.m_active_player_label
Initial value:
= widgets.Label(
value="| Next: 🟡",
layout=Layout(width="20%", justify_content="flex-end", align_items="center"),
)

Definition at line 251 of file gui_c4.py.

◆ m_axs

bitbully.gui_c4.GuiC4.m_axs = axs

Definition at line 605 of file gui_c4.py.

◆ m_board_size

float bitbully.gui_c4.GuiC4.m_board_size = 3.5

Definition at line 99 of file gui_c4.py.

◆ m_control_buttons

dict bitbully.gui_c4.GuiC4.m_control_buttons = {}

Definition at line 427 of file gui_c4.py.

◆ m_eval_labels

list bitbully.gui_c4.GuiC4.m_eval_labels
Initial value:
= [
widgets.Label(
value="",
layout=Layout(justify_content="center", align_items="center", width=width),
)
for _ in range(self.m_n_col)
]

Definition at line 465 of file gui_c4.py.

◆ m_eval_row

bitbully.gui_c4.GuiC4.m_eval_row
Initial value:
= HBox(
self.m_eval_labels,
layout=Layout(
display="flex",
flex_flow="row wrap",
justify_content="center",
align_items="center",
),
)

Definition at line 472 of file gui_c4.py.

◆ m_fig

bitbully.gui_c4.GuiC4.m_fig = fig

Definition at line 604 of file gui_c4.py.

◆ m_gameover

bool bitbully.gui_c4.GuiC4.m_gameover = False

Definition at line 153 of file gui_c4.py.

◆ m_height

bitbully.gui_c4.GuiC4.m_height = np.zeros(7, dtype=np.int32)

Definition at line 97 of file gui_c4.py.

◆ m_insert_buttons

bitbully.gui_c4.GuiC4.m_insert_buttons

Definition at line 623 of file gui_c4.py.

◆ m_logger

bitbully.gui_c4.GuiC4.m_logger = logging.getLogger(self.__class__.__name__)

Definition at line 63 of file gui_c4.py.

◆ m_movelist

bitbully.gui_c4.GuiC4.m_movelist = []

Definition at line 147 of file gui_c4.py.

◆ m_n_col

bitbully.gui_c4.GuiC4.m_n_col = 6, 7

Definition at line 94 of file gui_c4.py.

◆ m_n_row

bitbully.gui_c4.GuiC4.m_n_row

Definition at line 94 of file gui_c4.py.

◆ m_png

dict bitbully.gui_c4.GuiC4.m_png
Initial value:
= {
0: {"plain": png_empty, "corner": png_empty_m, "underline": png_empty_r},
1: {"plain": png_yellow, "corner": png_yellow_m},
2: {"plain": png_red, "corner": png_red_m},
}

Definition at line 88 of file gui_c4.py.

◆ m_redolist

bitbully.gui_c4.GuiC4.m_redolist = []

Definition at line 150 of file gui_c4.py.

◆ m_status_label

bitbully.gui_c4.GuiC4.m_status_label
Initial value:
= widgets.Label(
value="",
layout=Layout(width="80%"),
)

Definition at line 247 of file gui_c4.py.

◆ m_time_row

bitbully.gui_c4.GuiC4.m_time_row
Initial value:
= HBox(
[self.m_status_label, self.m_active_player_label],
layout=Layout(
display="flex",
flex_flow="row",
justify_content="flex-start",
align_items="center",
width="100%",
),
)

Definition at line 255 of file gui_c4.py.

◆ move_list_row

bitbully.gui_c4.GuiC4.move_list_row
Initial value:
= VBox(
[self.btn_copy_pos, self.btn_copy_moves_ag, self.ta_moves],
layout=Layout(
width="200px",
height="100%", # NEW: take all available height in sidebar
align_items="stretch",
flex="1 1 auto", # NEW: allow growing
),
)

Definition at line 294 of file gui_c4.py.

◆ notify_output

widgets bitbully.gui_c4.GuiC4.notify_output = widgets.Output()
static

Definition at line 607 of file gui_c4.py.

◆ output

bitbully.gui_c4.GuiC4.output = Output()

Definition at line 573 of file gui_c4.py.

◆ player_select_row

bitbully.gui_c4.GuiC4.player_select_row
Initial value:
= VBox(
[row1, row2],
layout=Layout(
display="flex",
flex_flow="column",
justify_content="flex-end",
align_items="flex-end",
),
)

Definition at line 235 of file gui_c4.py.

◆ red_player

str bitbully.gui_c4.GuiC4.red_player = self._agent_names[0] if self._agent_names else "human"

Definition at line 119 of file gui_c4.py.

◆ ta_moves

bitbully.gui_c4.GuiC4.ta_moves
Initial value:
= widgets.Textarea(
value="",
description="",
disabled=True,
layout=Layout(width="100%", height="100%"), # was "110px"
)

Definition at line 268 of file gui_c4.py.

◆ yellow_player

str bitbully.gui_c4.GuiC4.yellow_player = "human"

Definition at line 118 of file gui_c4.py.


The documentation for this class was generated from the following file: