Apr-11-2018, 12:27 PM
Hello !
I have a list of lists which represents a 16*16 board, so there are 16 lists of 16 integers. (representing different square types, starting with only zeros)
I want to be able, for exemple if there is a 2 or a 3 in one of the list, to expend a 2 to the next squares, only if they are 0. So up, down, left and right.
So I need to put a 2 in the same list as the first 2 or 3, one the index before and one the index after, but also one on the previous list at the same index, and one on the next list, still at the same index.
I've tried something like this :
Here's my list :
Anyone can help me fix this ?
I have a list of lists which represents a 16*16 board, so there are 16 lists of 16 integers. (representing different square types, starting with only zeros)
I want to be able, for exemple if there is a 2 or a 3 in one of the list, to expend a 2 to the next squares, only if they are 0. So up, down, left and right.
So I need to put a 2 in the same list as the first 2 or 3, one the index before and one the index after, but also one on the previous list at the same index, and one on the next list, still at the same index.
I've tried something like this :
for list in board: for i in list: if i == 2 or i == 3: if board[board.index(list)][list.index(i)+1] == 0 : board[board.index(list)][list.index(i)+1]=2 if board[board.index(list)][list.index(i)-1] == 0 : board[board.index(list)][list.index(i)-1]=2 if board[board.index(list)-1][list.index(i)] == 0 : board[board.index(list)-1][list.index(i)]=2 if board[board.index(list)+1][list.index(i)] == 0 : board[board.index(list)+1][list.index(i)]=2But it doesn't seem to work correctly and I can't find out why.
Here's my list :
board = [[0 for x in range (16)] for y in range(16)]I add a 3 in it :
board[5][2]=3Then if I try to apply my code, it modifies board to this :
print(board) [[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], [2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [2, 2, 3, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2], [2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2], [2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2], [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, 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]]But what I want is only board[5][1], board[5][3], board[4][2] and board [6][2] to be 2.
Anyone can help me fix this ?
