Весела обнови решението на 15.05.2013 16:43 (преди над 11 години)
+
+
+class World:
+ def __init__(self, width):
+ self.width = width
+ array = [Cell() for x in range(0, width)]
+ self.matrix = [array for x in range(0, width)]
+
+ def __getitem__(self, index):
+ if index > self.width or index < 0:
+ raise IndexError
+ return self.matrix[index]
+
+ def __setitem__(self, index):
+ if index > self.width or index < 0:
+ raise IndexError
+ return self.matrix[index]
+
+ def __len__(self):
+ return self.width
+
+ def printe(self):
+ for i in self.matrix:
+ for j in i:
+ print(j.contents)
+ print("\n")
+
+
+class Cell():
+
+ def __init__(self, contents=None):
+ if contents and not isinstance(contents, WorldObject):
+ raise TypeError
+ self.contents = contents
+
+ def is_empty(self):
+ return not self.contents
+
+
+class WorldObject:
+ pass
+
+
+class Food(WorldObject):
+ def __init__(self, energy):
+ self.energy = energy
+
+
+class PythonPart(WorldObject):
+ def __init__(self, location):
+ self.location = location
+
+
+class PythonHead(PythonPart):
+ def __init__(self, location):
+ self.location = location
+
+
+class Vec2D:
+ def __init__(self, x, y):
+ self.x = x
+ self.y = y
+
+ def __add__(self, right_vector):
+ result_vector = Vec2D(self.x + right_vector.x,
+ self.y + right_vector.y)
+ return result_vector
+
+ def __sub__(self, right_vector):
+ result_vector = Vec2D()
+
+ def __mul__(self, scalar):
+ result_vector = Vec2D(self.x * scalar, self.y * scalar)
+ return result_vector
+
+ def __neg__(self):
+ result_vector = Vec2D(self.x * -1, self.y * -1)
+ return result_vector
+
+
+class Python:
+ LEFT = Vec2D(-1, 0)
+ RIGHT = Vec2D(1, 0)
+ UP = Vec2D(0, 1)
+ DOWN = Vec2D(0, -1)
+ energy = 0
+ size = 0
+
+ def __init__(self, world, coords, size, direction):
+ self.world = world
+ self.coords = coords
+ self.size = size
+ self.direction = direction
+ self.head = PythonHead(coords)
+ self.body = []
+ positions = [(coords.x - direction.x*i, coords.y - direction.y * i)
+ for i in range(1, size+1)]
+ self.world[coords.x][coords.y].contents = self.head
+ for i in positions:
+ v = Vec2D(i[0], i[1])
+ p = PythonPart(v)
+ self.body.append(p)
+ self.world[v.x][v.y].contents = p
+
+ def move(self, direction):
+ if direction == -self.direction:
+ raise ValueError
+ newHead = self.head.location + direction
+ if (newHead.x < 0 or newHead.y < 0 or
+ newHead.x > self.world.width or newHead.y > self.world.width):
+ raise Death
+ a = self.world[newHead.x][newHead.y].contents
+
+ if a and isinstance(a, PythonPart):
+ raise Death
+
+ if isinstance(a, Food):
+ self.energy = self.energy + 1
+ self.size = self.size + 1
+ p = PythonPart(self.head.location)
+ self.body.insert(0, p)
+
+ else:
+ last_el = self.body[-1].location
+ self.world[last_el.x][last_el.x].contents = None
+ self.body.pop()
+ p = PythonPart(self.head.location)
+ self.body.insert(0, p)
+ self.world[p.location.x][p.location.y].contents = p
+ self.head.location = newHead
+ a = self.head
+
+ def printe(self):
+ print(self.head.location.x, self.head.location.y)
+ for i in self.body:
+ print(i.location.x, i.location.y)
+
+
+class Death(Exception):
+ pass