I'm having trouble with python arrays, my two-dimensional (array of arrays) has a per-row offset

Question:

I cannot for the life of me spot why each row has additional leading 0’s.

The array aiWalls[] defines x/y coordinates that should be a ‘1’ in the resulting array of arrays (2 dimensional array?) wallTable[]. Any coordinates in the same wallTable[] that do not have a matching pair in aiWalls[] should be a 0.

Python:

aiWalls = [(25, 25), (25, 75), (25, 125)]
wallTable = []
tileX = 0
tileY = 0
row = []
tableSizeX = 200
tableSizeY = 200
while tileY < tableSizeY:
    tileX = 0
    #row = [] #new row
    while tileX < tableSizeX:
        pos = (tileX,tileY)
        for node in aiWalls:
            if node[0] == pos[0] and node[1] == pos[1]:
                print("wall found @ " + str(pos))
                row.append(1) #add 1 to the row
                #print(row)
            elif node[0] != pos[0] or node[1] != pos[1]: 
                row.append(0)
        tileX = tileX + 25
    tileY = tileY + 25

    wallTable.append(row)
    row = []
for scan in wallTable:
    print(scan)

Output:

wall found @ (25, 25)
wall found @ (25, 75)
wall found @ (25, 125)
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 

I’m going to keep futzing with this periodically and if I solve it, I’ll answer with the corrections… If you can figure it out, I would really appreciate an explanation of where I’ve goofed.

Asked By: Aaron Casper

||

Answers:

Each time through your inner loop, you’re adding 3 values to the current row rather than just one, because you’re looping over the three elements in aiWalls each time.

I assume that what you want to do is check the three positions in aiWalls against the current position inside your loops. If one or more of them match, then you want to add a 1, else you want to add a 0.

Here’s a modified version of your code that does that:

aiWalls = [(25, 25), (25, 75), (25, 125)]
wallTable = []
tileX = 0
tileY = 0
row = []
tableSizeX = 200
tableSizeY = 200
while tileY < tableSizeY:
    tileX = 0
    while tileX < tableSizeX:
        pos = (tileX,tileY)
        for node in aiWalls:
            if node[0] == pos[0] and node[1] == pos[1]:
                print("wall found @ " + str(pos))
                row.append(1)
                break
        else:
            row.append(0)
        tileX = tileX + 25
    tileY = tileY + 25
    wallTable.append(row)
    row = []
for scan in wallTable:
    print(scan)

The use of for/else is a bit advanced. The else clause will be executed if the inner loop ran to completion never executing break.

Results:

wall found @ (25, 25)
wall found @ (25, 75)
wall found @ (25, 125)
[0, 0, 0, 0, 0, 0, 0, 0]
[0, 1, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0]
[0, 1, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0]
[0, 1, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0]
Answered By: CryptoFool
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.