forked from abpaudel/8-puzzle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.py
88 lines (72 loc) · 2.45 KB
/
board.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
import numpy as np
class Board:
parent = None
state = None
operator = None
depth = 0
zero = None
cost = 0
def __init__(self, state, parent=None, operator=None, depth=0):
self.parent = parent
self.state = np.array(state)
self.operator = operator
self.depth = depth
self.zero = self.find_0()
self.cost = self.depth + self.manhattan()
def __lt__(self, other):
if self.cost != other.cost:
return self.cost < other.cost
else:
op_pr = {'Up': 0, 'Down': 1, 'Left': 2, 'Right': 3}
return op_pr[self.operator] < op_pr[other.operator]
def __str__(self):
return str(self.state[:3]) + '\n' \
+ str(self.state[3:6]) + '\n' \
+ str(self.state[6:]) + ' ' + str(self.depth) + str(self.operator) + '\n'
def goal_test(self):
if np.array_equal(self.state, np.arange(9)):
return True
else:
return False
def find_0(self):
for i in range(9):
if self.state[i] == 0:
return i
def manhattan(self):
state = self.index(self.state)
goal = self.index(np.arange(9))
return sum((abs(state // 3 - goal // 3) + abs(state % 3 - goal % 3))[1:])
@staticmethod
def index(state):
index = np.array(range(9))
for x, y in enumerate(state):
index[y] = x
return index
def swap(self, i, j):
new_state = np.array(self.state)
new_state[i], new_state[j] = new_state[j], new_state[i]
return new_state
def up(self):
if self.zero > 2:
return Board(self.swap(self.zero, self.zero - 3), self, 'Up', self.depth + 1)
else:
return None
def down(self):
if self.zero < 6:
return Board(self.swap(self.zero, self.zero + 3), self, 'Down', self.depth + 1)
else:
return None
def left(self):
if self.zero % 3 != 0:
return Board(self.swap(self.zero, self.zero - 1), self, 'Left', self.depth + 1)
else:
return None
def right(self):
if (self.zero + 1) % 3 != 0:
return Board(self.swap(self.zero, self.zero + 1), self, 'Right', self.depth + 1)
else:
return None
def neighbors(self):
neighbors = [self.up(), self.down(), self.left(), self.right()]
return list(filter(None, neighbors))
__repr__ = __str__