Ejemplo n.º 1
0
def path_to_enemy_tail(my_snake_head, snake_id, snakes, waypoints, links,
                       grid):
    global taunt
    current_path = None
    for snake in snakes:
        if snake_id == snake['id']:
            continue
        enemy_tail = point_to_list(
            (snake['body'][-1]['x'], snake['body'][-1]['y']))
        if distance(enemy_tail, my_snake_head) < 2:
            continue
        tail_neighbours = neighbours(enemy_tail, grid, [])
        for n in tail_neighbours:
            path = None
            # print('Looking for tail at ' + str(n) + ' my head at ' + str(my_snake_head))
            if n in PATH_FINDING_OBSTACLES:
                continue
            path = find_path(my_snake_head, n, waypoints, links, grid,
                             PATH_FINDING_OBSTACLES)
            if path is not None:
                # print(str(path))
                if current_path is not None:
                    if path_distance(current_path) > path_distance(path):
                        current_path = path
                else:
                    current_path = path
    if current_path is not None:
        taunt = 'Following Enemy Tail'
        '''print('Found path to enemy tail')'''
    return current_path
Ejemplo n.º 2
0
def path_to_tail(my_snake_head, my_snake_tail, waypoints, links, grid):
    global taunt
    current_path = None
    tail_neighbours = neighbours(my_snake_tail, grid, [])
    if distance(my_snake_tail, my_snake_head) < 2:
        return current_path
    for n in tail_neighbours:
        path = None
        # print('Looking for tail at ' + str(n) + ' my head at ' + str(my_snake_head))
        if n[0] == my_snake_head[0] and n[1] == my_snake_head[1]:
            r = []
            r.append(my_snake_tail)
            r.append(my_snake_tail)
            current_path = r
            taunt = 'Just going to eat my tail...'
            break
        if n in PATH_FINDING_OBSTACLES:
            continue
        path = find_path(my_snake_head, n, waypoints, links, grid,
                         PATH_FINDING_OBSTACLES)
        if path is not None:
            # print(str(path))
            if current_path is not None:
                if path_distance(current_path) > path_distance(path):
                    current_path = path
            else:
                current_path = path
    if current_path is not None:
        taunt = 'Following my tail'

    return current_path
Ejemplo n.º 3
0
def path_to_safe_food(my_snake_head, my_snake_length, snake_id, goals, snakes,
                      waypoints, links, grid, my_snake_overlapping):
    global taunt
    current_path = None
    for goal in goals:
        if current_path is not None:
            if distance(my_snake_head, goal) > path_distance(current_path):
                continue
        easy = True
        for n in neighbours(goal, grid, PATH_FINDING_OBSTACLES):
            if n is DEAD_END:
                easy = False
        for snake in snakes:
            if (snake['id'] != snake_id):
                enemy_dist = distance(
                    point_to_list(
                        (snake['body'][0]['x'], snake['body'][0]['y'])), goal)
                if enemy_dist <= distance(my_snake_head, goal):
                    easy = False
                    break
        if not easy:
            continue
        # start = time.time()
        path = find_path(my_snake_head, goal, waypoints, links, grid,
                         PATH_FINDING_OBSTACLES)
        # end = time.time()
        # print('Time to get path from o_path: ' + str((end - start) * 1000) + 'ms')
        if path is not None:
            possible_move = smart_direction(my_snake_head, path[1], grid, [],
                                            my_snake_overlapping)
            # print('possible_move:' + str(possible_move))
            # print(path)
            block_pos = move_to_position(my_snake_head, possible_move)
            # display_grid(grid)
            # print('block pos:' + str(block_pos))
            temp_hold = grid[block_pos[0]][block_pos[1]]
            grid[block_pos[0]][block_pos[1]] = SNAKE_HEAD
            if my_snake_length <= enough_space(path[-1], my_snake_length, grid,
                                               BAD_POSITIONS):
                if current_path is not None:
                    if (path_distance(path) < path_distance(current_path)):
                        current_path = path
                else:
                    current_path = path
            grid[block_pos[0]][block_pos[1]] = temp_hold
            # display_grid(grid)
            # end = time.time()
            # print('Time to fill: ' + str((end - start) * 1000) + 'ms')
    if current_path is not None:
        taunt = 'Safe food'
    return current_path
Ejemplo n.º 4
0
def path_to_snake_body(my_snake_head, my_snake_id, snakes, waypoints, links,
                       grid):
    global taunt
    current_path = None
    could_go_to = flood_fill(my_snake_head, grid, DEATH_POSITIONS)
    for snake in snakes:
        body_pos_counter = 0
        snake['body'].reverse()
        for body in snake['body']:
            body_pos_counter = body_pos_counter + 1
            '''if body_pos_counter % 2:
                continue'''
            # TODO go for body only if it's index in body is greater then the distance to it.
            if current_path is not None:
                continue
                '''if path_distance(current_path) < distance(my_snake_head, point_to_list(body)):
                    continue'''
            body_neighbours = neighbours(point_to_list((body['x'], body['y'])),
                                         grid, [])
            for n in body_neighbours:
                if n not in could_go_to:
                    continue
                path = None
                if n in PATH_FINDING_OBSTACLES:
                    continue
                path = find_path(my_snake_head, n, waypoints, links, grid,
                                 PATH_FINDING_OBSTACLES)
                if path is None:
                    continue
                if (path_distance(path) < body_pos_counter):
                    continue
                if path is not None:
                    # print(str(path))
                    if current_path is not None:
                        if path_distance(current_path) > path_distance(path):
                            current_path = path
                    else:
                        current_path = path

    for snake in snakes:
        snake['body'].reverse()

    if current_path is not None:
        taunt = 'Following Anybody'
        '''print('Found path to snake body:' + str(current_path[-1]) + ' ' + str(path_distance(current_path)))
    else:
        print('Could not find path to any body of any snake')'''
    return current_path
Ejemplo n.º 5
0
def path_to_desperation_food(my_snake_head, my_snake_length, my_snake_health,
                             snake_id, goals, waypoints, links, grid,
                             my_snake_overlapping):
    # display_grid(grid)
    global taunt
    current_path = None
    possible_paths = []
    for goal in goals:
        if current_path is not None:
            if distance(my_snake_head, goal) > path_distance(current_path):
                continue
        # start = time.time()
        path = find_path(my_snake_head, goal, waypoints, links, grid,
                         PATH_FINDING_OBSTACLES)
        # end = time.time()
        # print('Time to get path from o_path: ' + str((end - start) * 1000) + 'ms')
        if path is not None:
            # print(str(my_snake_health) + ' ' + str(path_distance(path)) + ' ' + str(goal) + ' ' + str(path))
            # if goal[0] == 19 and goal[1] == 0:
            #    display_grid(grid)
            if my_snake_health < path_distance(path):
                # print('would be dead')
                continue
            possible_paths.append(path)
            possible_move = smart_direction(my_snake_head, path[1], grid,
                                            PATH_FINDING_OBSTACLES,
                                            my_snake_overlapping)
            block_pos = move_to_position(my_snake_head, possible_move)
            temp_hold = grid[block_pos[0]][block_pos[1]]
            grid[block_pos[0]][block_pos[1]] = SNAKE_HEAD
            if my_snake_length <= enough_space(goal, my_snake_length, grid,
                                               BAD_POSITIONS):
                if current_path is not None:
                    if (path_distance(path) < path_distance(current_path)):
                        current_path = path
                else:
                    current_path = path
            else:
                possible_paths.append(path)
            grid[block_pos[0]][block_pos[1]] = temp_hold
        # end = time.time()
        # print('Time to fill: ' + str((end - start) * 1000) + 'ms')
    # print('possible path: ' + str(len(possible_paths)) + ' ' + str(my_snake_health))
    if current_path is None:
        if len(possible_paths) > 0:
            current_path = possible_paths[0]
    taunt = 'Desperation food'
    return current_path
Ejemplo n.º 6
0
def path_to_bully_enemy(my_snake_head, my_snake_length, snake_id, goals,
                        snakes, waypoints, links, grid, my_snake_overlapping):
    global taunt
    current_path = None
    target_snake_head = None
    for snake in snakes:
        if snake_id == snake['id']:
            continue

        enemy_head = point_to_list(
            (snake['body'][0]['x'], snake['body'][0]['y']))
        if my_snake_length > len(snake['body']):
            head_neighbours = neighbours(enemy_head, grid, [])
        else:
            head_neighbours = []
        forward = get_forward_node(
            enemy_head,
            point_to_list((snake['body'][1]['x'], snake['body'][1]['y'])),
            grid)
        if forward is not None:
            move_dir = direction(enemy_head, forward)
            pos = move_to_position(enemy_head, move_dir)
            if grid[pos[0]][pos[1]] not in DEATH_POSITIONS:
                head_neighbours.append(forward)

        for n in head_neighbours:
            path = None
            # print('Looking for tail at ' + str(n) + ' my head at ' + str(my_snake_head))
            if n in DEATH_POSITIONS:
                continue
            # Check if another enemy is closer
            '''easy = True
            for s in snakes:
                if(not s['id'] == snake_id and not s['id'] == snake['id']):
                    enemy_dist = distance(enemy_head, n)
                    if enemy_dist <= distance(my_snake_head, n):
                        easy = False
                        break
            if not easy:
                # print('another enemy closer')
                continue'''
            path = find_path(my_snake_head, n, waypoints, links, grid,
                             DEATH_POSITIONS)

            if path is not None:
                if len(path) < 2:
                    continue
                    '''result = []
                    result.append((n[0], n[1]))
                    result.append((n[0], n[1]))
                    print('Moving in to the kill enemy, path is short')
                    if current_path is not None:
                        if(path_distance(result) < path_distance(current_path)):
                            current_path = result
                            continue
                    else:
                        current_path = result
                        continue'''

                # Check if move would lead to snake getting trapped
                possible_move = smart_direction(my_snake_head, path[1], grid,
                                                DEATH_POSITIONS,
                                                my_snake_overlapping)
                blocked_positions = []
                block_pos = move_to_position(my_snake_head, possible_move)
                blocked_positions.append(
                    (block_pos, grid[block_pos[0]][block_pos[1]]))
                grid[block_pos[0]][block_pos[1]] = SNAKE_HEAD
                # print('possible moves after attack: ' + str(neighbours(block_pos, grid, PATH_FINDING_OBSTACLES)))
                # Code to reset grid:
                '''for pos in blocked_positions:
                    grid[pos[0][0]][pos[0][1]] = pos[1]'''

                # Check if the enemy is in a position to block me off
                for n in neighbours(enemy_head, grid, DEATH_POSITIONS):
                    blocked_positions.append((n, grid[n[0]][n[1]]))
                    grid[n[0]][n[1]] = SNAKE_HEAD

                if len(neighbours(block_pos, grid,
                                  PATH_FINDING_OBSTACLES)) < 2:
                    for pos in blocked_positions:
                        grid[pos[0][0]][pos[0][1]] = pos[1]
                    continue

                target_surrounding_node = neighbours(block_pos, grid,
                                                     PATH_FINDING_OBSTACLES)[0]
                if my_snake_length <= enough_space(target_surrounding_node,
                                                   my_snake_length, grid,
                                                   BAD_POSITIONS):

                    if current_path is not None:
                        if (path_distance(path) < path_distance(current_path)):
                            current_path = path
                            target_snake_head = enemy_head
                    else:
                        current_path = path
                        target_snake_head = enemy_head
                for pos in blocked_positions:
                    grid[pos[0][0]][pos[0][1]] = pos[1]
    if current_path is not None:
        taunt = 'Attacking'
    else:
        return None
    return (current_path, target_snake_head)
Ejemplo n.º 7
0
def corner_enemy(my_snake_head, my_snake_length, my_snake_id, snakes,
                 waypoints, links, grid):
    global taunt
    current_path = None
    target_snake_head = None
    for snake in snakes:
        if my_snake_id == snake['id']:
            continue

        enemy_head = point_to_list(
            (snake['body'][0]['x'], snake['body'][0]['y']))

        enemy_head_neighbours = neighbours(enemy_head, grid, DEATH_POSITIONS)
        if len(enemy_head_neighbours) != 1:
            continue

        search_node = enemy_head_neighbours[0]
        search_node_neighbours = neighbours(enemy_head, grid, DEATH_POSITIONS)
        visted = []
        while len(search_node_neighbours) == 1:
            visted.append(search_node)
            search_node = search_node_neighbours[0]
            new_neigh = neighbours(search_node, grid, DEATH_POSITIONS)

            for v in visted:
                if v in new_neigh:
                    new_neigh.remove(v)
            search_node_neighbours = new_neigh

        exit_point = search_node
        if len(search_node_neighbours) == 0:
            continue
        # print('Exit point: ' + str(exit_point))
        my_path = find_path(my_snake_head, exit_point, waypoints, links, grid,
                            DEATH_POSITIONS)
        enemy_path = find_path(enemy_head, exit_point, waypoints, links, grid,
                               DEATH_POSITIONS)

        if my_path is None:
            continue

        if enemy_path is None:
            enemy_dist = distance(enemy_head, exit_point)
        else:
            enemy_dist = path_distance(enemy_path)

        larger_and_close = False
        if (path_distance(my_path) <= enemy_dist
                and my_snake_length > len(snake['body'])):
            larger_and_close = True

        if (path_distance(my_path) < enemy_dist or larger_and_close):
            if current_path is not None:
                if (path_distance(my_path) < path_distance(current_path)):
                    current_path = my_path
                    target_snake_head = enemy_head
            else:
                current_path = my_path
                target_snake_head = enemy_head

    if current_path is not None:
        taunt = 'Cornering'
    else:
        return None
    return (current_path, target_snake_head)