-
Notifications
You must be signed in to change notification settings - Fork 0
/
kuromasu.py
252 lines (214 loc) · 8.07 KB
/
kuromasu.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import copy
import queue
class Kuromasu:
instances = 0
steps = 0
def __init__(self, board, parent=None):
self.parent = parent
self.board = copy.deepcopy(board)
Kuromasu.instances += 1
def __str__(self):
out = '\n'.join([' '.join([cell for cell in row if cell != 'x']) for row in self.board if row.count('x') <= 2])
return out
def __eq__(self, other):
return isinstance(other, self.__class__) and self.board == other.board
def __hash__(self):
"""Shift-Add-XOR hash function."""
h = 0
for r in range(len(self.board)):
for c in range(len(self.board)):
h ^= (h << 5) + (h >> 2) + ord(self.board[r][c])
return h
def __lt__(self, other):
self_priority = self.f()
other_priority = other.f()
return self_priority < other_priority
@staticmethod
def reset():
Kuromasu.instances = 0
Kuromasu.steps = 0
def create_padding(self):
"""Create padding for easy bounds checking."""
for row in self.board:
row.insert(0, 'x')
row.append('x')
self.board.insert(0, ['x' for i in range(len(self.board[0]))])
self.board.append(['x' for i in range(len(self.board[0]))])
def is_solved(self):
"""Check if the board is in a correct state within the game rules."""
return (self.check_white_cells_visible_from_numbers()
and self.check_all_white_cells_accessible()
and self.check_black_cells_separate())
def check_white_cells_visible_from_numbers(self):
number_cells = self.get_number_cells()
for position in number_cells:
row, col = position
if int(self.board[row][col]) != self.count_white_cells_visible_from_number(position):
return False
return True
def get_number_cells(self):
number_cells = [(r, c) for r in range(1, len(self.board)) for c in range(1, len(self.board)) if self.board[r][c].isnumeric() and self.board[r][c] != '0']
return number_cells
def count_white_cells_visible_from_number(self, position):
row, col = position
# numbered cell is always visible
visible = 1
# north
for i in range(row - 1, 0, -1):
if self.board[i][col].isnumeric():
visible += 1
else:
break
# south
for i in range(row + 1, len(self.board)):
if self.board[i][col].isnumeric():
visible += 1
else:
break
# west
for i in range(col - 1, 0, -1):
if self.board[row][i].isnumeric():
visible += 1
else:
break
# east
for i in range(col + 1, len(self.board)):
if self.board[row][i].isnumeric():
visible += 1
else:
break
return visible
def check_all_white_cells_accessible(self):
"""
Traverse all connected white cells, check with total white cells.
WHITE_CELLS = ALL_CELLS - BLACK_CELLS.
"""
connected = 0
visited = []
A = []
A.append(self.get_first_white_cell())
while A:
cell = A.pop()
if cell not in visited:
visited += [cell]
neighbors = self.get_neighboring_white_cells(cell)
A += neighbors
connected += 1
return connected == (self.count_cells_total() - self.count_black_cells_total())
def get_first_white_cell(self):
positions = [(r, c) for r in range(1, len(self.board)) for c in range(1, len(self.board)) if self.board[r][c].isnumeric()]
return positions[0]
def get_neighboring_white_cells(self, position):
white_cells = []
row, col = position
for (r, c) in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
if self.board[row+r][col+c].isnumeric():
white_cells.append((row+r, col+c))
return white_cells
def count_cells_total(self):
return (len(self.board) - 2) * (len(self.board) - 2)
def count_black_cells_total(self):
black_cells_count = [(r, c) for r in range(1, len(self.board)) for c in range(1, len(self.board)) if self.board[r][c] == '#']
return len(black_cells_count)
def check_black_cells_separate(self):
black_cells = self.get_black_cells()
for position in black_cells:
row, col = position
for (r, c) in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
if self.board[row+r][col+c] == '#':
return False
return True
def get_black_cells(self):
black_cells = [(r, c) for r in range(len(self.board)) for c in range(len(self.board)) if self.board[r][c] == '#']
return black_cells
def get_new_states(self):
"""Generate new board states in a nested loop to exhaust every possibility."""
states = []
row, col = self.get_first_white_cell()
for r in range(row, len(self.board)):
for c in range(col, len(self.board)):
if self.board[r][c] == '0':
self.board[r][c] = '#'
states += [Kuromasu(board, self)]
# perserve previous state
self.board[r][c] = '0'
return states
def h(self):
correct_number_cells = 0
number_cells = self.get_number_cells()
for position in number_cells:
row, col = position
if int(self.board[row][col]) == self.count_white_cells_visible_from_number((row, col)):
correct_number_cells += 1
return len(number_cells) - correct_number_cells
def f(self):
"""
Objective function f(n).
g(x) corresponds to the amount of steps taken,
h(x) corresponds to the amount of correct number cells.
"""
return Kuromasu.steps + self.h()
@staticmethod
def solve_dfs(board):
start_state = Kuromasu(board)
start_state.create_padding()
if start_state.is_solved():
return start_state, Kuromasu.steps, Kuromasu.instances
A = queue.LifoQueue()
visited = set()
A.put(start_state)
visited.add(start_state)
while not A.empty():
state = A.get()
if state.is_solved():
return state.__str__(), Kuromasu.steps, Kuromasu.instances
for new_state in state.get_new_states():
if new_state not in visited:
visited.add(new_state)
A.put(new_state)
Kuromasu.steps += 1
return 'UNSOLVABLE', Kuromasu.steps, Kuromasu.instances
@staticmethod
def solve_a_star(board):
start_state = Kuromasu(board)
start_state.create_padding()
if start_state.is_solved():
return start_state, Kuromasu.steps, Kuromasu.instances
A = queue.PriorityQueue()
visited = set()
A.put(start_state)
visited.add(start_state)
while not A.empty():
state = A.get()
if state.is_solved():
return state.__str__(), Kuromasu.steps, Kuromasu.instances
for new_state in state.get_new_states():
if new_state not in visited:
visited.add(new_state)
A.put(new_state)
Kuromasu.steps += 1
return 'UNSOLVABLE', Kuromasu.steps, Kuromasu.instances
#def main():
# BOARD = [
# ['4', '0', '0'],
# ['0', '0', '0'],
# ['0', '0', '4'],
# ]
#
# print("DFS:")
# result, steps, instances = Kuromasu.solve_dfs(copy.deepcopy(BOARD))
# print(result)
# print("step count: ", steps)
# print("instances created: ", instances)
# print()
#
# Kuromasu.reset()
# print("A*:")
# result, steps, instances = Kuromasu.solve_a_star(copy.deepcopy(BOARD))
# print(result)
# print("step count: ", steps)
# print("instances created: ", instances)
# print()
#
#if __name__ == "__main__":
# main()