Десислава обнови решението на 14.05.2013 21:07 (преди почти 12 години)
+
+class Vec2D():
+ def __init__(self, x, y):
+ self.x = x
+ self.y = y
+
+ def __add__(self, vect):
+ x = vect.x + self.x
+ y = vect.y + self.y
+ return Vec2D(x, y)
+
+ def __sub__(self, vect):
+ x = self.x - vect.x
+ y = self.y - vect.y
+ return Vec2D(x, y)
+
+ def __mul__(self, scalar):
+ x = scalar * self.x
+ y = scalar * self.y
+ return Vec2D(x, y)
+
+ def __neg__(self):
+ x = -1 * self.x
+ y = -1 * self.y
+ return Vec2D(x, y)
+
+ def opposite(self, vect):
+ x = self.x * vect.x
+ y = self.y * vect.y
+ return x == 0 and y == 0
+
+ def __iter__(self):
+ yield self.x
+ yield self.y
+
+
+class WorldObject():
+ def __str__(self):
+ return '..'
+
+
+class Food(WorldObject):
+ def __init__(self, energy=0):
+ self.energy = energy
+
+ def __str__(self):
+ return ':3'
+
+
+class PythonPart(WorldObject):
+ def __str__(self):
+ return '##'
+
+
+class PythonHead(PythonPart):
+ def __str__(self):
+ return '@@'
+
+
+class Python():
+ LEFT = Vec2D(-1, 0)
+ RIGHT = Vec2D(1, 0)
+ UP = Vec2D(0, -1)
+ DOWN = Vec2D(0, 1)
+
+ def __init__(self, world, coords, size, direction):
+ self.world = world
+ self.size = size
+ self.direction = direction
+ self.energy = 0
+ self.setPythonCoords(coords, size)
+
+ def setPythonCoords(self, coords, size):
+ self.world[coords.x][coords.y] = Cell(PythonHead())
+ self.coords = [coords]
+ for part in range(1, size + 1):
+ x, y = -self.direction*part + coords
+ self.coords.append(Vec2D(x, y))
+ self.world[x][y] = Cell(PythonPart())
+
+ def move(self, direction):
+ if direction.opposite(self.direction):
+ raise ValueError()
+
+ self.direction = direction
+ head_pos = self.coords[0] + direction
+ head_x, head_y = head_pos
+
+ if -1 in [head_x, head_y]:
+ raise Death("Hit wall!")
+ if isinstance(self.world[head_x][head_y], PythonPart):
+ raise Death("Hit python part!")
+
+ if self.world[head_x][head_y].is_empty():
+ self.world[head_x][head_y] = Cell(PythonHead())
+ self.coords.insert(0, head_pos)
+ lastCoord_x = self.coords[self.size].x
+ lastCoord_y = self.coords[self.size].y
+ self.world[lastCoord_x][lastCoord_y] = Cell()
+ del self.coords[self.size]
+ elif isinstance(self.world[head_x][head_y].contents, Food):
+ self.energy += self.world[head_x][head_y].contents.energy
+ self.world[head_x][head_y] = Cell(PythonHead())
+ self.coords.insert(0, head_pos)
+
+
+class Death(Exception):
+ pass
+
+
+class Cell():
+ def __init__(self, contents=None):
+ if isinstance(contents, WorldObject):
+ self.contents = contents
+ elif contents:
+ raise TypeError()
+
+ def is_empty(self):
+ return 'contents' not in self.__dict__
+
+ def __str__(self):
+ if not self.is_empty():
+ self.contents.__str__()
+ else:
+ return '..'
+
+
+class World():
+ def __init__(self, width):
+ self.width = width
+ self.cells = [[Cell()]*width for x in range(width)]
+
+ def __getitem__(self, index):
+ if 0 > index < self.width:
+ raise IndexError
+ return self.cells[index]
+
+ def __setitem__(self, index, row):
+ self.rows[index] = row
+
+ def __len__(self):
+ return self.width