-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheightpuzzle.py
291 lines (228 loc) · 7.89 KB
/
eightpuzzle.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
from __future__ import print_function
import search
import random
class EightPuzzleState:
"""
This class defines the mechanics of the Eight Puzzle. The task of
recasting this puzzle as a search problem is left to the class
called EightPuzzleSearchProblem.
"""
def __init__(self, numbers):
"""
Constructs a new eight puzzle from an ordering of numbers.
numbers: a list of integers from 0 to 8 representing an instance
of the eight puzzle. 0 represents the blank space. Thus, the list
[1, 0, 2, 3, 4, 5, 6, 7, 8]
represents the eight puzzle:
-------------
| 1 | | 2 |
-------------
| 3 | 4 | 5 |
-------------
| 6 | 7 | 8 |
------------
The configuration of the puzzle is stored in a 2-dimensional list
(a list of lists) called 'cells'.
"""
self.cells = []
# A copy is made so as not to cause side-effects.
numbers = numbers[:]
numbers.reverse()
for row in range(3):
self.cells.append([])
for col in range(3):
self.cells[row].append(numbers.pop())
if self.cells[row][col] == 0:
self.blankLocation = row, col
def is_goal(self):
"""
Checks to see if the puzzle is in its goal state.
-------------
| | 1 | 2 |
-------------
| 3 | 4 | 5 |
-------------
| 6 | 7 | 8 |
-------------
>>> EightPuzzleState([0, 1, 2, 3, 4, 5, 6, 7, 8]).is_goal()
True
>>> EightPuzzleState([1, 0, 2, 3, 4, 5, 6, 7, 8]).is_goal()
False
"""
current = 0
for row in range(3):
for col in range(3):
if current != self.cells[row][col]:
return False
current += 1
return True
def legal_moves(self):
"""
Returns a list of legal moves from the current state.
Moves consist of moving the blank space up, down, left or right.
These are encoded as 'up', 'down', 'left' and 'right' respectively.
>>> EightPuzzleState([0, 1, 2, 3, 4, 5, 6, 7, 8]).legal_moves()
['down', 'right']
"""
moves = []
row, col = self.blankLocation
if row != 0:
moves.append('up')
if row != 2:
moves.append('down')
if col != 0:
moves.append('left')
if col != 2:
moves.append('right')
return moves
def result(self, move):
"""
Returns a new eightPuzzle with the current state and blankLocation
updated based on the provided move.
The move should be a string drawn from a list returned by legalMoves.
Illegal moves will raise an exception, which may be an array bounds
exception.
NOTE: This function *does not* change the current object. Instead,
it returns a new object.
"""
row, col = self.blankLocation
if move == 'up':
new_row = row - 1
new_col = col
elif move == 'down':
new_row = row + 1
new_col = col
elif move == 'left':
new_row = row
new_col = col - 1
elif move == 'right':
new_row = row
new_col = col + 1
else:
raise Exception("Illegal Move")
# Create a copy of the current eightPuzzle.
new_puzzle = EightPuzzleState([0, 0, 0, 0, 0, 0, 0, 0, 0])
new_puzzle.cells = [values[:] for values in self.cells]
# And update it to reflect the move
new_puzzle.cells[row][col] = self.cells[new_row][new_col]
new_puzzle.cells[new_row][new_col] = self.cells[row][col]
new_puzzle.blankLocation = new_row, new_col
return new_puzzle
# Utilities for comparison and display
def __eq__(self, other):
"""
Overloads '==' such that two eightPuzzles with the same configuration
are equal.
>>> EightPuzzleState([0, 1, 2, 3, 4, 5, 6, 7, 8]) == \
EightPuzzleState([1, 0, 2, 3, 4, 5, 6, 7, 8]).result('left')
True
"""
for row in range(3):
if self.cells[row] != other.cells[row]:
return False
return True
def __hash__(self):
return hash(str(self.cells))
def __get_ascii_string(self):
"""
Returns a display string for the maze
"""
lines = []
horizontal_line = ('-' * 13)
lines.append(horizontal_line)
for row in self.cells:
row_line = '|'
for col in row:
if col == 0:
col = ' '
row_line = row_line + ' ' + col.__str__() + ' |'
lines.append(row_line)
lines.append(horizontal_line)
return '\n'.join(lines)
def __str__(self):
return self.__get_ascii_string()
# TODO: Implement the methods in this class.
class EightPuzzleSearchProblem(search.SearchProblem):
"""
Implementation of a SearchProblem for the Eight Puzzle domain
Each state is represented by an instance of an eightPuzzle.
"""
def __init__(self, puzzle):
"""
Creates a new EightPuzzleSearchProblem which stores search
information.
"""
self.puzzle = puzzle
def get_start_state(self):
return puzzle
def is_goal_state(self, state):
return state.is_goal()
def get_successors(self, state):
"""
Returns list of (successor, action, stepCost) pairs where each
successor is either left, right, up, or down from the original
state and the cost is 1.0 for each.
"""
successor = []
for move in state.legal_moves():
successor.append((state.result(move), move, 1))
return successor
def get_cost_of_actions(self, actions):
"""
actions: A list of actions to take
This method returns the total cost of a particular sequence of actions.
The sequence must be composed of legal moves.
"""
return len(actions)
EIGHT_PUZZLE_DATA = [[1, 0, 2, 3, 4, 5, 6, 7, 8],
[1, 7, 8, 2, 3, 4, 5, 6, 0],
[4, 3, 2, 7, 0, 5, 1, 6, 8],
[5, 1, 3, 4, 0, 2, 6, 7, 8],
[1, 2, 5, 7, 6, 8, 0, 4, 3],
[0, 3, 1, 6, 8, 2, 7, 5, 4]]
def load_eight_puzzle(puzzle_number):
"""
puzzleNumber: The number of the eight puzzle to load.
Returns an eight puzzle object generated from one of the
provided puzzles in EIGHT_PUZZLE_DATA.
puzzleNumber can range from 0 to 5.
>>> print(load_eight_puzzle(0))
-------------
| 1 | | 2 |
-------------
| 3 | 4 | 5 |
-------------
| 6 | 7 | 8 |
-------------
"""
return EightPuzzleState(EIGHT_PUZZLE_DATA[puzzle_number])
def create_random_eight_puzzle(moves=100):
"""
moves: number of random moves to apply
Creates a random eight puzzle by applying a series of 'moves',
which are random moves to a solved puzzle.
"""
puzzle = EightPuzzleState([0, 1, 2, 3, 4, 5, 6, 7, 8])
for move in range(moves):
# Execute a random legal move.
puzzle = puzzle.result(random.sample(puzzle.legal_moves(), 1)[0])
return puzzle
if __name__ == '__main__':
try:
input = raw_input
except NameError:
pass
puzzle = create_random_eight_puzzle(25)
print('A random puzzle:')
print(puzzle)
problem = EightPuzzleSearchProblem(puzzle)
path = search.breadth_first_search(problem)
print('BFS found a path of %d moves: %s' % (len(path), str(path)))
curr = puzzle
i = 1
for a in path:
curr = curr.result(a)
print('After %d move%s: %s' % (i, ("", "s")[i > 1], a))
print(curr)
input("Press return for the next state...")
i += 1