-1

I need to assign the self.current_location from class Robot to the self.location in the Class Grid, but i failed that, there is my code:

class Grid:
def __init__(self,size=10,location=(0,0)):
    self.size=int(size)
    self.location=tuple(location)
    # self.grid_space=[self.size*['-']]*self.size
    # print(self.grid_space)
    self.grid_space=[]
    for i in range(self.size):
        self.grid_space.append(self.size*['-'])
def __str__(self):
    self.grid_space[self.location[0]][self.location[1]]="R"
    strrep=''
    for i in range(self.size):
        strrep+=" ".join(self.grid_space[i])+'\n'
    return strrep
def update(self,new_location):
    self.new_location=tuple(new_location)
    if self.location==new_location:
        return self.location
    else:
        self.location=new_location
        return self.location
def get_location(self):
    return self.location
class Robot(Grid):
    def __init__(self,modelname,current_location=(0,0)):
        self.modelname=str(modelname)
        self.grid_space=[]
        self.current_location=tuple(current_location)
        for i in range(10):
            self.grid_space.append(10*['-'])
    def __str__(self):
        return Grid().__str__()
    def move(self,destination):
        self.destination=tuple(destination)
        self.current_location=self.destination
        self.current_location=Grid().location
    

def main():
  print("Test the Grid object...")
  my_grid = Grid()
  print(f"Location : {my_grid.get_location()}")
  my_grid.update((5,5))
  print(my_grid)
  print()
   # create Robot object and test functions
  print("Test the Robot object...")
  my_robot = Robot("Generic")
  print("Initial position")
  print(my_robot)
  print("Position after move")
  my_robot.move((9,9))
  print(Grid().location)
 if __name__ == '__main__':
  main()

Can you guys tell me how to access the self.location and change it to the current_location after "move"?

1 Answers1

0

Typically you don't want to, it breaks encapsulation of classes. You can make an array of units for the class Grid, and then add the Robot unit to the Grid, the grid can read the location from the robot so it knows where on the grid the robot is supposed to be, and the robot can "move" using its own function. Doing this will cause Robot to update it's own current location attribute, and then the map can ask the robot for it's location when it needs to.

Aceinfurno
  • 36
  • 4