- 论坛徽章:
- 4
|
回复 6# 瑞安3980 - class SimpleArray(object):
- """
- SimpleArray is a simple 0 index based array container.
- """
- def __init__(self, width, height):
- self.width = width
- self.height = height
- self.data = []
- # Initialize data with all 0
- for index in range(self.width):
- subData = []
- for subIndex in range(self.height):
- subData.append(0)
- self.data.append(subData)
- def get(self, x, y):
- if 0 <= x < self.width:
- if 0 <= y < self.height:
- return self.data[x][y]
- raise IndexError("Index out of range.")
- def set(self, x, y, value):
- if 0 <= x < self.width:
- if 0 <= y < self.height:
- self.data[x][y] = value
- return
- raise IndexError("Index out of range.")
- def main():
- # Initialize data
- data = SimpleArray(32, 32)
- # Background is 0 already, just need to fill 1
- # Step 1, add 2 lines
- for x in range(9, 25): # set 25, because 25 is excluded
- for y in [9, 24]:
- data.set(x - 1, y - 1, 1)
- # Step 2, add 2 slashes
- xBase = yBase = 10
- for offset in range(7):
- data.set(xBase + offset - 1, yBase + offset - 1, 1)
- data.set(xBase + offset - 1, yBase + 13 - offset - 1, 1)
- # Step 3, check stored data
- for y in range(32):
- for x in range(32):
- print data.get(x, y),
- print
复制代码 |
|