On failed click: None instead of exception

This commit is contained in:
Jörn-Michael Miehe 2023-08-30 00:49:32 +00:00
parent 91d222f17f
commit 35859acb27
2 changed files with 11 additions and 8 deletions

View file

@ -89,10 +89,13 @@ 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) -> Self: def click(self, index: int) -> Self | None:
# 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!"
if self.rows[row][col] != "1":
# button is not clickable
return None
# get neighbor positions # get neighbor positions
nb_rc = [ nb_rc = [

View file

@ -1,5 +1,3 @@
import pytest
from pigeon_magnet_solver.board import Board from pigeon_magnet_solver.board import Board
@ -13,9 +11,11 @@ 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 (board2 := board.click(4)) is not None
assert board.click(5).binary_repr == (4, 11, 6) assert board2.binary_repr == (4, 14, 3)
assert (board2 := board.click(5)) is not None
assert board2.binary_repr == (4, 11, 6)
for idx in (3, 7): for idx in (3, 7):
with pytest.raises(AssertionError): assert board.click(idx) is None
board.click(idx)