如何计算算法完成迷宫所采取的步骤数
How to count number of steps taken for algorithm to finish a maze
最近,我想出了一个 'left hand rule' 迷宫算法。
但是,我需要打印出算法完成迷宫的总步数。
我已经完成了算法。这是精灵移动的 class(已编辑):
class sprite(turtle.Turtle): # Define sprite class
noOfSteps = 0
def __init__(self):
turtle.Turtle.__init__(self)
self.shape("turtle") # Shape of the sprite
self.color("black") # Colour of the sprite
self.setheading(270) # Make sprite face down
self.penup() # Lift the pen up so it does not leave a trail
self.speed('slowest') # Sets the speed that the sprite moves
def moveDown(self): # Define moveDown
if (self.heading() == 270): # Check turtle facing direction
x_walls = round(sprite.xcor(),0) # Turtle x coords
y_walls = round(sprite.ycor(),0) # Turtle y coords
if (x_walls, y_walls) in finish: # Check if turtle coordinates is the coordinates of the end point
print("Finished") # Print 'Finished' to confirm that the turtle has finished reaching the end point
endProgram() # End the program
print(str(self.noOfSteps))
if (x_walls + 24, y_walls) in walls: # Check to see if they are walls on the left
if(x_walls, y_walls - 24) not in walls: # Check to see if path ahead is clear
self.noOfSteps += 1
self.forward(24) # Make turtle move forward by 24 pixels(1 cell)
else:
self.noOfSteps += 1
self.right(90) # Make turtle turn 90 degrees right
else:
self.noOfSteps += 2
self.left(90) # Make turtle turn 90 degrees left
self.forward(24) # Make turtle move forward by 24 pixels(1 cell)
def moveLeft(self): # Define moveLeft
if (self.heading() == 0): # Check turtle facing direction
x_walls = round(sprite.xcor(),0) # Turtle x coords
y_walls = round(sprite.ycor(),0) # Turtle y coords
if (x_walls, y_walls) in finish: # Check if turtle coordinates is the coordinates of the end point
print("Finished") # Print 'Finished' to confirm that the turtle has finished reaching the end point
endProgram() # End the program
print(str(self.noOfSteps))
if (x_walls, y_walls + 24) in walls: # Check to see if they are walls on the left
if(x_walls + 24, y_walls) not in walls: # Check to see if path ahead is clear
self.noOfSteps += 1
self.forward(24) # Make turtle move forward by 24 pixels(1 cell)
else:
self.noOfSteps += 1
self.right(90) # Make turtle turn 90 degrees right
else:
self.noOfSteps += 2
self.left(90) # Make turtle turn 90 degrees left
self.forward(24) # Make turtle move forward by 24 pixels(1 cell)
def moveUp(self): # Define moveUp
if (self.heading() == 90): # Check turtle facing direction
x_walls = round(sprite.xcor(),0) # Turtle x coords
y_walls = round(sprite.ycor(),0) # Turtle y coords
if (x_walls, y_walls) in finish: # Check if turtle coordinates is the coordinates of the end point
print("Finished") # Print 'Finished' to confirm that the turtle has finished reaching the end point
endProgram() # End the program
print(str(self.noOfSteps))
if (x_walls -24, y_walls ) in walls: # Check to see if they are walls on the left
if (x_walls, y_walls + 24) not in walls: # Check to see if path ahead is clear
self.noOfSteps += 1
self.forward(24) # Make turtle move forward by 24 pixels(1 cell)
else:
self.noOfSteps += 1
self.right(90) # Make turtle turn 90 degrees right
else:
self.noOfSteps += 2
self.left(90) # Make turtle turn 90 degrees left
self.forward(24) # Make turtle move forward by 24 pixels(1 cell)
def moveRight(self): # Define moveRight
if (self.heading() == 180): # Check turtle facing direction
x_walls = round(sprite.xcor(),0) # Turtle x coords
y_walls = round(sprite.ycor(),0) # Turtle y coords
if (x_walls, y_walls) in finish: # Check if turtle coordinates is the coordinates of the end point
print("Finished") # Print 'Finished' to confirm that the turtle has finished reaching the end point
endProgram() # End the program
print(str(self.noOfSteps))
if (x_walls, y_walls - 24) in walls: # Check to see if they are walls on the left
if (x_walls - 24, y_walls) not in walls: # Check to see if path ahead is clear
self.noOfSteps += 1
self.forward(24) # Make turtle move forward by 24 pixels(1 cell)
else:
self.noOfSteps += 1
self.right(90) # Make turtle turn 90 degrees right
else:
self.noOfSteps += 2
self.left(90) # Make turtle turn 90 degrees left
self.forward(24) # Make turtle move forward by 24 pixels(1 cell)
这是激活classes的主程序:
# Main program
title = title()
building = Building() # Enable the building class
road = Road() # Enable the road class
start = Start() # Enable the start class
end = End() # Enable the end class
sprite = sprite() # Enable the sprite class
walls = [] # Create walls coordinate list
finish = [] # Create finish list
setupMaze(mazeSet) # Call the setup maze function
# Activation of the movement
while True:
sprite.moveDown()
sprite.moveLeft()
sprite.moveUp()
sprite.moveRight()
time.sleep(0.02)
我的输出是:
Finished
(Supposed to print out no. of steps here)
有谁知道如何解决这个问题?对不起,如果它有点冗长。我尽量缩短它。
您必须通过 self
访问 noOfSteps
。
self.noOfSteps += 1
最近,我想出了一个 'left hand rule' 迷宫算法。
但是,我需要打印出算法完成迷宫的总步数。
我已经完成了算法。这是精灵移动的 class(已编辑):
class sprite(turtle.Turtle): # Define sprite class
noOfSteps = 0
def __init__(self):
turtle.Turtle.__init__(self)
self.shape("turtle") # Shape of the sprite
self.color("black") # Colour of the sprite
self.setheading(270) # Make sprite face down
self.penup() # Lift the pen up so it does not leave a trail
self.speed('slowest') # Sets the speed that the sprite moves
def moveDown(self): # Define moveDown
if (self.heading() == 270): # Check turtle facing direction
x_walls = round(sprite.xcor(),0) # Turtle x coords
y_walls = round(sprite.ycor(),0) # Turtle y coords
if (x_walls, y_walls) in finish: # Check if turtle coordinates is the coordinates of the end point
print("Finished") # Print 'Finished' to confirm that the turtle has finished reaching the end point
endProgram() # End the program
print(str(self.noOfSteps))
if (x_walls + 24, y_walls) in walls: # Check to see if they are walls on the left
if(x_walls, y_walls - 24) not in walls: # Check to see if path ahead is clear
self.noOfSteps += 1
self.forward(24) # Make turtle move forward by 24 pixels(1 cell)
else:
self.noOfSteps += 1
self.right(90) # Make turtle turn 90 degrees right
else:
self.noOfSteps += 2
self.left(90) # Make turtle turn 90 degrees left
self.forward(24) # Make turtle move forward by 24 pixels(1 cell)
def moveLeft(self): # Define moveLeft
if (self.heading() == 0): # Check turtle facing direction
x_walls = round(sprite.xcor(),0) # Turtle x coords
y_walls = round(sprite.ycor(),0) # Turtle y coords
if (x_walls, y_walls) in finish: # Check if turtle coordinates is the coordinates of the end point
print("Finished") # Print 'Finished' to confirm that the turtle has finished reaching the end point
endProgram() # End the program
print(str(self.noOfSteps))
if (x_walls, y_walls + 24) in walls: # Check to see if they are walls on the left
if(x_walls + 24, y_walls) not in walls: # Check to see if path ahead is clear
self.noOfSteps += 1
self.forward(24) # Make turtle move forward by 24 pixels(1 cell)
else:
self.noOfSteps += 1
self.right(90) # Make turtle turn 90 degrees right
else:
self.noOfSteps += 2
self.left(90) # Make turtle turn 90 degrees left
self.forward(24) # Make turtle move forward by 24 pixels(1 cell)
def moveUp(self): # Define moveUp
if (self.heading() == 90): # Check turtle facing direction
x_walls = round(sprite.xcor(),0) # Turtle x coords
y_walls = round(sprite.ycor(),0) # Turtle y coords
if (x_walls, y_walls) in finish: # Check if turtle coordinates is the coordinates of the end point
print("Finished") # Print 'Finished' to confirm that the turtle has finished reaching the end point
endProgram() # End the program
print(str(self.noOfSteps))
if (x_walls -24, y_walls ) in walls: # Check to see if they are walls on the left
if (x_walls, y_walls + 24) not in walls: # Check to see if path ahead is clear
self.noOfSteps += 1
self.forward(24) # Make turtle move forward by 24 pixels(1 cell)
else:
self.noOfSteps += 1
self.right(90) # Make turtle turn 90 degrees right
else:
self.noOfSteps += 2
self.left(90) # Make turtle turn 90 degrees left
self.forward(24) # Make turtle move forward by 24 pixels(1 cell)
def moveRight(self): # Define moveRight
if (self.heading() == 180): # Check turtle facing direction
x_walls = round(sprite.xcor(),0) # Turtle x coords
y_walls = round(sprite.ycor(),0) # Turtle y coords
if (x_walls, y_walls) in finish: # Check if turtle coordinates is the coordinates of the end point
print("Finished") # Print 'Finished' to confirm that the turtle has finished reaching the end point
endProgram() # End the program
print(str(self.noOfSteps))
if (x_walls, y_walls - 24) in walls: # Check to see if they are walls on the left
if (x_walls - 24, y_walls) not in walls: # Check to see if path ahead is clear
self.noOfSteps += 1
self.forward(24) # Make turtle move forward by 24 pixels(1 cell)
else:
self.noOfSteps += 1
self.right(90) # Make turtle turn 90 degrees right
else:
self.noOfSteps += 2
self.left(90) # Make turtle turn 90 degrees left
self.forward(24) # Make turtle move forward by 24 pixels(1 cell)
这是激活classes的主程序:
# Main program
title = title()
building = Building() # Enable the building class
road = Road() # Enable the road class
start = Start() # Enable the start class
end = End() # Enable the end class
sprite = sprite() # Enable the sprite class
walls = [] # Create walls coordinate list
finish = [] # Create finish list
setupMaze(mazeSet) # Call the setup maze function
# Activation of the movement
while True:
sprite.moveDown()
sprite.moveLeft()
sprite.moveUp()
sprite.moveRight()
time.sleep(0.02)
我的输出是:
Finished
(Supposed to print out no. of steps here)
有谁知道如何解决这个问题?对不起,如果它有点冗长。我尽量缩短它。
您必须通过 self
访问 noOfSteps
。
self.noOfSteps += 1