def process_goal_for(self, goal_for, puck_to=None): # update scores self.goals[goal_for] += 1 self.state['goals'] = self.goals self.state['is_goal_move'] = goal_for # show goal state in video feed self.resolve_gui() # set new puck position if not puck_to: puck_to = ('left' if goal_for == 'right' else 'right') self.state['puck_pos'] = self.set_random_position_at(puck_to) self.state['puck_speed'] = { 'x': 0, 'y': utils.vector_l2norm(self.state['puck_speed']) } self.in_initial_state = 0 # set paddles to initial position self.state['paddle1_pos'] = { 'x': self.board.shape[0] * self.state['goal_size'] / 2 + 1, 'y': self.board.shape[0] / 2 } self.state['paddle2_pos'] = { 'x': self.board.shape[1] - self.board.shape[0] * self.state['goal_size'] / 2 - 1, 'y': self.board.shape[0] / 2 } return
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
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 """
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
def aim(self, pos, speed, pos_target, puck_radius, paddle_radius): """ Function that computes where to put the paddle for a target puck position Args: pos: puck position speed: puck speed pos_target: target position of puck Returns: dict: paddle position to achieve puck target position """ # target direction vector, normalized, opposite direction dir_vector = { 'x': pos_target['x'] - pos['x'], 'y': pos_target['y'] - pos['y'] } dir_vector = { k: -1 * v / utils.vector_l2norm(dir_vector) for k, v in dir_vector.items() } # normalized puck speed speed_n = {k: v / utils.vector_l2norm(speed) for k, v in speed.items()} intersection_vector = { 'x': dir_vector['x'] + speed_n['x'], 'y': dir_vector['y'] + speed_n['y'] } intersection_vector = { k: v / utils.vector_l2norm(intersection_vector) for k, v in intersection_vector.items() } # length of collision point from pos intersection_vector = { k: v * (puck_radius + paddle_radius) for k, v in intersection_vector.items() } # return { 'x': pos['x'] + intersection_vector['x'], 'y': pos['y'] + intersection_vector['y'] }
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
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
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
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'] * 2 pt_in_roi = None valor_tupla = None for p in path: if utils.distance_between_points(p[0], self.my_goal_center) < roi_radius: pt_in_roi = p break #print(type(pt_in_roi)) 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: #CAMBIAR A QUE CUANDO EL PADDLE ENEMIGO TOCA EL POCK SE DEJE DE MOVER HACIA EL CENTRO #juego lado izquierdo #vision = 0 if self.my_goal == 'left': if current_state['puck_pos']['x'] > 475: direction_vector = {'x': 150 - self.my_paddle_pos['x'], 'y': 256 - self.my_paddle_pos['y']} #QUE CUANDO NOS TOCA EL SAQUE LE PEGUE Y REGRESE AL CENTRO elif current_state['puck_pos']['x'] == 248.75: direction_vector = {'x': current_state['puck_pos']['x'] - 20 - self.my_paddle_pos['x'], 'y': 0} else: if self.my_paddle_pos['y'] <= 165: direction_vector = {'x': current_state['puck_pos']['y'] / 1.1 - self.my_paddle_pos['x'], 'y': current_state['puck_pos']['y']-self.my_paddle_pos['y']} #vertical elif self.my_paddle_pos['y'] > 165 and self.my_paddle_pos['y'] < 347: direction_vector = {'x': 150 - self.my_paddle_pos['x'], 'y': current_state['puck_pos']['y']-self.my_paddle_pos['y']} #arriba else: direction_vector = {'x': ((- 512 + current_state['puck_pos']['y']) / -1.1) - self.my_paddle_pos['x'], 'y': current_state['puck_pos']['y']-self.my_paddle_pos['y']} #Lado Derecho else: if current_state['puck_pos']['x'] < 475: direction_vector = {'x': 845 - self.my_paddle_pos['x'], 'y': 256 - self.my_paddle_pos['y']} #QUE CUANDO NOS TOCA EL SAQUE LE PEGUE Y REGRESE AL CENTRO elif current_state['puck_pos']['x'] == 746.25: direction_vector = {'x': current_state['puck_pos']['x'] + 20 - self.my_paddle_pos['x'], 'y': 0} else: if self.my_paddle_pos['y'] <= 165: direction_vector = {'x': (-(current_state['board_shape'][1] + 99.5) + current_state['puck_pos']['y']) / -1.1 - self.my_paddle_pos['x'], 'y': current_state['puck_pos']['y']-self.my_paddle_pos['y']} #vertical elif self.my_paddle_pos['y'] > 165 and self.my_paddle_pos['y'] < 347: direction_vector = {'x': 845 - self.my_paddle_pos['x'], 'y': current_state['puck_pos']['y']-self.my_paddle_pos['y']} #arriba else: direction_vector = {'x': (-(582.5 + current_state['puck_pos']['y']) / -1.1 ) - 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(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
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'] # computing both goal centers self.my_goal_top = { 'x': 100 if self.my_goal == 'left' else 800, 'y': (current_state['board_shape'][0] / 2) + ((current_state['board_shape'][0] * 0.45) / 2) } self.my_goal_down = { 'x': 100 if self.my_goal == 'left' else 800, 'y': (current_state['board_shape'][0] / 2) - ((current_state['board_shape'][0] * 0.45) / 2) } # estimate an aiming position if (self.up): target_pos = self.my_goal_down self.up = False else: target_pos = self.my_goal_top self.up = True # 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 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
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} #This is a test #Se sacan coordenadas de los vertices para el triangulo if self.my_goal_center.get('x') == 0: vCentroX = current_state['board_shape'][1]/2*(2/3) else: vCentroX = current_state['board_shape'][1]*(2/3) verticeCentro = [vCentroX, current_state['board_shape'][0]/2] verticeArriba = [self.my_goal_center.get('x'), 0] verticeAbajo = [self.my_goal_center.get('x'), current_state['board_shape'][0]] #Sacando las pendientes del triangulo if self.my_goal_center.get('x')==0: mUp = (verticeCentro[1]-verticeArriba[1])/(verticeCentro[0]-verticeArriba[0]) mDown = (verticeCentro[1]-verticeAbajo[1])/(verticeCentro[0]-verticeAbajo[0]) else: mUp = (verticeCentro[1]-verticeAbajo[1])/(verticeCentro[0]-verticeAbajo[0]) mDown = (verticeCentro[1]-verticeArriba[1])/(verticeCentro[0]-verticeArriba[0]) bUp= current_state['board_shape'][0]-mUp*(current_state['board_shape'][1]) bDown= 0-mDown*(current_state['board_shape'][1]) # find if puck path is inside my interest area pt_in_roi = None for p in path: #if utils.distance_between_points(p[0], self.my_goal_center)>utils.distance_between_points(self.my_paddle_pos, self.my_goal_center): bA = p[0]['y']-mUp*p[0]['x'] bB = p[0]['y']-mDown*p[0]['x'] if self.my_goal_center.get('x')==0: if bA>0 and bB<current_state['board_shape'][0]: pt_in_roi = p break else: if bA<bUp and bB>bDown: pt_in_roi = p break #Aqui se busca a donde apuntar para que no siempre sea en el centro medidaPorteria = (current_state['goal_size']*current_state['board_shape'][0]) apunta = {} if self.my_goal_center.get('x')==0: razon = current_state['paddle2_pos']['y']/current_state['board_shape'][0] movY = self.opponent_goal_center.get('y') - (medidaPorteria/2) + (1-razon)* medidaPorteria else: razon = current_state['paddle1_pos']['y']/current_state['board_shape'][0] movY = self.opponent_goal_center.get('y') - medidaPorteria/2 + (1-razon)* medidaPorteria apunta['x']= self.opponent_goal_center.get('x') apunta['y']= movY #tiro con rebote if (current_state['paddle1_pos']['y']>current_state['board_shape'][0]/4) and (current_state['paddle1_pos']['y']<current_state['board_shape'][0]*3/4) and (current_state['paddle2_pos']['y']>current_state['board_shape'][0]/4) and (current_state['paddle2_pos']['y']<current_state['board_shape'][0]*3/4): if self.my_goal_center.get('x') == 0: ypos= current_state['board_shape'][0]-current_state['paddle2_pos']['y'] d= ypos-current_state['puck_radius']*2 puntoD= [current_state['paddle2_pos']['x'], current_state['paddle2_pos']['y']+d] else: ypos= current_state['board_shape'][0]-current_state['paddle1_pos']['y'] d= ypos-current_state['puck_radius']*2 puntoD= [current_state['paddle1_pos']['x'], current_state['paddle1_pos']['y']+d] mD= (apunta['y']-puntoD[1])/(apunta['x']-puntoD[0]) bD= puntoD[1]-mD*puntoD[0] posX= (current_state['board_shape'][0]-current_state['puck_radius']-bD)/mD apunta['x']= posX apunta['y']= current_state['board_shape'][0]-current_state['puck_radius'] if pt_in_roi: # estimate an aiming position target_pos = utils.aim(pt_in_roi[0], pt_in_roi[1], apunta, 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 else: direction_vector = {'x': verticeCentro [0] - self.my_paddle_pos['x'], 'y': verticeCentro[1]- 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({'x': verticeCentro [0],'y': verticeCentro[1] }, 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']} 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. """ # 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
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
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
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
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
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'] * 2 -100 ######## 300 campo de vision #########################################################3 pt_in_roi = None for p in path: #print(utils.distance_between_points(p[0], self.my_goal_center), '<', roi_radius) if utils.distance_between_points(p[0], self.my_goal_center) < roi_radius: pt_in_roi = p break #Si el puck esta dentro de la cancha... if pt_in_roi: #Anti Auto-Gol goalR = current_state['board_shape'][0]*0.45/2 #Checar si el puck esta detras de mi puckPos = current_state['puck_pos'] if self.my_goal is 'left': #Si estoy en la izquierda if puckPos['x'] < self.my_paddle_pos['x']: #Si el puck esta detras de mi... #print("Puck esta detras de mi!") #Clalcular direccion del puck path = estimate_path(current_state, self.future_size) target_pos = {'x': current_state['board_shape'][0]*0.45/2, 'y': current_state['board_shape'][0]/2} moveToTarget = True for x in path: if x[1]['x'] > 0: #Puck va hacia la porteria contraria #print("Puck va hacia el enemigo ", x[1]['x']) #Me tengo que mover en diagonal para que no me pegue············································································· #1. Calculo mi vector de direccion hacia mi posicion de defensa de mi paddle target_pos = {'x': current_state['board_shape'][0]*0.45/2, '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(x[0], self.my_paddle_pos)) direction_vector = {k: v * movement_dist for k, v in direction_vector.items()} #Obtener path del paddle, basandome en la direccion del puck paddlepath = estimate_path_paddle(current_state, self.future_size, self.my_paddle_pos, direction_vector) for y in paddlepath: #Si el path de mi paddle intersecta el path de mi puck if y[0]['x'] > x[0]['x'] - current_state['puck_radius'] and y[0]['x'] < x[0]['x'] + current_state['puck_radius'] and y[0]['y'] > x[0]['y'] - current_state['puck_radius'] and y[0]['y'] < x[0]['y'] + current_state['puck_radius']: #print("Intersecta!") if self.my_paddle_pos['y'] > current_state['board_shape'][0]/2: target_pos = {'x': current_state['board_shape'][0]*0.45/2, 'y': current_state['board_shape'][0] - current_state['puck_radius']} #print("Me muevo pa arriba") else: target_pos = {'x': current_state['board_shape'][0]*0.45/2, 'y': current_state['puck_radius']} #print("Me muevo pa abajo") else: target_pos = {'x': current_state['board_shape'][0]*0.45/2, 'y': current_state['board_shape'][0]/2} #print("Me muevo normal") 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(x[0], 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: #Rebote ############################################################################################# pos_enemigo = current_state['paddle2_pos'] if pos_enemigo['y'] > self.my_goal_center['y']: #Si esta arriba... lineaRebote = current_state['puck_radius'] A = current_state['puck_pos'] D = {'x': A['x'], 'y': A['y']-lineaRebote} B = self.opponent_goal_center else: #Esta abajo... lineaRebote = current_state['board_shape'][0] - current_state['puck_radius'] A = current_state['puck_pos'] D = {'x': A['x'], 'y': -(A['y']) + current_state['puck_radius']} B = self.opponent_goal_center #Recta de rebote, donde corta en el eje y del radio del puck m1 = 0 n1 = lineaRebote #Recta entre B y D m2 = (D['y'] - B['y']) / (D['x'] - B['x']) n2 = (B['x']*D['y'] - D['x']*B['y']) / (B['x'] - D['x']) Cx = (n2 - n1) / (m1 - m2) Cy = m1 * Cx + n1 C = {'x': Cx, 'y': Cy} #print(current_state['paddle1_pos']) # estimate an aiming position target_pos = utils.aim(pt_in_roi[0], pt_in_roi[1], C, 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'], #donde quiero ir, donde estoy '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']} #Rectas del Triangulo ###################################################################################################### if self.my_goal is 'left': #Si estoy en la izquierda R1 = (100,0,(current_state['board_shape'][1]/2)-64,(current_state['board_shape'][0]/2)-64) R2 = (100,(current_state['board_shape'][0]),(current_state['board_shape'][1]/2)-64,(current_state['board_shape'][0]/2)-64) else: #Si estoy en la derecha R1 = (current_state['board_shape'][1]-100,0, (current_state['board_shape'][1]/2)+64, (current_state['board_shape'][0])/2) R2 = ((current_state['board_shape'][1]/2)+64, (current_state['board_shape'][0])/2, current_state['board_shape'][1]+100, current_state['board_shape'][0]) m1 = (R1[1]-R1[3])/(R1[0]-R1[2]) m2 = (R2[1]-R2[3])/(R2[0]-R2[2]) n1 = (R1[0]*R1[3] - R1[2]*R1[1]) / (R1[0] - R1[2]) n2 = (R2[0]*R2[3] - R2[2]*R2[1]) / (R2[0] - R2[2]) # Izquierda # R1 | Y = 0.4429065743944637x + -0.0 # R2 | Y = -0.5905420991926182x + 448.0 # Derecha # R1 | Y = -0.5905420991926182 x + 587.5893886966551 # R2 | Y = 0.5905420991926182 x + -75.58938869665513 #print("Y = ",m1,"x + ", n1) #print("Y = ",m2,"x + ", n2) #Calcular n de ambas rectas paralelas n1p = new_paddle_pos['y'] - new_paddle_pos['x'] * m1 n2p = new_paddle_pos['y'] - new_paddle_pos['x'] * m2 #Si me movimiento se va a pasar de la recta, no me muevo. if n1p < n1 or n2p > n2: new_paddle_pos = {'x': self.my_paddle_pos['x'], 'y': self.my_paddle_pos['y']} #print("Paralelo contra R1 = Y = ",m1," x + ",n1) #print("Paralelo contra R2 = Y = ",m2," x + ",n2) # 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 triangle 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: #Si estoy en la derecha *************************************************************************************************** if puckPos['x'] > self.my_paddle_pos['x']: #Si el puck esta detras de mi... #print("Puck esta detras de mi!") #Clalcular direccion del puck goalR = current_state['board_shape'][0]*0.45/2 path = estimate_path(current_state, self.future_size) target_pos = {'x': current_state['board_shape'][1] - goalR, 'y': current_state['board_shape'][0]/2} for x in path: if x[1]['x'] < 0: #Puck va hacia la porteria contraria #print("Puck va hacia el enemigo ", x[1]['x']) #Me tengo que mover en diagonal para que no me pegue············································································· #1. Calculo mi vector de direccion hacia mi posicion de defensa de mi paddle target_pos = {'x': current_state['board_shape'][1] - goalR, '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(x[0], self.my_paddle_pos)) direction_vector = {k: v * movement_dist for k, v in direction_vector.items()} #Obtener path del paddle, basandome en la direccion del puck paddlepath = estimate_path_paddle(current_state, self.future_size, self.my_paddle_pos, direction_vector) for y in paddlepath: #Si el path de mi paddle intersecta el path de mi puck if y[0]['x'] > x[0]['x'] - current_state['puck_radius'] and y[0]['x'] < x[0]['x'] + current_state['puck_radius'] and y[0]['y'] > x[0]['y'] - current_state['puck_radius'] and y[0]['y'] < x[0]['y'] + current_state['puck_radius']: #print("Intersecta!") if self.my_paddle_pos['y'] > current_state['board_shape'][0]/2: target_pos = {'x': current_state['board_shape'][1] - goalR, 'y': current_state['board_shape'][0] - current_state['puck_radius']} #print("Me muevo pa arriba") else: target_pos = {'x': current_state['board_shape'][1] - goalR, 'y': current_state['puck_radius']} #print("Me muevo pa abajo") else: target_pos = {'x': current_state['board_shape'][1] - goalR, 'y': current_state['board_shape'][0]/2} #print("Me muevo normal") 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(x[0], 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: #Rebote ############################################################################################# pos_enemigo = current_state['paddle2_pos'] if pos_enemigo['y'] > self.my_goal_center['y']: #Si esta arriba... lineaRebote = current_state['puck_radius'] A = current_state['puck_pos'] D = {'x': A['x'], 'y': A['y']-lineaRebote} B = self.opponent_goal_center else: #Esta abajo... lineaRebote = current_state['board_shape'][0] - current_state['puck_radius'] A = current_state['puck_pos'] D = {'x': A['x'], 'y': -(A['y']) + current_state['puck_radius']} B = self.opponent_goal_center #Recta de rebote, donde corta en el eje y del radio del puck m1 = 0 n1 = lineaRebote #Recta entre B y D m2 = (D['y'] - B['y']) / (D['x'] - B['x']) n2 = (B['x']*D['y'] - D['x']*B['y']) / (B['x'] - D['x']) Cx = (n2 - n1) / (m1 - m2) Cy = m1 * Cx + n1 C = {'x': Cx, 'y': Cy} #print(current_state['paddle1_pos']) # estimate an aiming position target_pos = utils.aim(pt_in_roi[0], pt_in_roi[1], C, 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'], #donde quiero ir, donde estoy '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']} #Rectas del Triangulo ###################################################################################################### if self.my_goal is 'left': #Si estoy en la izquierda R1 = (100,0,(current_state['board_shape'][1]/2)-64,(current_state['board_shape'][0]/2)-64) R2 = (100,(current_state['board_shape'][0]),(current_state['board_shape'][1]/2)-64,(current_state['board_shape'][0]/2)-64) else: #Si estoy en la derecha R1 = (current_state['board_shape'][1]-100,0, (current_state['board_shape'][1]/2)+64, (current_state['board_shape'][0])/2) R2 = ((current_state['board_shape'][1]/2)+64, (current_state['board_shape'][0])/2, current_state['board_shape'][1]+100, current_state['board_shape'][0]) m1 = (R1[1]-R1[3])/(R1[0]-R1[2]) m2 = (R2[1]-R2[3])/(R2[0]-R2[2]) n1 = (R1[0]*R1[3] - R1[2]*R1[1]) / (R1[0] - R1[2]) n2 = (R2[0]*R2[3] - R2[2]*R2[1]) / (R2[0] - R2[2]) # Izquierda # R1 | Y = 0.4429065743944637x + -0.0 # R2 | Y = -0.5905420991926182x + 448.0 # Derecha # R1 | Y = -0.5905420991926182 x + 587.5893886966551 # R2 | Y = 0.5905420991926182 x + -75.58938869665513 #print("Y = ",m1,"x + ", n1) #print("Y = ",m2,"x + ", n2) #Calcular n de ambas rectas paralelas n1p = new_paddle_pos['y'] - new_paddle_pos['x'] * m1 n2p = new_paddle_pos['y'] - new_paddle_pos['x'] * m2 #Si me movimiento se va a pasar de la recta, no me muevo. if n1p < n1 or n2p > n2: new_paddle_pos = {'x': self.my_paddle_pos['x'], 'y': self.my_paddle_pos['y']} #print("Paralelo contra R1 = Y = ",m1," x + ",n1) #print("Paralelo contra R2 = Y = ",m2," x + ",n2) # 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 triangle 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 # Jugador se regresa a su porteria si el puck no esta en su lado de la cancha. ################################################3 else: goalR = current_state['board_shape'][0]*0.45/2 if self.my_goal is 'left': #Si estoy en la izquierda target_pos = {'x': current_state['board_shape'][0]*0.45/2, 'y': current_state['board_shape'][0]/2} #current_state['board_shape'][0]/2 else: #Si estoy en la derecha target_pos = {'x': current_state['board_shape'][1] - goalR, 'y': current_state['board_shape'][0]/2} #current_state['board_shape'][0]/2 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
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.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': 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 self.my_goal== 'left': # if current_state['puck_pos']['x'] > current_state['board_shape'][1]/2: # new_paddle_pos = {'x': self.my_paddle_pos['x'] - 5, # 'y': self.my_paddle_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: # self.my_paddle_pos = new_paddle_pos # return self.my_paddle_pos # if self.my_goal== 'right': # if current_state['puck_pos']['x'] < current_state['board_shape'][1]/2: # new_paddle_pos = {'x': self.my_paddle_pos['x'] + 5, # 'y': self.my_paddle_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: # self.my_paddle_pos = new_paddle_pos # return self.my_paddle_pos int_radio = current_state['board_shape'][0] * current_state['goal_size'] * 2 int_area = None for p in path: if utils.distance_between_points(p[0], self.my_goal_center) < int_radio: int_area = p break if int_area: target = utils.aim(int_area[0], int_area[1], self.opponent_goal_center, current_state['puck_radius'], current_state['paddle_radius']) if target != self.my_paddle_pos: direction = {'x': target['x'] - self.my_paddle_pos['x'], 'y': target['y'] - self.my_paddle_pos['y']} direction = {k: v / utils.vector_l2norm(direction) for k, v in direction.items()} distance = min(current_state['paddle_max_speed'] * current_state['delta_t'], utils.distance_between_points(target, self.my_paddle_pos)) direction = {k: v * distance for k, v in direction.items()} new_paddle_pos = {'x': self.my_paddle_pos['x'] + direction['x'], 'y': self.my_paddle_pos['y'] + direction['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: target = self.my_goal_center if target != self.my_paddle_pos: direction = {'x': target['x'] - self.my_paddle_pos['x'], 'y': target['y'] - self.my_paddle_pos['y']} direction = {k: v / utils.vector_l2norm(direction) for k, v in direction.items()} distance = min(current_state['paddle_max_speed'] * current_state['delta_t'], utils.distance_between_points(target, self.my_paddle_pos)) direction = {k: v * distance for k, v in direction.items()} new_paddle_pos = {'x': self.my_paddle_pos['x'] + direction['x'], 'y': self.my_paddle_pos['y'] + direction['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
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
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
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
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