-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
150 lines (131 loc) · 4.87 KB
/
util.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
import numpy as np
class GRID:
def __init__(self, arrangement):
self.grid = {}
for stck in arrangement:
parent = None
for i in range(len(stck)):
if i == len(stck) - 1:
self.grid[stck[i]] = (parent, None)
break
self.grid[stck[i]] = (parent, stck[i+1])
parent = stck[i]
def MoveBlock(self, name, to_loc): # to_loc: string of block it is on or "Table"
if not name in self.grid:
print("block not found")
return False
if self.grid[name][1] is not None:
print("block can't be moved")
return False
if to_loc == "Table" or to_loc == " Table":
pass
elif not to_loc in self.grid:
print("target block not found")
return False
elif self.grid[to_loc][1] is not None:
print("target block occupied")
return False
if self.grid[name][0] is None: # is on the Table
if to_loc == "Table" or to_loc == " Table":
pass
else:
self.grid[name] = (to_loc, None)
self.grid[to_loc] = (self.grid[to_loc][0], name)
else:
self.grid[self.grid[name][0]] = (self.grid[self.grid[name][0]][0], None)
if to_loc == "Table" or to_loc == " Table":
self.grid[name] = (None, None)
else:
self.grid[name] = (to_loc, None)
self.grid[to_loc] = (self.grid[to_loc][0], name)
return True
def IsGoal(self, arrangement):
for stck in arrangement:
for i in range(len(stck)):
if not stck[i] in self.grid:
return False
if i == 0:
if self.grid[stck[i]][0] is not None:
return False
if i == len(stck) - 1:
if self.grid[stck[i]][1] is not None:
return False
else:
if self.grid[stck[i]][1] != stck[i+1]:
return False
return True
def VerifyPath(self, goal, path): # path: list of (name, to_loc) pairs
for p in path:
if not self.MoveBlock(*p):
return False
return self.IsGoal(goal)
def IsBlockOn(self, block, location): # location: other block or "Table"
if not block in self.grid:
return False
if location == "Table" or location == " Table":
return self.grid[block][0] == None
return self.grid[block][0] == location
def IsBlockOnTable(self, block):
if not block in self.grid:
return False
return self.grid[block][0] == None
def IsBlockUnderSomething(self, block):
if not block in self.grid:
return False
return self.grid[block][1] is not None
def BlockStackedHowHigh(self, block): # 0 = on table, 1 = on one block, etc.
count = -1
while block is not None:
if not block in self.grid:
return -1
count += 1
block = self.grid[block][0]
return count
def CountStackedOnTop(self, block): # how many blocks stack on this one
count = -1
while block is not None:
if not block in self.grid:
return -1
count += 1
block = self.grid[block][1]
return count
def BlocksUnder(self, block): # returns list of blocks under this one
lst = []
if not block in self.grid:
return None
block = self.grid[block][0]
while block is not None:
if not block in self.grid:
return None
lst.append(block)
block = self.grid[block][0]
return lst
def BlocksOnTop(self, block): # returns list of block on top of this one
lst = []
if not block in self.grid:
return None
block = self.grid[block][1]
while block is not None:
if not block in self.grid:
return None
lst.append(block)
block = self.grid[block][1]
return lst
def GetArraingement(self):
result = []
for k in list(self.grid.keys()):
if self.grid[k][0] is not None:
continue
p = k
m = []
while p:
m.append(p)
p = self.grid[p][1]
result.append(m)
return result
def GetCopy(self):
g = GRID([])
g.grid = self.grid.copy()
return g
def __str__(self):
return str(self.grid)