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)
35 (options, args) = getopt.getopt(sys.argv[1:], "f:", "file=")
37 for option in options:
38 if option[0] == "-f" or option[0] == "--file":
41 if not filename and len(args) > 0:
45 sys.stderr.write("No crossword file specified, exiting.\n")
48 if os.path.exists(filename) and os.path.isfile(filename):
49 crossworddata = codecs.open(filename, "r", "utf-8").read()
51 sys.stderr.write("Couldn't open file %s\n" %(filename))
54 def parsecrossword(crossworddata):
58 crossword = {"grid": [], "across": {}, "down": {}, "grid_questions_start": [], "grid_questions_end": []}
60 for line in crossworddata.split("\n"):
61 line = line.strip("\n")
66 elif line == "ACROSS":
77 crossword["grid"].append(line)
81 question_number = int(parts[0])
82 clue = " ".join(parts[1:])
83 crossword["across"][int(question_number)] = clue.encode('utf-8')
87 question_number = int(parts[0])
88 clue = " ".join(parts[1:])
89 crossword["down"][int(question_number)] = clue.encode('utf-8')
90 num_cols = len(crossword["grid"][0])
91 num_rows = len(crossword["grid"])
93 current_clue_number = 1
95 for row in range(0,num_rows):
96 crossword["grid_questions_start"].append([])
98 for row in range(0,num_rows):
99 for col in range(0,num_cols):
102 and crossword["grid"][row][col - 1] == "x" \
103 and crossword["grid"][row][col] != "x" \
104 and col < (num_cols - 1) \
105 and crossword["grid"][row][col + 1] != "x":
107 if col == 0 and crossword["grid"][row][col] != "x" \
108 and crossword["grid"][row][col + 1] != "x":
110 if row > 0 and crossword["grid"][row-1][col] == "x" \
111 and row < (num_rows - 1) \
112 and crossword["grid"][row][col] != "x" \
113 and crossword["grid"][row + 1][col] != "x":
115 if row == 0 and crossword["grid"][row][col] != "x" \
116 and crossword["grid"][row + 1][col] != "x":
119 crossword["grid_questions_start"][row].append(current_clue_number)
120 current_clue_number += 1
122 crossword["grid_questions_start"][row].append(0)
126 def crossword(stdscr, crossworddata):
127 crossword = parsecrossword(crossworddata)
129 grid_length = len(crossword["grid"][0])
131 stdscr.addch(cury, curx, curses.ACS_ULCORNER)
133 while ((curx - 1) / 4) < grid_length:
134 stdscr.addch(cury, curx, curses.ACS_HLINE)
135 stdscr.addch(cury, curx+1, curses.ACS_HLINE)
136 stdscr.addch(cury, curx+2, curses.ACS_HLINE)
137 stdscr.addch(cury, curx+3, curses.ACS_TTEE)
140 stdscr.addch(cury, curx, curses.ACS_URCORNER)
143 for line in crossword["grid"]:
148 stdscr.addch(cury, curx, curses.ACS_VLINE)
150 stdscr.addch(cury+1, curx, curses.ACS_PLUS)
152 stdscr.addch(cury+1, curx, curses.ACS_LTEE)
153 stdscr.addch(cury+1, curx + 1, curses.ACS_HLINE)
154 stdscr.addch(cury+1, curx + 2, curses.ACS_HLINE)
155 stdscr.addch(cury+1, curx + 3, curses.ACS_HLINE)
158 stdscr.addch(cury, curx, curses.ACS_BLOCK)
159 stdscr.addch(cury, curx+1, curses.ACS_BLOCK)
160 stdscr.addch(cury, curx+2, curses.ACS_BLOCK)
161 elif crossword["grid_questions_start"][curgridy][curgridx] > 0:
162 stdscr.addstr(cury, curx, ''.join([superscript_numbers[x].decode(code) for x in str(crossword["grid_questions_start"][curgridy][curgridx])]).encode(code))
166 stdscr.addch(cury, curx, curses.ACS_VLINE)
167 stdscr.addch(cury + 1, curx, curses.ACS_RTEE)
172 stdscr.addch(cury, curx, curses.ACS_LLCORNER)
174 while ((curx - 1) / 4) < grid_length:
175 stdscr.addch(cury, curx, curses.ACS_HLINE)
176 stdscr.addch(cury, curx+1, curses.ACS_HLINE)
177 stdscr.addch(cury, curx+2, curses.ACS_HLINE)
178 stdscr.addch(cury, curx+3, curses.ACS_BTEE)
181 stdscr.addch(cury, curx, curses.ACS_LRCORNER)
183 cury = (len(crossword["grid"]) * 2) + 1
185 stdscr.addstr(cury, curx, "Across")
187 for cluenumber in crossword["across"].keys():
188 stdscr.addstr(cury, curx, "%3s: %s" %(str(cluenumber), crossword["across"][cluenumber]))
192 stdscr.addstr(cury, curx, "Down")
194 for cluenumber in crossword["down"].keys():
195 stdscr.addstr(cury, curx, "%3s: %s" %(str(cluenumber), crossword["down"][cluenumber]))
203 while crossword["grid"][gridy][gridx] == "x":
207 stdscr.move(cury,curx)
211 if c == curses.ascii.ESC:
213 if c == curses.KEY_RIGHT:
214 if gridx < (len(crossword["grid"][0]) - 1):
217 while gridx < (len(crossword["grid"][0]) -1) \
218 and crossword["grid"][gridy][gridx] == "x":
221 while crossword["grid"][gridy][gridx] == "x":
224 stdscr.move(cury, curx)
225 if c == curses.KEY_LEFT:
230 and crossword["grid"][gridy][gridx] == "x":
233 while crossword["grid"][gridy][gridx] == "x":
236 stdscr.move(cury, curx)
237 if c == curses.KEY_UP:
242 and crossword["grid"][gridy][gridx] == "x":
245 while crossword["grid"][gridy][gridx] == "x":
248 stdscr.move(cury, curx)
249 if c == curses.KEY_DOWN:
250 if gridy < (len(crossword["grid"]) - 1):
253 while gridy < (len(crossword["grid"]) - 1) \
254 and crossword["grid"][gridy][gridx] == "x":
257 while crossword["grid"][gridy][gridx] == "x":
260 stdscr.move(cury, curx)
261 if curses.ascii.isalpha(c) or c == ord(" "):
262 stdscr.addch(cury, curx, c)
263 stdscr.move(cury, curx)
264 if c == curses.KEY_BACKSPACE or c == curses.KEY_DC:
265 stdscr.addch(cury, curx, ord(" "))
266 stdscr.move(cury, curx)
269 curses.wrapper(crossword, crossworddata)