Георги обнови решението на 15.05.2013 16:51 (преди над 11 години)
+class IndexError(Exception):
+
+ def __init__(self):
+ self.message = "Invalid index!"
+
+
+class Death(Exception):
+
+ def __init__(self):
+ self.message = "You died! Couse NOOB"
+
+
+class ValueError(Exception):
+
+ def __init__(self):
+ self.message = "Invalid direction!"
+
+
+class WorldObject(object):
+
+ def __init__(self, x=0, y=0):
+ self.x = x
+ self.y = y
+
+
+class Food(WorldObject):
+
+ def __init__(self, energy=0, x=0, y=0):
+ self.energy = energy
+ WorldObject.__init__(self, x, y)
+
+ def __str__(self):
+ return ":3"
+
+
+class PythonPart(WorldObject):
+
+ def __init__(self, x=0, y=0):
+ WorldObject.__init__(self, x, y)
+
+ def __str__(self):
+ return "##"
+
+
+class PythonHead(PythonPart):
+
+ def __init__(self, x=0, y=0):
+ PythonPart.__init__(self, x, y)
+
+ def __str__(self):
+ return "@@"
+
+
+class Vec2D(object):
+
+ def __init__(self, x=0, y=0):
+ self.x = x
+ self.y = y
+
+ def __add__(self, other):
+ if (isinstance(other, Vec2D)):
+ return Vec2D(self.x + other.x, self.y + other.y)
+
+ def __sub__(self, other):
+ if (isinstance(other, Vec2D)):
+ return Vec2D(self.x - other.x, self.y - other.y)
+
+ def __mul__(self, other):
+ if (isinstance(other, int)):
+ return Vec2D(self.x * other, self.y * other)
+
+ def __neg__(self):
+ return self * -1
+
+
+class Cell(object):
+
+ def __init__(self, content=".."):
+ if (content == ".."):
+ self.contents = content
+ elif (isinstance(content, WorldObject)):
+ self.contents = content
+ else:
+ raise TypeError
+
+ def __str__(self):
+ return self.contents
+
+ def is_empty(self):
+ return (self.contents == "..")
+
+
+class PositiveArray(object):
+
+ def __init__(self, array):
+ self.array = array
+
+ def __getitem__(self, coordinate):
+ if (coordinate < 0 or coordinate >= len(self.array)):
+ raise IndexError
+ else:
+ return self.array[coordinate]
+
+ def __setitem__(self, key, value):
+ if (key < 0 or key >= len(self.array)):
+ raise IndexError
+ #elif (self.array[key] != ".." or not isinstance(value, Cell)):
+ # raise ValueError
+ else:
+ self.array[key] = value
+
+
+class World(object):
+
+ def __init__(self, width):
+ self.width = width
+ self.world = PositiveArray([self.makeRows() for i in range(width)])
+
+ def makeRows(self):
+ return PositiveArray([Cell() for i in range(self.width)])
+
+ def __len__(self):
+ return self.width
+
+ def __getitem__(self, coords):
+ return self.world[coords]
+
+ def __setitem__(self, key, value):
+ self.world[key] = value
+
+ def __str__(self):
+ for i in range(self.width):
+ for j in range(self.width):
+ print (self.world[j][i].__str__(), end="")
+ print ("\n")
+ return ""
+
+
+class Python(object):
+
+ LEFT = Vec2D(-1, 0)
+ RIGHT = Vec2D(1, 0)
+ UP = Vec2D(0, -1)
+ DOWN = Vec2D(0, 1)
+
+ def __init__(self, world, coords, size, direction):
+ self.world = world
+ self.energy = 0
+ self.initialized = 0
+ self.direction = direction
+ self.head = coords
+ self.body = [PythonHead(coords.x, coords.y)]
+ for i in range(size):
+ bodypart = coords - self.direction * (i + 1)
+ self.body.append(PythonPart(bodypart.x, bodypart.y))
+ self.updateWorld()
+
+ def move(self, direction):
+ if (direction == -self.direction):
+ raise ValueError
+ else:
+ self.direction = direction
+ oldHead = PythonPart(self.head.x, self.head.y)
+ self.head = self.head + direction
+ self.body.pop(0)
+ self.body.insert(0, oldHead)
+ self.body.insert(0, PythonHead(self.head.x, self.head.y))
+ self.updateWorld()
+
+ def updateWorld(self):
+ try:
+ cell = self.world[self.head.x][self.head.y]
+ except IndexError:
+ raise Death
+ if (not cell.is_empty() and not isinstance(cell.contents, Food)):
+ raise Death
+ elif (not self.initialized):
+ for i in range(len(self.body)):
+ self.world[self.body[i].x][self.body[i].y] = Cell(self.body[i])
+ self.initialized = 1
+ else:
+ for i in range(2):
+ self.world[self.body[i].x][self.body[i].y] = Cell(self.body[i])
+ if (isinstance(cell.contents, Food)):
+ self.energy += cell.contents.energy
+ else:
+ lastPart = self.body.pop()
+ self.world[lastPart.x][lastPart.y] = Cell()