Решение на Питоните хапят! от Рейхан Садък

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

Към профила на Рейхан Садък

Резултати

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

Код

from Error import Error
class Error(Exception):
"""Base class for exception in Python Game"""
pass
class Death(Error):
""" Death python exception"""
pass
class Vec2D:
""" This class modles a 2 dimensional vector"""
def __init__(self, x, y):
self.x = x
self.y = y
def __add__(self, value):
return Vec2D(self.x + value.x, self.y + value.y)
def __sub__(self, value):
return Vec2D(self.x - value.x, self.y - value.y)
def __mul__(self, value):
return Vec2D( self.x * value, self.y * value)
def __neg__(self):
return Vec2D(-self.x, -self.y)
def __str__(self):
return "({}, {})".format(self.x, self.y)
class WorldObject(object):
""" This class models the world object"""
def __str__(self):
return ".."
class Food(WorldObject):
""" This class describes the Food"""
def __init__(self, energy):
self.energy = energy
def __str__(self):
return ":{}".format(self.energy)
class PythonPart(WorldObject):
""" This class models the parts of the python body"""
def __str__(self):
return "##"
class PythonHead(PythonPart):
"""This class models the head of the python"""
def __str__(self):
return "@@"
class Cell(object):
""" This class models the cell of the world"""
def __init__(self, content=None):
self.contents = content
if content is None:
self.isEmpty = True
else:
self.isEmpty = False
def is_empty(self):
return self.isEmpty
def __str__(self):
return "{}".format(self.contents)
class World:
def __init__(self, width):
self.width = width
self.cellsMatrix = list()
for index in range(0,width):
cellsArray = list()
for secondIndex in range(0,width):
cellsArray.append(Cell(WorldObject))
self.cellsMatrix.append(cellsArray)
def __len__(self):
return self.width
def __getitem__(self, key):
try:
if(key >= 0 and key < self.width):
return self.cellsMatrix[key]
else:
raise IndexError
except IndexError:
raise
def __setitem__(self, key, value):
try:
if(key >= 0 and key < self.width):
self.cellsMatrix[key] = value
else:
raise IndexError
except IndexError:
raise
class Python:
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.size = size
self.direction = direction
self.currentPosition = coords
self.pythonPositions = list()
self.pythonPositions.append(coords)
self.world[coords.x][coords.y] = Cell(PythonHead)
for index in range(1, size + 1):
self.world[coords.x - index * direction.x][coords.y - index * direction.y] = Cell(PythonPart)
self.pythonPositions.append(Vec2D(coords.x - index * direction.x, coords.y - index * direction.y))
self.pythonPositions.reverse()
def move(self, direction):
try:
if self.currentPosition.x + direction.x < 0 or self.currentPosition.x + direction.x >= len(self.world):
raise Death
elif self.currentPosition.y + direction.y < 0 or self.currentPosition.y + direction.y >= len(self.world):
raise Death
elif (self.world[self.currentPosition.x + direction.x][self.currentPosition.y + direction.y].contents is PythonPart or
self.world[self.currentPosition.x + direction.x][self.currentPosition.y + direction.y].contents is PythonHead):
raise Death
elif direction == self.LEFT and self.direction == self.RIGHT:
raise ValueError
elif direction == self.RIGHT and self.direction == self.LEFT:
raise ValueError
elif direction == self.UP and self.direction == self.DOWN:
raise ValueError
elif direction == self.DOWN and self.direction == self.UP:
raise ValueError
elif self.world[self.currentPosition.x + direction.x][self.currentPosition.y + direction.y].contents is WorldObject:
self.world[self.pythonPositions[0].x][self.pythonPositions[0].y] = Cell(WorldObject)
self.pythonPositions.pop()
self.pythonPositions.append(Vec2D(self.currentPosition.x + direction.x, self.currentPosition.y + direction.y))
self.world[self.pythonPositions[-1].x][self.pythonPositions[-1].y] = Cell(PythonHead)
self.world[self.pythonPositions[-2].x][self.pythonPositions[-2].y] = Cell(PythonPart)
self.direction = direction
elif type(self.world[self.currentPosition.x + direction.x][self.currentPosition.y + direction.y].contents) is Food:
self.energy += self.world[self.currentPosition.x + direction.x][self.currentPosition.y + direction.y].contents.energy
self.pythonPositions.append(Vec2D(self.currentPosition.x + direction.x, self.currentPosition.y + direction.y))
self.world[self.pythonPositions[-1].x][self.pythonPositions[-1].y] = Cell(PythonHead)
self.world[self.pythonPositions[-2].x][self.pythonPositions[-2].y] = Cell(PythonPart)
self.direction = direction
except Death:
raise
except ValueError:
raise
def __str__(self):
output = ""
for index in range (0, len(self.world)):
for secondIndex in range (0, len(self.world)):
if type(self.world[secondIndex][index].contents) is Food:
output += ":{}".format(self.world[secondIndex][index].contents.energy)
elif self.world[secondIndex][index].contents is PythonPart:
output += "##"
elif self.world[secondIndex][index].contents is PythonHead:
output += "@@"
elif self.world[secondIndex][index].contents is WorldObject:
output += ".."
else:
output += ".."
output += "\n"
output += "Energy is {}".format(self.energy)
return output

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

File "lib/language/python/runner.py", line 99, in main
    test = imp.load_source('test', test_module)
  File "/opt/python3.3/lib/python3.3/imp.py", line 109, in load_source
    return _LoadSourceCompatibility(name, pathname, file).load_module(name)
  File "<frozen importlib._bootstrap>", line 586, in _check_name_wrapper
  File "<frozen importlib._bootstrap>", line 1023, in load_module
  File "<frozen importlib._bootstrap>", line 1004, in load_module
  File "<frozen importlib._bootstrap>", line 562, in module_for_loader_wrapper
  File "<frozen importlib._bootstrap>", line 869, in _load_module
  File "<frozen importlib._bootstrap>", line 313, in _call_with_frames_removed
  File "/tmp/d20130606-14014-1c5tjua/test.py", line 3, in <module>
    from solution import *
  File "/tmp/d20130606-14014-1c5tjua/solution.py", line 1, in <module>
    from Error import Error

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

Рейхан обнови решението на 15.05.2013 03:06 (преди почти 11 години)

+from Error import Error
+
+class Error(Exception):
+ """Base class for exception in Python Game"""
+ pass
+
+class Death(Error):
+ """ Death python exception"""
+ pass
+
+class Vec2D:
+ """ This class modles a 2 dimensional vector"""
+ def __init__(self, x, y):
+ self.x = x
+ self.y = y
+
+ def __add__(self, value):
+ return Vec2D(self.x + value.x, self.y + value.y)
+
+ def __sub__(self, value):
+ return Vec2D(self.x - value.x, self.y - value.y)
+
+ def __mul__(self, value):
+ return Vec2D( self.x * value, self.y * value)
+
+ def __neg__(self):
+ return Vec2D(-self.x, -self.y)
+
+ def __str__(self):
+ return "({}, {})".format(self.x, self.y)
+
+class WorldObject(object):
+ """ This class models the world object"""
+ def __str__(self):
+ return ".."
+
+class Food(WorldObject):
+ """ This class describes the Food"""
+ def __init__(self, energy):
+ self.energy = energy
+
+ def __str__(self):
+ return ":{}".format(self.energy)
+
+class PythonPart(WorldObject):
+ """ This class models the parts of the python body"""
+ def __str__(self):
+ return "##"
+
+class PythonHead(PythonPart):
+ """This class models the head of the python"""
+ def __str__(self):
+ return "@@"
+
+class Cell(object):
+ """ This class models the cell of the world"""
+ def __init__(self, content=None):
+ self.contents = content
+ if content is None:
+ self.isEmpty = True
+ else:
+ self.isEmpty = False
+
+ def is_empty(self):
+ return self.isEmpty
+
+ def __str__(self):
+ return "{}".format(self.contents)
+
+class World:
+ def __init__(self, width):
+ self.width = width
+ self.cellsMatrix = list()
+ for index in range(0,width):
+ cellsArray = list()
+ for secondIndex in range(0,width):
+ cellsArray.append(Cell(WorldObject))
+ self.cellsMatrix.append(cellsArray)
+
+ def __len__(self):
+ return self.width
+
+ def __getitem__(self, key):
+ try:
+ if(key >= 0 and key < self.width):
+ return self.cellsMatrix[key]
+ else:
+ raise IndexError
+ except IndexError:
+ raise
+
+ def __setitem__(self, key, value):
+ try:
+ if(key >= 0 and key < self.width):
+ self.cellsMatrix[key] = value
+ else:
+ raise IndexError
+ except IndexError:
+ raise
+
+class Python:
+ 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.size = size
+ self.direction = direction
+ self.currentPosition = coords
+ self.pythonPositions = list()
+ self.pythonPositions.append(coords)
+
+ self.world[coords.x][coords.y] = Cell(PythonHead)
+ for index in range(1, size + 1):
+ self.world[coords.x - index * direction.x][coords.y - index * direction.y] = Cell(PythonPart)
+ self.pythonPositions.append(Vec2D(coords.x - index * direction.x, coords.y - index * direction.y))
+ self.pythonPositions.reverse()
+
+ def move(self, direction):
+ try:
+ if self.currentPosition.x + direction.x < 0 or self.currentPosition.x + direction.x >= len(self.world):
+ raise Death
+ elif self.currentPosition.y + direction.y < 0 or self.currentPosition.y + direction.y >= len(self.world):
+ raise Death
+ elif (self.world[self.currentPosition.x + direction.x][self.currentPosition.y + direction.y].contents is PythonPart or
+ self.world[self.currentPosition.x + direction.x][self.currentPosition.y + direction.y].contents is PythonHead):
+ raise Death
+ elif direction == self.LEFT and self.direction == self.RIGHT:
+ raise ValueError
+ elif direction == self.RIGHT and self.direction == self.LEFT:
+ raise ValueError
+ elif direction == self.UP and self.direction == self.DOWN:
+ raise ValueError
+ elif direction == self.DOWN and self.direction == self.UP:
+ raise ValueError
+ elif self.world[self.currentPosition.x + direction.x][self.currentPosition.y + direction.y].contents is WorldObject:
+ self.world[self.pythonPositions[0].x][self.pythonPositions[0].y] = Cell(WorldObject)
+ self.pythonPositions.pop()
+ self.pythonPositions.append(Vec2D(self.currentPosition.x + direction.x, self.currentPosition.y + direction.y))
+ self.world[self.pythonPositions[-1].x][self.pythonPositions[-1].y] = Cell(PythonHead)
+ self.world[self.pythonPositions[-2].x][self.pythonPositions[-2].y] = Cell(PythonPart)
+ self.direction = direction
+ elif type(self.world[self.currentPosition.x + direction.x][self.currentPosition.y + direction.y].contents) is Food:
+ self.energy += self.world[self.currentPosition.x + direction.x][self.currentPosition.y + direction.y].contents.energy
+ self.pythonPositions.append(Vec2D(self.currentPosition.x + direction.x, self.currentPosition.y + direction.y))
+ self.world[self.pythonPositions[-1].x][self.pythonPositions[-1].y] = Cell(PythonHead)
+ self.world[self.pythonPositions[-2].x][self.pythonPositions[-2].y] = Cell(PythonPart)
+ self.direction = direction
+
+ except Death:
+ raise
+ except ValueError:
+ raise
+
+ def __str__(self):
+ output = ""
+ for index in range (0, len(self.world)):
+ for secondIndex in range (0, len(self.world)):
+ if type(self.world[secondIndex][index].contents) is Food:
+ output += ":{}".format(self.world[secondIndex][index].contents.energy)
+ elif self.world[secondIndex][index].contents is PythonPart:
+ output += "##"
+ elif self.world[secondIndex][index].contents is PythonHead:
+ output += "@@"
+ elif self.world[secondIndex][index].contents is WorldObject:
+ output += ".."
+ else:
+ output += ".."
+ output += "\n"
+ output += "Energy is {}".format(self.energy)
+ return output