7 locale.setlocale(locale.LC_ALL, '')
8 code = locale.getpreferredencoding()
10 superscript_numbers = {}
13 superscript_numbers = {
14 "0": unichr(8304).encode(code),
15 "1": unichr(185).encode(code),
16 "2": unichr(178).encode(code),
17 "3": unichr(179).encode(code),
18 "4": unichr(8308).encode(code),
19 "5": unichr(8309).encode(code),
20 "6": unichr(8310).encode(code),
21 "7": unichr(8311).encode(code),
22 "8": unichr(8312).encode(code),
23 "9": unichr(8313).encode(code),
26 for number in range(0,10):
27 superscript_numbers[str(number)] = str(number)
29 crossworddata = codecs.open("g2-20090701.txt", "r", "utf-8").read()
31 def parsecrossword(crossworddata):
35 crossword = {"grid": [], "across": {}, "down": {}, "grid_questions_start": [], "grid_questions_end": []}
37 for line in crossworddata.split("\n"):
38 line = line.strip("\n")
43 elif line == "ACROSS":
54 crossword["grid"].append(line)
58 question_number = int(parts[0])
59 clue = " ".join(parts[1:])
60 crossword["across"][int(question_number)] = clue
64 question_number = int(parts[0])
65 clue = " ".join(parts[1:])
66 crossword["down"][int(question_number)] = clue
67 num_cols = len(crossword["grid"][0])
68 num_rows = len(crossword["grid"])
70 current_clue_number = 1
72 for row in range(0,num_rows):
73 crossword["grid_questions_start"].append([])
75 for row in range(0,num_rows):
76 for col in range(0,num_cols):
79 and crossword["grid"][row][col - 1] == "x" \
80 and crossword["grid"][row][col] != "x" \
81 and col < (num_cols - 1) \
82 and crossword["grid"][row][col + 1] != "x":
84 if col == 0 and crossword["grid"][row][col] != "x" \
85 and crossword["grid"][row][col + 1] != "x":
87 if row > 0 and crossword["grid"][row-1][col] == "x" \
88 and row < (num_rows - 1) \
89 and crossword["grid"][row][col] != "x" \
90 and crossword["grid"][row + 1][col] != "x":
92 if row == 0 and crossword["grid"][row][col] != "x" \
93 and crossword["grid"][row + 1][col] != "x":
96 crossword["grid_questions_start"][row].append(current_clue_number)
97 current_clue_number += 1
99 crossword["grid_questions_start"][row].append(0)
103 def crossword(stdscr, crossworddata):
104 crossword = parsecrossword(crossworddata)
106 grid_length = len(crossword["grid"][0])
108 stdscr.addch(cury, curx, curses.ACS_ULCORNER)
110 while ((curx - 1) / 4) < grid_length:
111 stdscr.addch(cury, curx, curses.ACS_HLINE)
112 stdscr.addch(cury, curx+1, curses.ACS_HLINE)
113 stdscr.addch(cury, curx+2, curses.ACS_HLINE)
114 stdscr.addch(cury, curx+3, curses.ACS_TTEE)
117 stdscr.addch(cury, curx, curses.ACS_URCORNER)
120 for line in crossword["grid"]:
125 stdscr.addch(cury, curx, curses.ACS_VLINE)
127 stdscr.addch(cury+1, curx, curses.ACS_PLUS)
129 stdscr.addch(cury+1, curx, curses.ACS_LTEE)
130 stdscr.addch(cury+1, curx + 1, curses.ACS_HLINE)
131 stdscr.addch(cury+1, curx + 2, curses.ACS_HLINE)
132 stdscr.addch(cury+1, curx + 3, curses.ACS_HLINE)
135 stdscr.addch(cury, curx, curses.ACS_BLOCK)
136 stdscr.addch(cury, curx+1, curses.ACS_BLOCK)
137 stdscr.addch(cury, curx+2, curses.ACS_BLOCK)
138 elif crossword["grid_questions_start"][curgridy][curgridx] > 0:
139 stdscr.addstr(cury, curx, ''.join([superscript_numbers[x].decode(code) for x in str(crossword["grid_questions_start"][curgridy][curgridx])]).encode(code))
143 stdscr.addch(cury, curx, curses.ACS_VLINE)
144 stdscr.addch(cury + 1, curx, curses.ACS_RTEE)
149 stdscr.addch(cury, curx, curses.ACS_LLCORNER)
151 while ((curx - 1) / 4) < grid_length:
152 stdscr.addch(cury, curx, curses.ACS_HLINE)
153 stdscr.addch(cury, curx+1, curses.ACS_HLINE)
154 stdscr.addch(cury, curx+2, curses.ACS_HLINE)
155 stdscr.addch(cury, curx+3, curses.ACS_BTEE)
158 stdscr.addch(cury, curx, curses.ACS_LRCORNER)
164 curses.wrapper(crossword, crossworddata)
165 print parsecrossword(crossworddata)