diff --git a/pigeon_magnet_solver/board.py b/pigeon_magnet_solver/board.py index b195f41..e43a69f 100644 --- a/pigeon_magnet_solver/board.py +++ b/pigeon_magnet_solver/board.py @@ -89,7 +89,7 @@ class Board: old_row = self.rows[row] self.rows[row] = old_row[:col] + value + old_row[col+1:] - def click(self, index: int) -> None: + def click(self, index: int) -> Self: # check is clickable row, col = index // self.width, index % self.width assert self.rows[row][col] == "1", f"{index} is not clickable!" @@ -105,6 +105,12 @@ class Board: # rotate neighbors nb_rot = rotate_r([self[rc] for rc in nb_rc]) - # set new neighbors + # create new state + result = self.__class__( + rows=self.rows.copy(), + buttons=self.buttons, + ) for rc, val in zip(nb_rc, nb_rot): - self[rc] = val + result[rc] = val + + return result diff --git a/tests/test_board.py b/tests/test_board.py index dcdb9ad..ba596f3 100644 --- a/tests/test_board.py +++ b/tests/test_board.py @@ -13,6 +13,9 @@ def test_default(): assert board.binary_repr == (4, 11, 3), "Wrong binary repr" assert board.solution_class == (1, 3, 2), "Wrong solution class" + assert board.click(4).binary_repr == (4, 14, 3) + assert board.click(5).binary_repr == (4, 11, 6) + for idx in (3, 7): with pytest.raises(AssertionError): board.click(idx)