0

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()
1
  • could your break be the problem? i mean ok you don't want to do anything when the index is at the edge but by using break u go out of your loop without iterating though all the elements. At least thats what it looks like to me at a mere glance. You should make use of additional conditonal for the controlflow and stay away from break in this case if you ask me. Commented Aug 13, 2018 at 18:14

1 Answer 1

1

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()
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.