def next_move(self, current_state):
        """ Function that computes the next move of your paddle
        Implement your algorithm here. This will be the only function
        used by the GameCore. Be aware of abiding all the game rules.
        Returns:
            dict: coordinates of next position of your paddle.
        """

        # update my paddle pos
        # I need to do this because GameCore moves my paddle randomly
        self.my_paddle_pos = current_state['paddle1_pos'] if self.my_goal == 'left' \
                                                              else current_state['paddle2_pos']

        # estimate puck path
        path = estimate_path(current_state, self.future_size)

        # computing both goal centers
        self.my_goal_center = {'x': 0 if self.my_goal == 'left' else current_state['board_shape'][1],
                               'y': current_state['board_shape'][0]/2}
        y = random.uniform(140, 370)
        self.opponent_goal_center = {'x': 0 if self.my_goal == 'right' else current_state['board_shape'][1],
                                     'y': y}


        # find if puck path is inside my interest area
        roi_radius = current_state['board_shape'][0] * current_state['goal_size'] * 2
        pt_in_roi = None
        for p in path:
            if utils.distance_between_points(p[0], self.my_goal_center) < roi_radius:
                pt_in_roi = p
                break

        if pt_in_roi:
            # estimate an aiming position
            target_pos = utils.aim(pt_in_roi[0], pt_in_roi[1],
                                   self.opponent_goal_center, current_state['puck_radius'],
                                   current_state['paddle_radius'])

            # move to target position, taking into account the max. paddle speed
            if target_pos != self.my_paddle_pos:
                direction_vector = {'x': target_pos['x'] - self.my_paddle_pos['x'],
                                    'y': target_pos['y'] - self.my_paddle_pos['y']}
                direction_vector = {k: v / utils.vector_l2norm(direction_vector)
                                    for k, v in direction_vector.items()}

                movement_dist = min(current_state['paddle_max_speed'] * current_state['delta_t'],
                                    utils.distance_between_points(target_pos, self.my_paddle_pos))
                direction_vector = {k: v * movement_dist
                                    for k, v in direction_vector.items()}
                new_paddle_pos = {'x': self.my_paddle_pos['x'] + direction_vector['x'],
                                  'y': self.my_paddle_pos['y'] + direction_vector['y']}

                # check if computed new position in not inside goal area
                # check if computed new position in inside board limits
                if utils.is_inside_goal_area_paddle(new_paddle_pos, current_state) is False and \
                     utils.is_out_of_boundaries_paddle(new_paddle_pos, current_state) is None:
                    self.my_paddle_pos = new_paddle_pos

        return self.my_paddle_pos
Ejemplo n.º 2
0
def is_in_goal_area(position, current_state, self):
    #If the point is less than the radius away from the goal centre, it's in the goal
    #It check the top and bottom points of the puck for a collision
    if utils.distance_between_points(self.my_goal_center, {'x': position['x'], 'y': position['y'] + current_state['puck_radius']}) < current_state['board_shape'][0] * current_state['goal_size'] / 2 \
    or utils.distance_between_points(self.my_goal_center, {'x': position['x'], 'y': position['y'] - current_state['puck_radius']}) < current_state['board_shape'][0] * current_state['goal_size'] / 2:
        return True
    else:
        return False
Ejemplo n.º 3
0
def next_move_paddle(paddle_pos, target_pos, current_state):
    """
        Args:
            paddle_pos:
            target_pos:

        Return:
            the next movement of the paddle of 1 frame x and y
            next_move
    """
    if target_pos != paddle_pos:
        direction_vector = {'x': target_pos['x'] - paddle_pos['x'],
                            'y': target_pos['y'] - paddle_pos['y']}
        direction_vector = {k: v / utils.vector_l2norm(direction_vector)
                            for k, v in direction_vector.items()}

        movement_dist = min(current_state['paddle_max_speed'] * current_state['delta_t'],
                            utils.distance_between_points(target_pos, paddle_pos))
        direction_vector = {k: v * movement_dist
                            for k, v in direction_vector.items()}
        next_move = {'x': paddle_pos['x'] + direction_vector['x'],
                            'y': paddle_pos['y'] + direction_vector['y']}

    # check if computed new position in not inside goal area
    # check if computed new position in inside board limits
    if utils.is_inside_goal_area_paddle(next_move, current_state) is False and \
            utils.is_out_of_boundaries_paddle(next_move, current_state) is None:
        return next_move
        
    return paddle_pos
    
    
    """
Ejemplo n.º 4
0
def transform_base_on_eye_pairs(image, region_info, region_skin_image, eye_pairs,
		m, n, tempX, tempY, file_output):
	faceBorder = find_longest_border(region_skin_image, m, n, tempX, tempY)
	downVector = [0, 1]
	# count = 0

	for eye1, eye2 in eye_pairs:
		centroid1 = [(eye1[1] + eye1[3]) / 2, (eye1[0] + eye1[2]) / 2]
		centroid2 = [(eye2[1] + eye2[3]) / 2, (eye2[0] + eye2[2]) / 2]
		pivot = [(centroid1[0] + centroid2[0]) / 2, (centroid1[1] + centroid2[1]) / 2]

		direction = get_face_direction(region_skin_image, m, n, tempX, tempY, faceBorder, centroid1, centroid2, pivot)		
		angleToRotate = ut.find_angle_between_two_vectors(downVector, direction)
		# print(count + 1, angleToRotate) 
		# print(direction)
		if (direction[0] > 0):
			angleToRotate = -angleToRotate

		#adjust pivot point to work with original image
		pivot[0] = pivot[0] + region_info[1]
		pivot[1] = pivot[1] + region_info[0]
		pivot = tuple(pivot)

		tempImage = image.copy()
		# cv2.rectangle(tempImage, (eye1[1], eye1[0]), (eye1[3], eye1[2]), (0, 255, 0), 1)
		# cv2.rectangle(tempImage, (eye2[1], eye2[0]), (eye2[3], eye2[2]), (0, 255, 0), 1)
		mat = cv2.getRotationMatrix2D(pivot, angleToRotate, 1.0)
		tempImage = cv2.warpAffine(tempImage, mat, tempImage.shape[1::-1])

		#print(tempImage[1,1,1])
		#count += 1
		value = split_to_get_face(tempImage, pivot, ut.distance_between_points(centroid1, centroid2), 
			file_output)
Ejemplo n.º 5
0
    def check_paddle_valid_move(self, new_pos, previous_pos, state, player):
        # check if is in move range
        max_offset = state['paddle_max_speed'] * state['delta_t'] + 0.000001
        if utils.distance_between_points(new_pos, previous_pos) > max_offset:
            raise ValueError('RULES VIOLATION: paddle moved faster than speed limit for ' +
                             player.my_display_name)

        # check if is inside board limits
        if utils.is_out_of_boundaries_paddle(new_pos, state) is not None:
            raise ValueError('RULES VIOLATION: Paddle moved beyond board limits for ' +
                             player.my_display_name)

        # check if is not inside goal area
        if utils.is_inside_goal_area_paddle(new_pos, state) is True:
            raise ValueError('RULES VIOLATION: Paddle moved inside goal area for ' +
                             player.my_display_name)

        # check if paddle is inside player's area
        if self.goal_sides['left'] is player and new_pos['x'] > \
             self.board.shape[1]/2 - self.state['puck_radius']:
            raise ValueError('RULES VIOLATION: Paddle moved beyond center line for ' +
                             player.my_display_name)
        if self.goal_sides['right'] is player and new_pos['x'] < \
             self.board.shape[1]/2 + self.state['puck_radius']:
            raise ValueError('RULES VIOLATION: Paddle moved beyond center line for ' +
                             player.my_display_name)
        return None
Ejemplo n.º 6
0
        def target_to_position(target_pos):
            # move to target position, taking into account the max. paddle speed
            if target_pos != self.my_paddle_pos:
                direction_vector = {
                    'x': target_pos['x'] - self.my_paddle_pos['x'],
                    'y': target_pos['y'] - self.my_paddle_pos['y']
                }
                direction_vector = {
                    k: v / utils.vector_l2norm(direction_vector)
                    for k, v in direction_vector.items()
                }

                movement_dist = min(
                    current_state['paddle_max_speed'] *
                    current_state['delta_t'],
                    utils.distance_between_points(target_pos,
                                                  self.my_paddle_pos))
                direction_vector = {
                    k: v * movement_dist
                    for k, v in direction_vector.items()
                }
                new_paddle_pos = {
                    'x': self.my_paddle_pos['x'] + direction_vector['x'],
                    'y': self.my_paddle_pos['y'] + direction_vector['y']
                }

                # check if computed new position in not inside goal area
                # check if computed new position in inside board limits
                if utils.is_inside_goal_area_paddle(new_paddle_pos, current_state) is False and \
                     utils.is_out_of_boundaries_paddle(new_paddle_pos, current_state) is None:
                    self.my_paddle_pos = new_paddle_pos
                else:
                    target_to_position(self.my_area_center)

            return self.my_paddle_pos
Ejemplo n.º 7
0
    def attack(self, current_state):
        state = copy.copy(current_state)

        length = state['board_shape'][0]
        Tx = self.my_paddle_pos['x']
        Tdy = length + self.my_paddle_pos['y']
        Tuy = -self.my_paddle_pos['y']
        self.opponent_goal_center = {'x': 0 if self.my_goal == 'right' else current_state['board_shape'][1],
                                     'y': current_state['board_shape'][0] / 2}
        Gy = self.opponent_goal_center['y']
        Gx = self.opponent_goal_center['x']

        t_maxA_puck = (utils.distance_between_points(state['puck_pos'], self.goal_sideA)) * (state['puck_speed']['y'])
        t_maxB_puck = (utils.distance_between_points(state['puck_pos'], self.goal_sideB)) * (state['puck_speed']['y'])
        t_max_paddle_A = utils.distance_between_points({'x': 0, 'y': self.my_opponent_pos['y']},
                                                       {'x': 0, 'y': self.goal_sideA['y']}) * state['paddle_max_speed']
        t_max_paddle_B = utils.distance_between_points({'x': 0, 'y': self.my_opponent_pos['y']},
                                                       {'x': 0, 'y': self.goal_sideB['y']}) * state['paddle_max_speed']

        '''if (state['puck_pos']['x'] < (state['board_shape'][1] / 2) and state['puck_pos']['x'] > self.my_paddle_pos[
            'x'] and self.my_goal == "left"):
            print("soy izquierdo")
            if (t_maxA_puck < t_max_paddle_A):
                print("apunta al extremo inferior directo")
                return {'x': state['board_shape'][1], 'y': self.goal_sideA['y'] + state['puck_radius']}
            if (t_maxB_puck < t_max_paddle_B):
                print("apunta al extremo superior directo")
                return {'x': state['board_shape'][1], 'y': self.goal_sideB['y'] - state['puck_radius']}
        elif (state['puck_pos']['x'] > (state['board_shape'][1] / 2) and state['puck_pos']['x'] < self.my_paddle_pos[
            'x'] and self.my_goal == "right"):
            print("soy derecho")
            if (t_maxA_puck < t_max_paddle_A):
                print("apunta al extremo inferior directo")
                print({'x': 0, 'y': self.goal_sideA['y'] + state['puck_radius']})
                return {'x': 0, 'y': self.goal_sideA['y'] + state['puck_radius']}
            if (t_maxB_puck < t_max_paddle_B):
                print("apunta al extremo superior directo")
                print({'x': 0, 'y': self.goal_sideB['y'] - state['puck_radius']})
                return {'x': 0, 'y': self.goal_sideB['y'] - state['puck_radius']}'''

        if (self.my_opponent_pos['y'] <= length / 2):
            # print("Abajo")
            return {'x': ((length - Tdy) / ((Tdy - Gy) / (Tx - Gx))) + Tx, 'y': length}
        else:
            # print("Arriba")
            return {'x': ((length - Tuy) / ((Tuy - Gy) / (Tx - Gx))) + Tx, 'y': 0}
Ejemplo n.º 8
0
def make_grid(line):
    ''' Create rectangular areas.'''
    x1, y1, x2, y2 = line
    space = 2 + 1
    vertical = x1 == x2
    dist = distance_between_points((x1, y1), (x2, y2))
    if dist < space:
        return
    if vertical:  # draw horizontal lines
        if y1 >= y2:
            return
        if y2 - y1 < 2 * space:
            return
        # east
        y = random.randint(y1 + space, y2 - space)
        x = x1
        if x + 1 < WIDTH:
            for xx in range(x + 1, WIDTH):
                if grid[y][xx] != '.':
                    break
                grid[y][xx] = '#'
            line = (x + 1, y, xx, y)
            make_grid(line)
        # west
        y = random.randint(y1 + space, y2 - space)
        x = x1
        if 0 < x - 1 + 1:
            for xx in reversed(range(0, x - 1 + 1)):
                if grid[y][xx] != '.':
                    break
                grid[y][xx] = '#'
            line = (xx, y, x - 1, y)
            make_grid(line)
    else:  # draw vertical lines
        if x1 >= x2:
            return
        if x2 - x1 < 2 * space:
            return
        # south
        x = random.randint(x1 + space, x2 - space)
        y = y1
        if y + 1 < HEIGHT:
            for yy in range(y + 1, HEIGHT):
                if grid[yy][x] != '.':
                    break
                grid[yy][x] = '#'
            line = (x, y + 1, x, yy)
            make_grid(line)
        # north
        x = random.randint(x1 + space, x2 - space)
        y = y1
        if 0 < y - 1 + 1:
            for yy in reversed(range(0, y - 1 + 1)):
                if grid[yy][x] != '.':
                    break
                grid[yy][x] = '#'
            line = (x, yy, x, y - 1)
            make_grid(line)
Ejemplo n.º 9
0
 def move_idle(self):
     if random.random() < 0.90:
         return
     if distance_between_points((self.x, self.y),
                                (self.org_x, self.org_y)) < 2:
         self.move_delta(random.randint(-1, 1), random.randint(-1, 1))
     else:
         direction = get_direction((self.x, self.y),
                                   (self.org_x, self.org_y))
         self.move_delta(direction.x, direction.y)
Ejemplo n.º 10
0
    def calculate_ride_points(self, car, ride, position, step):

        distance_to_start = distance_between_points(position[0], position[1],
                                                    ride.i_x, ride.i_y)
        ride_distance = distance_between_points(ride.i_x, ride.i_y, ride.f_x,
                                                ride.f_y)

        points = 0

        ride_is_possible = step + distance_to_start + ride_distance < ride.f_t
        early_arrival_is_possible = step + distance_to_start < ride.i_t

        if ride_is_possible:
            points = ride_distance
            step += distance_to_start + ride_distance
            position = (ride.f_x, ride.f_y)

            if early_arrival_is_possible:
                points += self.bonus

        return points, position, step
Ejemplo n.º 11
0
    def get_new_paddle_pos(self, target_pos, current_state):
        # move to target position, taking into account the max. paddle speed
        new_paddle_pos = None
        if target_pos and target_pos != self.my_paddle_pos:
            direction_vector = {
                'x': target_pos['x'] - self.my_paddle_pos['x'],
                'y': target_pos['y'] - self.my_paddle_pos['y']
            }
            direction_vector = {
                k: v / utils.vector_l2norm(direction_vector)
                for k, v in direction_vector.items()
            }

            movement_dist = min(
                current_state['paddle_max_speed'] * current_state['delta_t'],
                utils.distance_between_points(target_pos, self.my_paddle_pos))
            direction_vector = {
                k: v * movement_dist
                for k, v in direction_vector.items()
            }
            if self.my_goal == 'left':
                if current_state['puck_speed']['x'] >= 0:
                    new_paddle_pos = {
                        'x': self.my_paddle_pos['x'] + direction_vector['x'],
                        'y': self.my_paddle_pos['y'] + direction_vector['y']
                    }
                else:
                    new_paddle_pos = {
                        'x':
                        self.my_paddle_pos['x'],  # + direction_vector['x'],
                        'y': self.my_paddle_pos['y'] + direction_vector['y']
                    }
            else:
                if current_state['puck_speed']['x'] <= 0:
                    new_paddle_pos = {
                        'x': self.my_paddle_pos['x'] + direction_vector['x'],
                        'y': self.my_paddle_pos['y'] + direction_vector['y']
                    }
                else:
                    new_paddle_pos = {
                        'x':
                        self.my_paddle_pos['x'],  # + direction_vector['x'],
                        'y': self.my_paddle_pos['y'] + direction_vector['y']
                    }
        return new_paddle_pos
Ejemplo n.º 12
0
	def fetch_radial_cities(self,ghash,precision=3,n=2):
		'''
		Fetches cities around the specified ghash
		Sorts the cities by distance from the center ghash
		@param ghash: the ghash of the center point
		@type ghash: str
		'''
		# calc the center geo_point
		center_geo_point = geohash.decode(ghash)
		# create a list of ghashes around the 
		ghash = utils.chop_ghash(ghash, precision)
		ghash_list = utils.create_ghash_list(ghash, n)
		# get a list of all the city keys in the range of ghashes
		city_keys_set = set([])
		for ghash in ghash_list:
			city_keys = models.City.query(
						models.City.ghash >= ghash,
						models.City.ghash <= ghash+"{"
						).iter(
							batch_size = 50,
							keys_only = True)
			city_keys_set.update(city_keys)
		
		city_futures = ndb.get_multi_async(city_keys_set)
		cities = (c.get_result() for c in city_futures)
		
		cities_list = []
		# calculate the distance from the center ghash
		for city in cities:
			# package the city for the radius list
			city_dict = city.package_for_radius_list()
			# calc distance from center point
			distance = utils.distance_between_points(center_geo_point, city.geo_point)
			# add the distance 
			city_dict['distance'] = distance
			cities_list.append(city_dict)
		
		# sort the list of cities by distance
		cities_list = sorted(cities_list,key=lambda c: c['distance'])
		return cities_list[:10]
Ejemplo n.º 13
0
    def go_Pos(self, current_state, target_pos):
        if target_pos != self.my_paddle_pos:  # check if im already in the desired area
            direction_vector = {
                'x': target_pos['x'] - self.my_paddle_pos['x'],
                'y': target_pos['y'] - self.my_paddle_pos['y']
            }
            direction_vector = {
                k: v / utils.vector_l2norm(direction_vector)
                for k, v in direction_vector.items()
            }

            movement_dist = min(
                current_state['paddle_max_speed'] * current_state['delta_t'],
                utils.distance_between_points(target_pos, self.my_paddle_pos))
            direction_vector = {
                k: v * movement_dist
                for k, v in direction_vector.items()
            }
            new_paddle_pos = {
                'x': self.my_paddle_pos['x'] + direction_vector['x'],
                'y': self.my_paddle_pos['y'] + direction_vector['y']
            }

            # check if computed new position in not inside goal area
            # check if computed new position in inside board limits
            if utils.is_inside_goal_area_paddle(new_paddle_pos, current_state) is False and \
                    utils.is_out_of_boundaries_paddle(new_paddle_pos, current_state) is None:
                # Middle line
                if self.my_goal == "right":
                    if new_paddle_pos['x'] > current_state['board_shape'][
                            1] / 2 + current_state['paddle_radius']:
                        self.my_paddle_pos = new_paddle_pos
                else:  # left
                    if new_paddle_pos['x'] < current_state['board_shape'][
                            1] / 2 - current_state['paddle_radius']:
                        self.my_paddle_pos = new_paddle_pos
        return self.my_paddle_pos
Ejemplo n.º 14
0
    def next_move(self, current_state):
        """ Function that computes the next move of your paddle

        Implement your algorithm here. This will be the only function
        used by the GameCore. Be aware of abiding all the game rules.

        Returns:
            dict: coordinates of next position of your paddle.
        """
        # update my paddle pos
        # I need to do this because GameCore moves my paddle randomly
        self.my_paddle_pos = current_state['paddle1_pos'] if self.my_goal == 'left' \
                                                              else current_state['paddle2_pos']

        # estimate puck path
        path = estimate_path(current_state, self.future_size)

        # computing both goal centers
        self.my_goal_center = {
            'x':
            0 if self.my_goal == 'left' else current_state['board_shape'][1],
            'y': current_state['board_shape'][0] / 2
        }
        self.opponent_goal_center = {
            'x':
            0 if self.my_goal == 'right' else current_state['board_shape'][1],
            'y': current_state['board_shape'][0] / 2
        }

        # find if puck path is inside my interest area
        roi_radius = current_state['board_shape'][0] * current_state[
            'goal_size'] * 1.2

        if self.my_goal == 'left':
            if current_state['goals']['left'] > current_state['goals']['right']:
                roi_radius = current_state['board_shape'][0] * current_state[
                    'goal_size']
        else:
            if current_state['goals']['left'] < current_state['goals']['right']:
                roi_radius = current_state['board_shape'][0] * current_state[
                    'goal_size']

        pt_in_roi = None
        for p in path:
            if utils.distance_between_points(p[0],
                                             self.my_goal_center) < roi_radius:
                pt_in_roi = p
                break

        # can move and puck behind me
        if pt_in_roi and is_puck_behind(current_state, self.my_goal):
            if self.my_goal == 'left':
                if current_state['puck_pos'][
                        'y'] < current_state['board_shape'][0] / 2:
                    target_pos = {
                        'x': 0,
                        'y':
                        self.my_goal_center['y'] - current_state['goal_size']
                    }
                else:
                    target_pos = {
                        'x': 0,
                        'y':
                        self.my_goal_center['y'] + current_state['goal_size']
                    }
            else:
                if current_state['puck_pos'][
                        'y'] < current_state['board_shape'][0] / 2:
                    target_pos = {
                        'x': current_state['board_shape'][1],
                        'y':
                        self.my_goal_center['y'] - current_state['goal_size']
                    }
                else:
                    target_pos = {
                        'x': current_state['board_shape'][1],
                        'y':
                        self.my_goal_center['y'] + current_state['goal_size']
                    }

            # move to target
            if target_pos != self.my_paddle_pos:
                direction_vector = {
                    'x': target_pos['x'] - self.my_paddle_pos['x'],
                    'y': target_pos['y'] - self.my_paddle_pos['y']
                }
                direction_vector = {
                    k: v / utils.vector_l2norm(direction_vector)
                    for k, v in direction_vector.items()
                }

                movement_dist = min(
                    current_state['paddle_max_speed'] *
                    current_state['delta_t'],
                    utils.distance_between_points(target_pos,
                                                  self.my_paddle_pos))
                direction_vector = {
                    k: v * movement_dist
                    for k, v in direction_vector.items()
                }
                new_paddle_pos = {
                    'x': self.my_paddle_pos['x'] + direction_vector['x'],
                    'y': self.my_paddle_pos['y'] + direction_vector['y']
                }

                # check if computed new position in not inside goal area
                # check if computed new position in inside board limits
                if utils.is_inside_goal_area_paddle(new_paddle_pos, current_state) is False and \
                     utils.is_out_of_boundaries_paddle(new_paddle_pos, current_state) is None:
                    self.my_paddle_pos = new_paddle_pos

        # can move and puck on my side
        elif pt_in_roi and search(current_state, self.my_goal):
            #########################################################
            #########################################################
            #Left Side
            #########################################################
            #########################################################
            if self.my_goal == 'left':
                # estimate an aiming position
                # shoot center
                if is_enemy_high(current_state, self.my_goal) or is_enemy_low(
                        current_state, self.my_goal):
                    if is_enemy_high(current_state, self.my_goal):
                        target_point_y = self.opponent_goal_center[
                            'y'] - current_state['board_shape'][0] / 16 * 3
                    else:
                        target_point_y = self.opponent_goal_center[
                            'y'] + current_state['board_shape'][0] / 16 * 3
                    self.posN = {
                        'x': self.opponent_goal_center['x'],
                        'y': target_point_y
                    }
                    target_pos = utils.aim(pt_in_roi[0], pt_in_roi[1],
                                           self.posN,
                                           current_state['puck_radius'],
                                           current_state['paddle_radius'])

                # shoot high
                elif current_state['paddle2_pos'][
                        'y'] < current_state['board_shape'][0] / 2:
                    posy = current_state['board_shape'][0] + (
                        current_state['board_shape'][0] -
                        current_state['puck_pos']['y'])
                    m = ((self.opponent_goal_center['y'] +
                          current_state['board_shape'][0] / 16 * 3) -
                         posy) / (self.opponent_goal_center['x'] -
                                  current_state['puck_pos']['x'])
                    b = posy - (m * current_state['puck_pos']['x'])
                    target_point_x = ((current_state['board_shape'][0] -
                                       current_state['puck_radius']) - b) / m
                    self.posN = {
                        'x': target_point_x,
                        'y': current_state['board_shape'][0]
                    }
                    target_pos = utils.aim(pt_in_roi[0], pt_in_roi[1],
                                           self.posN,
                                           current_state['puck_radius'],
                                           current_state['paddle_radius'])

                # shoot low
                else:
                    posy = current_state['puck_pos']['y'] * -1
                    m = ((self.opponent_goal_center['y'] -
                          current_state['board_shape'][0] / 16 * 3) -
                         posy) / (self.opponent_goal_center['x'] -
                                  current_state['puck_pos']['x'])
                    b = posy - (m * current_state['puck_pos']['x'])
                    target_point_x = (current_state['puck_radius'] - b) / m
                    self.posN = {'x': target_point_x, 'y': 0}
                    target_pos = utils.aim(pt_in_roi[0], pt_in_roi[1],
                                           self.posN,
                                           current_state['puck_radius'],
                                           current_state['paddle_radius'])

                # move to target
                if target_pos != self.my_paddle_pos:
                    direction_vector = {
                        'x': target_pos['x'] - self.my_paddle_pos['x'],
                        'y': target_pos['y'] - self.my_paddle_pos['y']
                    }
                    direction_vector = {
                        k: v / utils.vector_l2norm(direction_vector)
                        for k, v in direction_vector.items()
                    }

                    movement_dist = min(
                        current_state['paddle_max_speed'] *
                        current_state['delta_t'],
                        utils.distance_between_points(target_pos,
                                                      self.my_paddle_pos))
                    direction_vector = {
                        k: v * movement_dist
                        for k, v in direction_vector.items()
                    }
                    new_paddle_pos = {
                        'x': self.my_paddle_pos['x'] + direction_vector['x'],
                        'y': self.my_paddle_pos['y'] + direction_vector['y']
                    }

                    # check if computed new position in not inside goal area
                    # check if computed new position in inside board limits
                    if utils.is_inside_goal_area_paddle(new_paddle_pos, current_state) is False and \
                         utils.is_out_of_boundaries_paddle(new_paddle_pos, current_state) is None:
                        self.my_paddle_pos = new_paddle_pos

            #########################################################
            #########################################################
            #Right Side
            #########################################################
            #########################################################
            else:
                # estimate an aiming position
                # shoot center +/- C
                if is_enemy_high(current_state, self.my_goal) or is_enemy_low(
                        current_state, self.my_goal):
                    if is_enemy_high(current_state, self.my_goal):
                        target_point_y = self.opponent_goal_center[
                            'y'] - current_state['board_shape'][0] / 16 * 3
                    else:
                        target_point_y = self.opponent_goal_center[
                            'y'] + current_state['board_shape'][0] / 16 * 3
                    self.posN = {
                        'x': self.opponent_goal_center['x'],
                        'y': target_point_y
                    }
                    target_pos = utils.aim(pt_in_roi[0], pt_in_roi[1],
                                           self.posN,
                                           current_state['puck_radius'],
                                           current_state['paddle_radius'])

                # shoot high
                elif current_state['paddle1_pos'][
                        'y'] < current_state['board_shape'][0] / 2:
                    posy = current_state['board_shape'][0] + (
                        current_state['board_shape'][0] -
                        current_state['puck_pos']['y'])
                    m = (self.opponent_goal_center['y'] -
                         posy) / (self.opponent_goal_center['x'] -
                                  current_state['puck_pos']['x'])
                    b = posy - (m * current_state['puck_pos']['x'])
                    target_point_x = ((current_state['board_shape'][0] -
                                       current_state['puck_radius']) - b) / m
                    self.posN = {
                        'x': target_point_x,
                        'y': current_state['board_shape'][0]
                    }
                    target_pos = utils.aim(pt_in_roi[0], pt_in_roi[1],
                                           self.posN,
                                           current_state['puck_radius'],
                                           current_state['paddle_radius'])

                # shoot low
                else:
                    posy = current_state['puck_pos']['y'] * -1
                    m = (self.opponent_goal_center['y'] -
                         posy) / (self.opponent_goal_center['x'] -
                                  current_state['puck_pos']['x'])
                    b = posy - (m * current_state['puck_pos']['x'])
                    target_point_x = (current_state['puck_radius'] - b) / m
                    self.posN = {'x': target_point_x, 'y': 0}
                    target_pos = utils.aim(pt_in_roi[0], pt_in_roi[1],
                                           self.posN,
                                           current_state['puck_radius'],
                                           current_state['paddle_radius'])

                # move to target
                if target_pos != self.my_paddle_pos:
                    direction_vector = {
                        'x': target_pos['x'] - self.my_paddle_pos['x'],
                        'y': target_pos['y'] - self.my_paddle_pos['y']
                    }
                    direction_vector = {
                        k: v / utils.vector_l2norm(direction_vector)
                        for k, v in direction_vector.items()
                    }

                    movement_dist = min(
                        current_state['paddle_max_speed'] *
                        current_state['delta_t'],
                        utils.distance_between_points(target_pos,
                                                      self.my_paddle_pos))
                    direction_vector = {
                        k: v * movement_dist
                        for k, v in direction_vector.items()
                    }
                    new_paddle_pos = {
                        'x': self.my_paddle_pos['x'] + direction_vector['x'],
                        'y': self.my_paddle_pos['y'] + direction_vector['y']
                    }

                    # check if computed new position in not inside goal area
                    # check if computed new position in inside board limits
                    if utils.is_inside_goal_area_paddle(new_paddle_pos, current_state) is False and \
                         utils.is_out_of_boundaries_paddle(new_paddle_pos, current_state) is None:
                        self.my_paddle_pos = new_paddle_pos

        # return to center
        else:
            if self.my_goal == 'left':
                target_pos = {
                    'x': current_state['board_shape'][1] / 16 * 3,
                    'y': self.my_goal_center['y']
                }
            else:
                target_pos = {
                    'x':
                    current_state['board_shape'][1] -
                    current_state['board_shape'][1] / 16 * 3,
                    'y':
                    self.my_goal_center['y']
                }

            if target_pos != self.my_paddle_pos:
                direction_vector = {
                    'x': target_pos['x'] - self.my_paddle_pos['x'],
                    'y': target_pos['y'] - self.my_paddle_pos['y']
                }
                direction_vector = {
                    k: v / utils.vector_l2norm(direction_vector)
                    for k, v in direction_vector.items()
                }

                movement_dist = min(
                    current_state['paddle_max_speed'] *
                    current_state['delta_t'],
                    utils.distance_between_points(target_pos,
                                                  self.my_paddle_pos))
                direction_vector = {
                    k: v * movement_dist
                    for k, v in direction_vector.items()
                }
                new_paddle_pos = {
                    'x': self.my_paddle_pos['x'] + direction_vector['x'],
                    'y': self.my_paddle_pos['y'] + direction_vector['y']
                }

                # check if computed new position in not inside goal area
                # check if computed new position in inside board limits
                if utils.is_inside_goal_area_paddle(new_paddle_pos, current_state) is False and \
                     utils.is_out_of_boundaries_paddle(new_paddle_pos, current_state) is None:
                    self.my_paddle_pos = new_paddle_pos

        # return coordinates
        return self.my_paddle_pos
Ejemplo n.º 15
0
 def get_points(self):
     return distance_between_points(self.i_x, self.i_y, self.f_x, self.f_y)
Ejemplo n.º 16
0
    def next_move(self, current_state):
        """ Function that computes the next move of your paddle
        Implement your algorithm here. This will be the only function
        used by the GameCore. Be aware of abiding all the game rules.
        Returns:
            dict: coordinates of next position of your paddle.
        """

        self.elapsed_game_tiks += 1

        # update my paddle pos
        # I need to do this because GameCore moves my paddle randomly
        self.my_paddle_pos = current_state['paddle1_pos'] if self.my_goal == 'left' \
            else current_state['paddle2_pos']
        self.my_opponent_pos = current_state['paddle2_pos'] if self.my_goal == 'left' \
            else current_state['paddle1_pos']

        # estimate puck path
        path = estimate_path(current_state, self.future_size)

        # computing both goal centers
        self.my_goal_center = {
            'x':
            0 if self.my_goal == 'left' else current_state['board_shape'][1],
            'y': current_state['board_shape'][0] / 2
        }

        self.opponent_goal_center = {
            'x':
            0 if self.my_goal == 'right' else current_state['board_shape'][1],
            'y': current_state['board_shape'][0] / 2
        }

        #Assign value to sides of opponent's goal
        self.goal_sideA = {
            'x': self.my_goal_center['x'],
            'y': (self.my_goal_center['y'] - (0.2 * 512))
        }
        self.goal_sideB = {
            'x': self.my_goal_center['x'],
            'y': (self.my_goal_center['y'] + (0.2 * 512))
        }

        # Logs go here:

        # Log the distance of the opponent to its goal:

        lower_extreme = (current_state['board_shape'][0] / 2) - (
            (current_state['board_shape'][0] * current_state['goal_size']) / 2)

        higher_extreme = (current_state['board_shape'][0] / 2) + (
            (current_state['board_shape'][0] * current_state['goal_size']) / 2)

        # Determine if the last shot was to the goal:
        if self.my_goal == 'left':
            if self.puck_last_x > current_state['puck_pos']['x'] and current_state['puck_pos']['y'] > lower_extreme and \
                    current_state['puck_pos']['y'] < higher_extreme and current_state['puck_pos']['x'] < (current_state['board_shape'][1] / 3) and self.puck_crossed == 0:

                self.opponent_shots_to_goal += 1  # Its a shot to our goal
                self.puck_crossed = 1

        else:
            if self.puck_last_x < current_state['puck_pos']['x'] and current_state['puck_pos']['y'] > lower_extreme and \
                    current_state['puck_pos']['y'] < higher_extreme and current_state['puck_pos']['x'] > ((current_state['board_shape'][1] / 3) * 2) and self.puck_crossed == 0:

                self.opponent_shots_to_goal += 1  # Its a shot to our goal
                self.puck_crossed = 1

        if self.my_goal == 'left':
            opponent_distance_from_goal = current_state['board_shape'][
                1] - self.my_opponent_pos['x']

            if self.puck_last_x < current_state['puck_pos'][
                    'x'] and current_state['puck_pos']['x'] > (
                        current_state['board_shape'][1] / 2):
                self.puck_crossed = 0

        else:
            opponent_distance_from_goal = self.my_opponent_pos['x']

            if self.puck_last_x > current_state['puck_pos'][
                    'x'] and current_state['puck_pos']['x'] < (
                        current_state['board_shape'][1] / 2):
                self.puck_crossed = 0

        self.opponent_distances_from_goal.append(opponent_distance_from_goal)

        self.puck_last_x = current_state['puck_pos']['x']

        # Determine if a shot was aiming to the goal:

        # Classify based on logs:
        self.classify(current_state, self.future_size)

        self.puck_last_y = current_state['puck_pos']['y']

        # Attack:

        final_pos = self.attack(current_state)

        # find if puck path is inside my interest area
        roi_radius = current_state['board_shape'][0] * current_state[
            'goal_size'] * self.my_goal_offset
        pt_in_roi = None
        for p in path:
            if utils.distance_between_points(
                    p[0],
                    self.my_goal_center) < roi_radius or self.quick_off == 1:
                pt_in_roi = p
                break

        if pt_in_roi:
            # estimate an aiming position

            # Attack:
            if self.my_current_mode == 0:

                self.my_goal_offset = 1.3

                target_pos = utils.aim(pt_in_roi[0], pt_in_roi[1], final_pos,
                                       current_state['puck_radius'],
                                       current_state['paddle_radius'])

            # Defend:
            elif self.my_current_mode == 1:
                if (self.my_goal == "left"):
                    position = ((current_state['board_shape'][1] / 6) * 4)

                    if current_state['puck_pos']['x'] > (
                        (current_state['board_shape'][1] / 6) * 4):

                        target_pos = self.defend(current_state)

                    else:

                        target_pos = utils.aim(pt_in_roi[0], pt_in_roi[1],
                                               final_pos,
                                               current_state['puck_radius'],
                                               current_state['paddle_radius'])
                else:
                    position = ((current_state['board_shape'][1] / 6) * 2)
                    if (current_state['puck_pos']['x'] < position):
                        target_pos = self.defend(current_state)

                    else:
                        target_pos = utils.aim(pt_in_roi[0], pt_in_roi[1],
                                               final_pos,
                                               current_state['puck_radius'],
                                               current_state['paddle_radius'])
            # Evade:
            else:
                target_pos = self.evade(current_state)

            # move to target position, taking into account the max. paddle speed
            # print(target_pos)

            if target_pos != self.my_paddle_pos:
                direction_vector = {
                    'x': target_pos['x'] - self.my_paddle_pos['x'],
                    'y': target_pos['y'] - self.my_paddle_pos['y']
                }
                direction_vector = {
                    k: v / utils.vector_l2norm(direction_vector)
                    for k, v in direction_vector.items()
                }

                movement_dist = min(
                    current_state['paddle_max_speed'] *
                    current_state['delta_t'],
                    utils.distance_between_points(target_pos,
                                                  self.my_paddle_pos))
                direction_vector = {
                    k: v * movement_dist
                    for k, v in direction_vector.items()
                }
                new_paddle_pos = {
                    'x': self.my_paddle_pos['x'] + direction_vector['x'],
                    'y': self.my_paddle_pos['y'] + direction_vector['y']
                }

                # check if computed new position in not inside goal area
                # check if computed new position in inside board limits
                if utils.is_inside_goal_area_paddle(new_paddle_pos, current_state) is False and \
                        utils.is_out_of_boundaries_paddle(new_paddle_pos, current_state) is None:
                    self.my_paddle_pos = new_paddle_pos

        return self.my_paddle_pos
Ejemplo n.º 17
0
    def generate(self):
        grid = [list(['#'] * self.width) for _ in range(self.height)]
        grids = []
        # random.seed(2)

        c = Vector(self.width * 2 // 4, self.height + 3)
        size = 2
        for idx, door in enumerate([
                Vector(c.x - size, c.y - size - 5),
                Vector(c.x + size, c.y - size - 5),
                Vector(c.x + 0, c.y - size // 2 - 5),
                Vector(c.x - size, c.y - size // 2 - 5),
        ]):
            grid = [list(['#'] * self.width) for _ in range(self.height)]
            holes = [door]
            for y in range(self.height):
                for x in range(self.width):
                    if distance_between_points((c.x, c.y), (x, y)) <= size:
                        grid[y][x] = '.'
                    elif random.random() < 0.2:
                        grid[y][x] = '.'
                        holes.append(Vector(x, y))

            if idx % 2 == 0:
                holes = sorted(holes,
                               key=lambda h: (distance_between_points(
                                   (h.x, h.y), (door.x, door.y)), h.y, 0))
            else:
                holes = sorted(holes,
                               key=lambda h: (distance_between_points(
                                   (h.x, h.y), (door.x, door.y)), h.y, -h.x))
            new_holes = [holes[0]]
            for hole in holes[1:]:
                if hole.y < new_holes[-1].y:
                    new_holes.append(hole)
            holes = new_holes

            grid[door.y][door.x] = '+'
            for hole in holes:
                x, y = hole
                grid[y][x] = '+'

            path = []
            for prev, curr in zip(holes, holes[1:]):
                x, y = prev
                while True:
                    dx, dy = get_direction((x, y), (curr.x, curr.y))
                    if grid[y + dy][x + dx] == '+':
                        break

                    if dx != 0 and dy != 0:  # diagonal
                        if random.random() < 0.5:
                            dx = 0
                        else:
                            dy = 0
                    grid[y + dy][x + dx] = '.'
                    path.append(Vector(x + dx, y + dy))
                    x += dx
                    y += dy

            for y in range(self.height):
                for x in range(self.width):
                    if grid[y][x] == '.':
                        if Vector(x, y) not in path:
                            if (x > c.x + size
                                    or x < c.x - size) or (y > c.y + size
                                                           or y < c.y - size):
                                # not in room
                                grid[y][x] = '#'
                    elif grid[y][x] == '+':
                        grid[y][x] = '.'
            grids.append(grid)

        for y in range(self.height):
            for x in range(self.width):
                if '.' in [g[y][x] for g in grids]:
                    grid[y][x] = '.'

        self.zone = Zone(grid, [], [], self._temperature)
Ejemplo n.º 18
0
    def next_move(self, current_state):
        """ Function that computes the next move of your paddle

        Implement your algorithm here. This will be the only function
        used by the GameCore. Be aware of abiding all the game rules.

        Returns:
            dict: coordinates of next position of your paddle.
        """

        # update my paddle pos
        # I need to do this because GameCore moves my paddle randomly
        self.my_paddle_pos = current_state['paddle1_pos'] if self.my_goal == 'left' \
                                                              else current_state['paddle2_pos']

        #Save the position of the enemy paddle
        enemy_paddle_pos = current_state['paddle1_pos'] if self.my_goal == 'right' \
                                                              else current_state['paddle2_pos']

        # estimate puck path
        path = estimate_path(current_state, self.future_size)

        # computing both goal centers
        self.my_goal_center = {
            'x':
            0 if self.my_goal == 'left' else current_state['board_shape'][1],
            'y': current_state['board_shape'][0] / 2
        }
        self.opponent_goal_center = {
            'x':
            0 if self.my_goal == 'right' else current_state['board_shape'][1],
            'y': current_state['board_shape'][0] / 2
        }

        y1 = random.uniform(250, 370)
        y2 = random.uniform(140, 250)
        #Targets where our paddle will aim
        attackTarget1 = {
            'x':
            current_state['board_shape'][1] * 0.7 if self.my_goal == 'left'
            else current_state['board_shape'][1] * 0.3,
            'y':
            512
        }
        attackTarget2 = {
            'x':
            current_state['board_shape'][1] * 0.7 if self.my_goal == 'left'
            else current_state['board_shape'][1] * 0.3,
            'y':
            0
        }
        """
        #Shinobi mode
        #new_paddle_pos = {'x': self.my_paddle_pos['x'], 'y': current_state['puck_pos']['y']}
        new_paddle_pos = self.my_paddle_pos
        new_paddle_pos['y'] = current_state['puck_pos']['y']
        if utils.is_inside_goal_area_paddle(new_paddle_pos, current_state) is False and \
            utils.is_out_of_boundaries_paddle(new_paddle_pos, current_state) is None and \
            is_inside_area51(new_paddle_pos ,current_state) is False and \
            utils.distance_between_points(current_state['puck_pos'], self.my_paddle_pos) > 50:
            self.my_paddle_pos = new_paddle_pos 
        """

        defenseMode = False
        # find if puck path is inside my interest area

        pt_in_roi = None
        for p in path:

            if self.my_goal == 'left':
                if current_state['puck_pos']['x'] <= 238.75:
                    roi_radius = current_state['board_shape'][
                        1] * current_state['goal_size']
                    defenseMode = True
                else:
                    roi_radius = current_state['board_shape'][
                        1] * current_state['goal_size'] * 2
            else:
                if current_state['puck_pos']['x'] >= 716.25:
                    roi_radius = current_state['board_shape'][
                        1] * current_state['goal_size']
                    defenseMode = True
                else:
                    roi_radius = current_state['board_shape'][
                        1] * current_state['goal_size'] * 2

            if utils.distance_between_points(p[0],
                                             self.my_goal_center) < roi_radius:
                pt_in_roi = p
                break

        if defenseMode:
            # estimate an aiming position
            target_pos = utils.aim(pt_in_roi[0], pt_in_roi[1],
                                   self.opponent_goal_center,
                                   current_state['puck_radius'],
                                   current_state['paddle_radius'])

            # move to target position, taking into account the max. paddle speed
            if target_pos != self.my_paddle_pos:
                direction_vector = {
                    'x': target_pos['x'] - self.my_paddle_pos['x'],
                    'y': target_pos['y'] - self.my_paddle_pos['y']
                }
                direction_vector = {
                    k: v / utils.vector_l2norm(direction_vector)
                    for k, v in direction_vector.items()
                }

                movement_dist = min(
                    current_state['paddle_max_speed'] *
                    current_state['delta_t'],
                    utils.distance_between_points(target_pos,
                                                  self.my_paddle_pos))
                direction_vector = {
                    k: v * movement_dist
                    for k, v in direction_vector.items()
                }
                new_paddle_pos = {
                    'x': self.my_paddle_pos['x'] + direction_vector['x'],
                    'y': self.my_paddle_pos['y'] + direction_vector['y']
                }

                # check if computed new position in not inside goal area
                # check if computed new position in inside board limits
                if utils.is_inside_goal_area_paddle(new_paddle_pos, current_state) is False and \
                     utils.is_out_of_boundaries_paddle(new_paddle_pos, current_state) is None:
                    self.my_paddle_pos = new_paddle_pos

                defenseMode = False

        #If the puck is on the enemy side, our paddle will return to the goal area
        elif (current_state['puck_pos']['x'] > 477.5 and self.my_goal
              == 'left') or (current_state['puck_pos']['x'] < 477.5
                             and self.my_goal == 'right'):

            #The target position is the center of the goal area
            if self.my_goal == 'left':
                target_pos = {'x': 116.2, 'y': 256}
            else:
                target_pos = {'x': 838.8, 'y': 256}

            # move to target position
            if target_pos != self.my_paddle_pos:
                direction_vector = {
                    'x': target_pos['x'] - self.my_paddle_pos['x'],
                    'y': target_pos['y'] - self.my_paddle_pos['y']
                }
                direction_vector = {
                    k: v / utils.vector_l2norm(direction_vector)
                    for k, v in direction_vector.items()
                }

                movement_dist = min(
                    current_state['paddle_max_speed'] *
                    current_state['delta_t'],
                    utils.distance_between_points(target_pos,
                                                  self.my_paddle_pos))
                direction_vector = {
                    k: v * movement_dist
                    for k, v in direction_vector.items()
                }
                new_paddle_pos = {
                    'x': self.my_paddle_pos['x'] + direction_vector['x'],
                    'y': self.my_paddle_pos['y'] + direction_vector['y']
                }

                # check if computed new position in not inside goal area
                # check if computed new position in inside board limits
                # check if computed new position is inside area51
                if utils.is_inside_goal_area_paddle(new_paddle_pos, current_state) is False and \
                    utils.is_out_of_boundaries_paddle(new_paddle_pos, current_state) is None:
                    self.my_paddle_pos = new_paddle_pos

        #If the puck is on our side and the puck is in front of our paddle, the paddle will try to hit it
        elif ((current_state['puck_pos']['x'] >= self.my_paddle_pos['x'] and self.my_goal == 'left') or \
            (current_state['puck_pos']['x'] <= self.my_paddle_pos['x'] and self.my_goal == 'right')):

            if pt_in_roi:

                if (enemy_paddle_pos['y'] > 256):
                    target_pos = utils.aim(pt_in_roi[0], pt_in_roi[1],
                                           attackTarget2,
                                           current_state['puck_radius'],
                                           current_state['paddle_radius'])
                else:
                    target_pos = utils.aim(pt_in_roi[0], pt_in_roi[1],
                                           attackTarget1,
                                           current_state['puck_radius'],
                                           current_state['paddle_radius'])

                # move to target position, taking into account the max. paddle speed
                if target_pos != self.my_paddle_pos:
                    direction_vector = {
                        'x': target_pos['x'] - self.my_paddle_pos['x'],
                        'y': target_pos['y'] - self.my_paddle_pos['y']
                    }
                    direction_vector = {
                        k: v / utils.vector_l2norm(direction_vector)
                        for k, v in direction_vector.items()
                    }

                    movement_dist = min(
                        current_state['paddle_max_speed'] *
                        current_state['delta_t'],
                        utils.distance_between_points(target_pos,
                                                      self.my_paddle_pos))
                    direction_vector = {
                        k: v * movement_dist
                        for k, v in direction_vector.items()
                    }
                    new_paddle_pos = {
                        'x': self.my_paddle_pos['x'] + direction_vector['x'],
                        'y': self.my_paddle_pos['y'] + direction_vector['y']
                    }

                    # check if computed new position in not inside goal area
                    # check if computed new position in inside board limits
                    # check if computed new position is inside area51
                    if utils.is_inside_goal_area_paddle(new_paddle_pos, current_state) is False and \
                        utils.is_out_of_boundaries_paddle(new_paddle_pos, current_state) is None:
                        self.my_paddle_pos = new_paddle_pos

        #If the puck is behind the paddle, the paddle avoids the puck to avoid comitting an autogoal
        elif current_state['puck_pos']['x'] < self.my_paddle_pos['x']:

            # move to target position, taking into account the max. paddle speed

            direction_vector = {
                'x': current_state['puck_pos']['x'] - self.my_paddle_pos['x'],
                'y': current_state['puck_pos']['y'] - self.my_paddle_pos['y']
            }
            direction_vector = {
                k: v / utils.vector_l2norm(direction_vector)
                for k, v in direction_vector.items()
            }

            movement_dist = min(
                current_state['paddle_max_speed'] * current_state['delta_t'],
                utils.distance_between_points(current_state['puck_pos'],
                                              self.my_paddle_pos))

            direction_vector = {
                k: v * -movement_dist
                for k, v in direction_vector.items()
            }
            new_paddle_pos = {
                'x': self.my_paddle_pos['x'] + direction_vector['x'],
                'y': self.my_paddle_pos['y'] + direction_vector['y']
            }

            new_paddle_pos = utils.rectify_circle_out_of_bounds(
                new_paddle_pos, self.my_goal, current_state)

            # check if computed new position in not inside goal area
            # check if computed new position in inside board limits
            if utils.is_inside_goal_area_paddle(new_paddle_pos, current_state) is False and \
                utils.is_out_of_boundaries_paddle(new_paddle_pos, current_state) is None:
                self.my_paddle_pos = new_paddle_pos

        #Return the new position for the paddle
        return self.my_paddle_pos
Ejemplo n.º 19
0
    def next_move(self, current_state):

        target_pos = {'x': 0, 'y': 0}

        if self.my_goal == 'left':
            self.my_paddle_pos = current_state['paddle1_pos']
        else:
            self.my_paddle_pos = current_state['paddle2_pos']

        path = estimate_path(current_state, 3)

        if len(path) > 2:
            direc = getDirection(path[0][0], path[2][0])
        else:
            direc = {'x': 0, 'y': 0}

        if get_side_puck(self.my_goal, current_state['puck_pos']
                         ) and direc['x'] == 0:  # Starts in my side

            x = current_state['puck_pos']['x'] - 30 * self.mul
            y = 256
            target_pos = {'x': x, 'y': y}

        elif (self.my_goal == 'left'
              and direc['x'] < 0) or (self.my_goal == 'right'
                                      and direc['x'] > 0):
            x = self.pos_x
            m, c = getEqu(path[0][0], path[2][0])

            y = x * m + c

            if y < 0 and y > -450:
                y *= -1
            elif y > 480 and y < 480 * 2:
                y = 512 - (y - 512)

            if (current_state['paddle2_pos']['y'] <
                    256) and y > 47 and y < 479:
                y -= 15
            elif (current_state['paddle2_pos']['y'] >
                  256) and y < 479 and y > 47:
                y += 15

            if y < 32: y = 32
            elif y >= 480: y = 479

            target_pos = {'x': x, 'y': y}

        else:
            x = self.pos_x
            if current_state['puck_pos']['y'] > 256:
                y = 280
            elif current_state['puck_pos']['y'] < 256:
                y = 232
            elif current_state['puck_pos']['y'] == 256:
                y = 256
            target_pos = {'x': x, 'y': y}

        direction_vector = {
            'x': target_pos['x'] - self.my_paddle_pos['x'],
            'y': target_pos['y'] - self.my_paddle_pos['y']
        }
        direction_vector = {
            k: v / utils.vector_l2norm(direction_vector)
            for k, v in direction_vector.items()
        }

        movement_dist = min(
            current_state['paddle_max_speed'] * current_state['delta_t'],
            utils.distance_between_points(target_pos, self.my_paddle_pos))
        direction_vector = {
            k: v * movement_dist
            for k, v in direction_vector.items()
        }

        self.my_paddle_pos = {
            'x': self.my_paddle_pos['x'] + direction_vector['x'],
            'y': self.my_paddle_pos['y'] + direction_vector['y']
        }

        self.my_goal_center = {
            'x':
            0 if self.my_goal == 'left' else current_state['board_shape'][1],
            'y': current_state['board_shape'][0] / 2
        }

        return self.my_paddle_pos
Ejemplo n.º 20
0
    def next_move(self, current_state):
        """ Function that computes the next move of your paddle
        Implement your algorithm here. This will be the only function
        used by the GameCore. Be aware of abiding all the game rules.
        Returns:
            dict: coordinates of next position of your paddle.
        """
       
        
       
        # Insert classifier here:
        self.elapsed_game_tiks += 1

        #print(self.elapsed_game_tiks)
        self.classify(current_state, self.future_size)

        # update my paddle pos
        # I need to do this because GameCore moves my paddle randomly
        self.my_paddle_pos = current_state['paddle1_pos'] if self.my_goal == 'left' \
            else current_state['paddle2_pos']
        self.my_opponent_pos = current_state['paddle2_pos'] if self.my_goal == 'left' \
            else current_state['paddle1_pos']
        

        # estimate puck path 
        path = estimate_path(current_state, self.future_size)

         # computing both goal centers
        self.my_goal_center = {'x': 0 if self.my_goal == 'left' else current_state['board_shape'][1],
                               'y': current_state['board_shape'][0] / 2}

        self.opponent_goal_center = {'x': 0 if self.my_goal == 'right' else current_state['board_shape'][1],
                                     'y': current_state['board_shape'][0] / 2}

        self.goal_sideA= {'x': self.my_goal_center['x'], 'y': (self.my_goal_center['y']-(0.2*512))}
        self.goal_sideB= {'x': self.my_goal_center['x'], 'y': (self.my_goal_center['y']+(0.2*512))}

        final_pos = self.attack(current_state)
       

        # find if puck path is inside my interest area
        roi_radius = current_state['board_shape'][0] * current_state['goal_size'] * self.my_goal_offset
        pt_in_roi = None
        for p in path:
            if utils.distance_between_points(p[0], self.my_goal_center) < roi_radius:
                pt_in_roi = p
                break

        if pt_in_roi:
            # print(final_pos)
            # estimate an aiming position

            if self.my_current_mode == 0:
                target_pos = utils.aim(pt_in_roi[0], pt_in_roi[1],
                                    final_pos, current_state['puck_radius'],
                                    current_state['paddle_radius'])

            # Defend:
            elif self.my_current_mode == 1:

                if(self.my_goal=="left"):
                    position= ((current_state['board_shape'][1] / 6) * 4)

                    if (current_state['puck_pos']['x'] > position):
                        target_pos = self.defend(current_state)

                    else:
                        target_pos = utils.aim(pt_in_roi[0], pt_in_roi[1],
                                            final_pos, current_state['puck_radius'],
                                            current_state['paddle_radius'])

                else:
                    position= ((current_state['board_shape'][1] / 6) * 2)
                    if (current_state['puck_pos']['x'] < position):
                        target_pos = self.defend(current_state)

                    else:
                        target_pos = utils.aim(pt_in_roi[0], pt_in_roi[1],
                                            final_pos, current_state['puck_radius'],
                                            current_state['paddle_radius'])
            # Evade:
            else:
                target_pos = self.evade(current_state)

            # move to target position, taking into account the max. paddle speed
            if target_pos != self.my_paddle_pos:
                direction_vector = {'x': target_pos['x'] - self.my_paddle_pos['x'],
                                    'y': target_pos['y'] - self.my_paddle_pos['y']}
                direction_vector = {k: v / utils.vector_l2norm(direction_vector)
                                    for k, v in direction_vector.items()}

                movement_dist = min(current_state['paddle_max_speed'] * current_state['delta_t'],
                                    utils.distance_between_points(target_pos, self.my_paddle_pos))
                direction_vector = {k: v * movement_dist
                                    for k, v in direction_vector.items()}
                new_paddle_pos = {'x': self.my_paddle_pos['x'] + direction_vector['x'],
                                  'y': self.my_paddle_pos['y'] + direction_vector['y']}

                # check if computed new position in not inside goal area
                # check if computed new position in inside board limits
                if utils.is_inside_goal_area_paddle(new_paddle_pos, current_state) is False and \
                        utils.is_out_of_boundaries_paddle(new_paddle_pos, current_state) is None:
                    self.my_paddle_pos = new_paddle_pos
        # time.sleep(2)
        # return {'x': -12, 'y': -6543}
        return self.my_paddle_pos
Ejemplo n.º 21
0
def get_faces_base_on_eye_pairs(system_data, image, region_info,
                                region_skin_image, eye_pairs, m, n, tempX,
                                tempY):
    faceBorder = find_longest_border(region_skin_image, m, n, tempX, tempY)
    downVector = [0, 1]
    # count = 0
    # minDist = 100000000
    # minImage = None

    result = []

    for eye1, eye2 in eye_pairs:
        centroid1 = [(eye1[1] + eye1[3]) / 2, (eye1[0] + eye1[2]) / 2]
        centroid2 = [(eye2[1] + eye2[3]) / 2, (eye2[0] + eye2[2]) / 2]
        pivot = [(centroid1[0] + centroid2[0]) / 2,
                 (centroid1[1] + centroid2[1]) / 2]

        direction = get_face_direction(region_skin_image, m, n, tempX, tempY,
                                       faceBorder, centroid1, centroid2, pivot)
        angleToRotate = ut.find_angle_between_two_vectors(
            downVector, direction)
        # print(count + 1, angleToRotate)
        # print(direction)
        if (direction[0] > 0):
            angleToRotate = -angleToRotate

        #adjust pivot point to work with original image
        pivot[0] = pivot[0] + region_info[1]
        pivot[1] = pivot[1] + region_info[0]
        pivot = tuple(pivot)

        tempImage = image.copy()
        # cv2.rectangle(tempImage, (eye1[1], eye1[0]), (eye1[3], eye1[2]), (0, 255, 0), 1)
        # cv2.rectangle(tempImage, (eye2[1], eye2[0]), (eye2[3], eye2[2]), (0, 255, 0), 1)
        mat = cv2.getRotationMatrix2D(pivot, angleToRotate, 1.0)
        tempImage = cv2.warpAffine(tempImage, mat, tempImage.shape[1::-1])

        tempImage = split_to_get_face(
            tempImage, pivot, ut.distance_between_points(centroid1, centroid2))
        if (tempImage is None):
            continue
        check, dist = pca.detect_face(
            tempImage,
            system_data.mean,
            system_data.eigenfaces,
            dist_threshold=system_data.detectionThreshold)

        print("DETECTION DIST", dist)
        # cv2.imshow("value", tempImage)
        # cv2.waitKey(0)
        # cv2.destroyWindow("value")

        if (check):
            # if (dist < minDist):
            # 	minDist = dist
            # 	minImage = tempImage

            result.append([dist, tempImage])

    # return minDist, minImage
    return result
Ejemplo n.º 22
0
    def next_move(self, current_state):
        """ Function that computes the next move of your paddle

        Implement your algorithm here. This will be the only function
        used by the GameCore. Be aware of abiding all the game rules.

        Returns:
            dict: coordinates of next position of your paddle.
        """

        # update my paddle pos
        # I need to do this because GameCore moves my paddle randomly
        self.my_paddle_pos = current_state['paddle1_pos'] if self.my_goal == 'left' \
                                                              else current_state['paddle2_pos']

        self.my_goal_center = {'x': 0 if self.my_goal == 'left' else current_state['board_shape'][1],
                               'y': current_state['board_shape'][0]/2}
                               
        enemy_goal_center = {'x': 0 if self.my_goal == 'right' else current_state['board_shape'][1],
                               'y': current_state['board_shape'][0]/2}

        path = []
        path = estimate_path(current_state, self.future_size)
        target = self.initial_pos

        roi_radius = current_state['board_shape'][0] * current_state['goal_size'] * 1.2
        pt_in_roi = None
        for p in path:
            if utils.distance_between_points(p[0], self.my_goal_center) < roi_radius:
                pt_in_roi = p
                break
        
        if self.my_goal == 'left':
            #enemy goals grater than us
            if current_state['goals']['right'] >= current_state['goals']['left']:
                self.new_initial_pos = {'x': self.initial_pos['x'] + 50, 'y':self.initial_pos['y']}
            else:
                self.new_initial_pos = self.initial_pos
            #if the puck is in the right field, the paddle defends
            if is_puck_right(current_state):
                # get the target position for the paddle
                target = self.new_initial_pos

            else: 
                if  current_state['puck_speed']['x'] <= 50:
                    if is_puck_behind(self.my_paddle_pos, current_state) == False:
                        my_enemy_puck_pos_quarter = enemy_puck_pos_quarter(self.my_goal, current_state)
                        #if the enemy is at the center of the field vertically
                        if my_enemy_puck_pos_quarter == 2:
                            target_shot = get_target_shot(enemy_goal_center, self.my_goal_center, current_state)
                            #print(target_shot)
                            if pt_in_roi:
                                target = utils.aim(pt_in_roi[0], pt_in_roi[1], {'x': target_shot['x'], 'y': target_shot['y']}, current_state['puck_radius'],
                                                    current_state['paddle_radius']) 
                            else:
                                target = utils.aim(current_state['puck_pos'], current_state['puck_speed'], {'x': target_shot['x'], 'y': target_shot['y']}, current_state['puck_radius'],
                                                    current_state['paddle_radius'])
                        else:
                            target_shot = get_target_shot_quarter(enemy_goal_center, self.my_goal, current_state)
                            if pt_in_roi:
                                target = utils.aim(pt_in_roi[0], pt_in_roi[1], {'x': target_shot['x'], 'y': target_shot['y']}, current_state['puck_radius'],
                                                    current_state['paddle_radius']) 
                            else:
                                target = utils.aim(current_state['puck_pos'], current_state['puck_speed'], {'x': target_shot['x'], 'y': target_shot['y']}, current_state['puck_radius'],
                                                    current_state['paddle_radius']) 
                    else:
                        defense_pos = get_defense_pos(self.my_paddle_pos, current_state)
                        target = defense_pos


                else:
                    target = self.new_initial_pos
            
            # 
            if target['x'] < (current_state['board_shape'][1] / 2):
                self.my_paddle_pos = next_move_paddle(self.my_paddle_pos, target, current_state)

        else:
            #enemy goals grater than us
            if current_state['goals']['left'] >= current_state['goals']['right']:
                self.new_initial_pos = {'x': self.initial_pos['x'] - 50, 'y':self.initial_pos['y']}
            else:
                self.new_initial_pos = self.initial_pos
            if is_puck_left(current_state):
                # get the target position for the paddle
                target = self.new_initial_pos

            else: 
                if  current_state['puck_speed']['x'] >= (-50):
                    if is_puck_behind_right(self.my_paddle_pos, current_state) == False:
                        my_enemy_puck_pos_quarter = enemy_puck_pos_quarter(self.my_goal, current_state)
                        #if the enemy is at the center of the field vertically
                        if my_enemy_puck_pos_quarter == 2:
                            target_shot = get_target_shot(enemy_goal_center, self.my_goal_center, current_state)
                            #print(target_shot)
                            if pt_in_roi:
                                target = utils.aim(pt_in_roi[0], pt_in_roi[1], {'x': target_shot['x'], 'y': target_shot['y']}, current_state['puck_radius'],
                                                    current_state['paddle_radius']) 
                            else:
                                target = utils.aim(current_state['puck_pos'], current_state['puck_speed'], {'x': target_shot['x'], 'y': target_shot['y']}, current_state['puck_radius'],
                                                current_state['paddle_radius'])
                        else:
                            target_shot = get_target_shot_quarter(enemy_goal_center, self.my_goal, current_state)
                            if pt_in_roi:
                                target = utils.aim(pt_in_roi[0], pt_in_roi[1], {'x': target_shot['x'], 'y': target_shot['y']}, current_state['puck_radius'],
                                                    current_state['paddle_radius']) 
                            else:
                                target = utils.aim(current_state['puck_pos'], current_state['puck_speed'], {'x': target_shot['x'], 'y': target_shot['y']}, current_state['puck_radius'],
                                                    current_state['paddle_radius'])
                    else:
                        defense_pos = get_defense_pos_right(self.my_paddle_pos, current_state)
                        target = defense_pos
                else:
                    target = self.new_initial_pos
            
            # 
            if target['x'] > (current_state['board_shape'][1] / 2):
                self.my_paddle_pos = next_move_paddle(self.my_paddle_pos, target, current_state)

        return self.my_paddle_pos
Ejemplo n.º 23
0
    def next_move(self, current_state):
        """ Function that computes the next move of your paddle

        Implement your algorithm here. This will be the only function
        used by the GameCore. Be aware of abiding all the game rules.

        Returns:
            dict: coordinates of next position of your paddle.
        """

        # update my paddle pos
        # I need to do this because GameCore moves my paddle randomly
        self.my_paddle_pos = current_state['paddle1_pos'] if self.my_goal == 'left' \
                                                              else current_state['paddle2_pos']

        self.my_goal_center = {
            'x':
            0 if self.my_goal == 'left' else current_state['board_shape'][1],
            'y': current_state['board_shape'][0] / 2
        }

        path = []
        path = estimate_path(current_state, self.future_size)
        target = self.initial_pos

        roi_radius = current_state['board_shape'][0] * current_state[
            'goal_size'] * 2
        pt_in_roi = None
        for p in path:
            if utils.distance_between_points(p[0],
                                             self.my_goal_center) < roi_radius:
                pt_in_roi = p
                break

        #if the puck is in the right field, the paddle defends
        if is_puck_right(current_state):
            # get the target position for the paddle
            target = self.initial_pos

        else:
            if current_state['puck_speed']['x'] <= 0 and is_puck_behind(
                    self.my_paddle_pos, current_state) == False:
                enemy_pos = enemy_puck_pos(self.my_goal, current_state)
                if enemy_pos == 0 and pt_in_roi:
                    target = utils.aim(
                        pt_in_roi[0], pt_in_roi[1], {
                            'x': current_state['paddle2_pos']['x'],
                            'y': current_state['board_shape'][0]
                        }, current_state['puck_radius'],
                        current_state['paddle_radius'])
                else:
                    target = utils.aim(pt_in_roi[0], pt_in_roi[1], {
                        'x': current_state['paddle2_pos']['x'],
                        'y': 0
                    }, current_state['puck_radius'],
                                       current_state['paddle_radius'])
            else:
                target = self.initial_pos

        #
        if target['x'] < (current_state['board_shape'][1] / 2):
            self.my_paddle_pos = next_move_paddle(self.my_paddle_pos, target,
                                                  current_state)

        return self.my_paddle_pos
    def next_move(self, current_state):
        """ Function that computes the next move of your paddle

        Implement your algorithm here. This will be the only function
        used by the GameCore. Be aware of abiding all the game rules.

        Returns:
            dict: coordinates of next position of your paddle.
        """
        if (self.my_goal == 'right'):
            if (current_state['puck_pos']['x'] <
                ((current_state['board_shape'][1] / 3) * 2)
                    or current_state['goals']['right'] >
                    current_state['goals']['left']
                    or current_state['puck_speed']['y'] > 140):
                if (current_state['puck_speed']['x']) < 0:
                    # update my paddle pos
                    # I need to do this because GameCore moves my paddle randomly
                    self.my_paddle_pos = current_state['paddle1_pos'] if self.my_goal == 'left' \
                        else current_state['paddle2_pos']
                    self.his_paddle_pos = current_state['paddle2_pos'] if self.my_goal == 'left' \
                        else current_state['paddle1_pos']

                    # estimate puck path
                    path = estimate_path(current_state, self.future_size)

                    # computing both goal centers
                    self.my_goal_center = {
                        'x':
                        0 if self.my_goal == 'left' else
                        current_state['board_shape'][1],
                        'y':
                        current_state['board_shape'][0] / 2
                    }
                    self.opponent_goal_center = {
                        'x':
                        0 if self.my_goal == 'right' else
                        current_state['board_shape'][1],
                        'y':
                        current_state['board_shape'][0] / 2
                    }
                    # find if puck path is inside my interest area
                    roi_radius = current_state['board_shape'][
                        0] * current_state['goal_size'] * 2
                    target_pos = {
                        'x': (current_state['board_shape'][1] / 8) * 7,
                        'y': (current_state['board_shape'][0] / 2)
                    }
                    # move to target position, taking into account the max. paddle speed
                    if target_pos != self.my_paddle_pos and target_pos['x'] > (current_state['board_shape'][1] / 2) - \
                            current_state['paddle_radius']:
                        direction_vector = {
                            'x': target_pos['x'] - self.my_paddle_pos['x'],
                            'y': target_pos['y'] - self.my_paddle_pos['y']
                        }
                        direction_vector = {
                            k: v / utils.vector_l2norm(direction_vector)
                            for k, v in direction_vector.items()
                        }

                        movement_dist = min(
                            current_state['paddle_max_speed'] *
                            current_state['delta_t'],
                            utils.distance_between_points(
                                target_pos, self.my_paddle_pos))
                        direction_vector = {
                            k: v * movement_dist
                            for k, v in direction_vector.items()
                        }
                        new_paddle_pos = {
                            'x':
                            self.my_paddle_pos['x'] + direction_vector['x'],
                            'y':
                            self.my_paddle_pos['y'] + direction_vector['y']
                        }

                        # check if computed new position in not inside goal area
                        # check if computed new position in inside board limits
                        if utils.is_inside_goal_area_paddle(new_paddle_pos, current_state) is False and \
                                utils.is_out_of_boundaries_paddle(new_paddle_pos, current_state) is None:
                            self.my_paddle_pos = new_paddle_pos
                    return self.my_paddle_pos
                else:

                    # update my paddle pos
                    # I need to do this because GameCore moves my paddle randomly
                    self.my_paddle_pos = current_state['paddle1_pos'] if self.my_goal == 'left' \
                        else current_state['paddle2_pos']

                    # estimate puck path
                    path = estimate_path(current_state, self.future_size)

                    # computing both goal centers
                    self.my_goal_center = {
                        'x':
                        0 if self.my_goal == 'left' else
                        current_state['board_shape'][1],
                        'y':
                        current_state['board_shape'][0] / 2
                    }
                    y = random.uniform(140, 370)
                    self.opponent_goal_center = {
                        'x':
                        0 if self.my_goal == 'right' else
                        current_state['board_shape'][1],
                        'y':
                        y
                    }

                    # find if puck path is inside my interest area
                    roi_radius = current_state['board_shape'][
                        0] * current_state['goal_size']
                    pt_in_roi = None
                    for p in path:
                        if utils.distance_between_points(
                                p[0], self.my_goal_center) < roi_radius:
                            pt_in_roi = p
                            break

                    if pt_in_roi:
                        # estimate an aiming position
                        target_pos = utils.aim(pt_in_roi[0], pt_in_roi[1],
                                               self.opponent_goal_center,
                                               current_state['puck_radius'],
                                               current_state['paddle_radius'])

                        # move to target position, taking into account the max. paddle speed
                        if target_pos != self.my_paddle_pos:
                            direction_vector = {
                                'x': target_pos['x'] - self.my_paddle_pos['x'],
                                'y': target_pos['y'] - self.my_paddle_pos['y']
                            }
                            direction_vector = {
                                k: v / utils.vector_l2norm(direction_vector)
                                for k, v in direction_vector.items()
                            }

                            movement_dist = min(
                                current_state['paddle_max_speed'] *
                                current_state['delta_t'],
                                utils.distance_between_points(
                                    target_pos, self.my_paddle_pos))
                            direction_vector = {
                                k: v * movement_dist
                                for k, v in direction_vector.items()
                            }
                            new_paddle_pos = {
                                'x':
                                self.my_paddle_pos['x'] +
                                direction_vector['x'],
                                'y':
                                self.my_paddle_pos['y'] + direction_vector['y']
                            }

                            # check if computed new position in not inside goal area
                            # check if computed new position in inside board limits
                            if utils.is_inside_goal_area_paddle(new_paddle_pos, current_state) is False and \
                                    utils.is_out_of_boundaries_paddle(new_paddle_pos, current_state) is None:
                                self.my_paddle_pos = new_paddle_pos

                    return self.my_paddle_pos
            else:
                # update my paddle pos
                # I need to do this because GameCore moves my paddle randomly
                self.my_paddle_pos = current_state['paddle1_pos'] if self.my_goal == 'left' \
                    else current_state['paddle2_pos']
                self.his_paddle_pos = current_state['paddle2_pos'] if self.my_goal == 'left' \
                    else current_state['paddle1_pos']

                # estimate puck path
                path = estimate_path(current_state, self.future_size)

                # computing both goal centers
                self.my_goal_center = {
                    'x':
                    0 if self.my_goal == 'left' else
                    current_state['board_shape'][1],
                    'y':
                    current_state['board_shape'][0] / 2
                }
                self.opponent_goal_center = {
                    'x':
                    0 if self.my_goal == 'right' else
                    current_state['board_shape'][1],
                    'y':
                    current_state['board_shape'][0] / 2
                }
                if current_state['puck_pos']['x'] < current_state[
                        'paddle2_pos']['x']:
                    self.bouncePoint = {
                        'x': (current_state['board_shape'][1] -
                              current_state['puck_pos']['x']) *
                        (current_state['puck_pos']['y'] /
                         current_state['board_shape'][0] / 2) if
                        (current_state['puck_pos']['y'] <
                         current_state['board_shape'][0] / 2) else
                        (current_state['board_shape'][1] -
                         current_state['puck_pos']['x'] *
                         (current_state['board_shape'][0] -
                          current_state['puck_pos']['y']) /
                         current_state['board_shape'][0] / 2),
                        'y':
                        current_state['board_shape'][0]
                        if self.his_paddle_pos['y'] <
                        current_state['board_shape'][0] / 2 else 0
                    }
                elif (current_state['puck_pos']['x'] >
                      current_state['paddle2_pos']['x']) and (
                          current_state['paddle2_pos']['y'] <
                          current_state['board_shape'][0] / 2):
                    self.bouncePoint = {
                        'x': current_state['paddle2_pos']['x'],
                        'y': (current_state['board_shape'][0] / 6) * 5
                    }
                else:
                    self.bouncePoint = {
                        'x': current_state['board_shape'][1],
                        'y': current_state['board_shape'][0] / 6
                    }
                # find if puck path is inside my interest area
                roi_radius = current_state['board_shape'][0] * current_state[
                    'goal_size'] * 2
                pt_in_roi = None
                for p in path:
                    if utils.distance_between_points(
                            p[0], self.my_goal_center) < roi_radius:
                        pt_in_roi = p
                        break
                if pt_in_roi:
                    # estimate an aiming position
                    if current_state['puck_pos']['x'] > (
                            current_state['board_shape'][1] / 12) * 7:
                        target_pos = utils.aim(pt_in_roi[0], pt_in_roi[1],
                                               self.bouncePoint,
                                               current_state['puck_radius'],
                                               current_state['paddle_radius'])
                    else:
                        target_pos = {
                            'x':
                            current_state['board_shape'][1] -
                            (current_state['board_shape'][1] / 8),
                            'y': ((current_state['puck_pos']['y'] / 2) +
                                  (current_state['board_shape'][0] / 4))
                        }

                    # move to target position, taking into account the max. paddle speed
                    if target_pos != self.my_paddle_pos and target_pos['x'] > (current_state['board_shape'][1] / 2) - \
                            current_state['paddle_radius']:
                        direction_vector = {
                            'x': target_pos['x'] - self.my_paddle_pos['x'],
                            'y': target_pos['y'] - self.my_paddle_pos['y']
                        }
                        direction_vector = {
                            k: v / utils.vector_l2norm(direction_vector)
                            for k, v in direction_vector.items()
                        }

                        movement_dist = min(
                            current_state['paddle_max_speed'] *
                            current_state['delta_t'],
                            utils.distance_between_points(
                                target_pos, self.my_paddle_pos))
                        direction_vector = {
                            k: v * movement_dist
                            for k, v in direction_vector.items()
                        }
                        new_paddle_pos = {
                            'x':
                            self.my_paddle_pos['x'] + direction_vector['x'],
                            'y':
                            self.my_paddle_pos['y'] + direction_vector['y']
                        }

                        # check if computed new position in not inside goal area
                        # check if computed new position in inside board limits
                        if utils.is_inside_goal_area_paddle(new_paddle_pos, current_state) is False and \
                                utils.is_out_of_boundaries_paddle(new_paddle_pos, current_state) is None:
                            self.my_paddle_pos = new_paddle_pos

                # time.sleep(2)
                # return {'x': -12, 'y': -6543}
                else:
                    target_pos = {
                        'x':
                        current_state['board_shape'][1] -
                        (current_state['board_shape'][1] / 8),
                        'y': ((current_state['puck_pos']['y'] / 3) +
                              (current_state['board_shape'][0] / 3))
                    }

                    # move to target position, taking into account the max. paddle speed
                    if target_pos != self.my_paddle_pos and target_pos['x'] > (current_state['board_shape'][1] / 2) - \
                            current_state['paddle_radius']:
                        direction_vector = {
                            'x': target_pos['x'] - self.my_paddle_pos['x'],
                            'y': target_pos['y'] - self.my_paddle_pos['y']
                        }
                        direction_vector = {
                            k: v / utils.vector_l2norm(direction_vector)
                            for k, v in direction_vector.items()
                        }

                        movement_dist = min(
                            current_state['paddle_max_speed'] *
                            current_state['delta_t'],
                            utils.distance_between_points(
                                target_pos, self.my_paddle_pos))
                        direction_vector = {
                            k: v * movement_dist
                            for k, v in direction_vector.items()
                        }
                        new_paddle_pos = {
                            'x':
                            self.my_paddle_pos['x'] + direction_vector['x'],
                            'y':
                            self.my_paddle_pos['y'] + direction_vector['y']
                        }

                        # check if computed new position in not inside goal area
                        # check if computed new position in inside board limits
                        if utils.is_inside_goal_area_paddle(new_paddle_pos, current_state) is False and \
                                utils.is_out_of_boundaries_paddle(new_paddle_pos, current_state) is None:
                            self.my_paddle_pos = new_paddle_pos
                return self.my_paddle_pos

        else:
            if (current_state['puck_pos']['x'] >
                    current_state['board_shape'][1] / 3
                    or current_state['goals']['left'] >
                    current_state['goals']['right']
                    or current_state['puck_speed']['y'] > 140):

                if (current_state['puck_speed']['x']) > 0:
                    # update my paddle pos
                    # I need to do this because GameCore moves my paddle randomly
                    self.my_paddle_pos = current_state['paddle1_pos'] if self.my_goal == 'left' \
                        else current_state['paddle2_pos']
                    self.his_paddle_pos = current_state['paddle2_pos'] if self.my_goal == 'left' \
                        else current_state['paddle1_pos']

                    # estimate puck path
                    path = estimate_path(current_state, self.future_size)

                    # computing both goal centers
                    self.my_goal_center = {
                        'x':
                        0 if self.my_goal == 'left' else
                        current_state['board_shape'][1],
                        'y':
                        current_state['board_shape'][0] / 2
                    }
                    self.opponent_goal_center = {
                        'x':
                        0 if self.my_goal == 'right' else
                        current_state['board_shape'][1],
                        'y':
                        current_state['board_shape'][0] / 2
                    }
                    # find if puck path is inside my interest area
                    roi_radius = current_state['board_shape'][
                        0] * current_state['goal_size'] * 2
                    target_pos = {
                        'x': (current_state['board_shape'][1] / 8),
                        'y': (current_state['board_shape'][0] / 2)
                    }
                    # move to target position, taking into account the max. paddle speed
                    if target_pos != self.my_paddle_pos and target_pos['x'] < (current_state['board_shape'][1] / 2) - \
                            current_state['paddle_radius']:
                        direction_vector = {
                            'x': target_pos['x'] - self.my_paddle_pos['x'],
                            'y': target_pos['y'] - self.my_paddle_pos['y']
                        }
                        direction_vector = {
                            k: v / utils.vector_l2norm(direction_vector)
                            for k, v in direction_vector.items()
                        }

                        movement_dist = min(
                            current_state['paddle_max_speed'] *
                            current_state['delta_t'],
                            utils.distance_between_points(
                                target_pos, self.my_paddle_pos))
                        direction_vector = {
                            k: v * movement_dist
                            for k, v in direction_vector.items()
                        }
                        new_paddle_pos = {
                            'x':
                            self.my_paddle_pos['x'] + direction_vector['x'],
                            'y':
                            self.my_paddle_pos['y'] + direction_vector['y']
                        }

                        # check if computed new position in not inside goal area
                        # check if computed new position in inside board limits
                        if utils.is_inside_goal_area_paddle(new_paddle_pos, current_state) is False and \
                                utils.is_out_of_boundaries_paddle(new_paddle_pos, current_state) is None:
                            self.my_paddle_pos = new_paddle_pos
                    return self.my_paddle_pos
                else:

                    self.my_paddle_pos = current_state['paddle1_pos'] if self.my_goal == 'left' \
                        else current_state['paddle2_pos']

                    # estimate puck path
                    path = estimate_path(current_state, self.future_size)

                    # computing both goal centers
                    self.my_goal_center = {
                        'x':
                        0 if self.my_goal == 'left' else
                        current_state['board_shape'][1],
                        'y':
                        current_state['board_shape'][0] / 2
                    }
                    self.opponent_goal_center = {
                        'x':
                        0 if self.my_goal == 'right' else
                        current_state['board_shape'][1],
                        'y':
                        current_state['board_shape'][0] / 2
                    }

                    # find if puck path is inside my interest area
                    roi_radius = current_state['board_shape'][
                        0] * current_state['goal_size']
                    pt_in_roi = None
                    for p in path:
                        if utils.distance_between_points(
                                p[0], self.my_goal_center) < roi_radius:
                            pt_in_roi = p
                            break

                    if pt_in_roi:
                        # estimate an aiming position
                        target_pos = utils.aim(pt_in_roi[0], pt_in_roi[1],
                                               self.opponent_goal_center,
                                               current_state['puck_radius'],
                                               current_state['paddle_radius'])

                        # move to target position, taking into account the max. paddle speed
                        if target_pos != self.my_paddle_pos:
                            direction_vector = {
                                'x': target_pos['x'] - self.my_paddle_pos['x'],
                                'y': target_pos['y'] - self.my_paddle_pos['y']
                            }
                            direction_vector = {
                                k: v / utils.vector_l2norm(direction_vector)
                                for k, v in direction_vector.items()
                            }

                            movement_dist = min(
                                current_state['paddle_max_speed'] *
                                current_state['delta_t'],
                                utils.distance_between_points(
                                    target_pos, self.my_paddle_pos))
                            direction_vector = {
                                k: v * movement_dist
                                for k, v in direction_vector.items()
                            }
                            new_paddle_pos = {
                                'x':
                                self.my_paddle_pos['x'] +
                                direction_vector['x'],
                                'y':
                                self.my_paddle_pos['y'] + direction_vector['y']
                            }

                            # check if computed new position in not inside goal area
                            # check if computed new position in inside board limits
                            if utils.is_inside_goal_area_paddle(new_paddle_pos, current_state) is False and \
                                    utils.is_out_of_boundaries_paddle(new_paddle_pos, current_state) is None:
                                self.my_paddle_pos = new_paddle_pos

                    return self.my_paddle_pos
            else:
                # update my paddle pos
                # I need to do this because GameCore moves my paddle randomly
                self.my_paddle_pos = current_state['paddle1_pos'] if self.my_goal == 'left' \
                    else current_state['paddle2_pos']
                self.his_paddle_pos = current_state['paddle2_pos'] if self.my_goal == 'left' \
                    else current_state['paddle1_pos']

                # estimate puck path
                path = estimate_path(current_state, self.future_size)

                # computing both goal centers
                self.my_goal_center = {
                    'x':
                    0 if self.my_goal == 'left' else
                    current_state['board_shape'][1],
                    'y':
                    current_state['board_shape'][0] / 2
                }
                self.opponent_goal_center = {
                    'x':
                    0 if self.my_goal == 'right' else
                    current_state['board_shape'][1],
                    'y':
                    current_state['board_shape'][0] / 2
                }
                if current_state['puck_pos']['x'] > current_state[
                        'paddle1_pos']['x']:
                    self.bouncePoint = {
                        'x': (current_state['board_shape'][1] -
                              current_state['puck_pos']['x']) *
                        (current_state['puck_pos']['y'] /
                         current_state['board_shape'][0] / 2) if
                        (current_state['puck_pos']['y'] <
                         current_state['board_shape'][0] / 2) else
                        (current_state['board_shape'][1] -
                         current_state['puck_pos']['x'] *
                         (current_state['board_shape'][0] -
                          current_state['puck_pos']['y']) /
                         current_state['board_shape'][0] / 2),
                        'y':
                        current_state['board_shape'][0]
                        if self.his_paddle_pos['y'] <
                        current_state['board_shape'][0] / 2 else 0
                    }
                elif (current_state['puck_pos']['x'] <
                      current_state['paddle1_pos']['x']) and (
                          current_state['paddle1_pos']['y'] <
                          current_state['board_shape'][0] / 2):
                    self.bouncePoint = {
                        'x': current_state['board_shape'][1],
                        'y': (current_state['board_shape'][0] / 6) * 5
                    }
                else:
                    self.bouncePoint = {
                        'x': 0,
                        'y': current_state['board_shape'][0] / 6
                    }

                # find if puck path is inside my interest area
                roi_radius = current_state['board_shape'][0] * current_state[
                    'goal_size'] * 2
                pt_in_roi = None
                for p in path:
                    if utils.distance_between_points(
                            p[0], self.my_goal_center) < roi_radius:
                        pt_in_roi = p
                        break

                if pt_in_roi:
                    # estimate an aiming position
                    if current_state['puck_pos'][
                            'x'] < current_state['board_shape'][1] / 2:
                        target_pos = utils.aim(pt_in_roi[0], pt_in_roi[1],
                                               self.bouncePoint,
                                               current_state['puck_radius'],
                                               current_state['paddle_radius'])
                    else:
                        target_pos = {
                            'x':
                            current_state['board_shape'][1] / 8,
                            'y': ((current_state['puck_pos']['y'] / 2) +
                                  (current_state['board_shape'][0] / 4))
                        }

                    # move to target position, taking into account the max. paddle speed
                    if target_pos != self.my_paddle_pos and target_pos['x'] < (current_state['board_shape'][1] / 2) - \
                            current_state['paddle_radius']:
                        direction_vector = {
                            'x': target_pos['x'] - self.my_paddle_pos['x'],
                            'y': target_pos['y'] - self.my_paddle_pos['y']
                        }
                        direction_vector = {
                            k: v / utils.vector_l2norm(direction_vector)
                            for k, v in direction_vector.items()
                        }

                        movement_dist = min(
                            current_state['paddle_max_speed'] *
                            current_state['delta_t'],
                            utils.distance_between_points(
                                target_pos, self.my_paddle_pos))
                        direction_vector = {
                            k: v * movement_dist
                            for k, v in direction_vector.items()
                        }
                        new_paddle_pos = {
                            'x':
                            self.my_paddle_pos['x'] + direction_vector['x'],
                            'y':
                            self.my_paddle_pos['y'] + direction_vector['y']
                        }

                        # check if computed new position in not inside goal area
                        # check if computed new position in inside board limits
                        if utils.is_inside_goal_area_paddle(new_paddle_pos, current_state) is False and \
                                utils.is_out_of_boundaries_paddle(new_paddle_pos, current_state) is None:
                            self.my_paddle_pos = new_paddle_pos

                # time.sleep(2)
                # return {'x': -12, 'y': -6543}
                return self.my_paddle_pos
Ejemplo n.º 25
0
    def next_move(self, current_state):
        """ Function that computes the next move of your paddle

        Implement your algorithm here. This will be the only function
        used by the GameCore. Be aware of abiding all the game rules.

        Returns:
            dict: coordinates of next position of your paddle.
        -------------------------other----------------------------------
        """

        # update my paddle pos
        # I need to do this because GameCore moves my paddle randomly
        self.my_paddle_pos = current_state['paddle1_pos'] if self.my_goal == 'left' \
                                                              else current_state['paddle2_pos']

        self.their_paddle_pos = current_state['paddle2_pos'] if self.my_goal == 'left' \
                                                              else current_state['paddle1_pos']

        # estimate puck path
        path = estimate_path(current_state, self.future_size)

        # computing both goal centers
        self.my_goal_center = {
            'x':
            0 if self.my_goal == 'left' else current_state['board_shape'][1],
            'y': current_state['board_shape'][0] / 2
        }
        y = random.uniform(140, 370)
        self.opponent_goal_center = {
            'x':
            0 if self.my_goal == 'right' else current_state['board_shape'][1],
            'y': y
        }

        # computing my area center

        self.my_area_center = {
            'x':
            current_state['board_shape'][1] /
            4 if self.my_goal == 'left' else current_state['board_shape'][1] /
            4 * 3,
            'y':
            current_state['board_shape'][0] / 2
        }

        # computing frontlines

        self.my_frontline = {
            'x':
            current_state['board_shape'][1] / 8 *
            3 if self.my_goal == 'left' else current_state['board_shape'][1] /
            8 * 5,
            'y':
            current_state['board_shape'][0] / 2
        }

        self.their_frontline = {
            'x':
            current_state['board_shape'][1] / 8 *
            5 if self.my_goal == 'left' else current_state['board_shape'][1] /
            8 * 3,
            'y':
            current_state['board_shape'][0] / 2
        }

        # computing backlines

        self.my_backline = {
            'x':
            current_state['board_shape'][1] /
            8 if self.my_goal == 'left' else current_state['board_shape'][1] /
            8 * 7,
            'y':
            current_state['board_shape'][0] / 2
        }

        self.their_backline = {
            'x':
            current_state['board_shape'][1] / 8 *
            7 if self.my_goal == 'left' else current_state['board_shape'][1] /
            8,
            'y':
            current_state['board_shape'][0] / 2
        }

        # find if puck path is inside my interest area
        roi_radius = current_state['board_shape'][0] * current_state[
            'goal_size'] * 1.2
        personal_space = current_state['paddle_radius'] * 3
        defensive_point = None
        offensive_point = None
        coming = False
        prevd = None
        behind = False

        for p in path:
            if utils.distance_between_points(
                    current_state['puck_pos'],
                    self.my_paddle_pos) < personal_space:
                offensive_point = p
                break

        for p in path:
            if utils.distance_between_points(p[0],
                                             self.my_goal_center) < roi_radius:
                defensive_point = p
                break

        for p in path:
            if prevd != None:

                if utils.distance_between_points(p[0],
                                                 self.my_goal_center) < prevd:

                    coming = True

            prevd = utils.distance_between_points(p[0], self.my_goal_center)

        if self.my_goal == 'left':
            if (current_state['puck_pos']['x'] < self.my_paddle_pos['x']):
                behind = True
        else:
            if (current_state['puck_pos']['x'] > self.my_paddle_pos['x']):
                behind = True
        '''
        -------------------------movement----------------------------------
        '''
        def indirect_goal_center(current_state):
            #find ideal xpos
            smallest_dist = 9999
            bounce_points = [0, 125, 249, 373, 497, 621, 745, 869, 995]
            behinderino = False
            xpos = 497

            if self.my_goal == 'left':
                if (current_state['puck_pos']['x'] < self.my_paddle_pos['x']):
                    behinderino = True
            else:
                if (current_state['puck_pos']['x'] > self.my_paddle_pos['x']):
                    behinderino = True

            if not behinderino:
                xpos = self.their_backline['x']

            if self.their_paddle_pos['y'] < (current_state['board_shape'][0] /
                                             2):
                poss = {'x': xpos, 'y': current_state['board_shape'][0]}
            else:
                poss = {'x': xpos, 'y': 0}
            return poss

        def NME_IN_PATH(target_pos):
            if (target_pos['y'] - self.their_paddle_pos['y']
                ) < current_state['paddle_radius'] * 2:
                return True
            else:
                return False

        def target_to_position(target_pos):
            # move to target position, taking into account the max. paddle speed
            if target_pos != self.my_paddle_pos:
                direction_vector = {
                    'x': target_pos['x'] - self.my_paddle_pos['x'],
                    'y': target_pos['y'] - self.my_paddle_pos['y']
                }
                direction_vector = {
                    k: v / utils.vector_l2norm(direction_vector)
                    for k, v in direction_vector.items()
                }

                movement_dist = min(
                    current_state['paddle_max_speed'] *
                    current_state['delta_t'],
                    utils.distance_between_points(target_pos,
                                                  self.my_paddle_pos))
                direction_vector = {
                    k: v * movement_dist
                    for k, v in direction_vector.items()
                }
                new_paddle_pos = {
                    'x': self.my_paddle_pos['x'] + direction_vector['x'],
                    'y': self.my_paddle_pos['y'] + direction_vector['y']
                }

                # check if computed new position in not inside goal area
                # check if computed new position in inside board limits
                if utils.is_inside_goal_area_paddle(new_paddle_pos, current_state) is False and \
                     utils.is_out_of_boundaries_paddle(new_paddle_pos, current_state) is None:
                    self.my_paddle_pos = new_paddle_pos
                else:
                    target_to_position(self.my_area_center)

            return self.my_paddle_pos

        """
        ----------------------------------engagement scenarios -------------------------------------
        """

        if behind and defensive_point:
            goalerino = indirect_goal_center(current_state)
            target_pos = utils.aim(defensive_point[0], defensive_point[1],
                                   goalerino, current_state['puck_radius'],
                                   current_state['paddle_radius'])
            return target_to_position(target_pos)
        elif behind and offensive_point:
            goalerino = {'x': self.my_goal_center['x'], 'y': 0}
            target_pos = utils.aim(offensive_point[0], offensive_point[1],
                                   goalerino, current_state['puck_radius'],
                                   current_state['paddle_radius'])
            return target_to_position(target_pos)

        elif defensive_point:
            # estimate an aiming position

            target_pos = utils.aim(defensive_point[0], defensive_point[1],
                                   self.opponent_goal_center,
                                   current_state['puck_radius'],
                                   current_state['paddle_radius'])

            if NME_IN_PATH(target_pos):
                target_pos = utils.aim(defensive_point[0], defensive_point[1],
                                       indirect_goal_center(current_state),
                                       current_state['puck_radius'],
                                       current_state['paddle_radius'])
                return target_to_position(target_pos)
            else:
                return target_to_position(target_pos)

        elif offensive_point:
            # estimate an aiming position

            target_pos = utils.aim(offensive_point[0], offensive_point[1],
                                   self.opponent_goal_center,
                                   current_state['puck_radius'],
                                   current_state['paddle_radius'])

            if NME_IN_PATH(target_pos):
                target_pos = utils.aim(offensive_point[0], offensive_point[1],
                                       indirect_goal_center(current_state),
                                       current_state['puck_radius'],
                                       current_state['paddle_radius'])
                return target_to_position(target_pos)
            else:
                return target_to_position(target_pos)

        elif coming:  #idle defensive

            target_pos = line_intersection(
                self.their_paddle_pos['x'], self.their_paddle_pos['y'],
                current_state['puck_pos']['x'], current_state['puck_pos']['y'],
                self.my_backline['x'], current_state['board_shape'][0],
                self.my_frontline['x'], 0)

            return target_to_position(target_pos)

        else:  #idle offensive

            target_pos = line_intersection(
                self.their_paddle_pos['x'], self.their_paddle_pos['y'],
                current_state['puck_pos']['x'], current_state['puck_pos']['y'],
                self.my_frontline['x'], current_state['board_shape'][0],
                self.my_frontline['x'], 0)

            return target_to_position(target_pos)

        print("HUH")
        return target_to_position(self.my_area_center)
    def next_move(self, current_state):
        self.my_paddle_pos = current_state[
            'paddle1_pos'] if self.my_goal == 'left' else current_state[
                'paddle2_pos']
        path = estimate_path(current_state, self.future_size)
        self.my_goal_center = {
            'x':
            995 /
            4 if self.my_goal == 'left' else current_state['board_shape'][1],
            'y':
            current_state['board_shape'][0] / 2
        }
        #995 * (3/4)
        #current_state['board_shape'][1]
        self.opponent_goal_center = {
            'x': 0 if self.my_goal == 'right' else 995,
            'y': current_state['board_shape'][0] / 2
        }
        roi_radius = current_state['board_shape'][0] * current_state[
            'goal_size'] * 2
        pt_in_roi = None
        for p in path:
            if utils.distance_between_points(p[0],
                                             self.my_goal_center) < roi_radius:
                pt_in_roi = p

        if pt_in_roi:
            target_pos = utils.aim(pt_in_roi[0], pt_in_roi[1],
                                   self.opponent_goal_center,
                                   current_state['puck_radius'],
                                   current_state['paddle_radius'])
            if self.my_goal is 'left':
                if self.my_paddle_pos['x'] < 995 / 3.8:
                    direction_vector = {
                        'x': target_pos['x'] - self.my_paddle_pos['x'],
                        'y': target_pos['y'] - self.my_paddle_pos['y']
                    }
                    direction_vector = {
                        k: v / utils.vector_l2norm(direction_vector)
                        for k, v in direction_vector.items()
                    }
                    movement_dist = min(
                        current_state['paddle_max_speed'] *
                        current_state['delta_t'],
                        utils.distance_between_points(target_pos,
                                                      self.my_paddle_pos))
                    direction_vector = {
                        k: v * movement_dist
                        for k, v in direction_vector.items()
                    }
                    new_paddle_pos = {
                        'x': self.my_paddle_pos['x'] + direction_vector['x'],
                        'y': self.my_paddle_pos['y'] + direction_vector['y']
                    }
                    if utils.is_inside_goal_area_paddle(
                            new_paddle_pos, current_state
                    ) is False and utils.is_out_of_boundaries_paddle(
                            new_paddle_pos, current_state) is None:
                        self.my_paddle_pos = new_paddle_pos
                else:
                    direction_vector = {
                        'x':
                        self.my_goal_center['x'] - self.my_paddle_pos['x'],
                        'y': self.my_goal_center['y'] - self.my_paddle_pos['y']
                    }
                    direction_vector = {
                        k: v / utils.vector_l2norm(direction_vector)
                        for k, v in direction_vector.items()
                    }
                    movement_dist = min(
                        current_state['paddle_max_speed'] *
                        current_state['delta_t'],
                        utils.distance_between_points(self.my_goal_center,
                                                      self.my_paddle_pos))
                    direction_vector = {
                        k: v * movement_dist
                        for k, v in direction_vector.items()
                    }
                    new_paddle_pos = {
                        'x': self.my_paddle_pos['x'] + direction_vector['x'],
                        'y': self.my_paddle_pos['y'] + direction_vector['y']
                    }
                    if utils.is_inside_goal_area_paddle(
                            new_paddle_pos, current_state
                    ) is False and utils.is_out_of_boundaries_paddle(
                            new_paddle_pos, current_state) is None:
                        self.my_paddle_pos = new_paddle_pos
            elif self.my_goal is 'right':
                if self.my_paddle_pos['x'] < 995 / .5:
                    direction_vector = {
                        'x': target_pos['x'] - self.my_paddle_pos['x'],
                        'y': target_pos['y'] - self.my_paddle_pos['y']
                    }
                    direction_vector = {
                        k: v / utils.vector_l2norm(direction_vector)
                        for k, v in direction_vector.items()
                    }
                    movement_dist = min(
                        current_state['paddle_max_speed'] *
                        current_state['delta_t'],
                        utils.distance_between_points(target_pos,
                                                      self.my_paddle_pos))
                    direction_vector = {
                        k: v * movement_dist
                        for k, v in direction_vector.items()
                    }
                    new_paddle_pos = {
                        'x': self.my_paddle_pos['x'] + direction_vector['x'],
                        'y': self.my_paddle_pos['y'] + direction_vector['y']
                    }
                    if utils.is_inside_goal_area_paddle(
                            new_paddle_pos, current_state
                    ) is False and utils.is_out_of_boundaries_paddle(
                            new_paddle_pos, current_state) is None:
                        self.my_paddle_pos = new_paddle_pos
                else:
                    direction_vector = {
                        'x':
                        self.my_goal_center['x'] - self.my_paddle_pos['x'],
                        'y': self.my_goal_center['y'] - self.my_paddle_pos['y']
                    }
                    direction_vector = {
                        k: v / utils.vector_l2norm(direction_vector)
                        for k, v in direction_vector.items()
                    }
                    movement_dist = min(
                        current_state['paddle_max_speed'] *
                        current_state['delta_t'],
                        utils.distance_between_points(self.my_goal_center,
                                                      self.my_paddle_pos))
                    direction_vector = {
                        k: v * movement_dist
                        for k, v in direction_vector.items()
                    }
                    new_paddle_pos = {
                        'x': self.my_paddle_pos['x'] + direction_vector['x'],
                        'y': self.my_paddle_pos['y'] + direction_vector['y']
                    }
                    if utils.is_inside_goal_area_paddle(
                            new_paddle_pos, current_state
                    ) is False and utils.is_out_of_boundaries_paddle(
                            new_paddle_pos, current_state) is None:
                        self.my_paddle_pos = new_paddle_pos
        return self.my_paddle_pos
Ejemplo n.º 27
0
    def next_move(self, current_state):
        """ Function that computes the next move of your paddle

        Implement your algorithm here. This will be the only function
        used by the GameCore. Be aware of abiding all the game rules.

        Returns:
            dict: coordinates of next position of your paddle.
        """

        # update my paddle pos
        # I need to do this because GameCore moves my paddle randomly
        self.my_paddle_pos = current_state['paddle1_pos'] if self.my_goal == 'left' \
                                                              else current_state['paddle2_pos']

        # estimate puck path
        path = estimate_path(current_state, self.future_size)

        # computing both goal centers
        x = random.uniform(-250, -150)
        self.my_goal_center = {
            'x':
            x if self.my_goal == 'left' else current_state['board_shape'][1] -
            x,
            'y':
            current_state['board_shape'][0] / 2
        }
        y = random.uniform(140, 370)
        self.opponent_goal_center = {
            'x':
            0 if self.my_goal == 'right' else current_state['board_shape'][1],
            'y': y
        }
        y = random.uniform(50, 150)
        # computing upper side centers, aiming in 3/4 when 1/4 in y = 0
        self.opponent_upper_side = {'x': current_state['board_shape'][1]/4 if self.my_goal == 'right' \
            else current_state['board_shape'][1]/2 + current_state['board_shape'][1]/4,
                               'y': 0 }
        self.opponent_upper_side_2 = {'x': current_state['board_shape'][1]/4 + y if self.my_goal == 'right' \
            else current_state['board_shape'][1]/2 + current_state['board_shape'][1]/4 - y,
                               'y': 0 }
        # computing lower side centers, aiming to 3/4 or 1/4 when y = height
        self.opponent_lower_side = {'x': current_state['board_shape'][1]/4 if self.my_goal == 'right' \
            else current_state['board_shape'][1]/2 + current_state['board_shape'][1]/8 + current_state['board_shape'][1]/16,
                               'y': current_state['board_shape'][0] }
        self.opponent_lower_side_2 = {'x': current_state['board_shape'][1]/4 + y if self.my_goal == 'right' \
            else current_state['board_shape'][1]/2 + current_state['board_shape'][1]/8 + current_state['board_shape'][1]/16 - y,
                               'y': current_state['board_shape'][0] }

        # Generates the dircetion of the shoot by tracking down the opponent's location
        if current_state['paddle2_pos']['y'] < current_state['board_shape'][
                0] / 2 + current_state['board_shape'][0] / 8:
            shoot = self.opponent_lower_side
        elif current_state['paddle2_pos']['y'] < current_state['board_shape'][
                0] / 2 - current_state['board_shape'][0] / 8:
            shoot = self.opponent_upper_side_2
        elif current_state['paddle2_pos']['y'] > current_state['board_shape'][
                0] / 2 + current_state['board_shape'][0] / 8:
            shoot = self.opponent_upper_side
        elif current_state['paddle2_pos']['y'] > current_state['board_shape'][
                0] / 2 - current_state['board_shape'][0] / 8:
            shoot = self.opponent_lower_side_2
        else:
            shoot = self.opponent_goal_center

        # find if puck path is inside my interest area
        roi_radius = current_state['board_shape'][0] * current_state[
            'goal_size'] * 2
        pt_in_roi = None
        for p in path:
            if utils.distance_between_points(p[0],
                                             self.my_goal_center) < roi_radius:
                pt_in_roi = p
                break

        if pt_in_roi and current_state['puck_pos'][
                'x'] > current_state['board_shape'][1] / 8:
            # estimate an aiming position
            target_pos = utils.aim(pt_in_roi[0], pt_in_roi[1], shoot,
                                   current_state['puck_radius'],
                                   current_state['paddle_radius'])

            # move to target position, taking into account the max. paddle speed
            if target_pos != self.my_paddle_pos:
                direction_vector = {
                    'x': target_pos['x'] - self.my_paddle_pos['x'],
                    'y': target_pos['y'] - self.my_paddle_pos['y']
                }
                direction_vector = {
                    k: v / utils.vector_l2norm(direction_vector)
                    for k, v in direction_vector.items()
                }

                movement_dist = min(
                    current_state['paddle_max_speed'] *
                    current_state['delta_t'],
                    utils.distance_between_points(target_pos,
                                                  self.my_paddle_pos))
                direction_vector = {
                    k: v * movement_dist
                    for k, v in direction_vector.items()
                }
                new_paddle_pos = {
                    'x': self.my_paddle_pos['x'] + direction_vector['x'],
                    'y': self.my_paddle_pos['y'] + direction_vector['y']
                }

                # New computed input
                #r = current_state['board_shape'][0]
                #if utils.detect_collision(current_state, self.my_paddle_pos, r):
                #   center = {'x': 0 if self.my_goal == 'left' else current_state['board_shape'][1],
                #            'y': current_state['board_shape'][0]/2}
                # r = current_state['goal_size']
                #self.my_paddle_pos = utils.nearest_point_in_circle(center, r, self.my_paddle_pos)
                #if path == center:
                #   self.my_paddle_pos = self.my_goal_center

                # check if computed new position in not inside goal area
                # check if computed new position in inside board limits
                if utils.is_inside_goal_area_paddle(new_paddle_pos, current_state) is False and \
                     utils.is_out_of_boundaries_paddle(new_paddle_pos, current_state) is None:
                    self.my_paddle_pos = new_paddle_pos

        return self.my_paddle_pos
Ejemplo n.º 28
0
    def next_move(self, current_state):
        """ Function that computes the next move of your paddle

        Implement your algorithm here. This will be the only function
        used by the GameCore. Be aware of abiding all the game rules.

        Returns:
            dict: coordinates of next position of your paddle.
        """
        # update my paddle pos
        # I need to do this because GameCore moves my paddle randomly
        self.my_paddle_pos = current_state['paddle1_pos'] if self.my_goal == 'left' \
                                                              else current_state['paddle2_pos']


        # estimate puck path
        path = estimate_path(current_state, self.future_size)

        # computing both goal centers
        self.my_goal_center = {'x': 0 if self.my_goal == 'left' else current_state['board_shape'][1],
                               'y': current_state['board_shape'][0]/2}
        self.opponent_goal_center = {'x': 0 if self.my_goal == 'right' else current_state['board_shape'][1],
                                     'y': current_state['board_shape'][0]/2}

        #print (current_state)

        if self.my_goal == 'left':
            if current_state['puck_pos']['x'] > current_state['board_shape'][1]/2 or current_state['puck_speed']['x'] > current_state['paddle_max_speed']:
                target_pos = {'x': current_state['board_shape'][1]/8,
                              'y': current_state['board_shape'][0]/2}
                direction_vector = {'x': target_pos['x'] - self.my_paddle_pos['x'],
                                    'y': target_pos['y'] - self.my_paddle_pos['y']}


                direction_vector = {k: v / utils.vector_l2norm(direction_vector)
                                    for k, v in direction_vector.items()}

                movement_dist = min(current_state['paddle_max_speed'] * current_state['delta_t'],
                                    utils.distance_between_points(target_pos, self.my_paddle_pos))
                direction_vector = {k: v * movement_dist
                                    for k, v in direction_vector.items()}
                new_paddle_pos = {'x': self.my_paddle_pos['x'] + direction_vector['x'],
                                  'y': self.my_paddle_pos['y'] + direction_vector['y']}



                # check if computed new position in not inside goal area
                # check if computed new position in inside board limits
                if utils.is_inside_goal_area_paddle(new_paddle_pos, current_state) is False and \
                     utils.is_out_of_boundaries_paddle(new_paddle_pos, current_state) is None:
                    self.my_paddle_pos = new_paddle_pos

            else:
                # find if puck path is inside my interest area
                roi_radius = current_state['board_shape'][0] * current_state['goal_size'] * 2
                pt_in_roi = None
                for p in path:
                    if utils.distance_between_points(p[0], self.my_goal_center) < roi_radius:
                        pt_in_roi = p
                        break

                if pt_in_roi:
                    # estimate an aiming position
                    #First two shoot the center
                    if current_state['paddle2_pos']['y'] <= current_state['board_shape'][0]/4:
                        target_pos = utils.aim(pt_in_roi[0], pt_in_roi[1],
                                               self.opponent_goal_center, current_state['puck_radius'],
                                               current_state['paddle_radius'])
                    elif current_state['paddle2_pos']['y'] >= (current_state['board_shape'][0] -  current_state['board_shape'][0]/4):
                        target_pos = utils.aim(pt_in_roi[0], pt_in_roi[1],
                                               self.opponent_goal_center, current_state['puck_radius'],
                                               current_state['paddle_radius'])
                    # Shoot low
                    elif current_state['paddle2_pos']['y'] > current_state['board_shape'][0]/2:
                        self.posN = {'x': (self.opponent_goal_center['x'] - self.my_paddle_pos['x'])/2,
                                     'y': 0}
                        target_pos = utils.aim(pt_in_roi[0], pt_in_roi[1],
                                              self.posN, current_state['puck_radius'],
                                               current_state['paddle_radius'])
                    # Shoot high
                    else:
                        self.posN = { 'x': (self.opponent_goal_center['x'] - self.my_paddle_pos['x'])/2,
                                'y': current_state['board_shape'][0]}
                        target_pos = utils.aim(pt_in_roi[0], pt_in_roi[1],
                                               self.posN, current_state['puck_radius'],
                                               current_state['paddle_radius'])



                    #if current_state['puck_speed']['x'] < current_state['paddle_max_speed'] * -1:
                    #    target_pos['x'] = 0

                    if target_pos != self.my_paddle_pos:
                        direction_vector = {'x': target_pos['x'] - self.my_paddle_pos['x'],
                                            'y': target_pos['y'] - self.my_paddle_pos['y']}
                        direction_vector = {k: v / utils.vector_l2norm(direction_vector)
                                            for k, v in direction_vector.items()}

                        movement_dist = min(current_state['paddle_max_speed'] * current_state['delta_t'],
                                            utils.distance_between_points(target_pos, self.my_paddle_pos))
                        direction_vector = {k: v * movement_dist
                                            for k, v in direction_vector.items()}
                        new_paddle_pos = {'x': self.my_paddle_pos['x'] + direction_vector['x'],
                                          'y': self.my_paddle_pos['y'] + direction_vector['y']}

                        # check if computed new position in not inside goal area
                        # check if computed new position in inside board limits
                        if utils.is_inside_goal_area_paddle(new_paddle_pos, current_state) is False and \
                             utils.is_out_of_boundaries_paddle(new_paddle_pos, current_state) is None:
                            self.my_paddle_pos = new_paddle_pos


        else:
            if current_state['puck_pos']['x'] < current_state['board_shape'][1]/2 or current_state['puck_speed']['x'] < current_state['paddle_max_speed'] * -1:
                target_pos = {'x': current_state['board_shape'][1] - current_state['board_shape'][1]/8,
                              'y': current_state['board_shape'][0]/2}
                direction_vector = {'x': target_pos['x'] - self.my_paddle_pos['x'],
                                    'y': target_pos['y'] - self.my_paddle_pos['y']}
                direction_vector = {k: v / utils.vector_l2norm(direction_vector)
                                    for k, v in direction_vector.items()}

                movement_dist = min(current_state['paddle_max_speed'] * current_state['delta_t'],
                                    utils.distance_between_points(target_pos, self.my_paddle_pos))
                direction_vector = {k: v * movement_dist
                                    for k, v in direction_vector.items()}
                new_paddle_pos = {'x': self.my_paddle_pos['x'] + direction_vector['x'],
                                  'y': self.my_paddle_pos['y'] + direction_vector['y']}



                # check if computed new position in not inside goal area
                # check if computed new position in inside board limits
                if utils.is_inside_goal_area_paddle(new_paddle_pos, current_state) is False and \
                     utils.is_out_of_boundaries_paddle(new_paddle_pos, current_state) is None:
                    self.my_paddle_pos = new_paddle_pos
            else:
                # find if puck path is inside my interest area
                roi_radius = current_state['board_shape'][0] * current_state['goal_size'] * 2
                pt_in_roi = None
                for p in path:
                    if utils.distance_between_points(p[0], self.my_goal_center) < roi_radius:
                        pt_in_roi = p
                        break

                if pt_in_roi:
                    # estimate an aiming position
                    # estimate an aiming position
                    #First two shoot the center
                    if current_state['paddle1_pos']['y'] <= current_state['board_shape'][0]/4:
                        target_pos = utils.aim(pt_in_roi[0], pt_in_roi[1],
                                               self.opponent_goal_center, current_state['puck_radius'],
                                               current_state['paddle_radius'])
                    elif current_state['paddle1_pos']['y'] >= (current_state['board_shape'][0] -  current_state['board_shape'][0]/4):
                        target_pos = utils.aim(pt_in_roi[0], pt_in_roi[1],
                                               self.opponent_goal_center, current_state['puck_radius'],
                                               current_state['paddle_radius'])
                    # Shoot low
                    elif current_state['paddle1_pos']['y'] > current_state['board_shape'][0]/2:
                        self.posN = {'x': (self.opponent_goal_center['x'] - self.my_paddle_pos['x'])/2,
                                     'y': 0}
                        target_pos = utils.aim(pt_in_roi[0], pt_in_roi[1],
                                              self.posN, current_state['puck_radius'],
                                               current_state['paddle_radius'])
                    # Shoot high
                    else:
                        self.posN = { 'x': (self.opponent_goal_center['x'] - self.my_paddle_pos['x'])/2,
                                      'y': current_state['board_shape'][0]}
                        target_pos = utils.aim(pt_in_roi[0], pt_in_roi[1],
                                               self.posN, current_state['puck_radius'],
                                               current_state['paddle_radius'])



                    if target_pos != self.my_paddle_pos:
                        direction_vector = {'x': target_pos['x'] - self.my_paddle_pos['x'],
                                            'y': target_pos['y'] - self.my_paddle_pos['y']}
                        direction_vector = {k: v / utils.vector_l2norm(direction_vector)
                                            for k, v in direction_vector.items()}

                        movement_dist = min(current_state['paddle_max_speed'] * current_state['delta_t'],
                                            utils.distance_between_points(target_pos, self.my_paddle_pos))
                        direction_vector = {k: v * movement_dist
                                            for k, v in direction_vector.items()}
                        new_paddle_pos = {'x': self.my_paddle_pos['x'] + direction_vector['x'],
                                          'y': self.my_paddle_pos['y'] + direction_vector['y']}

                        # check if computed new position in not inside goal area
                        # check if computed new position in inside board limits
                        if utils.is_inside_goal_area_paddle(new_paddle_pos, current_state) is False and \
                             utils.is_out_of_boundaries_paddle(new_paddle_pos, current_state) is None:
                            self.my_paddle_pos = new_paddle_pos


        # time.sleep(2)
        # return {'x': -12, 'y': -6543}
        return self.my_paddle_pos
Ejemplo n.º 29
0
    def chang_attack(self, current_state, path):
        # CHANG attack
        # find if puck path is inside my interest area
        goalActualSize = current_state['board_shape'][0] * current_state[
            'goal_size']
        pt_in_roi = None
        pastPath = None
        for p in path:
            if pastPath:
                # paths repeating, bounce walls vertically, TODO fine tune
                if abs(pastPath[0]['x'] - p[0]['x']) < 6:
                    if utils.is_inside_goal_area_paddle(
                            p[0], current_state
                    ) is False and utils.is_out_of_boundaries_paddle(
                            p[0], current_state) is None:
                        pt_in_roi = p
                        break
                # paths repeating, bounce  horizontal, TODO fine tune
                if abs(pastPath[0]['y'] - p[0]['y']) < 6:
                    if utils.is_inside_goal_area_paddle(
                            p[0], current_state
                    ) is False and utils.is_out_of_boundaries_paddle(
                            p[0], current_state) is None:
                        pt_in_roi = p
                        pt_in_roi[0]['y'] += current_state['puck_radius']
                        break

            # if future coordinate path is inside our goal
            if utils.distance_between_points(
                    p[0], self.my_goal_center) < goalActualSize:
                pt_in_roi = p
                break
            pastPath = p

        if pt_in_roi:
            # estimate an aiming position
            vertical_unit = current_state['board_shape'][0] / 4
            choice = random.randint(0, 2)
            if current_state['paddle2_pos'][
                    'y'] < vertical_unit or current_state['paddle2_pos'][
                        'y'] > vertical_unit * 3:
                target_pos = utils.aim(pt_in_roi[0], pt_in_roi[1],
                                       self.opponent_goal_center,
                                       current_state['puck_radius'],
                                       current_state['paddle_radius'])
                #print('CENTER SHOT')
            elif current_state['paddle2_pos']['y'] > vertical_unit * 2:
                target_pos = utils.aim(
                    pt_in_roi[0], pt_in_roi[1],
                    self.aim_bounce(pt_in_roi[0], self.opponent_goal_center,
                                    current_state['puck_radius'], 0),
                    current_state['puck_radius'],
                    current_state['paddle_radius'])
                #print('Top SHOT')
            else:
                target_pos = utils.aim(
                    pt_in_roi[0], pt_in_roi[1],
                    self.aim_bounce(pt_in_roi[0], self.opponent_goal_center,
                                    current_state['puck_radius'],
                                    current_state['board_shape'][0]),
                    current_state['puck_radius'],
                    current_state['paddle_radius'])
            # print('Bot SHOT')
            return self.go_Pos(current_state, target_pos)

        return self.my_paddle_pos  # no chang attacks found
Ejemplo n.º 30
0
def move_to_puck_path(current_state, self):
    # estimate puck path
    path = estimate_path(current_state, self.future_size)

    # find if puck path is inside my interest area
    roi_radius = current_state['board_shape'][0] * current_state[
        'goal_size'] * 2
    pt_in_roi = None
    for p in path:
        if utils.distance_between_points(p[0],
                                         self.my_goal_center) < roi_radius:
            pt_in_roi = p
            break

    if pt_in_roi:
        ricochet_point = get_ricochet_point(pt_in_roi[0],
                                            self.opponent_goal_center,
                                            current_state, self)
        # estimate an aiming position
        target_pos = utils.aim(pt_in_roi[0], pt_in_roi[1], ricochet_point,
                               current_state['puck_radius'],
                               current_state['paddle_radius'])

        # move to target position, taking into account the max. paddle speed
        if target_pos != self.my_paddle_pos:
            direction_vector = {
                'x': target_pos['x'] - self.my_paddle_pos['x'],
                'y': target_pos['y'] - self.my_paddle_pos['y']
            }
            direction_vector = {
                k: v / utils.vector_l2norm(direction_vector)
                for k, v in direction_vector.items()
            }

            movement_dist = min(
                current_state['paddle_max_speed'] * current_state['delta_t'],
                utils.distance_between_points(target_pos, self.my_paddle_pos))
            direction_vector = {
                k: v * movement_dist
                for k, v in direction_vector.items()
            }
            new_paddle_pos = {
                'x': self.my_paddle_pos['x'] + direction_vector['x'],
                'y': self.my_paddle_pos['y'] + direction_vector['y']
            }

            # check if computed new position in not inside goal area
            # check if computed new position in inside board limits
            if utils.is_inside_goal_area_paddle(new_paddle_pos, current_state) is False and \
                utils.is_out_of_boundaries_paddle(new_paddle_pos, current_state) is None:

                #print ("Moving to ", target_pos, " with a distance of ", utils.distance_between_points(target_pos, self.my_paddle_pos))
                #print ("Puck distance from target: ", utils.distance_between_points(target_pos, current_state['puck_pos']))
                #Check if the position can be reached
                if utils.distance_between_points(
                        target_pos, self.my_paddle_pos
                ) / 5 < utils.distance_between_points(
                        target_pos, current_state['puck_pos']) / 16:
                    #print ("Can reach ricochet position")
                    self.my_paddle_pos = new_paddle_pos
                else:
                    #print ("Can't reach position by ", utils.distance_between_points(target_pos, self.my_paddle_pos))
                    self.my_paddle_pos = {
                        'x': new_paddle_pos['x'],
                        'y': new_paddle_pos['y']
                    }

    return self.my_paddle_pos