click -> clone

This commit is contained in:
Jörn-Michael Miehe 2023-08-27 21:52:48 +00:00
parent 03b6d66c5b
commit 91d222f17f
2 changed files with 12 additions and 3 deletions

View file

@ -89,7 +89,7 @@ class Board:
old_row = self.rows[row] old_row = self.rows[row]
self.rows[row] = old_row[:col] + value + old_row[col+1:] 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 # check is clickable
row, col = index // self.width, index % self.width row, col = index // self.width, index % self.width
assert self.rows[row][col] == "1", f"{index} is not clickable!" assert self.rows[row][col] == "1", f"{index} is not clickable!"
@ -105,6 +105,12 @@ class Board:
# rotate neighbors # rotate neighbors
nb_rot = rotate_r([self[rc] for rc in nb_rc]) 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): for rc, val in zip(nb_rc, nb_rot):
self[rc] = val result[rc] = val
return result

View file

@ -13,6 +13,9 @@ def test_default():
assert board.binary_repr == (4, 11, 3), "Wrong binary repr" assert board.binary_repr == (4, 11, 3), "Wrong binary repr"
assert board.solution_class == (1, 3, 2), "Wrong solution class" 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): for idx in (3, 7):
with pytest.raises(AssertionError): with pytest.raises(AssertionError):
board.click(idx) board.click(idx)