# import random
from basebot import BaseBot


class Bot(BaseBot):

    def __init__(self):
        super(Bot, self).__init__()
        self.actions = []
        self.player_id = None
        # Custom
        self.hq_xy = []
        self.my_army = []
        self.enemies = []

    def move_unit(self, unit_id, direction):
        self.actions.append({
            'action_type': 'MOVE',
            'unit_id': unit_id,
            'direction': direction
        })

    def attack_tile(self, attack_from, attack_to):
        self.actions.append({
            'action_type': 'ATTACK',
            'from': attack_from,
            'to': attack_to
        })

    def get_units_location(self, map):
        self.hq_xy = []
        self.my_army = []
        self.enemies = []
        r_count = 0
        for r in map:
            c_count = 0
            for c in r:
                if 'HQ' in c:
                    # Get our HQ location
                    self.hq_xy = [r_count, c_count]
                if 'U:' in c:
                    # There are units in this tile
                    for unit in c.split(','):
                        unit_id = unit.split(':')[-1]
                        if 'U:' not in unit:
                            continue
                        if 'U:%s' % self.player_id in unit:
                            # friendly unit
                            self.my_army.append(([r_count, c_count, unit_id]))
                        else:
                            # enemy unit!
                            self.enemies.append(([r_count, c_count]))
                c_count += 1
            r_count += 1

    def attack_if_its_possible(self, attacker_position, delta_target):
        """Try to attack some tile

        :param attacker_position: Attacker position
        :param delta_target: Delta from attacker position where should be the target
        :return: True if attack could be done, False in other case
        """
        target_position = attacker_position
        target_position[0] += delta_target[0]
        target_position[1] += delta_target[1]
        if target_position in self.enemies:
            self.attack_tile(attacker_position, target_position)

            return True

        return False

    def on_turn(self, data_dict):
        self.player_id = data_dict['player_num']
        self.actions = []
        self.my_army = []
        self.enemies = []
        map = data_dict['map']
        self.get_units_location(map)
        print "HQ_XY: ", self.hq_xy
        print "UNITS LOCATION: ", self.my_army
        print "ENEMY UNITS: ", self.enemies

        directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]
        # random.shuffle(self.my_army)
        self.move_units(directions)
        # self.attack_with_units(directions)

        return {'ACTIONS': self.actions}

    def attack_with_units(self, directions):
        # All units must try to attack
        for attacker_position in {u[:-1] for u in self.my_army}:
            # random.shuffle(directions)
            for d in directions:
                if self.attack_if_its_possible(attacker_position, d):
                    break

    def move_units(self, directions):
        # Try to move all attackers in random direction
        for unit_id in (x[-1] for x in self.my_army):
            # directions = random.shuffle(directions)
            for d in directions:
                self.move_unit(unit_id, d)
                break

@@ -188,92 +188,27 @@
     
-# Custom%0A        self.hq_xy = %5B%5D%0A        self.my_army = %5B%5D%0A        self.enemies = %5B%5D
+self._reset_units()
 %0A%0A  
@@ -593,25 +593,28 @@
     def 
-g
+_res
 et_units
 _locatio
@@ -609,27 +609,13 @@
 nits
-_location(self, map
+(self
 ):%0A 
@@ -693,49 +693,80 @@
  %5B%5D%0A
+%0A
     
-    r_count = 0%0A        for r in map:
+def get_units_location(self, game_map):%0A        self._reset_units()
 %0A   
@@ -774,46 +774,94 @@
     
-    c
+for r
 _count
- = 0%0A            for c in r
+, row in enumerate(game_map):%0A            for c_count, tile in enumerate(r)
 :%0A  
@@ -881,25 +881,28 @@
  if 'HQ' in 
-c
+tile
 :%0A          
@@ -1012,17 +1012,20 @@
 'U:' in 
-c
+tile
 :%0A      
@@ -1101,17 +1101,20 @@
 unit in 
-c
+tile
 .split('
@@ -1509,16 +1509,16 @@
 y unit!%0A
+
         
@@ -1582,62 +1582,8 @@
 t%5D))
-%0A                c_count += 1%0A            r_count += 1
 %0A%0A  

rename variables in example bot.