Professional Documents
Culture Documents
class Crossword(object):
def __init__(self, cols, rows, empty = '-
', maxloops = 2000, available_words=[]):
self.cols = cols
self.rows = rows
self.empty = empty
self.maxloops = maxloops
self.available_words = available_words
self.randomize_word_list()
self.current_word_list = []
self.debug = 0
self.clear_grid()
count = 0
copy = Crossword(self.cols, self.rows, self.empty, self.maxloops,
self.available_words)
start_full = float(time.time())
while (float(time.time()) - start_full) < time_permitted or count
== 0: # only run for x seconds
self.debug += 1
copy.current_word_list = []
copy.clear_grid()
copy.randomize_word_list()
x = 0
while x < spins: # spins; 2 seems to be plenty
for word in copy.available_words:
if word not in copy.current_word_list:
copy.fit_and_add(word)
x += 1
#print copy.solution()
#print len(copy.current_word_list), len(self.current_word_list
), self.debug
# buffer the best crossword by comparing placed words
if len(copy.current_word_list) > len(self.current_word_list):
self.current_word_list = copy.current_word_list
self.grid = copy.grid
count += 1
return
def fit_and_add(self, word): # doesn't really check fit except for the
first word; otherwise just adds if score is good
fit = False
count = 0
coordlist = self.suggest_coord(word)
count += 1
return
if active_cell == letter:
score += 1
if vertical:
# check surroundings
if active_cell != letter: # don't check surroundings if cr
oss point
if not self.check_if_cell_clear(col+1, row): # check r
ight cell
return 0
if not self.check_if_cell_clear(col-
1, row): # check left cell
return 0
count += 1
return score
copy = self
for word in self.current_word_list:
copy.set_cell(word.col, word.row, word.number)
for r in range(copy.rows):
for c in copy.grid[r]:
outStr += '%s ' % c
outStr += '\n'
def word_bank(self):
outStr = ''
temp_list = duplicate(self.current_word_list)
random.shuffle(temp_list) # randomize word list
for word in temp_list:
outStr += '%s\n' % word.word
return outStr
class Word(object):
def __init__(self, word=None, clue=None):
self.word = re.sub(r'\s', '', word.lower())
self.clue = clue
self.length = len(self.word)
# the below are set when placed on board
self.row = None
self.col = None
self.vertical = None
self.number = None
def __repr__(self):
return self.word
### end class, start execution
#start_full = float(time.time())
word_list = ['saffron', 'The dried, orange yellow plant used to as dye and
as a cooking spice.'], \
['pumpernickel', 'Dark, sour bread made from coarse ground rye.'], \
['leaven', 'An agent, such as yeast, that cause batter or dough to rise..'
], \
['coda', 'Musical conclusion of a movement or composition.'], \
['paladin', 'A heroic champion or paragon of chivalry.'], \
['syncopation', 'Shifting the emphasis of a beat to the normally weak beat
.'], \
['albatross', 'A large bird of the ocean having a hooked beek and long, na
rrow wings.'], \
['harp', 'Musical instrument with 46 or more open strings played by plucki
ng.'], \
['piston', 'A solid cylinder or disk that fits snugly in a larger cylinder
and moves under pressure as in an engine.'], \
['caramel', 'A smooth chery candy made from suger, butter, cream or milk w
ith flavoring.'], \
['coral', 'A rock-
like deposit of organism skeletons that make up reefs.'], \
['dawn', 'The time of each morning at which daylight begins.'], \
['pitch', 'A resin derived from the sap of various pine trees.'], \
['fjord', 'A long, narrow, deep inlet of the sea between steep slopes.'],
\
['lip', 'Either of two fleshy folds surrounding the mouth.'], \
['lime', 'The egg-
shaped citrus fruit having a green coloring and acidic juice.'], \
['mist', 'A mass of fine water droplets in the air near or in contact with
the ground.'], \
['plague', 'A widespread affliction or calamity.'], \
['yarn', 'A strand of twisted threads or a long elaborate narrative.'], \
['snicker', 'A snide, slightly stifled laugh.']