Калоян обнови решението на 14.05.2013 20:00 (преди над 11 години)
+class Death(Exception):
+ pass
+
+
+class Vec2D:
+ def __init__(self, x, y):
+ self.x, self.y = x, y
+
+ 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 __mul__(self, scalar):
+ return Vec2D(self.x * scalar, self.y * scalar)
+
+ def __eq__(self, other):
+ return self.x == other.x and self.y == other.y
+
+ def __neg__(self):
+ return self * -1
+
+ def __str__(self):
+ return "({},{})".format(self.x, self.y)
+
+ def __iter__(self):
+ yield self.x
+ yield self.y
+
+
+class WorldObject:
+ pass
+
+
+class Food(WorldObject):
+ debug_symbol = '$'
+
+ def __init__(self, energy=0):
+ self.energy = energy
+
+
+class PythonPart(WorldObject):
+ debug_symbol = '#'
+
+
+class PythonHead(PythonPart):
+ debug_symbol = '@'
+
+
+class Cell:
+ def __init__(self, contents=None):
+ if contents is None or isinstance(contents, WorldObject):
+ self.contents = contents
+ else:
+ raise TypeError
+
+ def is_empty(self):
+ return self.contents is None
+
+
+class PositiveIndexArray:
+ def __init__(self, array):
+ self.array = array
+
+ def __getitem__(self, index):
+ if index < 0:
+ raise IndexError
+ else:
+ return self.array[index]
+
+ def __setitem__(self, index, value):
+ if index < 0:
+ raise IndexError
+ else:
+ self.array[index] = value
+
+
+class World:
+ def __init__(self, width):
+ self.width = width
+ rows = lambda: PositiveIndexArray([Cell() for j in range(width)])
+ self.grid = PositiveIndexArray([rows() for i in range(width)])
+ #self.grid = [[Cell() for j in range(width)] for i in range(width)]
+
+ def __len__(self):
+ return self.width
+
+ def __getitem__(self, x):
+ return self.grid[x]
+
+ def __setitem__(self, x, value):
+ self.grid[x] = value
+
+ def __str__(self):
+ whole_string = ''
+
+ for y in reversed(range(self.width)):
+ for x in range(self.width):
+ if self.grid[x][y].is_empty():
+ whole_string += ' .'
+ else:
+ whole_string += ' ' + self.grid[x][y].contents.debug_symbol
+ whole_string += '\n'
+
+ return whole_string
+
+ def is_valid_move_position(self, move_position):
+ x, y = move_position
+
+ if x < 0 or x > self.width or y < 0 or y > self.width:
+ return False
+
+ if isinstance(self.grid[x][y].contents, PythonPart):
+ return False
+
+ return True
+
+ def get_at(self, coords):
+ return self.grid[coords.x][coords.y]
+
+
+class Python:
+ UP = Vec2D(0, 1)
+ DOWN = Vec2D(0, -1)
+ LEFT = Vec2D(-1, 0)
+ RIGHT = Vec2D(1, 0)
+
+ def __init__(self, world, coords, size, direction):
+ self.world = world
+ self.coords = coords
+ self.size = size
+ self.direction = direction
+ self.energy = 0
+ self.body_parts = []
+
+ part_position = self.coords
+ self.world.get_at(part_position).contents = PythonHead()
+ self.head = self.world.get_at(part_position).contents # useless
+
+ for i in range(size):
+ part_position -= direction
+ self.world.get_at(part_position).contents = PythonPart()
+ self.body_parts.append(part_position)
+
+ def move(self, direction):
+ if self.direction + direction == Vec2D(0, 0):
+ raise ValueError
+
+ self.direction = direction
+ new_coords = self.coords + direction
+
+ if not self.world.is_valid_move_position(new_coords):
+ raise Death
+
+ if isinstance(self.world.get_at(new_coords).contents, Food):
+ self.body_parts.insert(0, self.coords)
+ self.energy += self.world.get_at(new_coords).contents.energy
+ self.size += 1
+ else:
+ last_part_coords = self.body_parts[-1]
+ for i in reversed(range(len(self.body_parts))):
+ self.body_parts[i] = self.body_parts[i-1]
+ self.world.get_at(last_part_coords).contents = None
+ self.body_parts[0] = self.coords
+
+ self.world.get_at(new_coords).contents = PythonHead()
+ self.world.get_at(self.coords).contents = PythonPart()
+ self.coords = new_coords
+
+ self.head = self.world.get_at(new_coords).contents # useless
По принцип в света можеш да имаш няколко питона и не е хубаво да могат да минават един през друг :)
Кой е казал, че те сега могат да минават един през друг?