Александър обнови решението на 15.05.2013 16:26 (преди над 11 години)
+class Vec2D:
+ def __init__(self, x, y):
+ self.x = x
+ self.y = 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, number):
+ return Vec2D(number*self.x, number*self.y)
+
+ def __neg__(self):
+ return Vec2D(-self.x, -self.y)
+
+ def __str__(self):
+ return "[{},{}]".format(self.x, self.y)
+
+ def __eq__(self, other):
+ return self.x == other.x and self.y == other.y
+
+ def __iter__(self):
+ yield self.x
+ yield self.y
+
+
+class World():
+ def __init__(self, width):
+ self.width = width
+ self.matrix = [[Cell() for i in range(width)] for j in range(width)]
+
+ def __getitem__(self, index):
+ if index >= self.width:
+ raise IndexError
+ else:
+ return self.matrix[index]
+
+ def __setitem__(self, index):
+ if index >= self.width:
+ raise IndexError
+ else:
+ return self.matrix[index]
+
+ def __str__(self):
+ result = ""
+ for i in range(self.width):
+ for j in range(self.width):
+ result += str(self.matrix[i][j])
+ #result += " red nomer {}\n".format(i)
+ result += "\n"
+ return result
+
+ def __len__(self):
+ return self.width
+
+
+class WorldObject():
+ def __init__(self):
+ self.contents = self
+
+
+class Cell():
+ def __init__(self, content=WorldObject()):
+ if not isinstance(content, WorldObject):
+ raise TypeError
+ self.contents = content
+
+ def is_empty(self):
+ return type(self.contents) not in [PythonPart, Food]
+
+ 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:
+ RIGHT = Vec2D(0, 1)
+ LEFT = Vec2D(0, -1)
+ UP = Vec2D(-1, 0)
+ DOWN = Vec2D(1, 0)
+
+ def __init__(self, world, coords, size, direction):
+ world.matrix[coords.x][coords.y] = PythonHead()
+ self.tail = []
+ self.tail.append(coords)
+ current = coords
+ for i in range(size):
+ current -= direction
+ if current.x < 0 or current.y < 0:
+ raise IndexError
+ else:
+ world.matrix[current.x][current.y] = PythonPart()
+ self.tail.append(current)
+
+ self.world = world
+ self.head_pos = coords
+ self.size = size
+ self.direction = direction
+ self.energy = 0
+
+ def _add_to_tail(self, new_coords):
+ self.tail.insert(0, new_coords)
+
+ def _remove_from_tail(self):
+ return self.tail.pop(len(self.tail) - 1)
+
+ def move(self, direction):
+ if self.direction == -direction:
+ raise ValueError
+ new_pos = self.head_pos + direction
+ new_x, new_y, size = new_pos.x, new_pos.y, self.world.width
+ exist = any(map(lambda x: x < 0 or x >= size, [new_x, new_y]))
+ if exist or isinstance(self.world.matrix[new_x][new_y], PythonPart):
+ raise Death
+ else:
+ self.world.matrix[new_x][new_y] = PythonHead()
+ self._add_to_tail(new_pos)
+ self.direction = direction
+ self.world.matrix[self.head_pos.x][self.head_pos.y] = PythonPart()
+ if not isinstance(self.world.matrix[new_x][new_y], Food):
+ to_be_removed = self._remove_from_tail()
+ self.world.matrix[to_be_removed.x][to_be_removed.y] = Cell()
+ else:
+ self.energy += self.world.matrix[new_x][new_y].energy
+ self.head_pos = new_pos
+
+
+class Death(Exception):
+ pass