Randmarket.files.wordpress.com



#!/usr/bin/env python3# Import the Halite SDK, which will let you interact with the game.import hlt# This library contains constant values.from hlt import constants# This library contains direction metadata to better interface with the game.from hlt.positionals import Direction, Position# other importsimport randomimport logging#from numpy import *# This game object contains the initial game state.game = hlt.Game()### preprocessamento. Caso queira calcular alguma coisa antes do inicio do jogoship_status = {}ship_last_position = {}### constantsmax_ships = 30min_halite = 30max_dist_dropoff = 12 # otimizar?max_dropoff = 2 # otimizar?### functions# devolve uma lista das cordenas ao redor de uma posi??odef get_full_surrounding_cardinals( posi, level, game_map): list_surrounding = [] # if level=1 then return cardinal return( posi.get_surrounding_cardinals()) # else return all around for x in range(-level,level+1,1): if abs(x) == level: for y in range(-level,level+1,1): temp_posi = game_map.normalize(posi + Position( x, y)) list_surrounding.append( temp_posi) else: temp_posi = game_map.normalize(posi + Position( x, -level)) list_surrounding.append( temp_posi) temp_posi = game_map.normalize(posi + Position( x, level)) list_surrounding.append( temp_posi) return( list_surrounding)# check which way is better to movedef moving( ship, game_map, force_move): new_position = ship.position max_halite = game_map[new_position].halite_amount * constants.MOVE_COST_RATIO max_halite_alternative = 0 # verifica se tem for?a para se mover e n?o esta num lugar com zero H if ship.halite_amount < max_halite and max_halite > min_halite and force_move != True: move = -1 # ship is staying put and harvesting else: # tem bastente H para checar se vale a pena se mexer # pega o quanto tem de halite ao redor e vai no maior se for maior que o lugar atual +10% # verifica se vale a pena mudar de lugar e se tem lugar para mudar e se n?o é o lugar de onde veio for posi in get_full_surrounding_cardinals( ship.position, 1, game_map): if game_map[posi].halite_amount >= max_halite and game_map[posi].is_empty: # and not posi == ship_last_position[ ship.id]: new_position = posi max_halite = game_map[posi].halite_amount # alternative move if game_map[posi].halite_amount >= max_halite_alternative and game_map[posi].is_empty: new_position_alternative = posi max_halite_alternative = game_map[posi].halite_amount # retrona o novo caminho if new_position == ship.position: if force_move == True: # forced to move to the second best option move = game_map.naive_navigate( ship, new_position_alternative) # verifica se tem algum H para ser harvest elif game_map[new_position].halite_amount == 0: # se n?o tinha nenhum H ent?o parece que esta travado => for?a o movimento max_halite = 0 for posi in get_full_surrounding_cardinals( ship.position, 1, game_map): if game_map[posi].halite_amount > max_halite: new_position = posi max_halite = game_map[posi].halite_amount if max_halite == 0: # random move move = random.choice(Direction.get_all_cardinals()) else: move = game_map.naive_navigate( ship, new_position) # ship parece que esta travado else: move = -1 # ship is staying put. Harvest? else: move = game_map.naive_navigate( ship, new_position) return( move)# find the nearest dropoffdef nearest_dropoff_position( ship, list_dropoffs, game_map, me): # start posi = me.shipyard.position # check if there is a dropoff otherwise return the shipyard's position if not list_dropoffs: ("No drop list...") return( posi) # min distance is to the shipyard min_distance = game_map.calculate_distance( ship.position, posi) # go through all dropoffs ("drop list {}" .format(list_dropoffs)) for dropoff in list_dropoffs: if game_map.calculate_distance( ship.position, dropoff.position) <= min_distance: posi = dropoff.position ("near dropoff: {}" .format( posi)) return( posi)# return a lista of all positions within a radiusdef get_search_radius(ship, radius): """ returns a list of all positions within square radius of a ship """ return [ship.position+Position(i, j) for i in range(-radius, radius+1) for j in range(-radius, radius+1)]# DICA: para saber em que turn está usar: game.turn_number# lembrando que o jogo tem de 400 a 500 turns# get the size of the mapmap_width = game.game_map.widthmap_height = game.game_map.("Map Size: {}, {}" .format( map_width, map_height))# ajusta variavel globalmax_dist_dropoff = (map_width-1)*2/4# get the amount of H in each cell of the map#mapa = [0]#for x in range(map_width):# for y in range(map_height):# mapa[x][y] = game.game_map[Position( x, y)].halite_amount# calculate the top 10 location# Respond with your name.game.ready("BootABot 2.1")while True: # Get the latest game state. game.update_frame() # You extract player metadata and the updated map metadata here for convenience. me = game.me game_map = game.game_map # A command queue holds all the commands you will run this turn. command_queue = [] # variaveis temporarias dropoff_bulding = False if not me.get_dropoffs(): dropoff_qtd = 0 else: dropoff_qtd = len( me.get_dropoffs()) nro_ship = len( me.get_ships()) # action for each ship for ship in me.get_ships(): # sanity check for each ship if ship.id not in ship_status: # assign a search pattern if nro_ship < max_ships * 2/3: ship_status[ ship.id] = "exploring" else: ship_status[ ship.id] = "hunting" if ship.id not in ship_last_position: ship_last_position[ ship.id] = ship.position #("0. {} na {} {}" .format(ship.id, ship.position, ship_status[ ship.id])) # verifica se ship ja esta cheio acima de 85% (otimizar) if ship.halite_amount >= constants.MAX_HALITE * 0.90: #ship.is_full: ship_status[ship.id] = "returning" # nearest dropoff or shipyard dropoff_posi = nearest_dropoff_position( ship, me.get_dropoffs(), game_map, me) ## ship is returning if ship_status[ship.id] == "returning": # returning # the ship is the shipyard or dropoff if ship.position == me.shipyard.position or ship.position == dropoff_posi: if nro_ship < max_ships * 2/3: ship_status[ ship.id] = "exploring" else: ship_status[ ship.id] = "hunting" ("1. {} has {} H, no shipyard/dropoff." .format(ship.id, ship.halite_amount)) # vai definir um movimento no proximo 'if' # if nearest dropoff is far then create a new dropoff # and if have enought H and there is not already a dropoff here and there isn't already a dropoff been build this round and there lest than the maximum of dropoffs elif game_map.calculate_distance( ship.position, dropoff_posi) >= max_dist_dropoff and me.halite_amount >= constants.DROPOFF_COST*1.5 and (not game_map[ship.position].has_structure) and dropoff_bulding == False and dropoff_qtd <= max_dropoff: ship_status[ship.id] == "dropoff" dropoff_bulding = True command_queue.append(ship.make_dropoff()) ("2. {} -> Novo dropoff em {}" .format(ship.id, ship.position)) # otherwise go to nearest dropoff/shipyard else: move = game_map.naive_navigate(ship, dropoff_posi) command_queue.append(ship.move(move)) # infos ("3. {} has {} H, going {} {}." .format(ship.id, ship.halite_amount, move, ship_status[ ship.id])) ## ship is exploring if ship_status[ship.id] == "exploring": # checa se ainda esta no shipyard/dropoff e for?a a sair if ship.position == me.shipyard.position or ship.position == dropoff_posi: # movimenta o ship (for?ado) move = moving( ship, game_map, True) command_queue.append( ship.move(move)) game_map[ship.position.directional_offset(move)].mark_unsafe( ship) ("4. {} has {} H, saindo do shipyard p {} {}." .format(ship.id, ship.halite_amount, move, ship_status[ship.id])) # For each of your ships, move if the ship is on a low halite location or the ship is full. elif ship.is_full or game_map[ship.position].halite_amount < min_halite: # ship.halite_amount >= constants.MAX_HALITE * 0.85: # movimenta o ship ou fica se for melhor move = moving( ship, game_map, False) if move == -1: # better stay and harvest command_queue.append(ship.stay_still()) ("5. {} has {} H, harvesting at {}." .format(ship.id, ship.halite_amount, ship.position)) else: command_queue.append( ship.move(move)) #OLD?: game_map[ship.position].mark_unsafe( ship.position.directional_offset(move)) game_map[ship.position.directional_offset(move)].mark_unsafe( ship) ("6. {} has {} H, going {} {}." .format(ship.id, ship.halite_amount, move, ship_status[ship.id])) # verifica se ship ja esta cheio acima de 85% (otimizar) if ship.is_full: # ship.halite_amount >= constants.MAX_HALITE * 0.85: ship_status[ship.id] = "returning" move = game_map.naive_navigate(ship, dropoff_posi) command_queue.append(ship.move(move)) ("7. {} has {} H, going {} {}." .format(ship.id, ship.halite_amount, move, ship_status[ ship.id])) # Else, collect halite. else: command_queue.append(ship.stay_still()) ("8. {} has {} H, harvesting at {}." .format(ship.id, ship.halite_amount, ship.position)) ## ship is 'hunting' if ship_status[ship.id] == "hunting": # checa se ainda esta no shipyard/dropoff ou a menos de 1 de distancia, e for?a a sair if ship.position == me.shipyard.position or ship.position == dropoff_posi or game_map.calculate_distance(ship.position, dropoff_posi) <= 1: # movimenta o ship (for?ado) move = moving( ship, game_map, True) command_queue.append( ship.move(move)) game_map[ship.position.directional_offset(move)].mark_unsafe( ship) ("9. {} has {} H, saindo do shipyard p {} {}." .format(ship.id, ship.halite_amount, move, ship_status[ship.id])) # checa se tem pouco H elif ship.halite_amount < min_halite: # movimenta por perto o ship ou fica se for melhor move = moving( ship, game_map, False) if move == -1: # better stay and harvest command_queue.append(ship.stay_still()) ("10. {} has {} H, harvesting at {}." .format(ship.id, ship.halite_amount, ship.position)) else: command_queue.append( ship.move(move)) game_map[ship.position.directional_offset(move)].mark_unsafe( ship) ("11. {} has {} H, going {} {}." .format(ship.id, ship.halite_amount, move, ship_status[ship.id])) else: # procura o maior deposito de H ao redor do ship grid = get_search_radius(ship, 3) max_halite = 0 position_potencial = ship.position for posi in grid: if game_map[posi].halite_amount > max_halite: position_potencial = posi max_halite = game_map[posi].halite_amount # movimenta o ship if position_potencial == ship.position: # actual position then harvest command_queue.append(ship.stay_still()) ("12. {} has {} H, harvesting at {}." .format(ship.id, ship.halite_amount, ship.position)) else: # move to the new position move = game_map.naive_navigate( ship, position_potencial) command_queue.append( ship.move(move)) game_map[ship.position.directional_offset(move)].mark_unsafe( ship) ("13. {} has {} H, going {} {}." .format(ship.id, ship.halite_amount, move, ship_status[ ship.id])) # marca a posicao como ocupada game_map[ship.position].mark_unsafe( ship) # atualiza a posicao atual ship_last_position[ ship.id] = ship.position # If we're on the 1st half of turns or we've less than the max ships and have enough halite, spawn a ship. # Don't spawn a ship if you currently have a ship at port, though. numero = len( me.get_ships()) if ( game.turn_number <= constants.MAX_TURNS/2 or numero <= max_ships) and me.halite_amount >= constants.SHIP_COST and not game_map[me.shipyard].is_occupied: command_queue.append(game.me.shipyard.spawn()) # ajusta a quantidade máxima de ships if game.turn_number > constants.MAX_TURNS/2: max_ships -= (game.turn_number - constants.MAX_TURNS//2)//20 max_ships = max( max_ships, 10) ("FIM: Tot H:{} Tot Ship {}." .format( me.halite_amount, numero)) # Send your moves back to the game environment, ending this turn. game.end_turn(command_queue) ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download