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("g2-20090701.txt", "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)
165 if c == curses.ascii.ESC:
167 if c == curses.KEY_RIGHT:
169 stdscr.move(cury, curx)
170 if c == curses.KEY_LEFT:
172 stdscr.move(cury, curx)
173 if c == curses.KEY_UP:
175 stdscr.move(cury, curx)
176 if c == curses.KEY_DOWN:
178 stdscr.move(cury, curx)
181 curses.wrapper(crossword, crossworddata)