From 216cd741d268b93fdcd58ec6511e1dab42846bc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= Date: Fri, 18 Aug 2023 20:12:22 +0000 Subject: [PATCH] check button clickability --- pigeon_magnet_solver/board.py | 15 +++++++++++++-- tests/test_board.py | 5 +++++ 2 files changed, 18 insertions(+), 2 deletions(-) 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"