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 check_paddle_valid_move(self, new_pos, previous_pos, state, player): # check if is in move range max_offset = state['paddle_max_speed'] * state['delta_t'] + 0.000001 if utils.distance_between_points(new_pos, previous_pos) > max_offset: raise ValueError('RULES VIOLATION: paddle moved faster than speed limit for ' + player.my_display_name) # check if is inside board limits if utils.is_out_of_boundaries_paddle(new_pos, state) is not None: raise ValueError('RULES VIOLATION: Paddle moved beyond board limits for ' + player.my_display_name) # check if is not inside goal area if utils.is_inside_goal_area_paddle(new_pos, state) is True: raise ValueError('RULES VIOLATION: Paddle moved inside goal area for ' + player.my_display_name) # check if paddle is inside player's area if self.goal_sides['left'] is player and new_pos['x'] > \ self.board.shape[1]/2 - self.state['puck_radius']: raise ValueError('RULES VIOLATION: Paddle moved beyond center line for ' + player.my_display_name) if self.goal_sides['right'] is player and new_pos['x'] < \ self.board.shape[1]/2 + self.state['puck_radius']: raise ValueError('RULES VIOLATION: Paddle moved beyond center line for ' + player.my_display_name) return None
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 bounce(self, current_state, path): pastPath = None for p in path: if pastPath: if abs(pastPath[0]['x'] - p[0]['x']) < 6: if utils.is_inside_goal_area_paddle( p[0], current_state ) is False and utils.is_out_of_boundaries_paddle( p[0], current_state) is None: return True # paths repeating, bounce horizontal, TODO fine tune if abs(pastPath[0]['y'] - p[0]['y']) < 6: if utils.is_inside_goal_area_paddle( p[0], current_state ) is False and utils.is_out_of_boundaries_paddle( p[0], current_state) is None: return True pastPath = p return False
def move_to_center(current_state, self): #return_vector = estimate_path_after_enemy_hit(current_state, self) vector_to_goal = { 'x': self.my_goal_vertical_tangent_x - self.my_paddle_pos['x'], 'y': self.my_goal_center['y'] - self.my_paddle_pos['y'] } #Get the distance vector hyp = math.sqrt(vector_to_goal['x']**2 + vector_to_goal['y']**2) #Get the movement vector if max. speed max_speed = current_state['paddle_max_speed'] * current_state['delta_t'] new_paddle_pos = { 'x': max_speed * vector_to_goal['x'] / hyp + self.my_paddle_pos['x'], 'y': max_speed * vector_to_goal['y'] / hyp + self.my_paddle_pos['y'] } #If the move is illegal, stay put 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: return new_paddle_pos return self.my_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 chang_attack(self, current_state, path): # CHANG attack # find if puck path is inside my interest area goalActualSize = current_state['board_shape'][0] * current_state[ 'goal_size'] pt_in_roi = None pastPath = None for p in path: if pastPath: # paths repeating, bounce walls vertically, TODO fine tune if abs(pastPath[0]['x'] - p[0]['x']) < 6: if utils.is_inside_goal_area_paddle( p[0], current_state ) is False and utils.is_out_of_boundaries_paddle( p[0], current_state) is None: pt_in_roi = p break # paths repeating, bounce horizontal, TODO fine tune if abs(pastPath[0]['y'] - p[0]['y']) < 6: if utils.is_inside_goal_area_paddle( p[0], current_state ) is False and utils.is_out_of_boundaries_paddle( p[0], current_state) is None: pt_in_roi = p pt_in_roi[0]['y'] += current_state['puck_radius'] break # if future coordinate path is inside our goal if utils.distance_between_points( p[0], self.my_goal_center) < goalActualSize: pt_in_roi = p break pastPath = p if pt_in_roi: # estimate an aiming position vertical_unit = current_state['board_shape'][0] / 4 choice = random.randint(0, 2) if current_state['paddle2_pos'][ 'y'] < vertical_unit or current_state['paddle2_pos'][ 'y'] > vertical_unit * 3: target_pos = utils.aim(pt_in_roi[0], pt_in_roi[1], self.opponent_goal_center, current_state['puck_radius'], current_state['paddle_radius']) #print('CENTER SHOT') elif current_state['paddle2_pos']['y'] > vertical_unit * 2: target_pos = utils.aim( pt_in_roi[0], pt_in_roi[1], self.aim_bounce(pt_in_roi[0], self.opponent_goal_center, current_state['puck_radius'], 0), current_state['puck_radius'], current_state['paddle_radius']) #print('Top SHOT') else: target_pos = utils.aim( pt_in_roi[0], pt_in_roi[1], self.aim_bounce(pt_in_roi[0], self.opponent_goal_center, current_state['puck_radius'], current_state['board_shape'][0]), current_state['puck_radius'], current_state['paddle_radius']) # print('Bot SHOT') return self.go_Pos(current_state, target_pos) return self.my_paddle_pos # no chang attacks found
def next_move(self, current_state): #Update paddle pos #Get positions self.my_paddle_pos = current_state['paddle1_pos'] if self.my_goal == 'left' \ else current_state['paddle2_pos'] self.enemy_paddle_pos = current_state['paddle2_pos'] if self.my_goal == 'left' \ else current_state['paddle1_pos'] self.back_off_speed = 3 if self.my_goal == 'left': self.enemy_paddle_speed = current_state['paddle2_speed'] else: self.enemy_paddle_speed = current_state['paddle1_speed'] #To adjust if the player's goal is on the right goal_dir = 1 if self.my_goal == 'right': goal_dir = -1 #Get 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 } #Get the tangent of the most forward goal position for defence reasons if self.my_goal == 'left': self.my_goal_vertical_tangent_x = current_state['board_shape'][ 0] * current_state['goal_size'] / 2 else: self.my_goal_vertical_tangent_x = current_state['board_shape'][ 1] - (current_state['board_shape'][0] * current_state['goal_size'] / 2) #Case 1: The paddle is behind the player but moving towards the enemy goal - try to avoid bouncing it back if current_state['puck_speed']['x'] * goal_dir > 0 and current_state[ 'puck_pos']['x'] * goal_dir < self.my_paddle_pos[ 'x'] * goal_dir: #Move to the centre of the goal new_paddle_pos = move_to_center(current_state, self) 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 #Case 2: the puck is moving towards the player's side elif current_state['puck_speed']['x'] * goal_dir <= self.back_off_speed: #If it's going towards the goal as a direct strike whg = will_hit_goal(current_state, self) if whg == True: #Move to intercept #print("Moving to intercept") new_paddle_pos = move_to_puck_path(current_state, self) 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 whg == 'R': #print("Ricochet period") #If it's a ricochet, maintain x with the puck but stay near the goal new_paddle_pos = move_to_goal(current_state, self) if (new_paddle_pos == -1): new_paddle_pos = self.my_paddle_pos 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: #Avoid it, move to centre new_paddle_pos = move_to_center(current_state, self) 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 #Case 3: The puck is moving away from the player's goal, get to a defensive position elif current_state['puck_speed']['x'] * goal_dir > 0: #new_paddle_pos = approximate_future_height(current_state, self) """new_paddle_pos = move_to_goal(current_state, self) 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""" new_paddle_pos = move_to_goal(current_state, self) if (new_paddle_pos == -1): new_paddle_pos = self.my_paddle_pos 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'] # 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} # 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'] # 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'] # 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. """ 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. """ # 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. """ 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): """ 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): 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 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
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. """ # 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 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. """ #calculate y_coordinates of the goal m_puck = None if not self.goal_y_coordinates: h = current_state['board_shape'][0] self.goal_y_coordinates = ((h / 2) * (1 - current_state['goal_size']), (h / 2) * (1 + current_state['goal_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.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) #recalculate puck path v_x = current_state['puck_speed']['x'] if v_x > 0: v_x_direction = 1 elif v_x < 0: v_x_direction = -1 else: v_x_direction = 0 self.puck_path = calculate_path(current_state) self.last_v_x_direction = v_x_direction goal_dir = None #check if puck is coming to my side, check if it has goal direction if self.puck_path and len(self.puck_path) > 0: if self.my_goal == "left" and v_x_direction < 0: #SI EL PUCK TIENE MENOS DE 2 REBOTES QUE INTERCEPTE EL TIRO A GOL if len(self.puck_path) < 3: path_to_my_side = self.puck_path[-1] #goal_dir = goal_direction(path_to_my_side, self.goal_y_coordinates) #SI EL PUCK TIENE 2 O MAS REBOTES CHECAMOS SI EL ULTIMO REBOTE ESTA CERCA DE LA PORTERIA #PARA INTERCEPTAR MEJOR LA PENULTIMA LINEA EN LUGAR DE LA ULTIMA else: if self.puck_path[-1][0][0] < current_state['board_shape'][ 1] * .20: #20% del largo de la cancha #print('Intercepta 2do') path_to_my_side = self.puck_path[-2] else: path_to_my_side = self.puck_path[-1] goal_dir = goal_direction(self.puck_path[-1], self.goal_y_coordinates) #slope try: #m_puck = (self.puck_path[-1][1][1] - self.puck_path[-1][0][1])/(self.puck_path[-1][1][0] - self.puck_path[-1][0][0]) m_puck = (path_to_my_side[1][1] - path_to_my_side[0][1]) / (path_to_my_side[1][0] - path_to_my_side[0][0]) except ZeroDivisionError: m_puck = 0 elif self.my_goal == "right" and v_x_direction > 0: if len(self.puck_path) < 3: path_to_my_side = self.puck_path[-1] else: if self.puck_path[-1][0][0] > current_state['board_shape'][ 1] * .80: #80% del largo de la cancha #print('Intercepta 2do') path_to_my_side = self.puck_path[-2] else: path_to_my_side = self.puck_path[-1] goal_dir = goal_direction(self.puck_path[-1], self.goal_y_coordinates) #slope try: m_puck = (path_to_my_side[1][1] - path_to_my_side[0][1]) / (path_to_my_side[1][0] - path_to_my_side[0][0]) except ZeroDivisionError: m_puck = 0 # 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.bounce_up = { 'x': current_state['board_shape'][1] * .60 if self.my_goal == 'left' else current_state['board_shape'][1] * .40, 'y': 0 } self.bounce_down = { 'x': current_state['board_shape'][1] * .60 if self.my_goal == 'left' else current_state['board_shape'][1] * .40, 'y': current_state['board_shape'][0] } if self.enemy_paddle_pos['y'] > current_state['board_shape'][0] / 2: self.next_target = self.bounce_up else: self.next_target = self.bounce_down #self.next_target = self.opponent_goal_center target_pos = None #if puck has goal direction, find the nearest point to intercept the puck if goal_dir: if m_puck != 0: m_player = -1 / m_puck b_player = self.my_paddle_pos[ 'y'] - m_player * self.my_paddle_pos['x'] #[-1] es ultima linea, [1] es el punto final, y el [1] es coordenada en y de ese punto b_puck = path_to_my_side[1][1] - m_puck * path_to_my_side[1][0] x = -(b_player - b_puck) / (m_player - m_puck) y = m_player * x + b_player else: x = self.my_paddle_pos['x'] y = self.puck_path[-1][1][1] target_pos = {'x': x, 'y': y} while True: side = 1 if self.my_goal == "left" else -1 if utils.is_inside_goal_area_paddle(target_pos, current_state): if m_puck != 0: x = x + side y = m_player * x + b_player else: x = x + side target_pos = {'x': x, 'y': y} else: break #print(target_pos) #if it is already blocking the goal, aim. Or if the shot is not on target. err = 0.1 if (target_pos and abs(self.my_paddle_pos['x'] - target_pos['x']) / target_pos['x'] < err and abs(self.my_paddle_pos['y'] - target_pos['y']) < err ) or not goal_dir: # 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 self.my_goal == 'left': if current_state['puck_speed']['x'] <= 0 or ( abs(current_state['puck_speed']['x']) < self.hit_puck_twice and current_state['goals']['right'] > current_state['goals']['left']): target_pos = self.aim( pt_in_roi[0], pt_in_roi[1], self.next_target, #self.opponent_goal_center, current_state['puck_radius'], current_state['paddle_radius']) else: target_pos = { 'x': self.starting_pos['x'], 'y': self.starting_pos['y'] } else: #SELF.MY_GOAL == 'RIGHT' if current_state['puck_speed']['x'] >= 0 or ( abs(current_state['puck_speed']['x']) < self.hit_puck_twice and current_state['goals']['left'] > current_state['goals']['right']): target_pos = self.aim( pt_in_roi[0], pt_in_roi[1], self.next_target, #self.opponent_goal_center, current_state['puck_radius'], current_state['paddle_radius']) else: target_pos = { 'x': self.starting_pos['x'], 'y': self.starting_pos['y'] } new_paddle_pos = self.get_new_paddle_pos(target_pos, current_state) # check if computed new position in not inside goal area # check if computed new position in inside board limits if new_paddle_pos: 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: print("invadiendo") if m_puck and len(self.puck_path) < 4: while True: if self.my_goal == 'left': if m_puck != 0: target_pos['x'] +=5 target_pos['y'] = m_player * target_pos['x'] + b_player else: #m_puck = 0 target_pos['x'] += 5 else: #SELF. MY GOAL ES RIGHT if m_puck != 0: target_pos['x'] -= 5 target_pos['y'] = m_player * target_pos['x'] + b_player else: #m_puck = 0 target_pos['x'] -= 5 new_paddle_pos = self.get_new_paddle_pos(target_pos, current_state) 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 break else: pass ''' # time.sleep(2) # return {'x': -12, 'y': -6543} return self.my_paddle_pos