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_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_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_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(): 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_can_build_solution_from_letters_dictionary(self): crossword = Crossword() crossword.size(3) letters = {} letters[0, 0] = "A" letters[1, 0] = "B" letters[2, 0] = "" letters[0, 1] = "" letters[1, 1] = "C" letters[2, 1] = "D" letters[0, 2] = "E" letters[1, 2] = "F" letters[2, 2] = "G" crossword.build_solution(letters) self.assertEquals(crossword.across_solutions[0], "AB") self.assertEquals(crossword.across_solutions[2], "EFG") self.assertEquals(crossword.down_solutions[0], "BCF")
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_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_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_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 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 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_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)
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_should_just_use_name_for_title_if_no_type(self): crossword = Crossword() crossword.name = "Geoffrey" self.assertEqual(crossword.title(), "Geoffrey")
def test_should_provide_helpful_title_when_no_creator_set(self): crossword = Crossword() crossword.type = "cryptic" crossword.number = 42 self.assertEqual(crossword.title(), "Cryptic Crossword No. 42")
def test_should_provide_helpful_title_with_creator(self): crossword = Crossword() crossword.type = "cryptic" crossword.number = 42 crossword.creator = "Philus Willus" self.assertEqual(crossword.title(), "Cryptic Crossword No. 42 set by Philus Willus")
def test_should_provide_link_to_full_version(self): crossword = Crossword() crossword.type = "quick" crossword.number = 42 self.assertEqual(crossword.url(), "/quick/42")
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_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_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")
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_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')
def latest(self, type): crosswords = Crossword.all().filter("type =", type).order("-date").fetch(limit=1) if len(crosswords) > 0: return crosswords[0] return None
# quick crossword next_quick_filename = os.path.join(data_dir, "next_quick.txt") if os.path.exists(next_quick_filename): f = open(next_quick_filename, "r") quick_serial = int(f.read()) f.close() else: quick_serial = 12559 ok = True while ok: print "Attempting to create Quick Crossword %d..." % quick_serial crossword = Crossword(home_page = home_page, cross_type="quick", data_dir = data_dir, prefix = "quick_crossword", serial_number = str(quick_serial), density = 128, border = 16, border_color = "#fff", smtp_server = smtp_server, from_email_address = from_email_address, to_email_address = to_email_address) ok = crossword.create() if ok: print "created" quick_serial += 1 f = open(next_quick_filename, "w") f.write(str(quick_serial)) f.close() # cryptic crossword next_cryptic_filename = os.path.join(data_dir, "next_cryptic.txt") if os.path.exists(next_cryptic_filename): f = open(next_cryptic_filename, "r") cryptic_serial = int(f.read()) f.close()
def find(self, type, number): crosswords = Crossword.all().filter("type =", type).filter("number =", int(number)).fetch(limit=1) if len(crosswords) > 0: return crosswords[0] return None
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")
from cPickle import dumps, loads from tempfile import mkstemp from crossword import Crossword def format_clues(clues): clues = map(lambda x: '%s. %s' % (x.num, x.clue), clues) return '\n'.join(clues) if __name__ == "__main__": puzzle = Crossword() puzzle.init_game(16, 15) print puzzle.get_clue(1, 'a').clue print puzzle.get_clue(2, 'a') print format_clues(puzzle.across_clues) print format_clues(puzzle.down_clues) print puzzle.all_clues puzzle.submit(1, 'a','PAIN') puzzle.submit(1, 'd', 'plains') puzzle.clear(1, 'a') puzzle.ghost(4, 'd', 'clams') puzzle.ghost(1, 'a','pear') puzzle.clear(1, 'a') puzzle.submit(65, 'a', 'Monk') puzzle.submit(48, 'd', 'drunks') puzzle.clear(65, 'a') # puzzle.ghost(1, 'd','PANE') print puzzle.save_game() # _handle, path = mkstemp() # f = open(path, 'w') # f.write(dumps(puzzle))
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')