def test_crossword_can_get_set_metadata(self): crossword = Crossword(1, 1) self.assertEqual(len(crossword.meta), 15) crossword.meta['creator'] = 'A' self.assertEqual(crossword.meta.creator, 'A') crossword.meta.creator = 'B' self.assertEqual(crossword.meta.creator, 'B') setattr(crossword.meta, 'creator', 'C') self.assertEqual(crossword.meta['creator'], 'C') self.assertEqual(getattr(crossword.meta, 'creator'), 'C') self.assertEqual(list(crossword.meta()), [ ('contributor', None), ('coverage', None), ('creator', 'C'), ('date', None), ('description', None), ('format', None), ('identifier', None), ('language', None), ('publisher', None), ('relation', None), ('rights', None), ('source', None), ('subject', None), ('title', None), ('type', None), ]) with self.assertRaises(AttributeError): crossword.meta.doesnotexist
def main(): """App entry point.""" puzzle = read_json_data('./data/crossword.json') words = read_json_data('./data/words.json') crossword_game = Crossword(puzzle, words) result = crossword_game.find_words() return result
def test_select_unassigned_variable(): crossword = Crossword("./Project3/crossword/data/test.txt", "./Project3/crossword/data/test_words.txt") creator = CrosswordCreator(crossword) creator.enforce_node_consistency() actual = creator.select_unassigned_variable(dict()) expected = Variable(0, 0, 'across', 3) assert expected == actual
def test_order_domain_values(): crossword = Crossword("./Project3/crossword/data/structure0.txt", "./Project3/crossword/data/words0.txt") creator = CrosswordCreator(crossword) creator.enforce_node_consistency() creator.ac3() across_3 = Variable(0, 1, 'across', 3) creator.order_domain_values(across_3, dict())
def test_invalid_cell_coordinates_raises_exception(self): crossword = Crossword(15, 15) with self.assertRaises(IndexError) as cm: crossword[-5, -5] self.assertEqual(str(cm.exception), "Invalid cell coordinates: (-5, -5)") crossword = Crossword(3, 3) with self.assertRaises(IndexError) as cm: crossword[-5, -5] self.assertEqual(str(cm.exception), "Invalid cell coordinates: (-5, -5)") crossword = Crossword(3, 3) with self.assertRaises(IndexError) as cm: crossword[3, 3] self.assertEqual(str(cm.exception), "Invalid cell coordinates: (3, 3)")
def test_order_domain_values_2(): crossword = Crossword("./Project3/crossword/data/test.txt", "./Project3/crossword/data/test_words.txt") creator = CrosswordCreator(crossword) creator.enforce_node_consistency() across_3 = Variable(0, 0, 'across', 3) expected = ['BAT','DAT','CAT'] actual = creator.order_domain_values(across_3, dict()) assert expected == actual
def test_get_neighbors(): crossword = Crossword("./Project3/crossword/data/structure0.txt", "./Project3/crossword/data/words0.txt") creator = CrosswordCreator(crossword) creator.enforce_node_consistency() x = Variable(4, 1, 'across', 4) actual = (creator.crossword.neighbors(x)) expected = {Variable(0, 1, 'down', 5), Variable(1, 4, 'down', 4)} assert actual == expected
def test_revise(): crossword = Crossword("./Project3/crossword/data/structure0.txt", "./Project3/crossword/data/words0.txt") creator = CrosswordCreator(crossword) creator.enforce_node_consistency() x = Variable(4, 1, 'across', 4) y = Variable(0, 1, 'down', 5) assert creator.revise(x, y) == True assert creator.domains[x] == {'NINE'}
def test_clues_can_be_sorted_using_function(self): crossword = Crossword(15, 15) crossword.clues.across[1] = "Clue 1" crossword.clues.across[10] = "Clue 10" crossword.clues.across[2] = "Clue 2" self.assertEqual(list(crossword.clues.across(sort=int)), [ (1, "Clue 1"), (2, "Clue 2"), (10, "Clue 10"), ])
def test_clues_are_sorted_in_numerical_order(self): crossword = Crossword(15, 15) crossword.clues.across[1] = "Clue 1" crossword.clues.across[10] = "Clue 10" crossword.clues.across[2] = "Clue 2" self.assertEqual(list(crossword.clues.across()), [ (1, "Clue 1"), (2, "Clue 2"), (10, "Clue 10"), ])
def test_clues_are_sorted_in_provided_order_when_not_sorting(self): crossword = Crossword(15, 15) crossword.clues.across["1"] = "Clue 1" crossword.clues.across["10"] = "Clue 10" crossword.clues.across["2"] = "Clue 2" self.assertEqual(list(crossword.clues.across(sort=None)), [ ("1", "Clue 1"), ("10", "Clue 10"), ("2", "Clue 2"), ])
def main(): codeUser, wordObjects = Authentication().login() correctAnswer = True while correctAnswer: userInput = input( 'Would you like to do the wordtrainer (WT) or crossword puzzle (CW): ' ) if userInput.lower() == 'wt': WordTrainer().keepAsking(codeUser, wordObjects) elif userInput.lower() == 'cw': correctAnswer = Crossword(wordObjects)
def test_enforce_node_consistency(): crossword = Crossword("./Project3/crossword/data/structure0.txt", "./Project3/crossword/data/words0.txt") creator = CrosswordCreator(crossword) creator.enforce_node_consistency() for var in creator.domains: for word in creator.domains[var]: assert len(word) == var.length x = Variable(4, 1, 'across', 4) y = Variable(0, 1, 'down', 5) assert creator.domains[x] == {'FOUR', 'FIVE', 'NINE'} assert creator.domains[y] == {'THREE', 'SEVEN', 'EIGHT'}
def test_crossword_can_iterate_over_all_clues(self): crossword = Crossword(5, 5) crossword.clues.across[1] = "This is an across clue 1" crossword.clues.across[2] = "This is an across clue 2" crossword.clues.across[3] = "This is an across clue 3" crossword.clues.down[2] = "This is a down clue" clues = list(crossword.clues.all()) self.assertEqual(clues, [ ('across', 1, "This is an across clue 1"), ('across', 2, "This is an across clue 2"), ('across', 3, "This is an across clue 3"), ('down', 2, "This is a down clue"), ])
def test_crossword_can_iterate_over_clues_of_direction(self): crossword = Crossword(5, 5) crossword.clues.across[1] = "This is an across clue" crossword.clues.across[2] = "This is an across clue" crossword.clues.across[3] = "This is an across clue" crossword.clues.down[2] = "This is a down clue" self.assertEqual(list(crossword.clues.across()), [ (1, "This is an across clue"), (2, "This is an across clue"), (3, "This is an across clue"), ]) self.assertEqual(list(crossword.clues.down()), [ (2, "This is a down clue"), ])
def test_crossword_supports_explicit_iteration(self): crossword = Crossword(15, 15) for y in range(crossword.height): for x in range(crossword.width): self.assertEqual(crossword[x, y], {}) for x, y in crossword.cells: self.assertEqual(crossword[x, y], {}) self.assertEqual(len(list(crossword.cells)), crossword.width * crossword.height) with self.assertRaises(AttributeError): crossword.cells = None
def test_ac3(): crossword = Crossword("./Project3/crossword/data/structure0.txt", "./Project3/crossword/data/words0.txt") creator = CrosswordCreator(crossword) creator.enforce_node_consistency() actual = creator.ac3() expected = True assert expected == actual across_3 = Variable(0, 1, 'across', 3) down_5 = Variable(0, 1, 'down', 5) acrross_4 = Variable(4, 1, 'across', 4) down_4 = Variable(1, 4, 'down', 4) assert creator.domains[across_3] == {"SIX"} assert creator.domains[down_5] == {"SEVEN"} assert creator.domains[acrross_4] == {"NINE"} assert creator.domains[down_4] == {"FIVE", "NINE"}
def test_clues_in_both_directions_sorted_with_function(self): crossword = Crossword(15, 15) crossword.clues.across[1] = "Clue 1" crossword.clues.across[10] = "Clue 10" crossword.clues.across[2] = "Clue 2" crossword.clues.down[1] = "Clue 1" crossword.clues.down[10] = "Clue 10" crossword.clues.down[2] = "Clue 2" self.assertEqual(list(crossword.clues.all(sort=int)), [ ('across', 1, "Clue 1"), ('across', 2, "Clue 2"), ('across', 10, "Clue 10"), ('down', 1, "Clue 1"), ('down', 2, "Clue 2"), ('down', 10, "Clue 10"), ])
def test_crossword_can_get_set_clues(self): crossword = Crossword(1, 1) crossword.clues.across[1] = "The clue" self.assertEqual(crossword.clues.across[1], "The clue") self.assertEqual(len(crossword.clues.across), 1) self.assertEqual(len(crossword.clues.down), 0) crossword.clues.down[1] = "Other clue" self.assertEqual(crossword.clues.down[1], "Other clue") self.assertEqual(crossword.clues.across[1], "The clue") self.assertEqual(crossword.clues.across, { 1: "The clue", }) self.assertEqual(crossword.clues.down, { 1: "Other clue", }) with self.assertRaises(AttributeError): crossword.clues.doesnotexist
def main(): # Check usage if len(sys.argv) not in [3, 4]: sys.exit("Usage: python generate.py structure words [output]") # Parse command-line arguments structure = sys.argv[1] words = sys.argv[2] output = sys.argv[3] if len(sys.argv) == 4 else None # Generate crossword crossword = Crossword(structure, words) creator = CrosswordCreator(crossword) assignment = creator.solve() # Print result if assignment is None: print("No solution.") else: creator.print(assignment) if output: creator.save(assignment, output)
def test_crossword_can_be_converted_to_dictionary(self): crossword = Crossword(5, 10) crossword.meta.creator = "The author" crossword.clues.across[1] = "This is a clue" self.assertEqual( crossword.content, { 'type': None, 'width': 5, 'height': 10, 'cells': [[{}] * 5] * 10, 'metadata': { 'contributor': None, 'coverage': None, 'creator': "The author", 'date': None, 'description': None, 'format': None, 'identifier': None, 'language': None, 'publisher': None, 'relation': None, 'rights': None, 'source': None, 'subject': None, 'title': None, 'type': None, }, 'clues': { 'across': { 1: "This is a clue", }, 'down': {}, }, 'block': None, 'empty': None, 'format': {}, })
def test_medium_crossword(): w = [ Word(6), # 1 Across Word(6), # 1 Down Word(3), # 2d Word(3), # 3d Word(6), # 4d Word(7), # 5a Word(3), # 6d Word(7), # 7a Word(3), # 8d Word(3), # 9d Word(6), # 10a ] w[0].addCollision(w[1], 0, 0) w[0].addCollision(w[2], 2, 0) w[0].addCollision(w[3], 4, 0) w[1].addCollision(w[5], 2, 0) w[1].addCollision(w[7], 4, 0) w[2].addCollision(w[5], 2, 2) w[3].addCollision(w[5], 2, 4) w[4].addCollision(w[5], 1, 6) w[4].addCollision(w[7], 3, 6) w[4].addCollision(w[10], 5, 5) w[5].addCollision(w[6], 3, 1) w[6].addCollision(w[7], 2, 3) w[7].addCollision(w[8], 2, 0) w[7].addCollision(w[9], 4, 0) w[8].addCollision(w[10], 2, 1) w[9].addCollision(w[10], 2, 3) dictionary = WordLengthDictionary() dictionary.loadFromFile("/usr/share/dict/words") xw = Crossword(w, dictionary) res = xw.solve() print(res) assert (res > 0)
from crossword import Crossword import random test_word1 = word_dictionary[random.randint(0, 52250)] test_word2 = word_dictionary[random.randint(0, 52250)] test_word3 = word_dictionary[random.randint(0, 52250)] def check_direction(spots): if spots[0][0] != spots[0][1]: return 'down' else: return 'across' cw = Crossword() possible_spots1 = cw.board.get_sequences(len(test_word1)) possible_spots2 = cw.board.get_sequences(len(test_word2)) possible_spots3 = cw.board.get_sequences(len(test_word3)) if possible_spots1: cw.insert_word(test_word1, possible_spots1[0], check_direction(possible_spots1[0])) if possible_spots2: cw.insert_word(test_word2, possible_spots2[0], check_direction(possible_spots2[0])) if possible_spots3: cw.insert_word(test_word3, possible_spots3[0], check_direction(possible_spots3[0])) cw.visualize() print(cw.words.across, cw.words.down)
def test_crossword_can_set_dict_as_cell(self): crossword = Crossword(3, 3) crossword[0, 0] = {'cell': 'A'} self.assertEqual(crossword[0, 0].cell, 'A')
from crossword import Crossword from grid import Grid from square import Square import pyglet if __name__ == '__main__': xword = Crossword( 'http://freecrosswordpuzzles.com.au/printablecrossword.aspx?cw=M1-1-2014', False) pyglet.app.run()
def test_invalid_row_coordinate_raises_exception(self): crossword = Crossword(3, 3) with self.assertRaises(IndexError) as cm: crossword[-5] self.assertEqual(str(cm.exception), "Invalid row coordinate: -5")
def test_crossword_width_needs_to_be_one_or_more(self): with self.assertRaises(ValueError) as cm: Crossword(0, 10) self.assertEqual(str(cm.exception), "Width needs to be at least one")
def test_crossword_can_set_attributes_as_needed(self): crossword = Crossword(10, 10) crossword[3, 3].cell = 'A' self.assertEqual(crossword[3, 3].cell, 'A')
def test_crossword_iteration_over_rows_and_columns(self): crossword = Crossword(10, 5) for row in crossword: self.assertEqual(len(row), 10) self.assertEqual(len([row for row in crossword]), 5)
def test_crossword_can_set_consecutive_values(self): crossword = Crossword(3, 3) crossword[0, 0].cell = 'A' self.assertEqual(crossword[0, 0].cell, 'A') crossword[0, 0].cell = 'B' self.assertEqual(crossword[0, 0].cell, 'B')