I have the following code for a maze solving bot:
def main():
axioms = {
"Y":"F",
"X":"R",
"-Y":"B",
"-X":"L"
}
def rotate():
axioms = list(axioms.items())
new = []
new.append((axioms[0][0], axioms[1][1]))
new.append((axioms[1][0], axioms[2][1]))
new.append((axioms[2][0], axioms[3][1]))
new.append((axioms[3][0], axioms[0][1]))
return dict(new)
def move(dir):
if dir == "F":
log("Moving forward.")
API.moveForward()
elif dir == "L":
log("Turning left.")
axioms = rotate()
API.turnLeft()
API.moveForward()
elif dir == "R":
log("Turning right.")
axioms = rotate()
axioms = rotate()
axioms = rotate()
API.turnRight()
API.moveForward()
elif dir == "B":
log("Turning back.")
axioms = rotate()
axioms = rotate()
API.turnLeft()
API.turnLeft()
API.moveForward()
def calculateNext(current):
#calculate possible turns
if current[3] == False:
if sensor.front() == True:
del current[2][(next(key for key, value in axioms.items() if value == "F"))]
if sensor.left() == True:
del current[2][(next(key for key, value in axioms.items() if value == "L"))]
if sensor.right() == True:
del current[2][(next(key for key, value in axioms.items() if value == "R"))]
current[3] = True
#find preferred turn
values = []
for temp in current[2]:
values.append(current[2][temp][1])
#check for inconsistency
def retry():
global preferred
if all(x==values[0] for x in values) == True and values[0] > current[1]:
current[1] += 2
retry()
else:
preferred = min(values)
retry()
preferedTurns = []
#for temp in current[2]:
preferedTurns.append(next(key for key, value in current[2].items() if value[1] == preferred))
return preferedTurns[0]
The problem is the axioms
variable. I have it outside of the calculateNext
function and when its values change, within calculateNext
such as,
def rotate():
axioms = list(axioms.items())
new = []
new.append((axioms[0][0], axioms[1][1]))
new.append((axioms[1][0], axioms[2][1]))
new.append((axioms[2][0], axioms[3][1]))
new.append((axioms[3][0], axioms[0][1]))
return dict(new)
...
...
...
def move(dir):
if dir == "F":
log("Moving forward.")
API.moveForward()
elif dir == "L":
log("Turning left.")
axioms = rotate(axioms)
API.turnLeft()
API.moveForward()
elif dir == "R":
log("Turning right.")
axioms = rotate(axioms)
axioms = rotate(axioms)
axioms = rotate(axioms)
API.turnRight()
API.moveForward()
elif dir == "B":
log("Turning back.")
axioms = rotate(axioms)
axioms = rotate(axioms)
API.turnLeft()
API.turnLeft()
API.moveForward()
they do not save to the variable and the same values are used instead of the new ones.