From ff488e052c52a912464ea89e2474d60754398803 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Y=C4=B1lmaz=20=C3=87ABUK?= Date: Sat, 21 Oct 2023 05:15:15 +0300 Subject: [PATCH 1/3] docs: remove method descriptions --- src/interfaces/board.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/interfaces/board.py b/src/interfaces/board.py index 07c26e5..32700ff 100644 --- a/src/interfaces/board.py +++ b/src/interfaces/board.py @@ -21,17 +21,6 @@ class Board: buttons (List[BoardButton]): A list of board buttons. labels (List[BoardLabel]): A list of board labels. tiles (Dict[Tuple[int, int], BoardTile]): A dictionary mapping coordinates to board tiles. - - Methods: - initialize_components(): Initialize buttons, tiles, and labels on the board. - initialize_buttons(): Initialize board buttons based on the value of SWAP. - create_button(swap_count: int) -> BoardButton: Create a board button for a specific swap count. - initialize_tiles(): Initialize board tiles. - initialize_labels(): Initialize board labels. - set_results(word_list: List[ResultWord]): Set results on the board labels. - reset_labels(): Reset the text on all board labels. - update_labels(word_list: List[ResultWord]): Update the text and paths on board labels. - button_command(swap: int): Handle a button click action, to be implemented in subclasses. """ def __init__(self, app: BaseUI) -> None: @@ -40,6 +29,7 @@ def __init__(self, app: BaseUI) -> None: Args: app (BaseUI): The base user interface for the board. """ + self.app: BaseUI = app self.double_swap: bool = SWAP >= 2 @@ -51,12 +41,14 @@ def __init__(self, app: BaseUI) -> None: def initialize_components(self) -> None: """Initialize the components of the board: buttons, tiles, and labels.""" + self.initialize_buttons() self.initialize_tiles() self.initialize_labels() def initialize_buttons(self) -> None: """Initialize board buttons based on the value of SWAP.""" + swap_options = [0, 1] if self.double_swap: @@ -76,6 +68,7 @@ def create_button(self, swap_count: int) -> BoardButton: Returns: BoardButton: The created board button. """ + label_text = f"{swap_count} Swap" return BoardButton( @@ -87,6 +80,7 @@ def create_button(self, swap_count: int) -> BoardButton: def initialize_tiles(self) -> None: """Initialize board tiles on the board.""" + for tile_index in range(25): coord_index = aux_to_indices(tile_index) @@ -94,6 +88,7 @@ def initialize_tiles(self) -> None: def initialize_labels(self) -> None: """Initialize board labels on the board.""" + for label_index in range(10): label = BoardLabel(self, label_index) @@ -105,11 +100,13 @@ def set_results(self, word_list: List[ResultWord]) -> None: Args: word_list (List[ResultWord]): A list of result words to be displayed on the labels. """ + self.reset_labels() self.update_labels(word_list) def reset_labels(self) -> None: """Reset the text on all board labels.""" + for label in self.labels: label.reset() @@ -119,6 +116,7 @@ def update_labels(self, word_list: List[ResultWord]) -> None: Args: word_list (List[ResultWord]): A list of result words to be displayed on the labels. """ + for label, result in zip(self.labels, word_list): text = result.text() path = result.path @@ -131,4 +129,5 @@ def button_command(self, swap: int) -> None: Args: swap (int): The number of swaps represented by the clicked button. """ + raise NotImplementedError() From 3f845e97191d2c19187ae0e35bbf3cf8965d1241 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Y=C4=B1lmaz=20=C3=87ABUK?= Date: Sat, 21 Oct 2023 10:32:51 +0300 Subject: [PATCH 2/3] refactor: update class and add docstrings - Refactored the BoardButton class in boardbutton.py to improve readability and maintainability. - Added docstrings to the class and its methods for better documentation. - Reorganized code for button positioning and styling. This commit enhances the clarity and maintainability of the BoardButton class, making it easier to understand and modify in the future. --- src/interfaces/board.py | 8 +- src/interfaces/boardbutton.py | 189 ++++++++++++++++++++++++++++------ 2 files changed, 162 insertions(+), 35 deletions(-) diff --git a/src/interfaces/board.py b/src/interfaces/board.py index 32700ff..bd355b8 100644 --- a/src/interfaces/board.py +++ b/src/interfaces/board.py @@ -69,12 +69,10 @@ def create_button(self, swap_count: int) -> BoardButton: BoardButton: The created board button. """ - label_text = f"{swap_count} Swap" - return BoardButton( - board=self, - num=swap_count, - text=label_text, + parent=self.app, + double_swap=self.double_swap, + swap_count=swap_count, command=lambda: self.button_command(swap_count), ) diff --git a/src/interfaces/boardbutton.py b/src/interfaces/boardbutton.py index de05393..0a6eecd 100644 --- a/src/interfaces/boardbutton.py +++ b/src/interfaces/boardbutton.py @@ -1,34 +1,163 @@ -import tkinter as tk -from tkinter.font import Font +from tkinter import ttk from typing import Callable class BoardButton: - """Represents a solve button""" - - def __init__(self, board, num, text, command: Callable): - self.board = board - app = board.app - - self.button = tk.Button(app.window) - self.button["bg"] = "#e9e9ed" - self.button["font"] = Font(family="Times", size=12) - self.button["fg"] = "#000000" - self.button["justify"] = "center" - self.button["text"] = text - self.button["command"] = command - - if self.board.double_swap: - self.button.place( - x=app.HORIZONTAL_PADDING + 67 * num, - y=app.VERTICAL_PADDING + 210, - width=67, - height=30, - ) - else: - self.button.place( - x=app.HORIZONTAL_PADDING + 100 * num, - y=app.VERTICAL_PADDING + 210, - width=100, - height=30, - ) + """ + Represents a button on a game board. + + Attributes: + INITIAL_HORIZONTAL_POSITION (int): The initial horizontal position for buttons. + INITIAL_VERTICAL_POSITION (int): The initial vertical position for buttons. + DOUBLE_SWAP_HORIZONTAL_POSITION (int): The horizontal position for buttons when double swapping. + BUTTON_HEIGHT (int): The height of the button. + + Args: + parent: The parent widget that contains the button. + double_swap (bool): Whether it's a double swap button. + swap_count (int): The number of swaps associated with the button. + command (Callable): The function to execute when the button is clicked. + """ + + INITIAL_HORIZONTAL_POSITION = 100 + INITIAL_VERTICAL_POSITION = 210 + DOUBLE_SWAP_HORIZONTAL_POSITION = 67 + BUTTON_HEIGHT = 30 + + def __init__(self, parent, double_swap: bool, swap_count: int, command: Callable): + """ + Initializes a BoardButton instance. + + Args: + parent: The parent widget that contains the button. + double_swap (bool): Whether it's a double swap button. + swap_count (int): The number of swaps associated with the button. + command (Callable): The function to execute when the button is clicked. + """ + + self.parent = parent + self.double_swap = double_swap + self.swap_count = swap_count + self.command = command + self.button = self.initialize() + + def initialize(self): + """ + Initializes the button, sets its style, and positions it on the screen. + Returns: + ttk.Button: The initialized button. + """ + master = self.parent.window + text = self.get_label() + command = self.command + + button = ttk.Button(master=master, text=text, command=command) + + self.configure_style() + self.set_position(button) + + return button + + def get_label(self): + """ + Generates the label text for the button based on the swap count. + + Returns: + str: The label text. + """ + return ( + f"{self.swap_count} Swap" + if self.swap_count == 1 + else f"{self.swap_count} Swaps" + ) + + def set_position(self, button): + """ + Sets the position of the button on the screen. + + Args: + button (ttk.Button): The button to position. + """ + width, height = self.calculate_size() + horizontal_position, vertical_position = self.calculate_position() + + button.place( + x=horizontal_position, y=vertical_position, width=width, height=height + ) + + def horizontal_position(self): + """ + Calculates the horizontal position based on double swap and swap count. + + Returns: + int: The horizontal position. + """ + return ( + self.DOUBLE_SWAP_HORIZONTAL_POSITION * self.swap_count + if self.double_swap + else self.INITIAL_HORIZONTAL_POSITION * self.swap_count + ) + + def calculate_width(self): + """ + Calculates the button width based on double swap. + + Returns: + int: The button width. + """ + return ( + self.DOUBLE_SWAP_HORIZONTAL_POSITION + if self.double_swap + else self.INITIAL_HORIZONTAL_POSITION + ) + + def calculate_size(self): + """ + Calculates the button size (width and height). + + Returns: + Tuple[int, int]: The button width and height. + """ + width = self.calculate_width() + height = self.BUTTON_HEIGHT + + return width, height + + def calculate_padding(self): + """ + Calculates the horizontal and vertical padding for button positioning. + + Returns: + Tuple[int, int]: The horizontal and vertical padding. + """ + horizontal_padding = self.parent.HORIZONTAL_PADDING + vertical_padding = self.parent.VERTICAL_PADDING + + return horizontal_padding, vertical_padding + + def calculate_position(self): + """ + Calculates the position of the button on the screen. + + Returns: + Tuple[int, int]: The horizontal and vertical position. + """ + horizontal_padding, vertical_padding = self.calculate_padding() + + horizontal_position = horizontal_padding + self.horizontal_position() + vertical_position = vertical_padding + self.INITIAL_VERTICAL_POSITION + + return horizontal_position, vertical_position + + @staticmethod + def configure_style(): + """ + Configures the style of the ttk button. + """ + style = ttk.Style() + style.configure( + style="TButton", + background="#e9e9ed", + font=("Times", 12), + foreground="#000000", + ) From 7da6162a9b9212cbe3ae9c218c05db30acfab7bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Y=C4=B1lmaz=20=C3=87ABUK?= Date: Sat, 21 Oct 2023 11:50:32 +0300 Subject: [PATCH 3/3] refactor: separate MultHandler class This commit separates the `MultHandler` class from `tkinterboard.py` into its own file `multhandler.py`. This modularization makes the codebase cleaner and more organized, promoting better code maintainability. Changes made in this commit: - Moved the `MultHandler` class to the `src/interfaces/multhandler.py` file. - Removed the `MultHandler` class definition from `tkinterboard.py`. No functional changes are made in this commit; it is purely a code organization update. --- src/interfaces/multhandler.py | 40 ++++++++++++++++++++++++++ src/interfaces/tkinterboard.py | 51 ++-------------------------------- 2 files changed, 42 insertions(+), 49 deletions(-) create mode 100644 src/interfaces/multhandler.py diff --git a/src/interfaces/multhandler.py b/src/interfaces/multhandler.py new file mode 100644 index 0000000..571026e --- /dev/null +++ b/src/interfaces/multhandler.py @@ -0,0 +1,40 @@ +from src.interfaces.board import Board + + +class MultHandler: + def __init__(self, board: Board) -> None: + self.board: Board = board + + self.mult_cord: tuple = None + self.letter_mult: int = None + self.letter_cord: tuple = None + + def set_mult_word(self, cord: tuple) -> None: + if self.mult_cord is not None: + self.board.tiles[self.mult_cord].multiplier("black") + + self.mult_cord = cord + self.configure_mult() + + def set_mult_letter(self, cord: tuple, mult: int) -> None: + if self.letter_cord is not None: + self.board.tiles[self.letter_cord].multiplier("black") + + self.letter_mult = mult + self.letter_cord = cord + self.configure_mult() + + def configure_mult(self) -> None: + if self.letter_cord is not None: + self.board.tiles[self.letter_cord].multiplier("gold") + if self.mult_cord is not None: + self.board.tiles[self.mult_cord].multiplier("deep pink") + + def remove_mult(self) -> None: + if self.letter_cord is not None: + self.board.tiles[self.letter_cord].multiplier("black") + if self.mult_cord is not None: + self.board.tiles[self.mult_cord].multiplier("black") + + self.letter_cord = None + self.mult_cord = None diff --git a/src/interfaces/tkinterboard.py b/src/interfaces/tkinterboard.py index 13bb739..a70665b 100644 --- a/src/interfaces/tkinterboard.py +++ b/src/interfaces/tkinterboard.py @@ -1,16 +1,14 @@ -from src.interfaces.board import Board from src.interfaces.baseui import BaseUI +from src.interfaces.board import Board +from src.interfaces.multhandler import MultHandler class TkinterBoard(Board): - """Represents a board with his logic""" - def __init__(self, app: BaseUI) -> None: super().__init__(app) self.mult: MultHandler = MultHandler(self) def button_command(self, swap: int) -> None: - """Execute SpellSolver when a button is pressed""" gameboard_string = "".join(tile.letter() for tile in self.tiles.values()) self.app.gameboard.load(gameboard_string) @@ -24,48 +22,3 @@ def button_command(self, swap: int) -> None: results = self.app.solve(swap) sorted = results.sorted(console=True) self.set_results(sorted) - - -class MultHandler: - """Handle Spellcast word & letter multipliers""" - - def __init__(self, board: Board) -> None: - self.board: Board = board - - self.mult_cord: tuple = None - self.letter_mult: int = None - self.letter_cord: tuple = None - - def set_mult_word(self, cord: tuple) -> None: - """Set a mult_word in a tile""" - if self.mult_cord is not None: - self.board.tiles[self.mult_cord].multiplier("black") - - self.mult_cord = cord - self.configure_mult() - - def set_mult_letter(self, cord: tuple, mult: int) -> None: - """Set a mult_TL in a tile""" - if self.letter_cord is not None: - self.board.tiles[self.letter_cord].multiplier("black") - - self.letter_mult = mult - self.letter_cord = cord - self.configure_mult() - - def configure_mult(self) -> None: - """Change colors of a tile based in the multipliers""" - if self.letter_cord != None: - self.board.tiles[self.letter_cord].multiplier("gold") - if self.mult_cord != None: - self.board.tiles[self.mult_cord].multiplier("deep pink") - - def remove_mult(self) -> None: - """Remove colors of a tile""" - if self.letter_cord != None: - self.board.tiles[self.letter_cord].multiplier("black") - if self.mult_cord != None: - self.board.tiles[self.mult_cord].multiplier("black") - - self.letter_cord = None - self.mult_cord = None