Цанислава обнови решението на 15.05.2013 03:50 (преди над 11 години)
+class Cell():
+ def __init__(self, contents=None):
+ if contents != None and contents.__class__ not in [WorldObject, Food, PythonPart, PythonHead]:
+ raise TypeError
+ self.contents = contents
+
+ def is_empty(self):
+ return self.contents == None
+
+ def is_food(self):
+ return self.contents.__class__ == Food
+
+ def is_safe(self):
+ return self.is_empty or self.is_food
+
+ def __str__(self):
+ return ".."
+
+
+class World():
+ def __init__(self, width):
+ self.width = width
+ self.field = [[Cell() for column in range(0, width)] for row in range(0, width)]
+
+ def __len__(self):
+ return self.width
+
+ def __getitem__(self, coord):
+ return self.field[coord]
+
+ def outside_range(self, x, y):
+ for i in x, y:
+ if i < 0 or i > self.__len__() - 1:
+ return True
+ return False
+
+
+class Vec2D():
+ def __init__(self, x=0,y=0):
+ self.vector = (x, y)
+
+ def __add__(self, other):
+ return Vec2D(self[0] + other[0], self[1] + other[1])
+
+ def __sub__(self, other):
+ return Vec2D(self[0] - other[0], self[1] - other[1])
+
+ def __mul__(self, number):
+ return Vec2D(number*self[0], number*self[1])
+
+ def __neg__(self):
+ return Vec2D(-self[0], -self[1])
+
+ def __getitem__(self, key):
+ return self.vector[key]
+
+
+class WorldObject():
+ def __init__(self, coords=Vec2D()):
+ self.coords = coords
+
+ def __str__(self):
+ object = self.super()
+ return object + ' at v' + self.coords
+
+
+class Food(WorldObject):
+ def __init__(self, coords=Vec2D(), energy=0):
+ WorldObject().__init__(coords)
+ self.coords = coords
+ self.energy = energy
+
+ def __str__(self):
+ return ":" + self.energy
+
+
+class PythonPart(WorldObject):
+ def __init__(self, coords):
+ self.coords = coords
+ WorldObject().__init__(coords)
+
+ def move(self, coords):
+ self.coords = coords
+
+ def __str__(self):
+ return "##"
+
+
+class PythonHead(WorldObject):
+ def __init__(self, coords):
+ self.coords = coords
+ WorldObject().__init__(coords)
+
+ def move(self, coords):
+ self.coords = coords
+
+ def __str__(self):
+ return "@@"
+
+
+class Death(Exception):
+ pass
+
+
+class Python:
+ LEFT = Vec2D(1, 0)
+ UP = Vec2D(0, 1)
+ RIGHT = Vec2D(-1, 0)
+ DOWN = Vec2D(0, -1)
+
+ def __init__(self, world, coords=Vec2D(), size=0, direction=Vec2D()):
+ self.world = world
+ self.coords = coords
+ self.size = size
+ self.direction = direction
+ self.head = PythonHead(coords)
+ self.body = []
+ self.body.append(self.head)
+ x, y = coords[0], coords[1]
+ self.world[x][y].contents = self.head
+ for part in range(0, size):
+ body_part = PythonPart(coords - direction*(part+1))
+ self.body.append(body_part)
+ x, y = body_part.coords
+ self.world[x][y].contents = body_part
+
+ def move(self, direction):
+ if direction + self.direction == Vec2D():
+ raise ValueError
+ x, y = self.coords
+ position = Vec2D(self.coords)
+ direct = Vec2D(direction)
+ coords_new = (position + direct).vector
+ x_new, y_new = coords_new
+ if self.world.outside_range(x_new, y_new) or not self.world[x_new][y_new].is_safe:
+ raise Death
+ food = self.world[x_new][y_new].is_food
+ self.head.move(coords_new)
+ self.coords = x_new, y_new
+ self.world[x_new][y_new].contents = self.head
+ coords = self.coords
+ for part in range(0, self.size):
+ coords_old = self.body[part].coords
+ self.body[part].move(coords)
+ self.world[x][y].contents = self.body[part]
+ coords = coords_old
+ if food:
+ body_part = PythonPart(coords)
+ self.body.append(body_part)
+ self.world[x][y].contents = body_part
+ self.size += 1