Python - Traversing and modifying 2d array not working

I have a 2d Array named Matrix. The array is populated with a bunch of o's and c's. I am trying to traverse the array element by element. If an element meets a certain set of rules, I would like to change the element to an N.

Below is my code for doing so. When I run my code, some elements are replaced with an N, but not all of the elements that should be.

I would really appreciate your help as to why this is happening, thanks!

2D Array:

https://nofile.io/f/74GXSntofsG/obstaclemapinput.txt

Outputted 2D Array:

https://nofile.io/f/ZhzK38x4Sqp/obstaclemap.txt

Code:

matrix_icrement_width = int(width/int(boxsize))
matrix_icrement_height = int(height/int(boxsize))
Matrix =  [[0 for x in range(matrix_icrement_width)] for y in range(matrix_icrement_height)] 

#The 2d array is populated however that code is long and irrelevant so I did not include it in my question 

def traverse_matrix():
for i in range (0,matrix_icrement_width):
    for j in range (0,matrix_icrement_height):
        if Matrix[i][j]== "o":
            #if u r on a wall, dont do anything
            break

        if Matrix[i][j-1] == "o":
            #if u were on a wall, but not anymore
            Matrix[i][j] = "N"

        if Matrix[i+1][j] == "c":
            #if the space below u is a path
            Matrix[i][j] = "N"

        if Matrix[i][j+1] == "o":
            #if the space infront of u is a wall
            Matrix[i][j] = "N"



def printMatrix():
    f = open('obstaclemap.txt', 'w')
    f.write('\n'.join([''.join(['{:4}'.format(item) for item in row]) 
      for row in Matrix]))
    f.close()

traverse_matrix()
printMatrix()

Upvotes: 0

Views: 99

Answers (1)

Krishna Choudhary
Krishna Choudhary

Reputation: 625

I think the issue is due to careless use of break instead of continue. Try substituting it and let me know the results.

matrix_icrement_width = int(width/int(boxsize))
matrix_icrement_height = int(height/int(boxsize))
Matrix =  [[0 for x in range(matrix_icrement_width)] for y in range(matrix_icrement_height)] 

#The 2d array is populated however that code is long and irrelevant so I did not include it in my question 

def traverse_matrix():
for i in range (0,matrix_icrement_width):
    for j in range (0,matrix_icrement_height):
        if Matrix[i][j]== "o":
            #if u r on a wall, dont do anything
            continue         #Modify this

        if Matrix[i][j-1] == "o":
            #if u were on a wall, but not anymore
            Matrix[i][j] = "N"

        if Matrix[i+1][j] == "c":
            #if the space below u is a path
            Matrix[i][j] = "N"

        if Matrix[i][j+1] == "o":
            #if the space infront of u is a wall
            Matrix[i][j] = "N"



def printMatrix():
    f = open('obstaclemap.txt', 'w')
    f.write('\n'.join([''.join(['{:4}'.format(item) for item in row]) 
      for row in Matrix]))
    f.close()

traverse_matrix()
printMatrix()

Upvotes: 1

Related Questions