1 # Get A Clue (C) 2010 V. Harishankar
2 # Crossword puzzle maker program
3 # Licensed under the GNU GPL v3
5 # Class for the puzzle data representation
7 # for export to PNG image
12 def __init__ (self
, item_char
='.', item_guess
= None, across_start
= False,
13 down_start
= False, occupied_across
= False,
14 occupied_down
= False, num
= 0, clue_across
= None,
15 clue_down
= None, revealed
= False):
16 # character in the cell
18 # guess of character in cell
19 self
.guess
= item_guess
20 # is the cell the start of an across word?
21 self
.across_start
= across_start
22 # is the cell the start of a down word?
23 self
.down_start
= down_start
24 # is the cell occupied by a letter in an across word?
25 self
.occupied_across
= occupied_across
26 # is the cell occupied by a letter in a down word?
27 self
.occupied_down
= occupied_down
28 # numbering of the cell if it is the start of a word
30 # clue across if the cell is the start of an across word
31 self
.clue_across
= clue_across
32 # clue down if the cell is the start of a down word
33 self
.clue_down
= clue_down
34 # is the letter revealed or hidden?
35 self
.revealed
= revealed
37 # clear the across data
38 def clear_across_data (self
):
39 self
.across_start
= False
40 self
.occupied_across
= False
41 self
.clue_across
= None
42 # if no down word starting at item
43 if self
.down_start
is False:
45 # if no down word at the item
46 if self
.occupied_down
is False:
52 def clear_down_data (self
):
53 self
.down_start
= False
54 self
.occupied_down
= False
56 # if no across word starting at item
57 if self
.across_start
is False:
59 # if no across word at the item
60 if self
.occupied_across
is False:
65 # reset a grid item completely - use only to destroy whole grid, otherwise
66 # use either clear_across_data () or clear_down_data () for erasing single
69 # character in the cell
71 # guess of character in cell
73 # is the cell the start of an across word?
74 self
.across_start
= False
75 # is the cell the start of a down word?
76 self
.down_start
= False
77 # is the cell occupied by a letter in an across word?
78 self
.occupied_across
= False
79 # is the cell occupied by a letter in a down word?
80 self
.occupied_down
= False
81 # numbering of the cell if it is the start of a word
83 # clue across if the cell is the start of an across word
84 self
.clue_across
= None
85 # clue down if the cell is the start of a down word
87 # is the letter revealed or hidden?
90 # exception for too long words
91 class TooLongWordException (Exception):
92 def __init__ (self
, word
, length
):
96 # exception for intersecting words
97 class IntersectWordException (Exception):
98 def __init__ (self
, word
, length
):
102 # exception when grid is sought to be changed when frozen
103 class FrozenGridException (Exception):
105 self
.msg
= "Grid is frozen and cannot be edited"
107 # exception when no word is found at a position
108 class NoWordException (Exception):
109 def __init__ (self
, row
, col
):
110 self
.pos
= (row
, col
)
112 # exception when no words are present in the grid
113 class NoWordsException (Exception):
115 self
.msg
= "No words in grid"
117 class CrosswordPuzzle
:
118 def __init__ (self
, rows
, cols
):
119 # define number of rows and columns
123 # initialize the list to hold the grid
126 # initial state of the grid is unfrozen
127 self
.frozen_grid
= False
129 # create the grid data
130 for i
in range (rows
):
131 self
.data
.append ([])
132 for j
in range (cols
):
133 self
.data
[i
].append (GridItem ())
136 def export_image (self
, pngfile
, htmlfile
=None, puztitle
="Crossword Puzzle",
138 # don't export if grid is not frozen
139 self
.assert_frozen_grid ()
141 # create cairo image surface and context
143 surf
= cairo
.ImageSurface (cairo
.FORMAT_RGB24
, self
.cols
*px
, self
.rows
*px
)
144 ctx
= cairo
.Context (surf
)
146 ctx
.set_source_rgb (1, 1, 1)
147 ctx
.rectangle (0, 0, self
.cols
*px
, self
.rows
*px
)
150 # get the clues across and down
151 clues_across
= self
.get_clues_across ()
152 clues_down
= self
.get_clues_down ()
155 # traverse through the grid
156 for row
in range (self
.rows
):
157 for col
in range (self
.cols
):
158 # if grid is un-occupied
159 if (self
.data
[row
][col
].occupied_across
is False and
160 self
.data
[row
][col
].occupied_down
is False):
161 ctx
.set_source_rgb (0, 0, 0)
162 ctx
.rectangle (col
*px
, row
*px
, px
, px
)
166 ctx
.set_source_rgb (1, 1, 1)
167 ctx
.rectangle (col
*px
, row
*px
, px
, px
)
169 ctx
.set_source_rgb (0, 0, 0)
170 ctx
.rectangle (col
*px
, row
*px
, px
, px
)
172 # if solution is not to be provided, number the grid
173 if solution
is False:
174 if self
.data
[row
][col
].numbered
<> 0:
175 ctx
.select_font_face ("Serif")
176 ctx
.set_font_size (10)
177 ctx
.move_to (col
*px
+5, row
*px
+10)
178 ctx
.show_text (str(self
.data
[row
][col
].numbered
))
181 ctx
.select_font_face ("Serif")
182 ctx
.set_font_size (16)
183 ctx
.move_to (col
*px
+10, row
*px
+20)
184 ctx
.show_text (self
.data
[row
][col
].char
)
186 surf
.write_to_png (open (pngfile
, "wb"))
188 # if solution is false, publish the clues and the image in a HTML file
189 if htmlfile
and solution
is False:
190 html_contents
= ["<html>", "<head>", "<title>"]
191 html_contents
.append (puztitle
)
192 html_contents
.append ("</title>")
193 html_contents
.append ("</head>")
194 html_contents
.append ("<body>")
195 html_contents
.append ("<h1>" + puztitle
+ "</h1>")
196 html_contents
.append ('<img src="' + pngfile
+ '" alt="puzzle" />')
198 html_contents
.append ("<h2>Across clues</h2>")
199 html_contents
.append ("<p>")
200 for word
, clue
in clues_across
:
201 # clue should be: <num> - clue text (chars)
202 clue_str
= str (self
.data
[word
[1]][word
[2]].numbered
) + " - " \
203 + clue
+ " (" + str (word
[3]) + ")"
204 html_contents
.append (clue_str
)
205 html_contents
.append ("<br />")
206 html_contents
.append ("</p>")
208 html_contents
.append ("<h2>Down clues</h2>")
209 html_contents
.append ("<p>")
210 for word
, clue
in clues_down
:
211 clue_str
= str (self
.data
[word
[1]][word
[2]].numbered
) + " - " \
212 + clue
+ " (" + str (word
[3]) + ")"
213 html_contents
.append (clue_str
)
214 html_contents
.append ("<br />")
215 html_contents
.append ("</p>")
216 html_contents
.append ("</body>")
217 html_contents
.append ("</html>")
219 html_str
= "\r\n".join (html_contents
)
221 fhtml
= open (htmlfile
, "wb")
222 fhtml
.write (html_str
)
225 # get the AcrossLite(TM) data for exporting
226 def export_acrosslite (self
, title
, author
, copyright
):
227 # don't export if grid is not frozen
228 self
.assert_frozen_grid ()
231 across_data
.append ("<ACROSS PUZZLE>\r\n")
232 across_data
.append ("<TITLE>\r\n")
233 across_data
.append (title
+ "\r\n")
234 across_data
.append ("<AUTHOR>\r\n")
235 across_data
.append (author
+ "\r\n")
236 across_data
.append ("<COPYRIGHT>\r\n")
237 across_data
.append (copyright
+ "\r\n")
238 across_data
.append ("<SIZE>\r\n")
239 str_size
= str (self
.cols
) + "x" + str (self
.rows
)
240 across_data
.append (str_size
+ "\r\n")
241 across_data
.append ("<GRID>\r\n")
242 for row
in range (self
.rows
):
243 for col
in range (self
.cols
):
244 if (self
.data
[row
][col
].occupied_across
is True or
245 self
.data
[row
][col
].occupied_down
is True):
246 across_data
.append (self
.data
[row
][col
].char
)
248 across_data
.append (".")
249 across_data
.append ("\r\n")
251 across_data
.append ("<ACROSS>\r\n")
252 clues_across
= self
.get_clues_across ()
253 for word
, clue
in clues_across
:
255 across_data
.append (clue
+ "\r\n")
257 across_data
.append ("(No clue yet)\r\n")
259 across_data
.append ("<DOWN>\r\n")
260 clues_down
= self
.get_clues_down ()
261 for word
, clue
in clues_down
:
263 across_data
.append (clue
+ "\r\n")
265 across_data
.append ("(No clue yet\r\n")
267 acrosslite_str
= "".join (across_data
)
268 return acrosslite_str
270 # get all the clues for across
271 def get_clues_across (self
):
274 for row
in range (self
.rows
):
275 for col
in range (self
.cols
):
276 if (self
.data
[row
][col
].occupied_across
is True and
277 self
.data
[row
][col
].across_start
is True):
278 word_across
= self
.get_word_across (row
, col
)
279 clues
.append ((word_across
, self
.data
[row
][col
].clue_across
))
280 # if no across words are found at all
282 raise NoWordsException
286 # get all the clues for down
287 def get_clues_down (self
):
290 for row
in range (self
.rows
):
291 for col
in range (self
.cols
):
292 if (self
.data
[row
][col
].occupied_down
is True and
293 self
.data
[row
][col
].down_start
is True):
294 word_down
= self
.get_word_down (row
, col
)
295 clues
.append ((word_down
, self
.data
[row
][col
].clue_down
))
296 # if no down words are found at all
298 raise NoWordsException
302 # getting a down word at a position
303 def get_word_down (self
, row
, col
):
304 # if index is out of bounds
305 if row
>= self
.rows
or col
>= self
.cols
:
306 raise NoWordException (row
, col
)
308 # if there is no occupied down letter at that position
309 if self
.data
[row
][col
].occupied_down
is False:
310 raise NoWordException (row
, col
)
312 # now traverse the grid to find the beginning of the word
315 # if it is occupied down and is the beginning of the word
316 if (self
.data
[i
][col
].occupied_down
is True and
317 self
.data
[i
][col
].down_start
is True):
324 # now seek the end of the word
326 if self
.data
[i
][col
].occupied_down
is True:
327 word_chars
.append (self
.data
[i
][col
].char
)
332 word
= "".join (word_chars
)
334 # return the word, starting row, column and length as a tuple
335 return (word
, start_row
, col
, len(word
))
337 # getting an across word at a position
338 def get_word_across (self
, row
, col
):
339 # if index is out of bounds
340 if row
>= self
.rows
or col
>= self
.cols
:
341 raise NoWordException (row
, col
)
343 # if there is no occupied across letter at that position
344 if self
.data
[row
][col
].occupied_across
is False:
345 raise NoWordException (row
, col
)
347 # now traverse the grid to look for the beginning of the word
350 # if it is occupied across and is the beginning of the word
351 if (self
.data
[row
][i
].occupied_across
is True and
352 self
.data
[row
][i
].across_start
is True):
359 # now seek the end of the word
361 if self
.data
[row
][i
].occupied_across
is True:
362 word_chars
.append (self
.data
[row
][i
].char
)
367 word
= "".join (word_chars
)
369 # return the word, starting column, row and length as a tuple
370 return (word
, row
, start_col
, len(word
))
372 # setting a down word
373 def set_word_down (self
, row
, col
, word
):
374 # if the grid is frozen then abort
375 self
.assert_unfrozen_grid ()
377 # if the word length greater than totalrows - startrow
378 if len(word
) > self
.rows
- row
:
379 raise TooLongWordException (word
, len(word
))
381 # is the word intersecting any other word?
382 for i
in range (len(word
)):
384 if self
.data
[row
+i
][col
].occupied_down
is True:
385 raise IntersectWordException (word
, len(word
))
386 # on the previous column except first column
388 # except the first and last col
389 if i
> 0 and i
< len(word
) - 1:
390 if self
.data
[row
+i
][col
-1].occupied_down
is True:
391 raise IntersectWordException (word
, len(word
))
392 # if the previous column is the end of an across word
393 if (self
.data
[row
+i
][col
-1].occupied_across
is True and
394 self
.data
[row
+i
][col
].occupied_across
is False):
395 raise IntersectWordException (word
, len(word
))
397 # on the next column except last column
398 if col
< len(word
) - 1:
399 # except the first and last row check if there is any
400 # down word in previous column
401 if i
> 0 and i
< len(word
) - 1:
402 if self
.data
[row
+i
][col
+1].occupied_down
is True:
403 raise IntersectWordException (word
, len(word
))
404 # check if there is any across word starting in the
406 if self
.data
[row
+i
][col
+1].across_start
is True:
407 raise IntersectWordException (word
, len(word
))
409 # also check the character before and after
410 if (row
> 0 and self
.data
[row
-1][col
].occupied_down
is True
411 and self
.data
[row
-1][col
].occupied_across
is True):
412 raise IntersectWordException (word
, len(word
))
413 if (row
+ len(word
) < self
.rows
and
414 self
.data
[row
+len(word
)][col
].occupied_across
is True and
415 self
.data
[row
+len(word
)][col
].occupied_down
is True):
416 raise IntersectWordException (word
, len(word
))
418 # set the down start to true
419 self
.data
[row
][col
].down_start
= True
421 for i
in range (len(word
)):
422 self
.data
[row
+i
][col
].occupied_down
= True
423 self
.data
[row
+i
][col
].char
= word
[i
].upper ()
426 # setting an across word
427 def set_word_across (self
, row
, col
, word
):
428 # if the grid is frozen then abort
429 self
.assert_unfrozen_grid ()
431 # is the word length greater than totalcols - startcol?
432 if len(word
) > self
.cols
- col
:
433 raise TooLongWordException (word
, len(word
))
435 # is the word intersecting any other word?
436 for i
in range (len(word
)):
438 if self
.data
[row
][col
+i
].occupied_across
is True:
439 raise IntersectWordException (word
, len(word
))
440 # on a previous row except first row
442 # if not the first or last col
443 if i
> 0 and i
< len(word
) - 1:
444 if self
.data
[row
-1][col
+i
].occupied_across
is True:
445 raise IntersectWordException (word
, len(word
))
446 # if the previous row is the end of a down word
447 if (self
.data
[row
-1][col
+i
].occupied_down
is True and
448 self
.data
[row
][col
+i
].occupied_down
is False):
449 raise IntersectWordException (word
, len(word
))
452 if (row
< (self
.rows
- 1)):
453 # except the first and last letter check if there is
454 # any across intersection
455 if i
> 0 and i
< len (word
) - 1:
456 if self
.data
[row
+1][col
+i
].occupied_across
is True:
457 raise IntersectWordException (word
, len(word
))
458 # if a down word is starting at any column below the
460 if self
.data
[row
+1][col
+i
].down_start
is True:
461 raise IntersectWordException (word
, len(word
))
463 # also check the character beyond and before and after
464 if (col
> 0 and (self
.data
[row
][col
-1].occupied_across
is True or
465 self
.data
[row
][col
-1].occupied_down
is True)):
466 raise IntersectWordException (word
, len(word
))
467 if (col
+ len(word
) < self
.cols
and
468 (self
.data
[row
][col
+len(word
)].occupied_across
is True or
469 self
.data
[row
][col
+len(word
)].occupied_down
is True)):
470 raise IntersectWordException (word
, len(word
))
472 # set across start to true
473 self
.data
[row
][col
].across_start
= True
476 for i
in range (len(word
)):
477 self
.data
[row
][col
+i
].char
= word
[i
].upper ()
478 self
.data
[row
][col
+i
].occupied_across
= True
480 # freeze the grid numbers etc.
481 def freeze_grid (self
):
484 # run through the grid
485 for row
in range (self
.rows
):
486 for col
in range (self
.cols
):
487 # if grid is blank set the character to #
488 if (self
.data
[row
][col
].occupied_across
is False
489 and self
.data
[row
][col
].occupied_down
is False):
490 self
.data
[row
][col
].char
= "#"
491 elif (self
.data
[row
][col
].across_start
is True or
492 self
.data
[row
][col
].down_start
is True):
493 self
.data
[row
][col
].numbered
= numbering
496 self
.frozen_grid
= True
498 # unfreeze the grid numbers etc.
499 def unfreeze_grid (self
):
500 # run through the grid
501 for row
in range (self
.rows
):
502 for col
in range (self
.cols
):
503 self
.data
[row
][col
].numbered
= 0
504 if (self
.data
[row
][col
].occupied_across
is False and
505 self
.data
[row
][col
].occupied_down
is False):
506 self
.data
[row
][col
].char
= '.'
508 self
.frozen_grid
= False
510 # raise an exception if the grid is frozen
511 def assert_unfrozen_grid (self
):
512 if self
.frozen_grid
is True:
513 raise FrozenGridException
515 # raise an exception if the grid is NOT frozen
516 def assert_frozen_grid (self
):
517 if self
.frozen_grid
is False:
518 raise FrozenGridException
520 # reset the entire grid
521 def reset_grid (self
):
522 # run through the grid
523 for row
in range (self
.rows
):
524 for col
in range (self
.cols
):
525 # re-initialize all data
526 self
.data
[row
][col
].reset ()
528 self
.frozen_grid
= False
530 # remove an across word at position
531 def remove_word_across (self
, row
, col
):
532 # if grid is frozen don't allow removal of word
533 assert_unfrozen_grid ()
535 word
, brow
, bcol
, l
= self
.get_word_across (row
, col
)
537 # traverse from the beginning to end of the word and erase it
540 if self
.data
[brow
][c
].occupied_across
is True:
541 self
.data
[brow
][c
].clear_across_data ()
546 # remove a down word at position
547 def remove_word_down (self
, row
, col
):
548 # if grid is frozen don't allow removal of word
549 self
.assert_unfrozen_grid ()
551 word
, brow
, bcol
, l
= self
.get_word_down (row
, col
)
552 # traverse from the beginn to end of the word and erase it
555 if self
.data
[r
][bcol
].occupied_down
is True:
556 self
.data
[r
][bcol
].clear_down_data ()