11 locale.setlocale(locale.LC_ALL, '')
12 code = locale.getpreferredencoding()
14 superscript_numbers = {}
17 superscript_numbers = {
18 "0": unichr(8304).encode(code),
19 "1": unichr(185).encode(code),
20 "2": unichr(178).encode(code),
21 "3": unichr(179).encode(code),
22 "4": unichr(8308).encode(code),
23 "5": unichr(8309).encode(code),
24 "6": unichr(8310).encode(code),
25 "7": unichr(8311).encode(code),
26 "8": unichr(8312).encode(code),
27 "9": unichr(8313).encode(code),
30 for number in range(0, 10):
31 superscript_numbers[str(number)] = str(number)
36 (options, args) = getopt.getopt(sys.argv[1:], "f:", "file=")
37 except getopt.GetoptError, err:
41 for option in options:
42 if option[0] == "-f" or option[0] == "--file":
45 if not filename and len(args) > 0:
49 sys.stderr.write("No crossword file specified, exiting.\n")
52 if os.path.exists(filename) and os.path.isfile(filename):
53 crossworddata = codecs.open(filename, "r", "utf-8").read()
55 sys.stderr.write("Couldn't open file %s\n" %(filename))
58 def parsecrossword(crossworddata):
62 crossword = {"grid": [],
65 "grid_questions_start": [],
66 "grid_questions_end": []}
68 for line in crossworddata.split("\n"):
69 line = line.strip("\n")
74 elif line == "ACROSS":
85 crossword["grid"].append(line)
89 question_number = int(parts[0])
90 clue = " ".join(parts[1:])
91 crossword["across"][int(question_number)] = \
96 question_number = int(parts[0])
97 clue = " ".join(parts[1:])
98 crossword["down"][int(question_number)] = clue.encode(code)
99 num_cols = len(crossword["grid"][0])
100 num_rows = len(crossword["grid"])
102 current_clue_number = 1
104 for row in range(0, num_rows):
105 crossword["grid_questions_start"].append([])
107 for row in range(0, num_rows):
108 for col in range(0, num_cols):
111 and crossword["grid"][row][col - 1] == "x" \
112 and crossword["grid"][row][col] != "x" \
113 and col < (num_cols - 1) \
114 and crossword["grid"][row][col + 1] != "x":
116 if col == 0 and crossword["grid"][row][col] != "x" \
117 and crossword["grid"][row][col + 1] != "x":
119 if row > 0 and crossword["grid"][row-1][col] == "x" \
120 and row < (num_rows - 1) \
121 and crossword["grid"][row][col] != "x" \
122 and crossword["grid"][row + 1][col] != "x":
124 if row == 0 and crossword["grid"][row][col] != "x" \
125 and crossword["grid"][row + 1][col] != "x":
128 crossword["grid_questions_start"][row] \
129 .append(current_clue_number)
130 current_clue_number += 1
132 crossword["grid_questions_start"][row].append(0)
136 def crossword(stdscr, crossworddata):
137 crossword = parsecrossword(crossworddata)
139 grid_length = len(crossword["grid"][0])
141 stdscr.addch(cury, curx, curses.ACS_ULCORNER)
143 while ((curx - 1) / 4) < grid_length:
144 stdscr.addch(cury, curx, curses.ACS_HLINE)
145 stdscr.addch(cury, curx+1, curses.ACS_HLINE)
146 stdscr.addch(cury, curx+2, curses.ACS_HLINE)
147 stdscr.addch(cury, curx+3, curses.ACS_TTEE)
150 stdscr.addch(cury, curx, curses.ACS_URCORNER)
153 for line in crossword["grid"]:
158 stdscr.addch(cury, curx, curses.ACS_VLINE)
160 stdscr.addch(cury+1, curx, curses.ACS_PLUS)
162 stdscr.addch(cury+1, curx, curses.ACS_LTEE)
163 stdscr.addch(cury+1, curx + 1, curses.ACS_HLINE)
164 stdscr.addch(cury+1, curx + 2, curses.ACS_HLINE)
165 stdscr.addch(cury+1, curx + 3, curses.ACS_HLINE)
168 stdscr.addch(cury, curx, curses.ACS_BLOCK)
169 stdscr.addch(cury, curx+1, curses.ACS_BLOCK)
170 stdscr.addch(cury, curx+2, curses.ACS_BLOCK)
171 elif crossword["grid_questions_start"][curgridy][curgridx] > 0:
172 stdscr.addstr(cury, curx, \
174 [superscript_numbers[x].decode(code) \
176 crossword["grid_questions_start"] \
177 [curgridy][curgridx])]\
182 stdscr.addch(cury, curx, curses.ACS_VLINE)
183 stdscr.addch(cury + 1, curx, curses.ACS_RTEE)
188 stdscr.addch(cury, curx, curses.ACS_LLCORNER)
190 while ((curx - 1) / 4) < grid_length:
191 stdscr.addch(cury, curx, curses.ACS_HLINE)
192 stdscr.addch(cury, curx+1, curses.ACS_HLINE)
193 stdscr.addch(cury, curx+2, curses.ACS_HLINE)
194 stdscr.addch(cury, curx+3, curses.ACS_BTEE)
197 stdscr.addch(cury, curx, curses.ACS_LRCORNER)
199 cury = (len(crossword["grid"]) * 2) + 1
201 stdscr.addstr(cury, curx, "Across")
203 for cluenumber in crossword["across"].keys():
204 stdscr.addstr(cury, curx, "%3s: %s" \
205 %(str(cluenumber), crossword["across"][cluenumber]))
209 stdscr.addstr(cury, curx, "Down")
211 for cluenumber in crossword["down"].keys():
212 stdscr.addstr(cury, curx, "%3s: %s" \
213 %(str(cluenumber), crossword["down"][cluenumber]))
221 while crossword["grid"][gridy][gridx] == "x":
225 stdscr.move(cury, curx)
229 if c == curses.ascii.ESC:
231 if c == curses.KEY_RIGHT:
232 if gridx < (len(crossword["grid"][0]) - 1):
235 while gridx < (len(crossword["grid"][0]) -1) \
236 and crossword["grid"][gridy][gridx] == "x":
239 while crossword["grid"][gridy][gridx] == "x":
242 stdscr.move(cury, curx)
243 if c == curses.KEY_LEFT:
248 and crossword["grid"][gridy][gridx] == "x":
251 while crossword["grid"][gridy][gridx] == "x":
254 stdscr.move(cury, curx)
255 if c == curses.KEY_UP:
260 and crossword["grid"][gridy][gridx] == "x":
263 while crossword["grid"][gridy][gridx] == "x":
266 stdscr.move(cury, curx)
267 if c == curses.KEY_DOWN:
268 if gridy < (len(crossword["grid"]) - 1):
271 while gridy < (len(crossword["grid"]) - 1) \
272 and crossword["grid"][gridy][gridx] == "x":
275 while crossword["grid"][gridy][gridx] == "x":
278 stdscr.move(cury, curx)
279 if curses.ascii.isalpha(c) or c == ord(" "):
280 stdscr.addch(cury, curx, c)
281 stdscr.move(cury, curx)
282 if c == curses.KEY_BACKSPACE or c == curses.KEY_DC:
283 stdscr.addch(cury, curx, ord(" "))
284 stdscr.move(cury, curx)
287 curses.wrapper(crossword, crossworddata)