8 locale.setlocale(locale.LC_ALL, '')
9 code = locale.getpreferredencoding()
11 superscript_numbers = {}
14 superscript_numbers = {
15 "0": unichr(8304).encode(code),
16 "1": unichr(185).encode(code),
17 "2": unichr(178).encode(code),
18 "3": unichr(179).encode(code),
19 "4": unichr(8308).encode(code),
20 "5": unichr(8309).encode(code),
21 "6": unichr(8310).encode(code),
22 "7": unichr(8311).encode(code),
23 "8": unichr(8312).encode(code),
24 "9": unichr(8313).encode(code),
27 for number in range(0,10):
28 superscript_numbers[str(number)] = str(number)
30 crossworddata = codecs.open("crossword.output", "r", "utf-8").read()
32 def parsecrossword(crossworddata):
36 crossword = {"grid": [], "across": {}, "down": {}, "grid_questions_start": [], "grid_questions_end": []}
38 for line in crossworddata.split("\n"):
39 line = line.strip("\n")
44 elif line == "ACROSS":
55 crossword["grid"].append(line)
59 question_number = int(parts[0])
60 clue = " ".join(parts[1:])
61 crossword["across"][int(question_number)] = clue
65 question_number = int(parts[0])
66 clue = " ".join(parts[1:])
67 crossword["down"][int(question_number)] = clue
68 num_cols = len(crossword["grid"][0])
69 num_rows = len(crossword["grid"])
71 current_clue_number = 1
73 for row in range(0,num_rows):
74 crossword["grid_questions_start"].append([])
76 for row in range(0,num_rows):
77 for col in range(0,num_cols):
80 and crossword["grid"][row][col - 1] == "x" \
81 and crossword["grid"][row][col] != "x" \
82 and col < (num_cols - 1) \
83 and crossword["grid"][row][col + 1] != "x":
85 if col == 0 and crossword["grid"][row][col] != "x" \
86 and crossword["grid"][row][col + 1] != "x":
88 if row > 0 and crossword["grid"][row-1][col] == "x" \
89 and row < (num_rows - 1) \
90 and crossword["grid"][row][col] != "x" \
91 and crossword["grid"][row + 1][col] != "x":
93 if row == 0 and crossword["grid"][row][col] != "x" \
94 and crossword["grid"][row + 1][col] != "x":
97 crossword["grid_questions_start"][row].append(current_clue_number)
98 current_clue_number += 1
100 crossword["grid_questions_start"][row].append(0)
104 def crossword(stdscr, crossworddata):
105 crossword = parsecrossword(crossworddata)
107 grid_length = len(crossword["grid"][0])
109 stdscr.addch(cury, curx, curses.ACS_ULCORNER)
111 while ((curx - 1) / 4) < grid_length:
112 stdscr.addch(cury, curx, curses.ACS_HLINE)
113 stdscr.addch(cury, curx+1, curses.ACS_HLINE)
114 stdscr.addch(cury, curx+2, curses.ACS_HLINE)
115 stdscr.addch(cury, curx+3, curses.ACS_TTEE)
118 stdscr.addch(cury, curx, curses.ACS_URCORNER)
121 for line in crossword["grid"]:
126 stdscr.addch(cury, curx, curses.ACS_VLINE)
128 stdscr.addch(cury+1, curx, curses.ACS_PLUS)
130 stdscr.addch(cury+1, curx, curses.ACS_LTEE)
131 stdscr.addch(cury+1, curx + 1, curses.ACS_HLINE)
132 stdscr.addch(cury+1, curx + 2, curses.ACS_HLINE)
133 stdscr.addch(cury+1, curx + 3, curses.ACS_HLINE)
136 stdscr.addch(cury, curx, curses.ACS_BLOCK)
137 stdscr.addch(cury, curx+1, curses.ACS_BLOCK)
138 stdscr.addch(cury, curx+2, curses.ACS_BLOCK)
139 elif crossword["grid_questions_start"][curgridy][curgridx] > 0:
140 stdscr.addstr(cury, curx, ''.join([superscript_numbers[x].decode(code) for x in str(crossword["grid_questions_start"][curgridy][curgridx])]).encode(code))
144 stdscr.addch(cury, curx, curses.ACS_VLINE)
145 stdscr.addch(cury + 1, curx, curses.ACS_RTEE)
150 stdscr.addch(cury, curx, curses.ACS_LLCORNER)
152 while ((curx - 1) / 4) < grid_length:
153 stdscr.addch(cury, curx, curses.ACS_HLINE)
154 stdscr.addch(cury, curx+1, curses.ACS_HLINE)
155 stdscr.addch(cury, curx+2, curses.ACS_HLINE)
156 stdscr.addch(cury, curx+3, curses.ACS_BTEE)
159 stdscr.addch(cury, curx, curses.ACS_LRCORNER)
161 cury = (len(crossword["grid"]) * 2) + 1
163 stdscr.addstr(cury, curx, "Across")
165 for cluenumber in crossword["across"].keys():
166 stdscr.addstr(cury, curx, "%3s: %s" %(str(cluenumber), crossword["across"][cluenumber]))
170 stdscr.addstr(cury, curx, "Down")
172 for cluenumber in crossword["down"].keys():
173 stdscr.addstr(cury, curx, "%3s: %s" %(str(cluenumber), crossword["down"][cluenumber]))
181 while crossword["grid"][gridy][gridx] == "x":
185 stdscr.move(cury,curx)
189 if c == curses.ascii.ESC:
191 if c == curses.KEY_RIGHT:
192 if gridx < (len(crossword["grid"][0]) - 1):
195 while gridx < (len(crossword["grid"][0]) -1) \
196 and crossword["grid"][gridy][gridx] == "x":
199 while crossword["grid"][gridy][gridx] == "x":
202 stdscr.move(cury, curx)
203 if c == curses.KEY_LEFT:
208 and crossword["grid"][gridy][gridx] == "x":
211 while crossword["grid"][gridy][gridx] == "x":
214 stdscr.move(cury, curx)
215 if c == curses.KEY_UP:
220 and crossword["grid"][gridy][gridx] == "x":
223 while crossword["grid"][gridy][gridx] == "x":
226 stdscr.move(cury, curx)
227 if c == curses.KEY_DOWN:
228 if gridy < (len(crossword["grid"]) - 1):
231 while gridy < (len(crossword["grid"]) - 1) \
232 and crossword["grid"][gridy][gridx] == "x":
235 while crossword["grid"][gridy][gridx] == "x":
238 stdscr.move(cury, curx)
239 if curses.ascii.isalpha(c) or c == ord(" "):
240 stdscr.addch(cury, curx, c)
241 stdscr.move(cury, curx)
242 if c == curses.KEY_BACKSPACE or c == curses.KEY_DC:
243 stdscr.addch(cury, curx, ord(" "))
244 stdscr.move(cury, curx)
247 curses.wrapper(crossword, crossworddata)