diff --git a/pigeon_magnet_solver/board.py b/pigeon_magnet_solver/board.py index 7d46c9a..bc63fc4 100644 --- a/pigeon_magnet_solver/board.py +++ b/pigeon_magnet_solver/board.py @@ -39,7 +39,7 @@ class Board: # check buttons length = self.height * self.width assert all( - button >= 0 and button < length + button in range(length) for button in self.buttons ), "Invalid buttons!" @@ -72,5 +72,16 @@ class Board: for column in self.columns ) + def __idx_to_xy(self, index: int) -> tuple[int, int]: + return index % self.width, index // self.width + + def is_clickable(self, index: int) -> bool: + # check index + assert index in self.buttons, f"{index} is not a button!" + + # check is button + x, y = self.__idx_to_xy(index) + return self.rows[y][x] == "1" + def click(self, index: int) -> None: - pass + assert self.is_clickable(index), f"{index} is not clickable!" diff --git a/tests/test_board.py b/tests/test_board.py index 9b3c5fd..18d52e7 100644 --- a/tests/test_board.py +++ b/tests/test_board.py @@ -10,3 +10,8 @@ def test_default(): assert board.columns == ("001x", "1101", "110x"), "Wrong columns" assert board.binary_repr == (4, 11, 3), "Wrong binary repr" assert board.solution_class == (1, 3, 2), "Wrong solution class" + + assert board.is_clickable(3) is False, "Button 3 should be inactive" + assert board.is_clickable(4) is True, "Button 4 should be active" + assert board.is_clickable(5) is True, "Button 5 should be active" + assert board.is_clickable(7) is False, "Button 7 should be inactive"