-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgrid_peg_solitaire_puzzle.py
232 lines (194 loc) · 7.81 KB
/
grid_peg_solitaire_puzzle.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
from puzzle import Puzzle
class GridPegSolitairePuzzle(Puzzle):
"""
Snapshot of peg solitaire on a rectangular grid. May be solved,
unsolved, or even unsolvable.
"""
def __init__(self, marker, marker_set):
"""
Create a new GridPegSolitairePuzzle self with
marker indicating pegs, spaces, and unused
and marker_set indicating allowed markers.
@type marker: list[list[str]]
@type marker_set: set[str]
"#" for unused, "*" for peg, "." for empty
"""
assert isinstance(marker, list)
assert len(marker) > 0
assert all([len(x) == len(marker[0]) for x in marker[1:]])
assert all([all(x in marker_set for x in row) for row in marker])
assert all([x == "*" or x == "." or x == "#" for x in marker_set])
self._marker, self._marker_set = marker, marker_set
def __str__(self):
"""
Return a human-readable string representation of GridPegSolitairePuzzle
self.
@type self: GridPegSolitairePuzzle
@rtype: str
>>> grid = [["*", "*", "*", "*", "*"]]
>>> grid += [["*", "*", "*", "*", "*"]]
>>> grid += [["*", "*", "*", "*", "*"]]
>>> grid += [["*", "*", ".", "*", "*"]]
>>> grid += [["*", "*", "*", "*", "*"]]
>>> g = GridPegSolitairePuzzle(grid, {"*", ".", "#"})
>>> print(g)
*****
*****
*****
**.**
*****
_____
"""
markers = self._marker[:]
string_ = ''
for i in markers:
for j in i:
string_ += j
string_ += '\n'
return string_[:-1] + "\n" + '_____'
def __eq__(self, other):
"""
Return whether GridPegSolitairePuzzle self is equal to other.
@type self: GridPegSolitairePuzzle
@type other: GridPegSolitairePuzzle | Any
@rtype: bool
>>> grid = [["*", "*", ".", "*"]]
>>> gpsp = GridPegSolitairePuzzle(grid, {"*", ".", "#"})
>>> grid_2 = [["#", "*", ".", "."]]
>>> gpsp_2 = GridPegSolitairePuzzle(grid_2, {"*", ".", "#"})
>>> gpsp_2 == gpsp
False
>>> gpsp_2 = GridPegSolitairePuzzle(grid, {"*", ".", "#"})
>>> gpsp_2 == gpsp
True
"""
return ((type(self) == type(other)) and
(self._marker == other._marker) and
(self._marker_set == other._marker_set))
# legal extensions consist of all configurations that can be reached by
# making a single jump from this configuration
def extensions(self):
"""
Return list of extensions of GridPegSolitairePuzzle self.
@type self: GridPegSolitairePuzzle
@rtype: list[GridPegSolitairePuzzle]
>>> grid = [["*", "*", ".", "*"]]
>>> gpsp = GridPegSolitairePuzzle(grid, {"*", ".", "#"})
>>> print(gpsp.extensions()[0])
..**
_____
>>> grid = [["*", "*", ".", "*", "#"]]
>>> gpsp = GridPegSolitairePuzzle(grid, {"*", ".", "#"})
>>> print(gpsp.extensions()[0])
..**#
_____
"""
grid_ = [x[:] for x in self._marker] # copy
list_extensions = []
for i in range(len(grid_)):
for j in range(len(self._marker[i])):
if grid_[i][j] == ".": # if that index is empty
# check to the right
if (j + 2 in range(len(grid_[0]))) and\
(grid_[i][j + 1] == "*") and\
(grid_[i][j + 2] == "*"):
grid_copy = [x[:] for x in self._marker]
grid_copy[i][j] = "*"
grid_copy[i][j + 1] = "."
grid_copy[i][j + 2] = "."
list_extensions.append(
GridPegSolitairePuzzle(
grid_copy, self._marker_set))
# check to the left
if (j - 2 in range(len(grid_[0]))) and\
(grid_[i][j - 1] == "*") and\
(grid_[i][j - 2] == "*"):
grid_copy = [x[:] for x in self._marker]
grid_copy[i][j] = "*"
grid_copy[i][j - 1] = "."
grid_copy[i][j - 2] = "."
list_extensions.append(
GridPegSolitairePuzzle(
grid_copy, self._marker_set))
# check above
if (i + 2 in range(len(grid_))) and\
(grid_[i + 1][j] == "*") and\
(grid_[i + 2][j] == "*"):
grid_copy = [x[:] for x in self._marker]
grid_copy[i][j] = "*"
grid_copy[i + 1][j] = "."
grid_copy[i + 2][j] = "."
list_extensions.append(
GridPegSolitairePuzzle(
grid_copy, self._marker_set))
# check below
if (i - 2 in range(len(grid_))) and\
(grid_[i - 1][j] == "*") and\
(grid_[i - 2][j] == "*"):
grid_copy = [x[:] for x in self._marker]
grid_copy[i][j] = "*"
grid_copy[i - 1][j] = "."
grid_copy[i - 2][j] = "."
list_extensions.append(
GridPegSolitairePuzzle(
grid_copy, self._marker_set))
return list_extensions
# override is_solved()
# A configuration is solved when there is exactly one "*" left
def is_solved(self):
"""
Return whether self is solved.
@return: GridPegSolitairePuzzle
@rtype: bool
>>> grid = [["*", ".", ".", "."]]
>>> gpsp = GridPegSolitairePuzzle(grid, {"*", ".", "#"})
>>> gpsp.is_solved()
True
>>> grid2 = [[".", ".", ".", "."]]
>>> gpsp2 = GridPegSolitairePuzzle(grid2, {"*", ".", "#"})
>>> gpsp2.is_solved()
False
"""
count = 0
for item in self._marker:
for symbol in item:
if symbol == "*":
count += 1
return count == 1
if __name__ == "__main__":
import doctest
doctest.testmod()
from puzzle_tools import depth_first_solve
# grid = [["*", "*", ".", "*"]] # 0.0001888275146484375 seconds bfs solved
# grid = [["#", "*", ".", "*", "*"]] # 0.00020813941955566406 seconds
# bfs solved
grid = [["#"], # 0.00023102760314941406 seconds bfs solved
["*"],
["*"],
["."],
["*"]]
# grid = [["*", "*", "*"], # 0.017332077026367188 seconds bfs solved
# ["*", "*", "*"],
# ["*", "*", "*"],
# [".", "*", "*"]]
# grid = [["*", "*", ".", "*", "*"], # 602.9188828468323 seconds bfs solved
# ["*", "*", "*", "*", "*"], # 4.51656699180603 seconds dfs solved
# ["*", "*", "*", "*", "*"],
# ["*", "*", ".", "*", "*"],
# ["*", "*", "*", "*", "*"]]
# grid = [["*", "*", "*", "*", "*"],
# ["*", "*", "*", "*", "*"],
# ["*", "*", "*", "*", "*"],
# ["*", "*", ".", "*", "*"],
# ["*", "*", "*", "*", "*"]]
gpsp = GridPegSolitairePuzzle(grid, {"*", ".", "#"})
import time
start = time.time()
# solution = breadth_first_solve(gpsp)
solution = depth_first_solve(gpsp)
while solution.children:
solution = solution.children[0]
end = time.time()
print("Solved 5x5 peg solitaire in {} seconds.".format(end - start))
# print("Using depth-first: \n{}".format(solution))
print(solution)