Иван обнови решението на 12.05.2013 14:50 (преди над 11 години)
+class World():
+ def __init__(self, width):
+ self.width = width
+ self.world = [[Cell() for y in range(width)]
+ for x in range(width)]
+
+ def __getitem__(self, line):
+ return self.world[line]
+
+ def __len__(self):
+ return self.width
+
+ def __str__(self):
+ the_world = ''
+ for i in range(self.width):
+ for j in range(self.width):
+ if self.world[i][j].contents == Cell.EMPTINESS:
+ the_world += self.world[i][j].contents
+ else:
+ the_world += self.world[i][j].contents.contents
+ the_world += '\n'
+ return the_world
+
+
+class WorldObject():
+ contents = ':3'
+
+ def __str__(self):
+ return self.contents
+
+
+class Cell():
+ EMPTINESS = '..'
+
+ def __init__(self, contents=EMPTINESS):
+ if isinstance(contents, WorldObject) or contents == self.EMPTINESS:
+ self.contents = contents
+ else:
+ raise TypeError
+
+ def is_empty(self):
+ if self.contents != self.EMPTINESS:
+ return False
+ else:
+ return True
+
+ def __str__(self):
+ if isinstance(self.contents, Cell):
+ return self.contents
+ else:
+ return self.contents.contents
+
+
+class Food(WorldObject):
+ energy = 0
+
+ def __init__(self, energy):
+ self.energy = energy
+
+
+class PythonPart(WorldObject):
+ contents = '##'
+
+
+class PythonHead(PythonPart):
+ contents = '@@'
+
+
+class Vec2D():
+ def __init__(self, x, y):
+ self.x = x
+ self.y = y
+
+ def __add__(self, other):
+ if isinstance(other, Vec2D):
+ return (self.x + other.x, self.y + other.y)
+ elif hasattr(other, "__getitem__"):
+ return (self.x + other[0], self.y + other[1])
+ else:
+ return (self.x + other, self.y + other)
+ __radd__ = __add__
+
+ def __mul__(self, other):
+ if isinstance(other, Vec2D):
+ return Vec2D(self.x * other.x, self.y * other.y)
+ if hasattr(other, "__getitem__"):
+ return Vec2D(self.x * other[0], self.y * other[1])
+ else:
+ return Vec2D(self.x * other, self.y * other)
+ __rmul__ = __mul__
+
+
+class Python():
+ x = 0
+ y = 0
+ LEFT = Vec2D(0, -1)
+ RIGHT = Vec2D(0, 1)
+ UP = Vec2D(-1, 0)
+ DOWN = Vec2D(1, 0)
+ energy = 0
+ tails = []
+ old_direction = DOWN
+
+ def __init__(self, world, coords, size, direction):
+ self.world = world
+ self.coords = coords
+ self.size = size
+ self.direction = direction
+ self.x = self.coords.x
+ self.y = self.coords.y
+ self.head = PythonHead()
+ self.world[self.x][self.y] = Cell(self.head)
+ self.tail = PythonPart()
+ self.tail_direction = direction * -1
+ self.make_tail()
+
+ def make_tail(self):
+ for i in range(1, self.size+1):
+ x, y = self.coords + self.tail_direction * i
+ if 0 <= x < self.world.width and 0 <= y < self.world.width:
+ self.world[x][y] = Cell(self.tail)
+ self.tails.append(self.world[x][y])
+ else:
+ raise Death
+
+ def move(self, direction):
+ old_head = self.world[self.x][self.y]
+ self.x, self.y = self.coords + direction
+ self.coords += direction
+ if isinstance(self.world[self.x][self.y].contents, PythonPart):
+ raise Death
+ elif isinstance(self.world[self.x][self.y].contents, Food):
+ self.energy += self.world[self.x][self.y].contents.energy
+ self.size += 1
+ self.world[self.x][self.y] = Cell(self.head)
+ old_head.contents = self.tail
+ new_tails = [old_head]
+ for i in self.tails:
+ new_tails.append(i)
+ self.tails = new_tails
+ else:
+ if 0 <= self.x < self.world.width \
+ and 0 <= self.y < self.world.width:
+ self.world[self.x][self.y] = Cell(self.head)
+ old_head.contents = self.tail
+ new_tails = [old_head]
+ self.tails[self.size - 1].contents = Cell.EMPTINESS
+ for i in self.tails:
+ new_tails.append(i)
+ self.tails = new_tails
+ else:
+ raise Death
+
+
+class Death(Exception):
+ pass