Question

i have written some code that i want to convert into something i can import instead of it being used as the main.

floorMap = [[000,000,000,000,000,999,999,999,999,999],
           [000,000,999,000,999,000,000,000,999,999],
           [000,000,999,000,999,000,000,000,999,999],
           [000,000,000,000,999,000,000,000,999,999],
           [999,000,000,000,999,000,999,999,999,999],
           [999,000,000,000,000,000,999,000,000,999],
           [999,000,000,000,999,999,999,000,000,999],
           [999,000,999,000,000,000,999,000,000,999],
           [999,000,999,999,999,000,000,000,000,999],
           [999,999,999,999,999,999,999,999,000,000]]


currentNum=0

wall=999
uncalculated=000

robotX=0
robotY=0
goalX=9
goalY=9

floorMap[goalY][goalX]=1

def changeSurroundings(X, Y):

        #left
        if(floorMap[X-1][Y]==uncalculated and X > 0):
            floorMap[X-1][Y]=currentNum   
        #right 
        if(X < len(floorMap[0])-1 and floorMap[X+1][Y]==uncalculated):
            floorMap[X+1][Y]=currentNum  
        #up
        if(floorMap[X][Y-1]==uncalculated and Y > 0):
            floorMap[X][Y-1]=currentNum  
        #down
        if(Y < len(floorMap)-1 and floorMap[X][Y+1]==uncalculated):
            floorMap[X][Y+1]=currentNum    


def printMap():
    i=0
    floorMap[goalY][goalX]='G'
    floorMap[robotY][robotX]='R'

    while(i<len(floorMap)):
        print floorMap[i]
        print ""
        i+=1
    print ""
    print ""

#------------------MOST IMPORTANT CHUNK OF CODE--------------

while(floorMap[robotY][robotX]==uncalculated):
    x=0
    while(x<len(floorMap[0])):
        y=0
        while(y<len(floorMap)):
            if(floorMap[x][y] > uncalculated and floorMap[x][y] < wall):
                currentNum=floorMap[x][y]+1
                changeSurroundings(x,y)                                
            y+=1
        x+=1

printMap()

my problem is that whenever i try to put the most important chunk of code at the bottom into a method like so;

def calcMap():  
while(floorMap[robotY][robotX]==uncalculated):
    x=0
    while(x<len(floorMap[0])):
        y=0
        while(y<len(floorMap)):
            if(floorMap[x][y] > uncalculated and floorMap[x][y] < wall):
                currentNum=floorMap[x][y]+1
                changeSurroundings(x,y)                                
            y+=1
        x+=1

printMap()

it breaks my code. why? I dont seem to get any errors, it just gets stuck on one of the nested loops. i dont see any reason it should be doing this, but you guys probably will ;)

Thanks, Logan

Was it helpful?

Solution

Your problem comes from your global variables, in particular currentNum. This is basically what you're doing :

current = 0

def f():
    current = 1
    g()

def g():
    print(current)

f()  # Output: 0

What you need to do is:

current = 0

def f():
    global current
    current = 1
    g()

def g():
    print(current)

f()  # Output: 1

Or better :

def f():
    current = 1
    g(current)

def g(current):
    print(current)

f()  # Output: 1

Also, you should consider using a more pythonic synthax for your calcMap function, something like :

def calc_map(): 
    while floor_map[robot_x][robot_y] == uncalculated :
        for x,array in enumerate(floor_map):
            for y,element in enumerate(array):
                if uncalculated < element < wall:
                    current_num = element + 1
                    change_surroundings(x, y, current_num) 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top