Филарета обнови решението на 13.05.2013 23:33 (преди над 11 години)
+class World:
+ def __init__(self, width):
+ self.width = width
+ self.world = [[Cell() for x in range(width)] for y in range(width)]
+
+ def __getitem__(self, index):
+ if index < 0:
+ raise IndexError
+ else:
+ return self.world[index]
+
+ def __len__(self):
+ return self.width
+
+ def __str__(self):
+ world = ''
+ for row in self.world:
+ for cell in row:
+ world = world + cell.__str__()
+ world = world + '\n'
+ return world
+
+
+class Vec2D():
+ def __init__(self, x, y):
+ self.x = x
+ self.y = y
+
+ def __mul__(self, value):
+ return Vec2D(self.x * value, self.y * value)
+
+ 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 __neg__(self):
+ return Vec2D(- self.x, - self.y)
+
+ def __eq__(self, other):
+ return self.x == other.x and self.y == other.y
+
+ def __ne__(self, other):
+ return not self.__eq__(other)
+
+ def __getitem__(self, key):
+ if(key == 0):
+ return self.y
+ elif(key == 1):
+ return self.x
+ else:
+ raise StopIteration
+
+ def __repr__(self):
+ return "v(" + str(self.x) + "," + str(self.y) + ")"
+
+
+class WorldObject:
+ pass
+
+
+class Food(WorldObject):
+ def __init__(self, energy):
+ self.energy = energy
+
+
+class Cell(WorldObject):
+ def __init__(self, contents=None):
+ if isinstance(contents, WorldObject) or contents is None:
+ self.contents = contents
+ else:
+ raise TypeError
+
+ def is_empty(self):
+ return self.contents is None
+
+ def add_content(self, contents):
+ self.contents = contents
+
+ def free_cell(self):
+ self.contents = None
+
+ def __str__(self):
+ if self.is_empty():
+ return '..'
+ else:
+ return self.contents.__str__()
+
+
+class Death(Exception):
+ pass
+
+
+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)
+ energy = 0
+ directions = []
+ direction = Vec2D(0, 0)
+ python = []
+ grown = False
+
+ def __init__(self, world, coords, size, direction):
+ self.size = size
+ self.head = coords
+ self.world = world
+ self._set_on_board(direction)
+
+ def _set_on_board(self, direction):
+ self.world[self.head.y][self.head.x].add_content(PythonHead())
+ self.python.append(self.head)
+ self.directions.append(direction)
+ for i in range(1, self.size + 1):
+ cell = self.head + (-direction) * i
+ self.python.append(cell)
+ self.world[cell.y][cell.x].add_content(PythonPart())
+ self.directions.append(direction)
+
+ def in_range(self, cell):
+ check_x = cell.x >= 0 and cell.x < self.world.width
+ check_y = cell.y >= 0 and cell.y < self.world.width
+ return check_x and check_y
+
+ def _move(self, part, direction):
+ content = self.world[part.y][part.x].contents
+ self.world[part.y][part.x].free_cell()
+ part = part + direction
+ if not self.in_range(part):
+ raise Death
+ new_cell = self.world[part.y][part.x]
+ if new_cell.is_empty():
+ new_cell.add_content(content)
+ elif isinstance(new_cell.contents, Food):
+ self.energy += new_cell.contents.energy
+ self.grown = True
+ else:
+ raise Death
+ return part
+
+ def _change_direction(self, direction):
+ for last_dir in self.directions:
+ if last_dir != direction:
+ last_dir = direction
+ break
+
+ def move(self, direction):
+ if self.direction == -direction:
+ raise ValueError
+ if any(direct != direction for direct in self.directions):
+ self._change_direction(direction)
+ for i in range(self.size+1):
+ self.python[i] = self._move(self.python[i], self.directions[i])
+ if self.grown:
+ new_part = -self.directions[-1] + self.python[-1]
+ self.python.append(new_part)
+ self.directions.append(self.directions[-1])
+ self.size += 1
+ self.grown = False
+ self.direction = direction
Питонът ти е нещо безсмъртен :) Имаш грешка и при растежа.