%load_ext autoreload
%autoreload 2
from eightpuzzle import *
# These are not goal states
s = State([[1, 4, 2], [3, 7, " "], [6, 8, 5]])
print(s.is_goal())
print(s)
s = State([[2, 6, 1], [7, " ", 3], [5, 8, 4]])
print(s.is_goal())
print(s)
# These are goal states
s = State([[1, 2, 3], [4, " ", 5], [6, 7, 8]])
print(s.is_goal())
print(s)
s = State([[" ", 1, 2], [3, 4, 5], [6, 7, 8]])
print(s.is_goal())
print(s)
False 1 4 2 3 7 6 8 5 False 2 6 1 7 3 5 8 4 True 1 2 3 4 5 6 7 8 True 1 2 3 4 5 6 7 8
s = State([[1, 2, 3], [4, " ", 5], [6, 7, 8]])
print(s)
for n in s.get_neighbs():
print(n)
1 2 3 4 5 6 7 8 1 3 4 2 5 6 7 8 1 2 3 4 7 5 6 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8
Here's a fairly easy puzzle that can be solved with tree search
s = State([[1, 4, 2], [3, 7, " "], [6, 8, 5]])
for step in s.solve():
print(step)
Expanded: 67 1 4 2 3 7 6 8 5 1 4 2 3 7 5 6 8 1 4 2 3 7 5 6 8 1 4 2 3 5 6 7 8 1 2 3 4 5 6 7 8
Here's a harder puzzle that needs graph search
s = State([[2, 6, 1], [7, " ", 3], [5, 8, 4]])
for step in s.solve():
print(step)
Expanded: 51274 2 6 1 7 3 5 8 4 2 6 1 7 3 5 8 4 2 6 7 3 1 5 8 4 2 6 7 3 1 5 8 4 2 3 6 7 1 5 8 4 2 3 6 7 1 5 8 4 2 3 6 7 1 4 5 8 2 3 6 7 1 4 5 8 2 3 6 7 1 4 5 8 2 3 6 1 4 7 5 8 2 3 6 1 4 7 5 8 2 3 6 1 4 7 5 8 2 3 1 4 6 7 5 8 2 3 1 4 6 7 5 8 2 3 1 4 6 7 5 8 1 2 3 4 6 7 5 8 1 2 3 4 6 7 5 8 1 2 3 4 5 6 7 8