Skip to content

Commit

Permalink
Merge pull request #40 from yilmazcabuk/main
Browse files Browse the repository at this point in the history
refactor: separate MultHandler class and other improvements
  • Loading branch information
fabaindaiz authored Oct 21, 2023
2 parents 6b10161 + 7da6162 commit b54bf85
Show file tree
Hide file tree
Showing 4 changed files with 213 additions and 94 deletions.
27 changes: 12 additions & 15 deletions src/interfaces/board.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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

Expand All @@ -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:
Expand All @@ -76,24 +68,25 @@ def create_button(self, swap_count: int) -> BoardButton:
Returns:
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),
)

def initialize_tiles(self) -> None:
"""Initialize board tiles on the board."""

for tile_index in range(25):
coord_index = aux_to_indices(tile_index)

self.tiles[coord_index] = BoardTile(self, tile_index)

def initialize_labels(self) -> None:
"""Initialize board labels on the board."""

for label_index in range(10):
label = BoardLabel(self, label_index)

Expand All @@ -105,11 +98,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()

Expand All @@ -119,6 +114,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
Expand All @@ -131,4 +127,5 @@ def button_command(self, swap: int) -> None:
Args:
swap (int): The number of swaps represented by the clicked button.
"""

raise NotImplementedError()
189 changes: 159 additions & 30 deletions src/interfaces/boardbutton.py
Original file line number Diff line number Diff line change
@@ -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",
)
40 changes: 40 additions & 0 deletions src/interfaces/multhandler.py
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit b54bf85

Please sign in to comment.