Exemplo n.º 1
0
def move():
    data = bottle.request.json
    board = data.get('board')
    board_height = board.get('height')
    board_width = board.get('width')

    # print(board)
    GRID = buildGrid(board, board_height, board_width)
    # print(GRID)
    # TODO: Do things with data
    # get the snake
    VINCE = data['you']
    xhead = VINCE['body'][0]['x']
    yhead = VINCE['body'][0]['y']
    head_tuple = (xhead, yhead)

    # first food bit
    first_food_bit = (board['food'][0]['x'], board['food'][0]['y'])
    AS = AStar()
    path_to_food = AS.find_path(GRID, head_tuple, first_food_bit)
    print(path_to_food)
    directions = ['up', 'down', 'left', 'right']
    direction = random.choice(directions)
    direction = get_dir(head_tuple, path_to_food[1])
    print(direction)
    return {'move': direction, 'taunt': 'battlesnake-python!'}
Exemplo n.º 2
0
    def move(self):
        self.step_num += 1
        # if bomb in the neigbourhood
        if self.parent.target in neighbours((self.x, self.y), True):
            # The agent found the bomb
            self.bomb_location = self.parent.target
            if self.is_scout:
                # bomb deactivated
                self.parent.bomb_found = True
                return True
            else:
                self.color = QColor(200, 96, 109, 255)
                # the path is being reinitialized in
                # order to be show to the scout
                self.bomb_location = self.parent.target

        # if agent is scout and scout knows the bomb
        if self.bomb_location and self.is_scout:
            if self.path_to_bomb:
                (self.x, self.y) = self.path_to_bomb.pop()
                return True
            # create a shortcut
            target = self.bomb_location
            known_region = self.path
            start = (self.x, self.y)
            print 'calling astar'
            shortest_path = AStar()
            print 'will find the shortest path'
            self.path_to_bomb = shortest_path.find_path(start, target, known_region)
            return True

        # select to move randomly avoiding
        # to go to the last position
        order_list = ['u', 'r', 'd', 'l']
        random.shuffle(order_list)

        # get the available moves
        moves = []
        for order in order_list:
            if order == 'u':
                moves = self.up(moves)
            elif order == 'r':
                moves = self.right(moves)
            elif order == 'd':
                moves = self.down(moves)
            elif order == 'l':
                moves = self.left(moves)

        # if moves available
        if len(moves) != 0:
            # check if agents meet each other
            for agent in self.parent.agents:
                if (agent.x, agent.y) in neighbours((self.x, self.y)):
                    self.exchanges += 1
                    agent.path += self.path
                    self.path += agent.path
                    self.path = list(set(self.path))
                    agent.path = list(set(agent.path))
                    if (agent.is_scout) and (self.bomb_location) and (not agent.bomb_location):
                        agent.bomb_location = self.bomb_location
                        return True
                    else:
                        if self.bomb_location and not agent.bomb_location:
                            
                            agent.bomb_location = self.bomb_location
                            agent.color = QColor(200, 96, 109, 255)

            for m in moves:
                # The agent prefers to go to a
                # square he has not visited
                if m in self.path:
                    #we put this move at the end
                    moves.remove(m)
                    moves.append(m)
            (self.x, self.y) = moves[0]
            if moves[0] in self.path:
                self.path.remove((self.x, self.y))
            self.path.append((self.x, self.y))
        else:
            print 'cannot move'
            return False
        return True