-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate.py
234 lines (197 loc) · 7.04 KB
/
state.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
from cell import Cell
from clue import Clue
from submission import Submission
import puz
class State:
def __init__(self, puzzle, clues):
self.height = puzzle.height
self.width = puzzle.width
self.puzzle = puzzle
self.show_incorrect_cells = False
self.current_clue_index = 0
self.current_clue_direction = "Across"
self.clues_across = [x for x in clues if x.direction == Clue.ACROSS]
self.clues_down = [x for x in clues if x.direction == Clue.DOWN]
self.grid = [[None for i in range(self.width)] for j in range(self.height)]
circle_idxs = puzzle.markup().get_markup_squares()
self._populate_grid(puzzle, circle_idxs)
self._connect_with_clues(puzzle)
#private helper method
def _populate_grid(self, puzzle, circle_idxs):
number = 1 # IRL indexes by 1
for row in range(self.height):
for col in range(self.width):
flat_array_idx = row * self.width + col
color = puzzle.fill[flat_array_idx]
if color == Cell.BLACK:
self.grid[row][col] = Cell(color=Cell.BLACK, x=col, y=row)
continue
circled = True if flat_array_idx in circle_idxs else False
if self._cell_starts_across_clue(row, col) or self._cell_starts_down_clue(row, col):
self.grid[row][col] = Cell(color=Cell.WHITE,
x=col,
y=row,
number=number,
answer=puzzle.solution[flat_array_idx],
circled=circled)
number += 1
else:
self.grid[row][col] = Cell(color=Cell.WHITE,
x=col,
y=row,
answer=puzzle.solution[flat_array_idx],
circled=circled)
def _connect_with_clues(self, puzzle):
hor_clues_count = 0
ver_clues_count = 0
for row in range(self.height):
for col in range(self.width):
hor_clue_index = None
ver_clue_index = None
if puzzle.fill[row*self.width + col] == Cell.BLACK:
continue
if self._cell_starts_across_clue(row, col):
hor_clue_index = hor_clues_count
hor_clues_count += 1
else:
hor_clue_index = self._get_clue_index(row, col - 1, Clue.ACROSS)
if self._cell_starts_down_clue(row, col):
ver_clue_index = ver_clues_count
ver_clues_count += 1
else:
ver_clue_index = self._get_clue_index(row - 1, col, Clue.DOWN)
if hor_clue_index != None:
self.grid[row][col].clue_across = self.clues_across[hor_clue_index]
self.clues_across[hor_clue_index].cells.append(self.grid[row][col])
if ver_clue_index != None:
self.grid[row][col].clue_down = self.clues_down[ver_clue_index]
self.clues_down[ver_clue_index].cells.append(self.grid[row][col])
def get_color(self, row, col):
flat_array_idx = row * self.width + col
if (self.puzzle.fill[flat_array_idx] == '.'):
return Cell.BLACK
else:
Cell.WHITE
def _get_answer(self, row, col):
flat_array_idx = row * self.width + col
return ord(self.puzzle.solution[flat_array_idx])
# private helper method
def _cell_starts_across_clue(self, row, col):
if (col == 0):
if (self.get_color(row, col + 1) != Cell.BLACK):
return True
else:
return False
elif (self.get_color(row, col - 1) == Cell.BLACK):
if (col == self.width - 1 or self.get_color(row, col + 1) == Cell.BLACK):
return False
else:
return True
else:
return False
# private helper method
def _cell_starts_down_clue(self, row, col):
if (row == 0):
if (self.get_color(row + 1, col) != Cell.BLACK):
return True
else:
return False
elif (self.get_color(row - 1, col) == Cell.BLACK):
if (row == self.height - 1 or self.get_color(row + 1, col) == Cell.BLACK):
return False
else:
return True
else:
return False
def _get_clue_index(self, row, col, direction):
if self.grid[row][col].color == Cell.BLACK:
return None
if direction == Clue.ACROSS and self.grid[row][col].clue_across == None:
return None
if direction == Clue.DOWN and self.grid[row][col].clue_down == None:
return None
if direction == Clue.ACROSS:
return self.clues_across.index(self.grid[row][col].clue_across)
else:
return self.clues_down.index(self.grid[row][col].clue_down)
def parse_number(self, number, is_across):
for row in range(self.height):
for col in range(self.width):
if self.grid[row][col].number == number:
if is_across and self._cell_starts_across_clue(row, col):
return row, col
elif not is_across and self._cell_starts_down_clue(row, col):
return row, col
else:
return None, None
def parse_submission(self, place):
number, direction = place.split()
number = int(number)
is_across = (direction.lower() == "a")
row, col = self.parse_number(number, is_across)
return row, col, is_across
def submit_letter_exact(self, row, col, letter):
if (letter == ' '):
self.delete_letter_exact(row, col)
elif row < self.height and col < self.width and self.grid[row][col].color != Cell.BLACK:
old_letter = self.grid[row][col].content
self.grid[row][col].content = letter
if (old_letter != ' ' and old_letter != None):
return
def delete_letter_exact(self, row, col):
if self.grid[row][col].color != Cell.BLACK:
old_letter = self.grid[row][col]
self.grid[row][col].content = None
if (old_letter == ' ' or old_letter == None):
return
def submit_letter(self, row, col, offset, letter, is_across):
if is_across:
self.submit_letter_exact(row, col + offset - 1, letter)
else:
self.submit_letter_exact(row + offset - 1, col, letter)
def delete_letter(self, row, col, offset, is_across):
if is_across:
self.submit_letter_exact(row, col + offset - 1, None)
else:
self.submit_letter_exact(row + offset - 1, col, None)
def submit_word(self, row, col, word, is_across):
for i in range(len(word)):
if row >= self.height or col >= self.width or self.grid[row][col].color == Cell.BLACK:
return
self.submit_letter_exact(row, col, word[i])
if is_across:
col += 1
else:
row += 1
def delete_word(self, row, col, is_across):
while row < self.height and col < self.width and self.grid[row][col].color != Cell.BLACK:
self.delete_letter_exact(row, col)
if is_across:
col += 1
else:
row += 1
def submit(self, submission_type, clue, solution = None, offset = None):
try:
row, col, is_across = self.parse_submission(clue)
except Exception, e:
return
if row is None:
return
if is_across:
self.current_clue_index = self._get_clue_index(row, col, Clue.ACROSS)
self.current_clue_direction = "Across"
else:
self.current_clue_index = self._get_clue_index(row, col, Clue.DOWN)
self.current_clue_direction = "Down"
if submission_type == Submission.ADD_LETTER:
self.submit_letter(row, col, offset, solution, is_across)
if submission_type == Submission.ADD_WORD:
self.submit_word(row, col, solution, is_across)
if submission_type == Submission.DELETE_LETTER:
self.delete_letter(row, col, offset, is_across)
if submission_type == Submission.DELETE_WORD:
self.delete_word(row, col, is_across)
def check_solution(self):
self.show_incorrect_cells = True
def uncheck_solution(self):
self.show_incorrect_cells = False