I worked a day on a program that will attempt to solve a maze. While I am not a professional and I programmed it in blocks, I am pretty satisfied with the way it turned out. Give me some feedback!
(On website Vexcode VR using Wall Maze playground btw)
Here’s what it does. (Check the console for logging)
- Turns 360 degrees and scans every 90 to see if there is a wall in the way or if it could fall off of the map.
- It then randomly decides which direction to go in. (This may take a while, as the RNG has to pick a valid number)
- It repeats step one, step two, and this time it cannot backtrack on itself. It has to go in a direction other than the one it came in.
- Knowing eventually it would find itself in a dead end, It disregards the anti-backtracking policy in the program, and turns around to go the other direction.
- It repeats this until it eventually reaches the checkered box. (I have only witnessed my program do this once, but its the best I can do without programming my robot to have some sort of memory of where it has been before, which is super hard.)
I will include the text version for usage and testing below.
vexcode_brain_precision = 0
myVariable = 0
numberUp = 0
numberLeft = 0
numberRight = 0
numberDown = 0
turnReset = 0
repeatingCount = 0
up = False
down = False
left = False
right = False
turnDone = False
def when_started1():
global myVariable, numberUp, numberLeft, numberRight, numberDown, turnReset, repeatingCount, up, down, left, right, turnDone, vexcode_brain_precision
drivetrain.set_turn_velocity(100, PERCENT)
drivetrain.set_drive_velocity(100, PERCENT)
numberUp = 0
numberDown = 0
numberLeft = 0
numberRight = 0
while True:
down = False
left = False
right = False
up = False
if front_eye.near_object():
brain.new_line()
brain.print("wall in front of me!")
up = True
drivetrain.turn_for(RIGHT, 90, DEGREES)
if front_eye.near_object():
brain.new_line()
brain.print("wall to the right")
right = True
drivetrain.turn_for(RIGHT, 90, DEGREES)
if front_eye.near_object():
brain.new_line()
brain.print("wall down ")
down = True
else:
if location.position(Y, MM) < -800:
brain.new_line()
brain.print("dropoff or wall downward")
down = True
drivetrain.turn_for(RIGHT, 90, DEGREES)
if front_eye.near_object():
brain.new_line()
brain.print("wall to the left")
left = True
drivetrain.turn_for(RIGHT, 90, DEGREES)
turnDone = False
myVariable = 0
repeatingCount = 0
while not turnDone:
myVariable = round(random.randint((1 - 1), 4))
if up and down and left and not right:
brain.new_line()
brain.print("Dead End!")
numberUp = 0
numberDown = 0
numberLeft = 0
numberRight = 0
if down and right and up and not left:
brain.new_line()
brain.print("Dead End!")
numberUp = 0
numberDown = 0
numberLeft = 0
numberRight = 0
if down and right and left and not up:
brain.new_line()
brain.print("Dead End!")
numberUp = 0
numberDown = 0
numberLeft = 0
numberRight = 0
if left and right and up and not down:
brain.new_line()
brain.print("Dead End!")
numberUp = 0
numberDown = 0
numberLeft = 0
numberRight = 0
if myVariable == 1 and not up and numberDown == 0:
brain.new_line()
brain.print("going Up")
drivetrain.drive_for(FORWARD, 250, MM)
drivetrain.turn_to_heading(0, DEGREES)
turnDone = True
numberUp = 1
numberDown = 0
numberLeft = 0
numberRight = 0
else:
if myVariable == 2 and not left and numberRight == 0:
brain.new_line()
brain.print("going Left")
drivetrain.turn_for(LEFT, 90, DEGREES)
drivetrain.drive_for(FORWARD, 250, MM)
drivetrain.turn_to_heading(0, DEGREES)
turnDone = True
numberUp = 0
numberDown = 0
numberLeft = 1
numberRight = 0
else:
if myVariable == 3 and not right and numberLeft == 0:
brain.new_line()
brain.print("going Right")
drivetrain.turn_for(RIGHT, 90, DEGREES)
drivetrain.drive_for(FORWARD, 250, MM)
drivetrain.turn_to_heading(0, DEGREES)
turnDone = True
numberUp = 0
numberDown = 0
numberLeft = 0
numberRight = 1
else:
if myVariable == 4 and not down and numberUp == 0:
brain.new_line()
brain.print("going Down")
drivetrain.turn_for(RIGHT, 90, DEGREES)
drivetrain.turn_for(RIGHT, 90, DEGREES)
drivetrain.drive_for(FORWARD, 250, MM)
drivetrain.turn_to_heading(0, DEGREES)
turnDone = True
numberUp = 0
numberDown = 1
numberLeft = 0
numberRight = 0
else:
if not turnDone:
brain.new_line()
brain.print("Reroll: ")
brain.print(repeatingCount + 1, precision=vexcode_brain_precision)
repeatingCount = repeatingCount + 1
wait(5, MSEC)
wait(5, MSEC)
vr_thread(when_started1())