Мария обнови решението на 15.05.2013 16:24 (преди над 11 години)
+from collections import deque
+
+
+class World:
+ def __init__(self, width):
+ self.size = width
+ self.world = [[Cell() for y in range(0, width)]
+ for x in range(0, width)]
+
+ def __getitem__(self, index):
+ if index in range(0, self.size):
+ return self.world[index]
+ raise IndexError
+
+ def length(self):
+ return self.size
+
+ def clean(self, x, y):
+ self.world[x][y].empty()
+
+ def __str__(self):
+ result = ""
+ for row in range(0, self.size):
+ result += "row" + str(row)
+ for cell in self.world[row]:
+ result += cell.visualize()
+ result += "\n"
+ return result
+
+
+class Cell:
+ def __init__(self, *args):
+ if args:
+ if not isinstance(args[0], WorldObject):
+ raise TypeError
+ else:
+ self.contents = args[0]
+ else:
+ self.contents = None
+
+ def is_empty(self):
+ if self.contents:
+ return False
+ return True
+
+ def fill(self, content):
+ if not isinstance(content, WorldObject):
+ raise TypeError
+ else:
+ self.contents = content
+
+ def empty(self):
+ self.contents = None
+
+ def visualize(self):
+ TYPES = {Food: ":3",
+ PythonPart: "##",
+ PythonTail: "##",
+ PythonHead: "@@"}
+ if type(self.contents) in TYPES:
+ return TYPES[type(self.contents)]
+ return ".."
+
+
+def len(world):
+ return world.length()
+
+
+class Vec2D:
+ def __init__(self, x, y):
+ self.x = x
+ self.y = y
+ self.content = [x, y]
+
+ def __add__(self, other):
+ if isinstance(other, Vec2D):
+ return (self.x + other.x, self.y + other.y)
+ return (self.x + other[0], self.y + other[1])
+
+ __radd__ = __add__
+
+ def __iadd__(self, other):
+ if isinstance(other, Vec2D):
+ self.x += other.x
+ self.y += other.y
+ else:
+ self.x += other[0]
+ self.y += other[1]
+ return self
+
+ def __mul__(self, scalar):
+ return (scalar*self.x, scalar*self.y)
+
+ def __sub__(self, other):
+ if isinstance(other, Vec2D):
+ return (self.x - other.x, self.y - other.y)
+ return (self.x - other[0], self.y - other[1])
+
+ def __rsub__(self, other):
+ if isinstance(other, Vec2D):
+ return (other.x - self.x, other.y - self.y)
+ return (other[0] - self.x, other[1] - self.y)
+
+ def __isub__(self, other):
+ if isinstance(other, Vec2D):
+ self.x -= other.x
+ self.y -= other.y
+ else:
+ self.x -= other[0]
+ self.y -= other[1]
+ return self
+
+ def __eq__(self, other):
+ return self.x == other.x and self.y == other.y
+
+ def __neg__(self):
+ return Vec2D(-self.x, -self.y)
+
+ def __iter__(self):
+ self.index = 0
+ return self
+
+ def __next__(self):
+ if self.index < 2:
+ self.index += 1
+ return self.content[self.index-1]
+ else:
+ raise StopIteration
+
+
+class WorldObject:
+ pass
+
+
+class Food(WorldObject):
+ def __init__(self, energy):
+ self.energy = energy
+
+
+class PythonPart(WorldObject):
+ def __init__(self, coords):
+ self.coords = coords
+
+ def move(self, direction, world):
+ self.coords += direction
+ self.put(world)
+
+ def put(self, world):
+ x = self.coords.x
+ y = self.coords.y
+ world[x][y].fill(self)
+
+
+class PythonTail(PythonPart):
+ def move(self, direction, world):
+ world.clean(self.coords.x, self.coords.y)
+ self.coords += direction
+ self.put(world)
+
+ def put(self, world):
+ x = self.coords.x
+ y = self.coords.y
+ world[x][y].fill(self)
+
+
+class PythonHead(PythonPart):
+ def __init__(self, coords):
+ self.coords = coords
+ self.found_food = False
+
+ def move(self, direction, world, x, y):
+ world.clean(x, y)
+ coords = Vec2D(x, y)
+ PythonPart(coords).put(world)
+ coords += direction
+ return self.put(world, coords)
+
+ def put(self, world, coords):
+ x = coords.x
+ y = coords.y
+ if (x, y) in [(a, b) for a in range(0, len(world))
+ for b in range(0, len(world))]:
+ if world[x][y].is_empty():
+ world[x][y].fill(self)
+ self.found_food = False
+ return 0
+ elif isinstance(world[x][y].contents, Food):
+ energy = world[x][y].energy
+ world.clean(self.coords.x, self.coords.y)
+ world[x][y].fill(self)
+ self.found_food = true
+ return energy
+ else:
+ raise Death
+ else:
+ raise Death
+
+
+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, size, direction):
+ self.size = size
+ self.energy = 0
+ self.direction = direction
+ self.directions = deque([direction])
+ self.coords = coords
+ self.head = PythonHead(self.coords)
+ self.x, self.y = self.coords
+ self.world = world
+ self.head.put(self.world, coords)
+ position = coords
+ for body_part in range(0, self.size):
+ position -= direction
+ PythonPart(position).put(self.world)
+ self.tail = PythonPart(position)
+
+ def move(self, direction):
+ if self.direction == -direction:
+ raise ValueError
+ self.direction = direction
+ self.directions.appendleft([direction])
+ self.energy += self.head.move(direction, self.world, self.x, self.y)
+ if not self.head.found_food:
+ self.tail.move(self.directions.pop(), self.world)
+ else:
+ self.size += 1