]> git.sommitrealweird.co.uk Git - curses-crossword.git/blob - curses-crossword.py
Encode clues in real charset...
[curses-crossword.git] / curses-crossword.py
1 #!/usr/bin/python
2
3 import curses
4 import curses.ascii
5 import locale
6 import codecs
7
8 locale.setlocale(locale.LC_ALL, '')
9 code = locale.getpreferredencoding()
10
11 superscript_numbers = {}
12
13 try:
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),
25     }
26 except:
27     for number in range(0,10):
28         superscript_numbers[str(number)] = str(number)
29
30 crossworddata = codecs.open("crossword.output", "r", "utf-8").read()
31
32 def parsecrossword(crossworddata):
33     ingrid = False
34     inacross = False
35     indown = False
36     crossword = {"grid": [], "across": {}, "down": {}, "grid_questions_start": [], "grid_questions_end": []}
37
38     for line in crossworddata.split("\n"):
39         line = line.strip("\n")
40         if line == "GRID":
41             ingrid = True
42             inacross = False
43             indown = False
44         elif line == "ACROSS":
45             ingrid = False
46             inacross = True
47             indown = False
48         elif line == "DOWN":
49             ingrid = False
50             inacross = False
51             indown = True
52         else:
53             if ingrid:
54                 if line != "":
55                     crossword["grid"].append(line)
56             if inacross:
57                 if line != "":
58                     parts = line.split()
59                     question_number = int(parts[0])
60                     clue = " ".join(parts[1:])
61                     crossword["across"][int(question_number)] = clue.encode('utf-8')
62             if indown:
63                 if line != "":
64                     parts = line.split()
65                     question_number = int(parts[0])
66                     clue = " ".join(parts[1:])
67                     crossword["down"][int(question_number)] = clue.encode('utf-8')
68     num_cols = len(crossword["grid"][0])
69     num_rows = len(crossword["grid"])
70
71     current_clue_number = 1
72
73     for row in range(0,num_rows):
74         crossword["grid_questions_start"].append([])
75
76     for row in range(0,num_rows):
77         for col in range(0,num_cols):
78             have_clue = False
79             if col > 0 \
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":
84                 have_clue = True
85             if col == 0 and crossword["grid"][row][col] != "x" \
86                 and crossword["grid"][row][col + 1] != "x":
87                 have_clue = True
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":
92                 have_clue = True
93             if row == 0 and crossword["grid"][row][col] != "x" \
94                 and crossword["grid"][row + 1][col] != "x":
95                 have_clue = True
96             if have_clue:
97                 crossword["grid_questions_start"][row].append(current_clue_number)
98                 current_clue_number += 1
99             else:
100                 crossword["grid_questions_start"][row].append(0)
101
102     return crossword
103
104 def crossword(stdscr, crossworddata):
105     crossword = parsecrossword(crossworddata)
106     cury = 0
107     grid_length = len(crossword["grid"][0])
108     curx = 0
109     stdscr.addch(cury, curx, curses.ACS_ULCORNER)
110     curx += 1
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)
116         curx += 4
117     curx -= 1
118     stdscr.addch(cury, curx, curses.ACS_URCORNER)
119     cury += 1
120     curgridy=0
121     for line in crossword["grid"]:
122         datastr = ""
123         curx = 0
124         curgridx=0
125         for c in line:
126             stdscr.addch(cury, curx, curses.ACS_VLINE)
127             if curx > 0:
128                 stdscr.addch(cury+1, curx, curses.ACS_PLUS)
129             else:
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)
134             curx += 1
135             if c == "x":
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))
141             curx += 3
142             curgridx += 1
143         else:
144             stdscr.addch(cury, curx, curses.ACS_VLINE)
145             stdscr.addch(cury + 1, curx, curses.ACS_RTEE)
146         cury += 2
147         curgridy += 1
148     cury -= 1
149     curx = 0
150     stdscr.addch(cury, curx, curses.ACS_LLCORNER)
151     curx += 1
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)
157         curx += 4
158     curx -= 1
159     stdscr.addch(cury, curx, curses.ACS_LRCORNER)
160     # draw the clues in
161     cury = (len(crossword["grid"]) * 2) + 1
162     curx = 0
163     stdscr.addstr(cury, curx, "Across")
164     cury += 1
165     for cluenumber in crossword["across"].keys():
166         stdscr.addstr(cury, curx, "%3s: %s" %(str(cluenumber), crossword["across"][cluenumber]))
167         cury += 1
168
169     cury += 1
170     stdscr.addstr(cury, curx, "Down")
171     cury += 1
172     for cluenumber in crossword["down"].keys():
173         stdscr.addstr(cury, curx, "%3s: %s" %(str(cluenumber), crossword["down"][cluenumber]))
174         cury += 1
175
176     curx = 3
177     cury = 1
178     gridx = 0
179     gridy = 0
180
181     while crossword["grid"][gridy][gridx] == "x":
182         curx += 4
183         gridx += 1
184
185     stdscr.move(cury,curx)
186
187     while 1:
188         c = stdscr.getch()
189         if c == curses.ascii.ESC:
190             break
191         if c == curses.KEY_RIGHT:
192             if gridx < (len(crossword["grid"][0]) - 1):
193                 gridx += 1
194                 curx += 4
195                 while gridx < (len(crossword["grid"][0]) -1) \
196                     and crossword["grid"][gridy][gridx] == "x":
197                     gridx += 1
198                     curx += 4
199                 while crossword["grid"][gridy][gridx] == "x":
200                     gridx -= 1
201                     curx -= 4
202                 stdscr.move(cury, curx)
203         if c == curses.KEY_LEFT:
204             if gridx > 0:
205                 curx -= 4
206                 gridx -= 1
207                 while gridx > 0 \
208                     and crossword["grid"][gridy][gridx] == "x":
209                         gridx -= 1
210                         curx -= 4
211                 while crossword["grid"][gridy][gridx] == "x":
212                     gridx += 1
213                     curx += 4
214                 stdscr.move(cury, curx)
215         if c == curses.KEY_UP:
216             if gridy > 0:
217                 gridy -= 1
218                 cury -= 2
219                 while gridy > 0 \
220                     and crossword["grid"][gridy][gridx] == "x":
221                         gridy -= 1
222                         cury -= 2
223                 while crossword["grid"][gridy][gridx] == "x":
224                     gridy += 1
225                     cury += 2
226                 stdscr.move(cury, curx)
227         if c == curses.KEY_DOWN:
228             if gridy < (len(crossword["grid"]) - 1):
229                 gridy += 1
230                 cury += 2
231                 while gridy < (len(crossword["grid"]) - 1) \
232                     and crossword["grid"][gridy][gridx] == "x":
233                     gridy += 1
234                     cury += 2
235                 while crossword["grid"][gridy][gridx] == "x":
236                     gridy -= 1
237                     cury -= 2
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)
245
246
247 curses.wrapper(crossword, crossworddata)