-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbrute.py
93 lines (68 loc) · 1.38 KB
/
brute.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
class Brute:
'''Brute force solver'''
board = None
index = 0
def __init__(self, board):
self.board = board
def solve(self):
'''
Solves the given board. Raises exception if unsolvable.
Args:
None
Returns:
None
'''
self.index = 0
while True:
#Increment current value of cell
current = self.board.get_cell(self.index)
if current == "*":
current = 1
else:
current += 1
#Find the first move that works for that cell
while True:
try:
self.board.play_move(self.index, current)
break
except Exception:
current += 1
if current == 10:
break
#No possible moves
if current == 10:
self.board.play_move(self.index, "*")
if not self.back_track():
raise Exception("Unsolvable board")
else:
#If at the end of the board
if not self.front_track():
return
def front_track(self):
'''
Find the next biggest index that isn't static
Args:
None
Returns:
The index
'''
self.index += 1
while self.index in self.board.static_indices:
self.index += 1
if self.index == 81:
return False
return True
def back_track(self):
'''
Find the previous biggest index that isn't static
Args:
None
Returns:
The index
'''
self.index -= 1
while self.index in self.board.static_indices:
self.index -= 1
if self.index == -1:
return False
return True