From a3b68a2b22479d6eda36a6654652b843defec6eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gon=C3=A7alo=20S=2E=20Martins?= Date: Wed, 18 May 2016 17:35:38 +0100 Subject: [PATCH] Fix a bug where words could be repeated in the grid. --- crossword_generator.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crossword_generator.py b/crossword_generator.py index 10816e8..bdffefe 100755 --- a/crossword_generator.py +++ b/crossword_generator.py @@ -129,7 +129,7 @@ def draw_words(words, n_words=100): else: while len(selected_words) < n_words: # Choose candidate randomly - candidate = words[random.randint(0, len(words)-1)] + candidate = words.pop(random.randint(0, len(words)-1)) # Append candidate if it's not already there if candidate not in selected_words: selected_words.append(candidate) @@ -198,7 +198,7 @@ def generate_grid(words, dim): # Fill in grid occupancy = 0 - while occupancy < 0.75: + while occupancy < 0.6: # Generate new possibilities, if needed if not possibilities: print("Getting new words!") @@ -224,7 +224,7 @@ def generate_grid(words, dim): # (we're reading the list backwards, I'd like to replace this with a # comprehension) for i in range(len(possibilities)-1, -1, -1): - if not is_valid(possibilities[i], grid): + if not is_valid(possibilities[i], grid) or possibilities[i]["word"] == new["word"]: possibilities.pop(i)