I am new to programming and i am just going through a python book. I want to have multiple robots to run inside a map. There will be multiple Robot(s) in a Map. what do i need to do to the map class to have it work this way? I know this is vague but i am 14 and trying hard to explain this.
class Map:
def __init__(self):
self.robot = []
def add_robot(self, robot):
self.robot.add(Robot)
def is_occupied(self, x, y):
for r in self.robot:
if r.xpos == x and r.ypos == y:
return True
return False
class Robot(Map):
def __init__(self):
self.xpos = 0
self.ypos = 0
def step(self, axis):
if axis in "xX":
if self.is_occupied(self.xpos+1, self.ypos):
self.xpos += 1
print "step X axis"
elif axis in "yY":
self.ypos += 1
def walk(self, axis, steps=2):
for i in range(steps):
self.step(axis)
def get_pos(self):
print "X:%i Y:%i" % (self.xpos, self.ypos)
robot1 = Robot()
robot1.walk("x", 5)
robot1.get_pos()
If i do not have the ‘Map’ class this works just fine but i cannot get map class to work. I got help making the map class but i cant get it to work with my Robot class.
You have
Robotas a subclass ofMap; this is not how inheritance should be used. Think “subclass” rather than “child class.”Consider this:
A human IS A animal; it can speak, but it can eat.
A robot is obviously not a map. I believe you are thinking of a map having robots, and it should work if you make
Robotnot a subclass. Oh, and changeself.robottoself.robots.