In a level editor I have to make, I need to take a 2d list with lots of empty spaces (0's) and clip and add to it untill there are no 0's on the outside, inside is ok. The first thing that I need to do is clip off all the extra 0's on each side ex:
map = [
[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, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0],
[0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0],
[0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0],
[0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0],
[0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0],
[0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
]
def clipMap(map):
"""Need the code for"""
map = clipMap(map)
#map should now be:
[1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 1, 0, 0, 1, 1],
[1, 0, 0, 1, 0, 1, 0, 1],
[1, 1, 1, 1, 1, 0, 0, 1],
[1, 0, 1, 0, 0, 1, 0, 1],
[1, 0, 1, 0, 0, 1, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1]
Note that I am just using 1 and 0, when in the editor there are lots of different blocks that would be 1 in this case.
EDIT: I have tried this, but it was really buggy:
def clipMap(map):
minX = 0
minY = 0
maxX = len(map[0])
maxY = len(map)
#Find min and max
for y in map:
for x in map[y]:
if map[y][x] != 0:
if x < minX:
minX = x
else if x > maxX:
maxX = x
if y < minY:
minY = y
else if y > maxY:
maxY = y
#remove them
X = slice(minX, maxX)
Y = slice(minY, maxY)
map = map[Y]
for y in map:
map[y] = map[y][X]
return map