]> git.sommitrealweird.co.uk Git - curses-crossword.git/blob - curses-crossword.py
add basic movement
[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("g2-20090701.txt", "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
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
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     curx = 3
161     cury = 1
162     stdscr.move(1,3)
163     while 1:
164         c = stdscr.getch()
165         if c == curses.ascii.ESC:
166             break
167         if c == curses.KEY_RIGHT:
168             curx += 4
169             stdscr.move(cury, curx)
170         if c == curses.KEY_LEFT:
171             curx -= 4
172             stdscr.move(cury, curx)
173         if c == curses.KEY_UP:
174             cury -= 2
175             stdscr.move(cury, curx)
176         if c == curses.KEY_DOWN:
177             cury += 2
178             stdscr.move(cury, curx)
179
180
181 curses.wrapper(crossword, crossworddata)