Add function for moving turtles with hanoi function
This commit is contained in:
parent
17e173cc44
commit
9266b01e42
1 changed files with 29 additions and 18 deletions
|
|
@ -1,38 +1,38 @@
|
||||||
import turtle
|
import turtle
|
||||||
import random
|
import random
|
||||||
|
|
||||||
def stackTowers(num):
|
def stackTowers(num, turts):
|
||||||
A = []
|
A = []
|
||||||
B = []
|
B = []
|
||||||
C = []
|
C = []
|
||||||
rounds = 0
|
rounds = 0
|
||||||
|
rods = {"A":A, "B":B, "C":C}
|
||||||
|
|
||||||
for i in range(num):
|
for i in range(num):
|
||||||
A.insert(0, i+1)
|
A.insert(0, i+1)
|
||||||
|
|
||||||
|
|
||||||
def hanoi(num, start, aux, end):
|
def hanoi(num, start, aux, end, turts):
|
||||||
nonlocal rounds
|
nonlocal rounds
|
||||||
|
|
||||||
if num == 1:
|
if num == 1:
|
||||||
end += [start.pop()]
|
end += [start.pop()]
|
||||||
rounds += 1
|
rounds += 1
|
||||||
print(rounds, A, B, C)
|
moveTurtles(turts[end[-1] - 1], rods)
|
||||||
else:
|
else:
|
||||||
hanoi(num - 1, start, end, aux)
|
hanoi(num - 1, start, end, aux, turts)
|
||||||
end += [start.pop()]
|
end += [start.pop()]
|
||||||
rounds += 1
|
rounds += 1
|
||||||
print(rounds, A, B, C)
|
moveTurtles(turts[end[-1] - 1], rods)
|
||||||
hanoi(num -1, aux, start, end)
|
hanoi(num -1, aux, start, end, turts)
|
||||||
|
|
||||||
hanoi(num, A, B, C)
|
hanoi(num, A, B, C, turts)
|
||||||
|
|
||||||
|
|
||||||
def createRods(t):
|
def createRods(t):
|
||||||
for i in range(3):
|
for i in range(3):
|
||||||
t.penup()
|
t.penup()
|
||||||
t.goto(300*i-200, 100)
|
t.goto(300*(i-1), 100)
|
||||||
t.pendown()
|
t.pendown()
|
||||||
t.right(90)
|
t.right(90)
|
||||||
t.forward(200)
|
t.forward(200)
|
||||||
|
|
@ -50,32 +50,43 @@ def generateColor():
|
||||||
def createTurtles(num):
|
def createTurtles(num):
|
||||||
turtles = []
|
turtles = []
|
||||||
|
|
||||||
|
totalHeight = num*15
|
||||||
|
|
||||||
for i in range(num):
|
for i in range(num):
|
||||||
newTurt = turtle.Turtle()
|
newTurt = turtle.Turtle()
|
||||||
newTurt.shape("square")
|
newTurt.shape("square")
|
||||||
newTurt.shapesize((i+1)*(1/5), (i+1)*2, 1)
|
newTurt.shapesize(1/2, (i+1)*2, 1)
|
||||||
newTurt.color(generateColor())
|
newTurt.color(generateColor())
|
||||||
newTurt.penup()
|
newTurt.penup()
|
||||||
newTurt.goto(-200, i*-15)
|
newTurt.goto(-300, (num*15 - 100) - i*15)
|
||||||
|
newTurt.name = str(i+1)
|
||||||
|
|
||||||
turtles.append(newTurt)
|
turtles.append(newTurt)
|
||||||
|
|
||||||
return(turtle)
|
return turtles
|
||||||
|
|
||||||
def moveTurtles():
|
def moveTurtles(turt, rods):
|
||||||
pass
|
rodPos = {"A":-300, "B":0, "C":300}
|
||||||
|
x = turt.xcor()
|
||||||
|
y = turt.ycor()
|
||||||
|
|
||||||
|
for rods, turtList in rods.items():
|
||||||
|
if int(turt.name) in turtList:
|
||||||
|
x = rodPos[rods]
|
||||||
|
y = len(turtList) * 15 - 100
|
||||||
|
|
||||||
|
turt.goto(x, y)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
num = 7
|
num = 8
|
||||||
tRod = turtle.Turtle()
|
tRod = turtle.Turtle()
|
||||||
tRod.pensize(5)
|
tRod.pensize(5)
|
||||||
wn = turtle.Screen()
|
wn = turtle.Screen()
|
||||||
|
|
||||||
createRods(tRod)
|
createRods(tRod)
|
||||||
stackTowers(num)
|
|
||||||
|
|
||||||
turts = createTurtles(num)
|
turts = createTurtles(num)
|
||||||
|
stackTowers(num, turts)
|
||||||
|
|
||||||
wn.exitonclick()
|
wn.exitonclick()
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue