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": [], "across": {}, "down": {}, "grid_questions_start": [], "grid_questions_end": []}
64 for line in crossworddata.split("\n"):
65 line = line.strip("\n")
70 elif line == "ACROSS":
81 crossword["grid"].append(line)
85 question_number = int(parts[0])
86 clue = " ".join(parts[1:])
87 crossword["across"][int(question_number)] = clue.encode(code)
91 question_number = int(parts[0])
92 clue = " ".join(parts[1:])
93 crossword["down"][int(question_number)] = clue.encode(code)
94 num_cols = len(crossword["grid"][0])
95 num_rows = len(crossword["grid"])
97 current_clue_number = 1
99 for row in range(0,num_rows):
100 crossword["grid_questions_start"].append([])
102 for row in range(0,num_rows):
103 for col in range(0,num_cols):
106 and crossword["grid"][row][col - 1] == "x" \
107 and crossword["grid"][row][col] != "x" \
108 and col < (num_cols - 1) \
109 and crossword["grid"][row][col + 1] != "x":
111 if col == 0 and crossword["grid"][row][col] != "x" \
112 and crossword["grid"][row][col + 1] != "x":
114 if row > 0 and crossword["grid"][row-1][col] == "x" \
115 and row < (num_rows - 1) \
116 and crossword["grid"][row][col] != "x" \
117 and crossword["grid"][row + 1][col] != "x":
119 if row == 0 and crossword["grid"][row][col] != "x" \
120 and crossword["grid"][row + 1][col] != "x":
123 crossword["grid_questions_start"][row].append(current_clue_number)
124 current_clue_number += 1
126 crossword["grid_questions_start"][row].append(0)
130 def crossword(stdscr, crossworddata):
131 crossword = parsecrossword(crossworddata)
133 grid_length = len(crossword["grid"][0])
135 stdscr.addch(cury, curx, curses.ACS_ULCORNER)
137 while ((curx - 1) / 4) < grid_length:
138 stdscr.addch(cury, curx, curses.ACS_HLINE)
139 stdscr.addch(cury, curx+1, curses.ACS_HLINE)
140 stdscr.addch(cury, curx+2, curses.ACS_HLINE)
141 stdscr.addch(cury, curx+3, curses.ACS_TTEE)
144 stdscr.addch(cury, curx, curses.ACS_URCORNER)
147 for line in crossword["grid"]:
152 stdscr.addch(cury, curx, curses.ACS_VLINE)
154 stdscr.addch(cury+1, curx, curses.ACS_PLUS)
156 stdscr.addch(cury+1, curx, curses.ACS_LTEE)
157 stdscr.addch(cury+1, curx + 1, curses.ACS_HLINE)
158 stdscr.addch(cury+1, curx + 2, curses.ACS_HLINE)
159 stdscr.addch(cury+1, curx + 3, curses.ACS_HLINE)
162 stdscr.addch(cury, curx, curses.ACS_BLOCK)
163 stdscr.addch(cury, curx+1, curses.ACS_BLOCK)
164 stdscr.addch(cury, curx+2, curses.ACS_BLOCK)
165 elif crossword["grid_questions_start"][curgridy][curgridx] > 0:
166 stdscr.addstr(cury, curx, ''.join([superscript_numbers[x].decode(code) for x in str(crossword["grid_questions_start"][curgridy][curgridx])]).encode(code))
170 stdscr.addch(cury, curx, curses.ACS_VLINE)
171 stdscr.addch(cury + 1, curx, curses.ACS_RTEE)
176 stdscr.addch(cury, curx, curses.ACS_LLCORNER)
178 while ((curx - 1) / 4) < grid_length:
179 stdscr.addch(cury, curx, curses.ACS_HLINE)
180 stdscr.addch(cury, curx+1, curses.ACS_HLINE)
181 stdscr.addch(cury, curx+2, curses.ACS_HLINE)
182 stdscr.addch(cury, curx+3, curses.ACS_BTEE)
185 stdscr.addch(cury, curx, curses.ACS_LRCORNER)
187 cury = (len(crossword["grid"]) * 2) + 1
189 stdscr.addstr(cury, curx, "Across")
191 for cluenumber in crossword["across"].keys():
192 stdscr.addstr(cury, curx, "%3s: %s" %(str(cluenumber), crossword["across"][cluenumber]))
196 stdscr.addstr(cury, curx, "Down")
198 for cluenumber in crossword["down"].keys():
199 stdscr.addstr(cury, curx, "%3s: %s" %(str(cluenumber), crossword["down"][cluenumber]))
207 while crossword["grid"][gridy][gridx] == "x":
211 stdscr.move(cury,curx)
215 if c == curses.ascii.ESC:
217 if c == curses.KEY_RIGHT:
218 if gridx < (len(crossword["grid"][0]) - 1):
221 while gridx < (len(crossword["grid"][0]) -1) \
222 and crossword["grid"][gridy][gridx] == "x":
225 while crossword["grid"][gridy][gridx] == "x":
228 stdscr.move(cury, curx)
229 if c == curses.KEY_LEFT:
234 and crossword["grid"][gridy][gridx] == "x":
237 while crossword["grid"][gridy][gridx] == "x":
240 stdscr.move(cury, curx)
241 if c == curses.KEY_UP:
246 and crossword["grid"][gridy][gridx] == "x":
249 while crossword["grid"][gridy][gridx] == "x":
252 stdscr.move(cury, curx)
253 if c == curses.KEY_DOWN:
254 if gridy < (len(crossword["grid"]) - 1):
257 while gridy < (len(crossword["grid"]) - 1) \
258 and crossword["grid"][gridy][gridx] == "x":
261 while crossword["grid"][gridy][gridx] == "x":
264 stdscr.move(cury, curx)
265 if curses.ascii.isalpha(c) or c == ord(" "):
266 stdscr.addch(cury, curx, c)
267 stdscr.move(cury, curx)
268 if c == curses.KEY_BACKSPACE or c == curses.KEY_DC:
269 stdscr.addch(cury, curx, ord(" "))
270 stdscr.move(cury, curx)
273 curses.wrapper(crossword, crossworddata)