From f472943d056d35c1e8c8d7e915d8b0d274dfb8c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gon=C3=A7alo=20S=2E=20Martins?= Date: Tue, 17 May 2016 22:04:24 +0100 Subject: [PATCH] Add a function to generate all possibilities from the grid dimensions and the word list. --- crossword_generator.py | 48 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 7 deletions(-) diff --git a/crossword_generator.py b/crossword_generator.py index 5e83d7c..bff3acf 100755 --- a/crossword_generator.py +++ b/crossword_generator.py @@ -11,23 +11,46 @@ import random # Auxiliary Functions -def generate_possibilities(words): +def generate_possibilities(words, dim): """ This function generates the possibilities (see generate_grid) """ # Initialize the list of possibilities possibilities = [] # For every word in the list - #for word in words: + for word in words: + # D = E + for i in range(dim[0]): + for j in range(dim[1] - len(word)): + possibilities.append({"word": word, + "location": [i, j], + "D": "E"}) + + # D = S + for i in range(dim[0] - len(word)): + for j in range(dim[1]): + possibilities.append({"word": word, + "location": [i, j], + "D": "S"}) + + # ... and return all of the possibilities. + return possibilities def is_valid(possibility, grid): """ This function determines whether a possibility is still valid in the - given grid. + given grid. (see generate_grid) TODO: Explain rules """ ... +def add_possibility_to_grid(possibility, grid): + """ Adds a possibility to the given grid, which is modified in-place. + (see generate_grid) + """ + ... + + # Basic Functions def read_word_list(filename, n_words=100): """ This function reads the file and returns the words read. It expects a @@ -75,7 +98,18 @@ def generate_grid(words, dim): p["location"] = the [i,j] list with the location p["D"] = the direction of the possibility (E for ->, S for down) """ - ... + print("Generating {} grid with {} words.".format(dim, len(words))) + # Initialize grid + grid = [[-1]*dim[1]]*dim[0] + print("Initial grid:") + write_grid(grid, True) + + # Generate all possibilities + possibilities = generate_possibilities(words, dim) + #print(possibilities) + #print("Generated {} possibilities".format(len(possibilities))) + + def write_grid(grid, screen=False, out_file="table.tex"): @@ -137,7 +171,7 @@ def test_write_grid(): if __name__ == "__main__": words = read_word_list("words.txt") - print(words) - #grid = generate_grid(words, [20,20]) - #write_grid(grid) + #print(words) + grid = generate_grid(words, [20,20]) + write_grid(grid) #test_write_grid()