Александър обнови решението на 15.05.2013 16:48 (преди над 11 години)
+import math
+
+
+class Column:
+ def __init__(self, height):
+ self.height = height
+ self.cells = height * [Cell()]
+
+ def __getitem__(self, y):
+ if y < 0 or y > self.height:
+ raise IndexError()
+ else:
+ return self.cells[y]
+
+ def __setitem__(self, y, cell):
+ if y < 0 or y > self.height:
+ raise IndexError()
+ else:
+ self.cells[y] = cell
+
+
+class World:
+
+ def __init__(self, width):
+ self.width = width
+ self.matrix = width * [Column(width)]
+
+ def __len__(self):
+ return self.width
+
+ def __getitem__(self, x):
+ if x < 0 or x >= self.width:
+ raise IndexError()
+ else:
+ return self.matrix[x]
+
+
+class Cell:
+
+ def __init__(self, _contents=None):
+ if isinstance(_contents, WorldObject) or _contents is None:
+ self.contents = _contents
+ else:
+ raise TypeError('expected world object')
+
+ def is_empty(self):
+ return self.contents is None
+
+
+class WorldObject:
+ pass
+
+
+class Food(WorldObject):
+
+ def __init__(self, energy):
+ self.energy = energy
+
+
+class PythonPart(WorldObject):
+ pass
+
+
+class PythonHead(PythonPart):
+ pass
+
+
+class Vec2D:
+
+ def __init__(self, x, y):
+ self.x, self.y = x, y
+
+ def __add__(self, other):
+ return Vec2D(self.x + other.x, self.y + other.y)
+
+ def __sub__(self, other):
+ return Vec2D(self.x - other.x, self.y - other.y)
+
+ def __mul__(self, other):
+ return Vec2D(other * self.x, other * self.y)
+
+ def __neg__(self):
+ return Vec2D(- self.x, - self.y)
+
+ def __eq__(self, other):
+ return self.x == other.x and self.y == other.y
+
+ def __iter__(self):
+ return IterVec(self)
+
+class IterVec:
+ def __init__(self, vector):
+ self.i = 0
+ self.vector = vector
+
+ def __iter__(self):
+ return self
+
+ def __next__(self):
+ if self.i >= 2:
+ raise StopIteration
+ else:
+ self.i += 1
+ return [self.vector.x, self.vector.y][self.i - 1]
+
+class Python:
+ LEFT = Vec2D(-1, 0)
+ RIGHT = Vec2D(1, 0)
+ UP = Vec2D(0, 1)
+ DOWN = Vec2D(0, -1)
+ energy = 0
+
+ def move(self, direction):
+ self.coords += direction
+ self.direction = direction
+ if self.coords.x < 0 or self.coords.y < 0 or\
+ self.coords.x > len(self.world) or self.coords.y > len(self.world):
+ raise Death
+
+ def __init__(self, world, coords, size, direction):
+ self.world = world
+ self.coords = coords
+ self.size = size
+ self.direction = direction
+
+ self.world[coords.x][coords.y] = Cell(PythonHead())
+ print(self.world[coords.x][coords.y].contents)
+
+
+ for s in range(1, size + 1):
+ self.world[coords.x - direction.x * s]\
+ [coords.y - direction.y * s] = Cell(PythonPart())
+
+
+class Death(Exception):
+ pass