2

I know the question is not very precise, and that is because I have no idea what is going on either. I have narrowed down the problem to a couple of lines, but to me the execution seems rather weird.

def loadmaze(backup):
    test = createmaze(10,10) #This creates a new 10x10 matrix
    maze = placewalls(test, "x") #The X is a string with wall positions
    if maze != test: 
    #when placewalls executed correctly, and returned a maze different from the original
        showmaze(maze) #simply printing maze to console
    else:
        return backup #so nothing changes, looks as if function never happened
    return maze,10,10
    # this returns the new maze, with walls, and the dimensions of the new maze
def placewalls(amaze,positions):
    #placing walls, definitely correct
    return amaze-with-walls

Obviously I changed the variable names so it's clear what I'm doing. The problem is, when I call the function loadmaze(maze), it never returns a maze with walls. Somehow the test and maze values are always identical. I do not understand how this can be as the maze with walls is assigned to maze after test, which at that point does not have walls. According to the debug, test also contains the walls after the third line has been executed. Please help, I'm terribly confused.

10
  • 2
    You'll need to show the real placewalls code. Clearly "somethinggoeswrong" is true so that it returns its first argument. Commented Dec 3, 2016 at 13:14
  • I'm certain somethinggoeswrong is false and it is in fact returning the correct maze including walls. remember that the backup does not have walls. Commented Dec 3, 2016 at 13:16
  • 1
    As already said by @Daniel, too little code to really say what goes wrong. But could it be the case that you are confused how Python passes variables around (and what variables are)? maze and test might be the same object, but now just with walls. See for example in the Python FAQ. Commented Dec 3, 2016 at 13:23
  • Like @stephan said, looks like # placing walls subroutine modifies the object which the first parameter refers to. Please share the full code of the placewalls function. Commented Dec 3, 2016 at 13:24
  • I do know what variables are, but how can test be the same as maze when maze gets another value (with walls) after test has been assigned an empty maze? Commented Dec 3, 2016 at 13:26

1 Answer 1

1

As @stephan said, the variables test and maze refer to the same objects in the memory. So I changed maze = placewalls(test,"x") with maze = placewalls(createmaze(10,10),"x"), thus creating a new object different from maze. This then results in test and maze being two different mazes.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.