From c32fc6894276ed40c30e3cfc9230a346573ec7e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gon=C3=A7alo=20S=2E=20Martins?= Date: Wed, 18 May 2016 13:06:23 +0100 Subject: [PATCH] Add new rules for possibility exclusion and a new stopping condition. --- crossword_generator.py | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/crossword_generator.py b/crossword_generator.py index 0a2cbbf..300ad1a 100755 --- a/crossword_generator.py +++ b/crossword_generator.py @@ -60,13 +60,34 @@ def is_valid(possibility, grid): # Detect collisions for k, letter in enumerate(list(word)): if D is "E": + # Collisions if grid[i][j+k] != 0 and grid[i][j+k] != letter: return False + # Proximity + if grid[i][j+k] != letter: + if (i < len(grid) - 1 and grid[i+1][j+k] != 0) or (i > 0 and grid[i-1][j+k]): + return False if D is "S": + # Collisions if grid[i+k][j] != 0 and grid[i+k][j] != letter: return False + # Proximity + if grid[i+k][j] != letter: + if (j < len(grid[0]) - 1 and grid[i+k][j+1] != 0) or (i > 0 and grid[i+k][j-1]): + return False + + # Start and End + if D is "E": + if j > 0 and grid[i][j-1] != 0: + return False + if D is "S": + if i > 0 and grid[i-1][j] != 0: + return False # Detect proximity + #for k, letter in enumerate(list(word)): + # if D is "E": + # if D is "S": # If we can't find any collisions, it must be okay! return True @@ -162,9 +183,9 @@ def generate_grid(words, dim): # Fill in grid occupancy = 0 - while occupancy < 0.5: + while occupancy < 0.5 and possibilities: # Add new possibility - new = possibilities[random.randint(0, len(possibilities)-1)] + new = possibilities.pop(random.randint(0, len(possibilities)-1)) # Debug prints #print("Adding new word:")