-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblockPuzzle.py
57 lines (48 loc) · 1.3 KB
/
blockPuzzle.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
#!/usr/bin/python
class BlockPuzzle(object):
def __init__(self, n, xs=None):
"""Create an nxn block puzzle
Use XS to initialize to a specific state.
"""
self.n = n
self.n2 = n * n
if xs is None:
self.xs = [(x + 1) % self.n2 for x in xrange(self.n2)]
else:
self.xs = list(xs)
self.hsh = None
self.last_move = []
def __hash__(self):
if self.hsh is None:
self.hsh = hash(tuple(self.xs))
return self.hsh
def __repr__(self):
return "BlockPuzzle(%d, %s)" % (self.n, self.xs)
def show(self):
ys = ["%2d" % x for x in self.xs]
xs = [" ".join(ys[kk:kk+self.n]) for kk in xrange(0,self.n2, self.n)]
return "\n".join(xs)
def __eq__(self, other):
return self.xs == other.xs
def copy(self):
return BlockPuzzle(self.n, self.xs)
def get_moves(self):
# Find the 0 tile, and then generate any moves we
# can by sliding another block into its place.
tile0 = self.xs.index(0)
def swap(i):
j = tile0
tmp = list(self.xs)
last_move = tmp[i]
tmp[i], tmp[j] = tmp[j], tmp[i]
result = BlockPuzzle(self.n, tmp)
result.last_move = last_move
return result
if tile0 - self.n >= 0:
yield swap(tile0-self.n)
if tile0 +self.n < self.n2:
yield swap(tile0+self.n)
if tile0 % self.n > 0:
yield swap(tile0-1)
if tile0 % self.n < self.n-1:
yield swap(tile0+1)