Skip to content

Commit

Permalink
fix unchecked words
Browse files Browse the repository at this point in the history
  • Loading branch information
pmaher86 committed Jan 5, 2024
1 parent db1402d commit 0136570
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
18 changes: 10 additions & 8 deletions src/blacksquare/crossword.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def __init__(
cells = [Cell(self, (i, j), grid[i][j]) for i, j in np.ndindex(*shape)]
self._grid = np.array(cells, dtype=object).reshape(shape)

if symmetry.requires_square and self._num_rows != self._num_cols:
if symmetry is not None and symmetry.requires_square and self._num_rows != self._num_cols:
raise ValueError(f"{symmetry.value} symmetry requires a square grid.")

self._numbers = np.zeros_like(self._grid, dtype=int)
Expand Down Expand Up @@ -502,20 +502,22 @@ def set_word(self, word_index: WordIndex, value: str) -> None:
word_mask = self._get_word_mask(word_index)
cells = self._grid[word_mask]
cross_indices = [
(direction.opposite, n)
(direction.opposite, n) if n else None
for n in self._get_direction_numbers(direction.opposite)[word_mask]
]
for i in range(len(value)):
cells[i].value = value[i]
edge = (word_index, cross_indices[i])
if cells[i].value == EMPTY:
cross_index = cross_indices[i]
edge = (word_index, cross_index) if cross_index else None
if cells[i].value == EMPTY and edge:
self._dependency_graph.add_edge(*edge)
elif edge in self._dependency_graph.edges:
elif edge and edge in self._dependency_graph.edges:
self._dependency_graph.remove_edge(*edge)
for wi in [word_index] + cross_indices:
word = self[wi]
if not word.is_open() and wi in self._dependency_graph.nodes:
self._dependency_graph.remove_node(wi)
if wi:
word = self[wi]
if not word.is_open() and wi in self._dependency_graph.nodes:
self._dependency_graph.remove_node(wi)

def set_cell(self, index: CellIndex, value: CellValue) -> None:
"""Sets a cell to a new value.
Expand Down
20 changes: 15 additions & 5 deletions tests/test_crossword.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ def test_invalid_index(self, xw, bad_index):
with pytest.raises(IndexError):
xw[bad_index] = "A"

def test_unchecked_word_indexing(self):
xw = Crossword(5, 5, symmetry=None)
ind = [1, 2, 3]
for i, j in zip(ind, ind[::-1]):
xw[i, j] = BLACK
xw[DOWN, 1] = "ABCDE"
xw[DOWN, 1] = " "
xw.fill()


class TestParsing:
def test_parsing_indices(self):
Expand Down Expand Up @@ -80,14 +89,15 @@ def test_clues_after_reparse(self, xw: Crossword):
assert c == ""

def test_single_letter(self):
grid = [['#', '#', '#', '#', 'A'],
['B', 'A', 'D', '#', 'C'],
['#', '#', '#', '#', 'T']]
grid = [
["#", "#", "#", "#", "A"],
["B", "A", "D", "#", "C"],
["#", "#", "#", "#", "T"],
]
xw = Crossword(grid=grid)
assert xw[ACROSS, 2].value == "BAD"



class TestConversion:
def test_to_puz(self, xw, tmp_path):
filename = tmp_path / "test.puz"
Expand All @@ -102,7 +112,7 @@ def test_from_puz(self):
def test_to_pdf(self, xw, tmp_path):
filename = tmp_path / "test.pdf"
xw.to_pdf(filename, ["Line 1", "Line 2"])
assert tmp_path.exists()
assert tmp_path.exists()


class TestCrosswordProperties:
Expand Down

0 comments on commit 0136570

Please sign in to comment.