Решение на Питоните хапят! от Иван Капукаранов

Обратно към всички решения

Към профила на Иван Капукаранов

Резултати

  • 6 точки от тестове
  • 0 бонус точки
  • 6 точки общо
  • 13 успешни тест(а)
  • 0 неуспешни тест(а)

Код

from collections import deque
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):
if line < 0:
raise IndexError
else:
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 is None:
the_world += Cell.EMPTINESS
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=None):
if isinstance(contents, WorldObject) or contents is None:
self.contents = contents
else:
raise TypeError
def is_empty(self):
if self.contents is not None:
return False
else:
return True
def __str__(self):
if self.contents is None:
return self.EMPTINESS
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
self.content = [x, y]
def __iter__(self):
self.count = 0
return self
def __next__(self):
if self.count < 2:
self.count += 1
return self.content[self.count-1]
else:
raise StopIteration
def __add__(self, other):
if isinstance(other, Vec2D):
return Vec2D(self.x + other.x, self.y + other.y)
elif hasattr(other, "__getitem__"):
return Vec2D(self.x + other[0], self.y + other[1])
else:
return Vec2D(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__
def __neg__(self):
return self * -1
def __sub__(self, other):
if isinstance(other, Vec2D):
return Vec2D(self.x - other.x, self.y - other.y)
elif (hasattr(other, "__getitem__")):
return Vec2D(self.x - other[0], self.y - other[1])
else:
return Vec2D(self.x - other, self.y - other)
def __rsub__(self, other):
if isinstance(other, Vec2D):
return Vec2D(other.x - self.x, other.y - self.y)
if (hasattr(other, "__getitem__")):
return Vec2D(other[0] - self.x, other[1] - self.y)
else:
return Vec2D(other - self.x, other - self.y)
def __str__(self):
return '({}, {})'.format(self.x, self.y)
def __eq__(self, other):
if isinstance(other, Vec2D):
return self.x == other.x and self.y == other.y
elif hasattr(other, "__getitem__") and len(other) == 2:
return self.x == other[0] and self.y == other[1]
else:
return False
def __ne__(self, other):
if isinstance(other, Vec2D):
return self.x != other.x or self.y != other.x
elif hasattr(other, "__getitem__") and len(other) == 2:
return self.x == other[0] and self.y == other[1]
else:
return True
class Python():
x = 0
y = 0
LEFT = Vec2D(0, -1)
RIGHT = Vec2D(0, 1)
UP = Vec2D(-1, 0)
DOWN = Vec2D(1, 0)
energy = 0
old_direction = Vec2D(0, 0)
def __init__(self, world, coords, size, direction):
self.world = world
self.coords = coords
self.size = size
self.direction = direction
self.x, self.y = self.coords
self.head = PythonHead()
self.world[self.x][self.y] = Cell(self.head)
self.tail = PythonPart()
self.tails = deque([])
self.__make_tail()
def __make_tail(self):
for i in range(1, self.size+1):
x, y = self.coords + -self.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):
if direction == -self.old_direction:
raise ValueError
else:
self.old_direction = 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
self.tails.appendleft(old_head)
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
self.tails[self.size - 1].contents = Cell.EMPTINESS
self.tails.appendleft(old_head)
else:
raise Death
class Death(Exception):
pass

Лог от изпълнението

.............
----------------------------------------------------------------------
Ran 13 tests in 0.045s

OK

История (6 версии и 1 коментар)

Иван обнови решението на 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

Иван обнови решението на 13.05.2013 11:53 (преди почти 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]
+ if line < 0:
+ raise IndexError
+ else:
+ 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
+ old_direction = Vec2D(0, 0)
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()
+ self.__make_tail()
- def make_tail(self):
+ 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 __check_directions(self, direction):
+ a = self.old_direction.x
+ b = self.old_direction.y
+ c = direction.x * -1
+ d = direction.y * -1
+ return (a, b) == (c, d)
+
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
+ if self.__check_directions(direction):
+ raise ValueError
else:
- if 0 <= self.x < self.world.width \
- and 0 <= self.y < self.world.width:
+ self.old_direction = 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]
- self.tails[self.size - 1].contents = Cell.EMPTINESS
for i in self.tails:
new_tails.append(i)
self.tails = new_tails
else:
- raise Death
+ 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

Иван обнови решението на 14.05.2013 00:31 (преди почти 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):
if line < 0:
raise IndexError
else:
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__
+ def __neg__(self):
+ return self * -1
+
+ def __sub__(self, other):
+ if isinstance(other, Vec2D):
+ return Vec2D(self.x - other.x, self.y - other.y)
+ elif (hasattr(other, "__getitem__")):
+ return Vec2D(self.x - other[0], self.y - other[1])
+ else:
+ return Vec2D(self.x - other, self.y - other)
+
+ def __rsub__(self, other):
+ if isinstance(other, Vec2D):
+ return Vec2D(other.x - self.x, other.y - self.y)
+ if (hasattr(other, "__getitem__")):
+ return Vec2D(other[0] - self.x, other[1] - self.y)
+ else:
+ return Vec2D(other - self.x, other - self.y)
+
+ def __str__(self):
+ return '({}, {})'.format(self.x, self.y)
+
+ def __eq__(self, other):
+ if isinstance(other, Vec2D):
+ return self.x == other.x and self.y == other.y
+ elif hasattr(other, "__getitem__") and len(other) == 2:
+ return self.x == other[0] and self.y == other[1]
+ else:
+ return False
+
+ def __ne__(self, other):
+ if isinstance(other, Vec2D):
+ return self.x != other.x or self.y != other.x
+ elif hasattr(other, "__getitem__") and len(other) == 2:
+ return self.x == other[0] and self.y == other[1]
+ else:
+ return True
+
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 = Vec2D(0, 0)
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 __check_directions(self, direction):
a = self.old_direction.x
b = self.old_direction.y
c = direction.x * -1
d = direction.y * -1
return (a, b) == (c, d)
def move(self, direction):
if self.__check_directions(direction):
raise ValueError
else:
self.old_direction = 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

Иван обнови решението на 14.05.2013 01:11 (преди почти 11 години)

+from collections import deque
+
+
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):
if line < 0:
raise IndexError
else:
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__
def __neg__(self):
return self * -1
def __sub__(self, other):
if isinstance(other, Vec2D):
return Vec2D(self.x - other.x, self.y - other.y)
elif (hasattr(other, "__getitem__")):
return Vec2D(self.x - other[0], self.y - other[1])
else:
return Vec2D(self.x - other, self.y - other)
def __rsub__(self, other):
if isinstance(other, Vec2D):
return Vec2D(other.x - self.x, other.y - self.y)
if (hasattr(other, "__getitem__")):
return Vec2D(other[0] - self.x, other[1] - self.y)
else:
return Vec2D(other - self.x, other - self.y)
def __str__(self):
return '({}, {})'.format(self.x, self.y)
def __eq__(self, other):
if isinstance(other, Vec2D):
return self.x == other.x and self.y == other.y
elif hasattr(other, "__getitem__") and len(other) == 2:
return self.x == other[0] and self.y == other[1]
else:
return False
def __ne__(self, other):
if isinstance(other, Vec2D):
return self.x != other.x or self.y != other.x
elif hasattr(other, "__getitem__") and len(other) == 2:
return self.x == other[0] and self.y == other[1]
else:
return True
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 = Vec2D(0, 0)
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.tails = deque([])
self.__make_tail()
def __make_tail(self):
for i in range(1, self.size+1):
- x, y = self.coords + self.tail_direction * i
+ x, y = self.coords + -self.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 __check_directions(self, direction):
- a = self.old_direction.x
- b = self.old_direction.y
- c = direction.x * -1
- d = direction.y * -1
- return (a, b) == (c, d)
-
def move(self, direction):
- if self.__check_directions(direction):
+ if direction == -self.old_direction:
raise ValueError
else:
self.old_direction = 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
+ self.tails.appendleft(old_head)
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
+ self.tails.appendleft(old_head)
else:
raise Death
class Death(Exception):
pass

Иван обнови решението на 14.05.2013 10:30 (преди почти 11 години)

from collections import deque
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):
if line < 0:
raise IndexError
else:
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
+ self.content = [x, y]
+ def __iter__(self):
+ self.count = 0
+ return self
+
+ def __next__(self):
+ if self.count < 2:
+ self.count += 1
+ return self.content[self.count-1]
+ else:
+ raise StopIteration
+
def __add__(self, other):
if isinstance(other, Vec2D):
- return (self.x + other.x, self.y + other.y)
+ return Vec2D(self.x + other.x, self.y + other.y)
elif hasattr(other, "__getitem__"):
- return (self.x + other[0], self.y + other[1])
+ return Vec2D(self.x + other[0], self.y + other[1])
else:
- return (self.x + other, self.y + other)
+ return Vec2D(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__
def __neg__(self):
return self * -1
def __sub__(self, other):
if isinstance(other, Vec2D):
return Vec2D(self.x - other.x, self.y - other.y)
elif (hasattr(other, "__getitem__")):
return Vec2D(self.x - other[0], self.y - other[1])
else:
return Vec2D(self.x - other, self.y - other)
def __rsub__(self, other):
if isinstance(other, Vec2D):
return Vec2D(other.x - self.x, other.y - self.y)
if (hasattr(other, "__getitem__")):
return Vec2D(other[0] - self.x, other[1] - self.y)
else:
return Vec2D(other - self.x, other - self.y)
def __str__(self):
return '({}, {})'.format(self.x, self.y)
def __eq__(self, other):
if isinstance(other, Vec2D):
return self.x == other.x and self.y == other.y
elif hasattr(other, "__getitem__") and len(other) == 2:
return self.x == other[0] and self.y == other[1]
else:
return False
def __ne__(self, other):
if isinstance(other, Vec2D):
return self.x != other.x or self.y != other.x
elif hasattr(other, "__getitem__") and len(other) == 2:
return self.x == other[0] and self.y == other[1]
else:
return True
class Python():
x = 0
y = 0
LEFT = Vec2D(0, -1)
RIGHT = Vec2D(0, 1)
UP = Vec2D(-1, 0)
DOWN = Vec2D(1, 0)
energy = 0
old_direction = Vec2D(0, 0)
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.x, self.y = self.coords
self.head = PythonHead()
self.world[self.x][self.y] = Cell(self.head)
self.tail = PythonPart()
self.tails = deque([])
self.__make_tail()
def __make_tail(self):
for i in range(1, self.size+1):
x, y = self.coords + -self.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):
if direction == -self.old_direction:
raise ValueError
else:
self.old_direction = 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
self.tails.appendleft(old_head)
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
self.tails[self.size - 1].contents = Cell.EMPTINESS
self.tails.appendleft(old_head)
else:
raise Death
class Death(Exception):
pass

Иван обнови решението на 15.05.2013 14:51 (преди почти 11 години)

from collections import deque
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):
if line < 0:
raise IndexError
else:
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
+ if self.world[i][j].contents is None:
+ the_world += Cell.EMPTINESS
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:
+ def __init__(self, contents=None):
+ if isinstance(contents, WorldObject) or contents is None:
self.contents = contents
else:
raise TypeError
def is_empty(self):
- if self.contents != self.EMPTINESS:
+ if self.contents is not None:
return False
else:
return True
def __str__(self):
- if isinstance(self.contents, Cell):
- return self.contents
+ if self.contents is None:
+ return self.EMPTINESS
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
self.content = [x, y]
def __iter__(self):
self.count = 0
return self
def __next__(self):
if self.count < 2:
self.count += 1
return self.content[self.count-1]
else:
raise StopIteration
def __add__(self, other):
if isinstance(other, Vec2D):
return Vec2D(self.x + other.x, self.y + other.y)
elif hasattr(other, "__getitem__"):
return Vec2D(self.x + other[0], self.y + other[1])
else:
return Vec2D(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__
def __neg__(self):
return self * -1
def __sub__(self, other):
if isinstance(other, Vec2D):
return Vec2D(self.x - other.x, self.y - other.y)
elif (hasattr(other, "__getitem__")):
return Vec2D(self.x - other[0], self.y - other[1])
else:
return Vec2D(self.x - other, self.y - other)
def __rsub__(self, other):
if isinstance(other, Vec2D):
return Vec2D(other.x - self.x, other.y - self.y)
if (hasattr(other, "__getitem__")):
return Vec2D(other[0] - self.x, other[1] - self.y)
else:
return Vec2D(other - self.x, other - self.y)
def __str__(self):
return '({}, {})'.format(self.x, self.y)
def __eq__(self, other):
if isinstance(other, Vec2D):
return self.x == other.x and self.y == other.y
elif hasattr(other, "__getitem__") and len(other) == 2:
return self.x == other[0] and self.y == other[1]
else:
return False
def __ne__(self, other):
if isinstance(other, Vec2D):
return self.x != other.x or self.y != other.x
elif hasattr(other, "__getitem__") and len(other) == 2:
return self.x == other[0] and self.y == other[1]
else:
return True
class Python():
x = 0
y = 0
LEFT = Vec2D(0, -1)
RIGHT = Vec2D(0, 1)
UP = Vec2D(-1, 0)
DOWN = Vec2D(1, 0)
energy = 0
old_direction = Vec2D(0, 0)
def __init__(self, world, coords, size, direction):
self.world = world
self.coords = coords
self.size = size
self.direction = direction
self.x, self.y = self.coords
self.head = PythonHead()
self.world[self.x][self.y] = Cell(self.head)
self.tail = PythonPart()
self.tails = deque([])
self.__make_tail()
def __make_tail(self):
for i in range(1, self.size+1):
x, y = self.coords + -self.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):
if direction == -self.old_direction:
raise ValueError
else:
self.old_direction = 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
self.tails.appendleft(old_head)
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
self.tails[self.size - 1].contents = Cell.EMPTINESS
self.tails.appendleft(old_head)
else:
raise Death
class Death(Exception):
pass