Милан обнови решението на 13.05.2013 16:33 (преди над 11 години)
+from collections import deque
+
+
+class World:
+
+ def __init__(self, width):
+ self.width = width
+ self.cells = [CellRow(self.width) for _ in range(self.width)]
+
+ def __getitem__(self, x):
+ World.check_index_validity(x, self.width)
+ return self.cells[x]
+
+ def __len__(self):
+ return self.width
+
+ @staticmethod
+ def check_index_validity(index, max_index):
+ if not 0 <= index < max_index:
+ raise IndexError
+
+
+class Cell:
+
+ def __init__(self, contents=None):
+ if contents is not None and not isinstance(contents, WorldObject):
+ raise TypeError
+ self.contents = contents
+
+ def is_empty(self):
+ return self.contents is None
+
+ def contents(self):
+ return self.contents
+
+
+class CellRow:
+
+ def __init__(self, width):
+ self.width = width
+ self.row = [Cell() for repeat in range(self.width)]
+
+ def __getitem__(self, y):
+ World.check_index_validity(y, self.width)
+ return self.row[y]
+
+ def __setitem__(self, index, item):
+ World.check_index_validity(index, self.width)
+ self.row[index] = item
+
+
+class Vec2D(tuple):
+
+ def __new__(cls, a, b):
+ return tuple.__new__(cls, (a, b))
+
+ def get_x(self):
+ return self[0]
+
+ def get_y(self):
+ return self[1]
+
+ def __add__(self, other):
+ return Vec2D(self.get_x() + other.get_x(),
+ self.get_y() + other.get_y())
+
+ def __sub__(self, other):
+ return Vec2D(self.get_x() - other.get_x(),
+ self.get_y() - other.get_y())
+
+ def __mul__(self, other):
+ return Vec2D(self.get_x() * other, self.get_y() * other)
+
+ def __neg__(self):
+ return Vec2D(-self.get_x(), -self.get_y())
+
+
+class WorldObject:
+ pass
+
+
+class Food(WorldObject):
+
+ def __init__(self, energy=0):
+ self.energy = energy
+
+
+class PythonPart(WorldObject):
+ pass
+
+
+class PythonHead(PythonPart):
+ pass
+
+
+class Python:
+
+ LEFT = Vec2D(0, -1)
+ RIGHT = Vec2D(0, 1)
+ UP = Vec2D(-1, 0)
+ DOWN = Vec2D(1, 0)
+
+ def __init__(self, world, coords, size, direction):
+ self.head = PythonHead()
+ self.head_coords = coords
+
+ self.world = world
+ self.world[coords.get_x()][coords.get_y()].contents = self.head
+
+ self.energy = 0
+ self.direction = direction
+
+ self.body = deque()
+ if size > 0:
+ self._place_python_body(size)
+
+ def move(self, direction):
+ if direction == -self.direction:
+ raise ValueError
+
+ new_coords = self.head_coords + direction
+
+ try:
+ new_position = self.world[new_coords.get_x()][new_coords.get_y()]
+ except IndexError:
+ raise Death
+
+ if isinstance(new_position.contents, PythonPart):
+ raise Death
+
+ self.direction = direction
+ self.world[self.head_coords.get_x()][
+ self.head_coords.get_y()].contents = PythonPart()
+ self.body.appendleft(self.head_coords)
+ self.head_coords = new_coords
+
+ if isinstance(new_position.contents, Food):
+ self.energy += new_position.contents.energy
+ else:
+ last_part = self.body.pop()
+ self.world[last_part.get_x()][last_part.get_y()] = Cell()
+
+ new_position.contents = self.head
+
+ def _place_python_body(self, size):
+ opposite_direction = -self.direction
+ position = self.head_coords
+
+ for repeat in range(size):
+ position = position + opposite_direction
+
+ pos_x = position.get_x()
+ pos_y = position.get_y()
+ self.world[pos_x][pos_y].contents = PythonPart()
+
+ self.body.append(position)
+
+
+class Death(Exception):
+ pass
Питонът ти трябва да има атрибут size
който да отговаря на броят на частите от тялото му.