"""

Created on Thu Jan 11 09:26:46 2018

@author: Michael

"""

import os

import time

ship_requirement = 10

damage_requirement = 1000

def get_ships(data):

return int(data.split("producing ")[1].split(" ships")[0])

def get_damage(data):

return int(data.split("dealing ")[1].split(" damage")[0])

def get_rank(data):

return int(data.split("rank #")[1].split(" and")[0])

player_1_wins = 0

player_2_wins = 0

player_3_wins = 0

player_4_wins = 0

for num in range(20000):

try:

print("Currently on: {}".format(num))

if player_1_wins > 0 or player_2_wins > 0 or player_3_wins > 0 or player_4_wins >0:

p1_pct = round(player_1_wins/(player_1_wins+player_2_wins+player_3_wins+player_4_wins)*100.0, 4)

p2_pct = round(player_2_wins/(player_1_wins+player_2_wins+player_3_wins+player_4_wins)*100.0, 4)

p3_pct = round(player_3_wins/(player_1_wins+player_2_wins+player_3_wins+player_4_wins)*100.0, 4)

p4_pct = round(player_4_wins/(player_1_wins+player_2_wins+player_3_wins+player_4_wins)*100.0, 4)

print("Player 1 win: {}%; Player 2 win: {}%; Player 3 win: {}%; Player 4 win: {}%.".format(p1_pct, p2_pct, p3_pct, p4_pct))

os.system('halite.exe -d "240 160" "python MyBot-1.py" "python MyBot-2.py" "python MyBot-3.py" "python MyBot-4.py" >> data.gameout')

with open('data.gameout', 'r') as f:

contents = f.readlines()

CharlesBot1 = contents[-4]

CharlesBot2 = contents[-3]

CharlesBot3 = contents[-2]

CharlesBot4 = contents[-1]

print(CharlesBot1)

print(CharlesBot2)

print(CharlesBot3)

print(CharlesBot4)

CharlesBot1_ships = get_ships(CharlesBot1)

CharlesBot1_dmg = get_damage(CharlesBot1)

CharlesBot1_rank = get_rank(CharlesBot1)

CharlesBot2_ships = get_ships(CharlesBot2)

CharlesBot2_dmg = get_damage(CharlesBot2)

CharlesBot2_rank = get_rank(CharlesBot2)

CharlesBot3_ships = get_ships(CharlesBot3)

CharlesBot3_dmg = get_damage(CharlesBot3)

CharlesBot3_rank = get_rank(CharlesBot3)

CharlesBot4_ships = get_ships(CharlesBot4)

CharlesBot4_dmg = get_damage(CharlesBot4)

CharlesBot4_rank = get_rank(CharlesBot4)

print("Charles1 rank: {} ships: {} dmg: {}".format(CharlesBot1_rank,CharlesBot1_ships,CharlesBot1_dmg))

print("Charles2 rank: {} ships: {} dmg: {}".format(CharlesBot2_rank,CharlesBot2_ships,CharlesBot2_dmg))

print("Charles3 rank: {} ships: {} dmg: {}".format(CharlesBot3_rank,CharlesBot3_ships,CharlesBot3_dmg))

print("Charles4 rank: {} ships: {} dmg: {}".format(CharlesBot4_rank,CharlesBot4_ships,CharlesBot4_dmg))

if CharlesBot1_rank == 1:

print("c1 won")

player_1_wins += 1

if CharlesBot1_ships >= ship_requirement and CharlesBot1_dmg >= damage_requirement:

with open("c1_input.vec","r") as f:

input_lines = f.readlines()

with open("train.in","a") as f:

for l in input_lines:

f.write(l)

with open("c1_out.vec","r") as f:

output_lines = f.readlines()

with open("train.out","a") as f:

for l in output_lines:

f.write(l)

elif CharlesBot2_rank == 1:

print("c2 won")

player_2_wins += 1

if CharlesBot2_ships >= ship_requirement and CharlesBot2_dmg >= damage_requirement:

with open("c2_input.vec","r") as f:

input_lines = f.readlines()

with open("train.in","a") as f:

for l in input_lines:

f.write(l)

with open("c2_out.vec","r") as f:

output_lines = f.readlines()

with open("train.out","a") as f:

for l in output_lines:

f.write(l)

elif CharlesBot3_rank == 1:

print("c3 won")

player_3_wins += 1

if CharlesBot3_ships >= ship_requirement and CharlesBot3_dmg >= damage_requirement:

with open("c3_input.vec","r") as f:

input_lines = f.readlines()

with open("train.in","a") as f:

for l in input_lines:

f.write(l)

with open("c3_out.vec","r") as f:

output_lines = f.readlines()

with open("train.out","a") as f:

for l in output_lines:

f.write(l)

elif CharlesBot4_rank == 1:

print("c4 won")

player_4_wins += 1

if CharlesBot4_ships >= ship_requirement and CharlesBot4_dmg >= damage_requirement:

with open("c4_input.vec","r") as f:

input_lines = f.readlines()

with open("train.in","a") as f:

for l in input_lines:

f.write(l)

with open("c4_out.vec","r") as f:

output_lines = f.readlines()

with open("train.out","a") as f:

for l in output_lines:

f.write(l)

time.sleep(2)

except Exception as e:

print(str(e))

time.sleep(2)

(tsnsflow2) C:\Users\Michael>cd halitechoicebot

(tsnsflow2) C:\Users\Michael\halitechoicebot>python dcreat4p.py

Currently on: 0

The process cannot access the file because it is being used by another process.

Init Message received from player 3, Traceback (most recent call la.

Init Message received from player 2, Traceback (most recent call la.

Init Message received from player 0, Traceback (most recent call la.

Turn 1

list index out of range

Currently on: 1

The process cannot access the file because it is being used by another process.

Init Message received from player 3, Traceback (most recent call la.

Init Message received from player 2, Traceback (most recent call la.

Init Message received from player 0, Traceback (most recent call la.

Turn 1

list index out of range

Currently on: 2

The process cannot access the file because it is being used by another process.

Init Message received from player 3, Traceback (most recent call la.

Init Message received from player 2, Traceback (most recent call la.

Init Message received from player 0, Traceback (most recent call la.

Turn 1

list index out of range

Currently on: 3

The process cannot access the file because it is being used by another process.

Init Message received from player 3, Traceback (most recent call la.

Init Message received from player 2, Traceback (most recent call la.

Init Message received from player 0, Traceback (most recent call la.

Turn 1

list index out of range

Currently on: 4

The process cannot access the file because it is being used by another process.

Init Message received from player 3, Traceback (most recent call la.

Init Message received from player 2, Traceback (most recent call la.

Init Message received from player 0, Traceback (most recent call la.

Turn 1

list index out of range

MyBot-1,MyBot-2,MyBot-3,MyBot-4 with only the version number that changes between them

import hlt

import logging

from collections import OrderedDict

import numpy as np

import random

import os

VERSION = 1

HM_ENT_FEATURES = 5

PCT_CHANGE_CHANCE = 30

DESIRED_SHIP_COUNT = 20

game = hlt.Game("Charles{}".format(VERSION))

logging.info("CharlesBot-{} Start".format(VERSION))

ship_plans = {}

if os.path.exists("c{}_input.vec".format(VERSION)):

os.remove("c{}_input.vec".format(VERSION))

if os.path.exists("c{}_out.vec".format(VERSION)):

os.remove("c{}_out.vec".format(VERSION))

def key_by_value(dictionary, value):

for k,v in dictionary.items():

if v[0] == value:

return k

return -99

def fix_data(data):

new_list = []

last_known_idx = 0

for i in range(HM_ENT_FEATURES):

try:

if i < len(data):

last_known_idx=i

new_list.append(data[last_known_idx])

except:

new_list.append(0)

return new_list

while True:

game_map = game.update_map()

command_queue = []

team_ships = game_map.get_me().all_ships()

all_ships = game_map._all_ships()

enemy_ships = [ship for ship in game_map._all_ships() if ship not in team_ships]

my_ship_count = len(team_ships)

enemy_ship_count = len(enemy_ships)

all_ship_count = len(all_ships)

my_id = game_map.get_me().id

empty_planet_sizes = {}

our_planet_sizes = {}

enemy_planet_sizes = {}

for p in game_map.all_planets():

radius = p.radius

if not p.is_owned():

empty_planet_sizes[radius] = p

elif p.owner.id == game_map.get_me().id:

our_planet_sizes[radius] = p

elif p.owner.id != game_map.get_me().id:

enemy_planet_sizes[radius] = p

hm_our_planets = len(our_planet_sizes)

hm_empty_planets = len(empty_planet_sizes)

hm_enemy_planets = len(enemy_planet_sizes)

empty_planet_keys = sorted([k for k in empty_planet_sizes])[::-1]

our_planet_keys = sorted([k for k in our_planet_sizes])[::-1]

enemy_planet_keys= sorted([k for k in enemy_planet_sizes])[::-1]

for ship in game_map.get_me().all_ships():

try:

if ship.docking_status != ship.DockingStatus.UNDOCKED:

# Skip this ship

continue

shipid = ship.id

change = False

if random.randint(1,100) <= PCT_CHANGE_CHANCE:

change = True

entities_by_distance = game_map.nearby_entities_by_distance(ship)

entities_by_distance = OrderedDict(sorted(entities_by_distance.items(), key=lambda t: t[0]))

closest_empty_planets = [entities_by_distance[distance][0] for distance in entities_by_distance if isinstance(entities_by_distance[distance][0], hlt.entity.Planet) and not entities_by_distance[distance][0].is_owned()]

closest_empty_planet_distances = [distance for distance in entities_by_distance if isinstance(entities_by_distance[distance][0], hlt.entity.Planet) and not entities_by_distance[distance][0].is_owned()]

closest_my_planets = [entities_by_distance[distance][0] for distance in entities_by_distance if isinstance(entities_by_distance[distance][0], hlt.entity.Planet) and entities_by_distance[distance][0].is_owned() and (entities_by_distance[distance][0].owner.id == game_map.get_me().id)]

closest_my_planets_distances = [distance for distance in entities_by_distance if isinstance(entities_by_distance[distance][0], hlt.entity.Planet) and entities_by_distance[distance][0].is_owned() and (entities_by_distance[distance][0].owner.id == game_map.get_me().id)]

closest_enemy_planets = [entities_by_distance[distance][0] for distance in entities_by_distance if isinstance(entities_by_distance[distance][0], hlt.entity.Planet) and entities_by_distance[distance][0] not in closest_my_planets and entities_by_distance[distance][0] not in closest_empty_planets]

closest_enemy_planets_distances = [distance for distance in entities_by_distance if isinstance(entities_by_distance[distance][0], hlt.entity.Planet) and entities_by_distance[distance][0] not in closest_my_planets and entities_by_distance[distance][0] not in closest_empty_planets]

closest_team_ships = [entities_by_distance[distance][0] for distance in entities_by_distance if isinstance(entities_by_distance[distance][0], hlt.entity.Ship) and entities_by_distance[distance][0] in team_ships]

closest_team_ships_distances = [distance for distance in entities_by_distance if isinstance(entities_by_distance[distance][0], hlt.entity.Ship) and entities_by_distance[distance][0] in team_ships]

closest_enemy_ships = [entities_by_distance[distance][0] for distance in entities_by_distance if isinstance(entities_by_distance[distance][0], hlt.entity.Ship) and entities_by_distance[distance][0] not in team_ships]

closest_enemy_ships_distances = [distance for distance in entities_by_distance if isinstance(entities_by_distance[distance][0], hlt.entity.Ship) and entities_by_distance[distance][0] not in team_ships]

largest_empty_planet_distances = []

largest_our_planet_distances = []

largest_enemy_planet_distances = []

for i in range(HM_ENT_FEATURES):

try: largest_empty_planet_distances.append(key_by_value(entities_by_distance, empty_planet_sizes[empty_planet_keys[i]]))

except:largest_empty_planet_distances.append(-99)

try: largest_our_planet_distances.append(key_by_value(entities_by_distance, our_planet_sizes[our_planet_keys[i]]))

except:largest_our_planet_distances.append(-99)

try: largest_enemy_planet_distances.append(key_by_value(entities_by_distance, enemy_planet_sizes[enemy_planet_keys[i]]))

except:largest_enemy_planet_distances.append(-99)

entity_lists = [fix_data(closest_empty_planet_distances),

fix_data(closest_my_planets_distances),

fix_data(closest_enemy_planets_distances),

fix_data(closest_team_ships_distances),

fix_data(closest_enemy_ships_distances),

fix_data(empty_planet_keys),

fix_data(our_planet_keys),

fix_data(enemy_planet_keys),

fix_data(largest_empty_planet_distances),

fix_data(largest_our_planet_distances),

fix_data(largest_enemy_planet_distances)]

input_vector = []

for i in entity_lists:

for ii in i[:HM_ENT_FEATURES]:

input_vector.append(ii)

input_vector += [my_ship_count,

enemy_ship_count,

hm_our_planets,

hm_empty_planets,

hm_enemy_planets]

if my_ship_count > DESIRED_SHIP_COUNT:

'''ATTACK: [1,0,0]'''

output_vector = 3*[0] #[0,0,0]

output_vector[0] = 1 #[1,0,0]

ship_plans[ship.id] = output_vector

elif change or ship.id not in ship_plans:

'''

pick new "plan"

'''

output_vector = 3*[0]

output_vector[random.randint(0,2)] = 1

ship_plans[ship.id] = output_vector

else:

'''continue to execute existing plan'''

output_vector = ship_plans[ship.id]

try:

# ATTACK ENEMY SHIP #

if np.argmax(output_vector) == 0: #[1,0,0]

'''

type: 0

Find closest enemy ship, and attack!

'''

if not isinstance(closest_enemy_ships[0], int):

navigate_command = ship.navigate(

ship.closest_point_to(closest_enemy_ships[0]),

game_map,

speed=int(hlt.constants.MAX_SPEED),

ignore_ships=False)

if navigate_command:

command_queue.append(navigate_command)

# MINE ONE OF OUR PLANETS #

elif np.argmax(output_vector) == 1:

'''

type: 1

Mine closest already-owned planet

'''

if not isinstance(closest_my_planets[0], int):

target = closest_my_planets[0]

if len(target._docked_ship_ids) < target.num_docking_spots:

if ship.can_dock(target):

command_queue.append(ship.dock(target))

else:

navigate_command = ship.navigate(

ship.closest_point_to(target),

game_map,

speed=int(hlt.constants.MAX_SPEED),

ignore_ships=False)

if navigate_command:

command_queue.append(navigate_command)

else:

#attack!

if not isinstance(closest_enemy_ships[0], int):

navigate_command = ship.navigate(

ship.closest_point_to(closest_enemy_ships[0]),

game_map,

speed=int(hlt.constants.MAX_SPEED),

ignore_ships=False)

if navigate_command:

command_queue.append(navigate_command)

elif not isinstance(closest_empty_planets[0], int):

target = closest_empty_planets[0]

if ship.can_dock(target):

command_queue.append(ship.dock(target))

else:

navigate_command = ship.navigate(

ship.closest_point_to(target),

game_map,

speed=int(hlt.constants.MAX_SPEED),

ignore_ships=False)

if navigate_command:

command_queue.append(navigate_command)

#attack!

elif not isinstance(closest_enemy_ships[0], int):

navigate_command = ship.navigate(

ship.closest_point_to(closest_enemy_ships[0]),

game_map,

speed=int(hlt.constants.MAX_SPEED),

ignore_ships=False)

if navigate_command:

command_queue.append(navigate_command)

# FIND AND MINE AN EMPTY PLANET #

elif np.argmax(output_vector) == 2:

'''

type: 2

Mine an empty planet.

'''

if not isinstance(closest_empty_planets[0], int):

target = closest_empty_planets[0]

if ship.can_dock(target):

command_queue.append(ship.dock(target))

else:

navigate_command = ship.navigate(

ship.closest_point_to(target),

game_map,

speed=int(hlt.constants.MAX_SPEED),

ignore_ships=False)

if navigate_command:

command_queue.append(navigate_command)

else:

#attack!

if not isinstance(closest_enemy_ships[0], int):

navigate_command = ship.navigate(

ship.closest_point_to(closest_enemy_ships[0]),

game_map,

speed=int(hlt.constants.MAX_SPEED),

ignore_ships=False)

if navigate_command:

command_queue.append(navigate_command)

except Exception as e:

logging.info(str(e))

with open("c{}_input.vec".format(VERSION),"a") as f:

f.write(str( [round(item,3) for item in input_vector] ))

f.write('

')

with open("c{}_out.vec".format(VERSION),"a") as f:

f.write(str(output_vector))

f.write('

')

except Exception as e:

logging.info(str(e))

game.send_command_queue(command_queue)

# TURN END