-1

I have an algorithm to make a map of a floor...

def floor_room_IDs():
    global floor_roomIDs
    rand_ID = [4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]
    r_y = 0
    for Layer in floor_roomIDs:
        r_x = 0
        for Tile in Layer:
            floor_roomIDs[r_y][r_x] = floor_map[r_y][r_x]
            r_x += 1
        r_y += 1
    r_y = 0
    for Layer in floor_roomIDs:
        r_x = 0
        for Title in Layer:
            if floor_roomIDs[r_y][r_x] == '1':
                room_ID = rand_ID.pop(random.randint(0, len(rand_ID)-1))
                room_ID_level = globals()['r' + str(room_ID)]
                globals()['rc' + str(room_ID)] = room_ID_level.copy()   
                roomC_ID = 'rc' + str(room_ID)
                floor_roomIDs[r_y][r_x] = str(roomC_ID)
            if floor_roomIDs[r_y][r_x] == 2:
                floor_roomIDs[r_y][r_x] = 'r2'
            if floor_roomIDs[r_y][r_x] == '3':
                floor_roomIDs[r_y][r_x] = 'r3'            
            r_x += 1
        r_y += 1 
def floor_doors():
    global floor_map
    r_y = 0
    for Layer in floor_map:
        r_x = 0
        for Tile in Layer:
            if floor_map[r_y][r_x] == '1' or floor_map[r_y][r_x] == 2:
                door_right = False
                door_bottom = False
                door_left = False 
                door_top = False
                if r_x < 8:
                    if floor_map[r_y][r_x+1] != '/':
                        door_right = True
                if r_y < 8:
                    if floor_map[r_y+1][r_x] != '/':
                        door_bottom = True 
                if r_x > 0:
                    if floor_map[r_y][r_x-1] != '/':
                        door_left = True 
                if r_y > 0:
                    if floor_map[r_y-1][r_x] != '/':
                        door_top = True
                if door_top == True:
                    floor_map[r_y][r_x] = 'a'
                if door_left == True:
                    floor_map[r_y][r_x] = 'b'
                if door_bottom == True:
                    floor_map[r_y][r_x] = 'c'   
                if door_right == True:
                    floor_map[r_y][r_x] = 'd'     
                if door_top == True and door_left == True:
                    floor_map[r_y][r_x] = 'e' 
                if door_top == True and door_bottom == True:
                    floor_map[r_y][r_x] = 'f' 
                if door_top == True and door_right == True:
                    floor_map[r_y][r_x] = 'g'   
                if door_left == True and door_bottom == True:
                    floor_map[r_y][r_x] = 'h'
                if door_left == True and door_right == True:
                    floor_map[r_y][r_x] = 'i'  
                if door_bottom == True and door_right == True:
                    floor_map[r_y][r_x] = 'j'  
                if door_top == True and door_left == True and door_right == True:
                    floor_map[r_y][r_x] = 'k'   
                if door_top == True and door_bottom == True and door_right == True:
                    floor_map[r_y][r_x] = 'l'    
                if door_left == True and door_bottom == True and door_right == True:
                    floor_map[r_y][r_x] = 'm'
                if door_top == True and door_bottom == True and door_left == True:
                    floor_map[r_y][r_x] = 'n'
                if door_top == True and door_bottom == True and door_right == True and door_left == True:
                    floor_map[r_y][r_x] = 'o'                 
            r_x += 1        
        r_y += 1  
        
                            
                                
def floor_validate(): 
    global room_amount
    room_amount = 0
    end_room = 0
    r_y = 0
    for Layer in floor_map:
        r_x = 0
        for Tile in Layer: 
            if floor_map[r_y][r_x] == '1':
                room_amount += 1
            if r_y == 0 or r_y == 1 or r_y == 8 or r_y == 7 or r_x == 0 or r_x == 1 or r_x == 8 or r_x == 7:
                if floor_map[r_y][r_x] == '1':
                    if end_room == 0:
                        if r_y == 8 and r_x != 8 and r_x != 0:
                            if floor_map[r_y-1][r_x] == '1':
                                end_room += 1
                            if floor_map[r_y][r_x+1] == '1':
                                end_room += 1
                            if floor_map[r_y][r_x-1] == '1':
                                end_room += 1
                        if r_y == 0 and r_x != 8 and r_x != 0:
                            if floor_map[r_y+1][r_x] == '1':
                                end_room += 1
                            if floor_map[r_y][r_x+1] == '1':
                                end_room += 1 
                            if floor_map[r_y][r_x-1] == '1':
                                end_room += 1         
                        if r_x == 0 and r_y != 8 and r_y != 0:
                            if floor_map[r_y+1][r_x] == '1':
                                end_room += 1
                            if floor_map[r_y-1][r_x] == '1':
                                end_room += 1 
                            if floor_map[r_y][r_x+1] == '1':
                                end_room += 1    
                        if r_x == 8 and r_y != 8 and r_y != 0:
                            if floor_map[r_y+1][r_x] == '1':
                                end_room += 1
                            if floor_map[r_y-1][r_x] == '1':
                                end_room += 1 
                            if floor_map[r_y][r_x-1] == '1':
                                end_room += 1                      
                        if r_y == 0 and r_x == 0:
                            if floor_map[r_y+1][r_x] == '1':
                                end_room += 1
                            if floor_map[r_y][r_x+1] == '1':
                                end_room += 1
                        if r_y == 0 and r_x == 8:
                            if floor_map[r_y+1][r_x] == '1':
                                end_room += 1
                            if floor_map[r_y][r_x-1] == '1':
                                end_room += 1 
                        if r_y == 8 and r_x == 0:
                            if floor_map[r_y-1][r_x] == '1':
                                end_room += 1
                            if floor_map[r_y][r_x+1] == '1':
                                end_room += 1      
                        if r_y == 8 and r_x == 8:
                            if floor_map[r_y-1][r_x] == '1':
                                end_room += 1
                            if floor_map[r_y][r_x-1] == '1':
                                end_room += 1    
                        if end_room > 1:
                            end_room = 0
                        if end_room == 1:
                            floor_map[r_y][r_x] = 2
                            
                        
            r_x += 1
        r_y += 1
    if room_amount < 18 or room_amount > 22 or end_room != 1:
        make_floor_outline()
    else:
        floor_room_IDs()
        floor_doors()
        
def make_floor_outline():
    global floor_map
    global floor_roomIDs
    read = [['/','/','/','/','/','/','/','/','/'],
            ['/','/','/','/','/','/','/','/','/'],
            ['/','/','/','/','/','/','/','/','/'],
            ['/','/','/','/','/','/','/','/','/'],
            ['/','/','/','/','/','/','/','/','/'],
            ['/','/','/','/','/','/','/','/','/'],
            ['/','/','/','/','/','/','/','/','/'],
            ['/','/','/','/','/','/','/','/','/'],
            ['/','/','/','/','/','/','/','/','/']]
    floor_map = [['/','/','/','/','/','/','/','/','/'],
             ['/','/','/','/','/','/','/','/','/'],
             ['/','/','/','/','/','/','/','/','/'],
             ['/','/','/','/','/','/','/','/','/'],
             ['/','/','/','/','3','/','/','/','/'],
             ['/','/','/','/','/','/','/','/','/'],
             ['/','/','/','/','/','/','/','/','/'],
             ['/','/','/','/','/','/','/','/','/'],
             ['/','/','/','/','/','/','/','/','/']]
    floor_roomIDs = [['/','/','/','/','/','/','/','/','/'],
             ['/','/','/','/','/','/','/','/','/'],
             ['/','/','/','/','/','/','/','/','/'],
             ['/','/','/','/','/','/','/','/','/'],
             ['/','/','/','/','3','/','/','/','/'],
             ['/','/','/','/','/','/','/','/','/'],
             ['/','/','/','/','/','/','/','/','/'],
             ['/','/','/','/','/','/','/','/','/'],
             ['/','/','/','/','/','/','/','/','/']]
    for i in range(0, 17):
        r_y = 0
        for Layer in floor_map:
            r_x = 0
            for Tile in Layer:
                if Tile == '3':
                    floor_map[r_y][r_x+1] = '1'
                    floor_map[r_y][r_x-1] = '1'
                    floor_map[r_y+1][r_x] = '1'
                    floor_map[r_y-1][r_x] = '1'
                if Tile == '1':
                    if read[r_y][r_x] == '/':
                        read[r_y][r_x] = '1'
                        if r_y < 8:
                            if floor_map[r_y+1][r_x] == '/':
                                random_roomcount = random.randint(0,3)
                                if random_roomcount == 1:
                                    floor_map[r_y+1][r_x] = '1'
                        if r_y > 0:
                            if floor_map[r_y-1][r_x] == '/':
                                random_roomcount = random.randint(0,3)
                                if random_roomcount == 1:
                                    floor_map[r_y-1][r_x] = '1'
                        if r_x < 8:
                            if floor_map[r_y][r_x+1] == '/':
                                random_roomcount = random.randint(0,3)
                                if random_roomcount == 1:
                                    floor_map[r_y][r_x+1] = '1' 
                        if r_x > 0:
                            if floor_map[r_y][r_x-1] == '/':    
                                random_roomcount = random.randint(0,3)
                                if random_roomcount == 1:
                                    floor_map[r_y][r_x-1] = '1'  
                    
                r_x += 1    
            r_y += 1  
    floor_validate()    

I call this whenever I want to make a new floor. I have rooms which are arrays, r1 = [..] r2 = [..] etc. What I try to do is make a copy of one of these arrays and use that as my room, so that I don't change the original array. However when I go to the next floor, and the function is called again, and copies of the arrays are made, they are not the same as the original arrays, but instead as the arrays that were copied the first time I called the function. Why is this?

Owen Penn
  • 163
  • 9

1 Answers1

2

Using = in python never makes copies of anything. Never.

To make copies it's best to use the copy module in python. You can make shallowcopies and deepcopies of objects in python.

You should use deepcopy if your list contains (references to) other lists or other objects.

>>> import copy
>>> l = [1, 2, 3]
>>> lc = copy.deepcopy(l)
>>> lc
[1, 2, 3]
>>> lc.append(4)
>>> l
[1, 2, 3]
>>> lc
[1, 2, 3, 4]
Diptangsu Goswami
  • 5,554
  • 3
  • 25
  • 36