Reputation: 45
Recently, i came up with a 'left hand rule' algorithm for a maze.
However, i need to print out the total number of steps for the algorithm to finish the maze.
I have already completed the algorithm. This is the class for the moving of the sprite(edited):
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)
This is the main program to activate the 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)
and my output is:
Finished
(Supposed to print out no. of steps here)
Does anyone know how to solve this issue? Sorry if its a little lengthy. I tried shortening it as much as i can.
Upvotes: 0
Views: 421