-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhanoi.py
273 lines (240 loc) · 4.11 KB
/
hanoi.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
fileStr="""\
--{ RES }--
V;200
--{ CFG }--
999;207;32;
14;-1;30;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;12;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;-1;
998;0;
998;3;Cleaner shallow, 8 Pos.;Waste;Cleaner deep, 8 Pos.;
998;;;;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;3;384 Well, landscape;;;
998;cs1;;;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
998;0;
--{ RPG }--
Vector("HOME1","1","1",0,0,2,2,0,0);
"""
diskCount = 7
pegCount = 3
#initialize everything
state = []
for peg in xrange(0,pegCount):
state.append([])
for disk in xrange(0,diskCount):
state[0].append(disk)
def topNum(col):
c = state[col]
if len(c)==0:
return -1
return c[-1]
def printState():
result="\n";
for disk in xrange(diskCount-1,-1,-1):
for peg in xrange(0,pegCount):
if disk<len(state[peg]):
result = result + str(state[peg][disk])
else:
result = result + "."
result = result +"\n"
print result
def doMove(a,b):
pFrom = [a,len(state[a])-1]
pTo = [b,len(state[b])]
print ("move from ("+str(pFrom[0])+","+str(pFrom[1])+") to ("+str(pTo[0])+","+str(pTo[1])+").")
state[b].append(state[a].pop())
printState()
pickPlateAt(pFrom[0],pFrom[1])
dropPlateAt(pTo[0],pTo[1])
def doAction(colA,colB):
nA=topNum(colA)
nB=topNum(colB)
if (nA==-1) and (nB==-1):
return
if (nA>nB):
doMove(colA,colB)
else:
doMove(colB,colA)
def solved():
for colN in xrange(1,pegCount):
if len(state[colN])==diskCount:
return True
return False
homePos=[1016.5,0,0]
curPos=homePos[:]
plateBasePositions = [
[638.9,91.2,136.3],
[638.9,190.3,136.3],
[638.9,289.5,136.3]
]
heightOffset = 12.5
#go to home
#Vector("HOME1","1","1",0,0,2,2,0,0);
#move to X,Y,Z
#ROMA(3,80,75,X,Y,Z,150,1,0);
#open gripper to X
#ROMA(0,X,75,4,5,6,150,1,0);
#close gripper to X
#ROMA(1,X,12,4,5,6,150,1,0);
def pickPlateAt(x,y):
pos = plateCoord(x,y)
print "pos = " + str(pos)
cmdMoveAbove(pos)
cmdMoveTo(pos)
pickPlate()
cmdMoveAbove(pos)
def dropPlateAt(x,y):
pos = plateCoord(x,y)
cmdMoveAbove(pos)
cmdMoveTo(pos)
dropPlate()
cmdMoveAbove(pos)
def pickPlate():
global fileStr
pickVal = 78
fileStr=fileStr+"ROMA(1,"+str(pickVal)+",125,4,5,6,150,1,0);\n"
def dropPlate():
global fileStr
pickVal = 86
fileStr=fileStr+"ROMA(0,"+str(pickVal)+",75,4,5,6,150,1,0);\n"
def plateCoord(x,y):
basePos = plateBasePositions[x][:]
basePos[2]=basePos[2]-heightOffset*y;
return basePos
def cmdMoveTo(newPos):
global curPos, fileStr
diff =[ newPos[0]-curPos[0], newPos[1]-curPos[1],newPos[2]-curPos[2]]
print str(curPos)+" + " +str(diff) +" = "+str(newPos)
curPos=newPos[:]
fileStr = fileStr+"ROMA(3,80,75,"+str(-diff[1])+","+str(-diff[0])+","+str(diff[2])+",150,1,0);\n"
def cmdMoveAbove(newPos):
newPos = newPos[:]
newPos[2]=plateBasePositions[0][2]-heightOffset*(3+diskCount)
cmdMoveTo(newPos)
#move to z=0
def cmdMoveUp():
fileStr = fileStr+"ROMA(3,80,75,0,0,"++str(-curPos[2])+",150,1,0);\n"
curPos[2]=0
#open the hand at the start of each file
dropPlate()
printState()
#algorithm
if diskCount%2==0:
while True:
doAction(0,1)
if solved():
break;
doAction(0,2)
if solved():
break;
doAction(1,2)
if solved():
break;
else:
while True:
doAction(0,2)
if solved():
break;
doAction(0,1)
if solved():
break;
doAction(2,1)
if solved():
break;
text_file = open("hanoi.gem", "w")
fileStr=fileStr.replace('\n','\r\n')
text_file.write(fileStr)
text_file.close()