0

My ultimate goal with this program I'm making is to use the A Star algorithm to let a robot find its goal in the grid. I'm not at the A star stage quite yet and I'm trying to figure out how to move the robot, but I'm not sure how to move the robot from it's current position to its next position (north, south, east, west).

Note: I have not taken into consideration the grid boundaries or walls when moving quite yet. If anyone has a good idea on how I could tackle that, feel free to share your knowledge.

Also, I can't seem to print out my grid, I don't use python often and I'm not entirely sure how to print out the grid. How would I print out the grid with certain characters representing a robot, walls, traps or paths?

Thanks in advance.

class robot:

    def __init__ (self, name, grid, position):
        self.name = name
        self.position = position
        self.grid = grid

    def robot_position(position):
        position = (x,y)
        return robot.position

    def robot_neighbours(self, position):
        position = (x,y)
        results = [(x+1, y), (x, y-1), (x-1, y), (x, y+1)]
        return results

    def move_north():
        north = (x,y+1)
        robot.robot_position = north
        return robot.robot_position





class game_board:

    def boundary(self, point): #defines the grids boundaries
        point = (x,y)
        return 0 <= x < self.width and 0 <=y < self.height

    def node_neighbours(self, point): #defines the current nodes neighbours
        point = (x,y)
        results = [(x+1, y), (x, y-1), (x-1, y), (x, y+1)]
        results = filter(self.grid_boundary, results) #filters out the boundary results
        results = filter(self.can_pass_through, results) #filters out the coordinates that you can pass through from those you can't
        return results

    def can_pass_through(self, point):
        return point not in self.walls
        return point not in self.traps

#constructsthe2Dgrid
    def __init__(self, width, height): 
        self.width = width
        self.height = height
        self.name = " . "
        self.walls = []
        self.traps = []

    def build_grid(grid):
        for x in range(grid.width):
            for y in range (grid.height):
                print(x, y)
        return "grid created" + width + " " + height
Chris
  • 785
  • 10
  • 24
  • There's a few issues with your classes and functions and it's somewhat difficult to address your question without also addressing other problems in your code. Perhaps you should consider breaking this problem down into its smallest parts, then make and verify the functionality of each part before moving on to the next. It will also help you ask more narrow and precise questions, which will allow people to help you better. – sytech Oct 24 '16 at 18:41

1 Answers1

-1

Generally, the issue is that you are using () for lists, when it should be []. Right now, you are generating tuples, but if you are doing math operations, it is best to use lists.

Just one example in the code:

def robot_neighbours(self, position):
   position = [x, y]
   results = [[x+1, y], [x, y-1], [x-1, y], [x, y+1]]
   return results`

Hope that helped, Narusan

EDIT: The main difference is that using lists, position[0] is a valid argument, but using tuples it is not. For your movement, you would write something like

new_pos = [position[0]+1, position[1]]
Narusan
  • 482
  • 1
  • 5
  • 18
  • Ah okay. I suppose tuples are considered to be one element, so x, y wouldn't be separate? if that makes sense. Where as lists of course have separate elements. – Chris Oct 24 '16 at 18:21
  • See this issue [link](http://stackoverflow.com/questions/626759/whats-the-difference-between-list-and-tuples) – Narusan Oct 24 '16 at 18:44
  • Tuples are perfectly acceptable here. It's true that tuples are immutable, but OP is does not need to mutate any values in this function. I believe the intent of `robot_neighbors` is to return the positions that surround that robot on the grid. You can most certainly make `new_pos` just like that even if `position` is a tuple. Similarly, OP can make `results` a tuple without issue, even if `position` is a tuple also. – sytech Oct 25 '16 at 12:37