Skip to content

Instantly share code, notes, and snippets.

@mon231
Last active August 3, 2020 12:45
Show Gist options
  • Save mon231/ff6a8c7450b5f55d23180093b4aa22d6 to your computer and use it in GitHub Desktop.
Save mon231/ff6a8c7450b5f55d23180093b4aa22d6 to your computer and use it in GitHub Desktop.
import random
from math import sqrt
are_there_neutrals = True
current_biggest = None
def get_growth(obj):
return obj.growth_rate()
def get_num_of_ships(obj):
return obj.num_ships()
def get_yahas(obj): #ship for growth
if obj.growth_rate() == 0:
return 0
return obj.num_ships() / obj.growth_rate()
def close_chiph_neutral(obj):
return sqrt(pow(obj.x() - current_biggest.x(), 2) + pow(obj.y() - current_biggest.y(), 2)) * obj.num_ships()
def get_attacked_planets(pw):
mine_attacked_planets = []
enemy_fleets = pw.enemy_fleets()
for enemy_fleet in enemy_fleets:
planet = pw.get_planet(enemy_fleet.destination_planet())
if planet.owner() == 1:
mine_attacked_planets.append(planet)
list(dict.fromkeys(mine_attacked_planets))
return mine_attacked_planets
def do_turn(pw):
if len(pw.my_planets()) == 0:
return
if len(get_attacked_planets(pw)) > 1:
defend(pw)
if not are_there_neutrals:
attack(pw)
else:
attack_neutral(pw)
def defend(pw):
mine_attacked_planets = get_attacked_planets(pw)
if len(mine_attacked_planets) > 0:
urgent_defence_planet = min(mine_attacked_planets, key=get_num_of_ships)
my_planets = sorted(pw.my_planets(), key=get_num_of_ships, reverse=True)
sources_planets = []
sources_prices = []
for i in range(3):
if i >= len(my_planets):
break
if(my_planets[i].num_ships() // 5 > urgent_defence_planet.num_ships()):
pw.issue_order(my_planets[i], urgent_defence_planet, my_planets[i].num_ships() // 3)
return False
def attack(pw):
enemy_planets = pw.enemy_planets()
if len(enemy_planets) > 0:
dst = min(enemy_planets, key=get_yahas)
my_planets = sorted(pw.my_planets(), key=get_num_of_ships, reverse=True)
price_ships = dst.num_ships() + 2
sources_planets = []
sources_prices = []
sum_ships = 0
i = 0
while(i < len(my_planets) and sum_ships / 1.5 < price_ships):
if(my_planets[i].num_ships() >= price_ships * 2.5):
sources_planets.append(my_planets[i])
sources_prices.append(my_planets[i].num_ships() // 2)
sum_ships += sources_prices[-1]
else:
return defend(pw)
for i in range(len(sources_planets)):
pw.issue_order(sources_planets[i], dst, sources_prices[i])
else:
return defend(pw)
def attack_neutral(pw):
global current_biggest
global are_there_neutrals
if len(pw.neutral_planets()) >= 1:
my_planets = sorted(pw.my_planets(), key=get_num_of_ships, reverse=True)
current_biggest = my_planets[0]
dest = min(pw.neutral_planets(), key=close_chiph_neutral)
price_ships = dest.num_ships() + 1
sources_planets = []
sources_prices = []
sum_ships = 0
i = 0
while(sum_ships / 1.5 < price_ships and i < len(my_planets)):
if(my_planets[i].num_ships() >= price_ships * 2.5):
sources_planets.append(my_planets[i])
sources_prices.append(my_planets[i].num_ships() // 2)
sum_ships += sources_prices[-1]
else:
return attack(pw)
for i in range(len(sources_planets)):
pw.issue_order(sources_planets[i], dest, sources_prices[i])
else:
are_there_neutrals = False
attack(pw)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment