I am a new young programmer and I am learning python. I am just making a sample program to learn how to make bigger programs.
class Robot():
def __init__(self):
self.xpos = 0
self.ypos = 0
def step(self, axis):
print "step"
if axis in "xX":
self.xpos += 1
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)
robot = Robot()
robot.walk("x", steps=3)
All this does is keep track of where an individual robot is. How can i keep track if i have two robots and if they are in the same location.
Example:
robot1 = Robot()
robot2 = Robot()
robot1.walk("x",5)
robot2.walk("x",5)
they would be in the same location so how would i check to see if any robots are in the same location?
If you create a class like
Map, it is simple to write a method that checks the location of all robots:This has the advantage of allowing many other operations you might want to perform on the map- you can tell if the path is clear, for example.
Incidentally, you might want to include a reference on each Robot a reference to the Map that it’s on (which can be set by the
add_robotmethod, so that the robot can tell whether its movement is possible when you call itsmovemethod.