<commit_before>"""
strategies
~~~~~~~~~~

Various strategies.

:copyright: (c) 2013 by <NAME> and <NAME>.
:license: BSD, see LICENSE for more details.
"""

from graph import (State, make_walkable, split_into_extended_islands,
                   split_into_subgraphs, make_flooded)


def get_direction(current, target):

    x = target.x - current.x
    y = target.y - current.y
    if x == -1:
        return 'WEST'
    elif x == 1:
        return 'EAST'
    elif y == -1:
        return 'NORTH'
    elif y == 1:
        return 'SOUTH'
    else:
        return 'CURRENT'



class Strategy(object):

    def __init__(self, debug=False, round_=0):

        self.actions = []
        self.position = (0, 0)
        self.floodlevel = 0

        self.extended_islands = None

        self.debug = debug
        self.round_ = round_


    def do(self, cmd, direction, x=None, y=None):
        self.actions.append((cmd + ' ' + direction, x, y))

        if cmd == 'GO':
            self.position = (x, y)


    def commit(self):

        actions = self.actions
        self.actions = []

        return actions

    def get_actions(self, graph, position):
        raise NotImplementedError()


    def split_graph_into_extended_islands(self, graph):

        self.extended_islands = split_into_extended_islands(graph)


    def dry_one_if_possible(self, graph):


class DryCurrentStrategy(Strategy):

    def play(self, board, position):

        for i in range(3):
            self.do('DRY', 'CURRENT')

            return self.commit()


def find_current_island(islands, x, y):
    for island in islands:
        for node in island.nodes:
            if node.x == x and node.y == y:
                return island

def log(msg):

    with open('/tmp/kkrash.log', 'a') as fh:
        fh.write(msg + '\n')

class MoveAwayFromWaterStrategy(Strategy):

    def play(self, board, position):

        graph = Graph.from_board(board)
        dry = make_dry(graph)

        islands = split_into_subgraphs(dry)

        island = find_current_island(islands, *position)
        if island is None:
            # No where to go to
            for i in range(3): self.do("GO", "CURRENT")
            return self.commit()

        island.calculate_distance_to_water()
        

        target = island.nodes[0]
        
        for node in island.nodes:
            if node.distance_to_water > target.distance_to_water:
                target = node
            if node.x == position[0] and node.y == position[1]:
                current = node
        
        if current == target:
            for i in range(3):
                self.do('GO', 'CURRENT')

            return self.commit()

        def mark(node, distance):

            node.distance = distance<commit_msg>Add some usefull methods to Strategy<commit_after>"""
strategies
~~~~~~~~~~

Various strategies.

:copyright: (c) 2013 by <NAME> and <NAME>.
:license: BSD, see LICENSE for more details.
"""

from graph import (State, make_walkable, split_into_extended_islands,
                   split_into_subgraphs, make_flooded)


def get_direction(current, target):

    x = target.x - current.x
    y = target.y - current.y
    if x == -1:
        return 'WEST'
    elif x == 1:
        return 'EAST'
    elif y == -1:
        return 'NORTH'
    elif y == 1:
        return 'SOUTH'
    else:
        return 'CURRENT'



class Strategy(object):

    def __init__(self, debug=False, round_=0):

        self.actions = []
        self.position = (0, 0)
        self.floodlevel = 0

        self.extended_islands = None

        self.debug = debug
        self.round_ = round_


    def do(self, cmd, direction, x=None, y=None):
        self.actions.append((cmd + ' ' + direction, x, y))

        if cmd == 'GO':
            self.position = (x, y)


    def commit(self):

        actions = self.actions
        self.actions = []

        return actions

    def get_actions(self, graph, position):
        raise NotImplementedError()


    def split_graph_into_extended_islands(self, graph):

        self.extended_islands = split_into_extended_islands(graph)


    def dry_one_if_possible(self, graph):

        def _set_dry_state(node):
            graph.get_node(node.x, node.y).state = State.redry

            for island in self.extended_islands:
                n = island.get_node(node.x, node.y)
                if n is not None:
                    n.state = State.redry

        current_node = graph.get_node(*self.position)

        if current_node.state == State.flooded:
            self.do('DRY', 'CURRENT', current_node.x, current_node.y)
            _set_dry_state(current_node)
            return True

        for node in current_node.neighbors:
            if node.state == State.flooded:
                direction = get_direction(current_node, node)
                self.do('DRY', direction, node.x, node.y)
                _set_dry_state(node)
                return True

        return False

def find_current_island(islands, x, y):
    for island in islands:
        for node in island.nodes:
            if node.x == x and node.y == y:
                return island

    def go(self, graph, start, target):

        next_node = graph.get_next_node_on_path_to(start, target)

        if next_node is not None:
            direction = get_direction(start, next_node)
            self.do('GO', direction, next_node.x, next_node.y)



    def find_target(self, graph):

        current_node = graph.get_node(*self.position)

        target_island = None
        for island in self.extended_islands:
            other_node = graph.get_node(island.nodes[0].x, island.nodes[0].y)
            if not graph.is_reachable(current_node, other_node):
                continue

            if target_island is None:
                target_island = island
            else:
                if island.calculate_island_value() > target_island.calculate_island_value():
                    target_island = island

        target = target_island.get_middle()

        return graph.get_node(target.x, target.y)




            node.distance = distance
{'fraction_non_alphanumeric': 0.10482283464566929, 'fraction_numerical': 0.005249343832020997, 'mean_word_length': 1.97705078125, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7078447', 'n_tokens_mistral': 1947, 'n_tokens_neox': 1807, 'n_words': 469}