-
Notifications
You must be signed in to change notification settings - Fork 0
/
myBot2.py
123 lines (96 loc) · 4.56 KB
/
myBot2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env python3
# Python 3.6
# 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
# This library allows you to generate random numbers.
import random
import numpy as np
# np.set_printoptions(threshold=np.nan)
import pandas as pd
# Logging allows you to save messages for yourself. This is required because the regular STDOUT
# (print statements) are reserved for the engine-bot communication.
import logging
logging.basicConfig(filename='mybot2.log', filemode='w', level=logging.DEBUG)
""" <<<Game Begin>>> """
# This game object contains the initial game state.
game = hlt.Game()
# At this point "game" variable is populated with initial map data.
# This is a good place to do computationally expensive start-up pre-processing.
# As soon as you call "ready" function below, the 2 second per turn timer will start.
me = game.me
game_map = game.game_map
occ_arr = np.zeros((game_map.width,game_map.height))
hlt_amt = np.zeros((game_map.width,game_map.height))
direction_order = [Direction.North, Direction.South, Direction.East, Direction.West, Direction.Still]
ship_max_hlt = constants.MAX_HALITE
max_turns = constants.MAX_TURNS
ship_cost = constants.SHIP_COST
yard = [me.shipyard.position.x,me.shipyard.position.y]
drops = me.get_dropoffs()
drops.append(yard)
logging.debug("drops {}".format(drops))
ship_states = {}
def get_info(game_map, occ_arr, hlt_amt,me):
for y in range(game_map.height):
for x in range(game_map.width):
this_cell = game_map[hlt.Position(x, y)]
occ_arr[x,y] = this_cell.is_occupied
hlt_amt[x,y] = this_cell.halite_amount
for ship in me.get_ships():
occ_arr[ship.position.x,ship.position.y] = 2
return occ_arr, hlt_amt
def create_ship_states(me,ship_states):
'''
Test the ship and decide collecting or depositing or something else in future
'''
for ship in me.get_ships():
if ship not in ship_states:
ship_states[ship.id] = "collecting"
if ship.halite_amount >= constants.MAX_HALITE *.9:
ship_states[ship.id] = "depositing"
logging.info("ship_states{}".format(ship_states))
return ship_states
def f1(d1):
v=list(d1.values())
k=list(d1.keys())
return k[v.index(max(v))]
def movement(ship,ship_states,occ_arr,hlt_amt,drops,yard):
if ship_states[ship.id] == "collecting":
position_options = ship.position.get_surrounding_cardinals() + [ship.position]
logging.debug('position_options {}'.format(position_options))
choice_dic = {}
for pos in position_options:
choice_dic[(pos.x,pos.y)] = hlt_amt[pos.x,pos.y]
logging.debug("Choice dic before f1{}".format(choice_dic))
directional_choice = f1(choice_dic)
direction_x = np.sign(directional_choice.x - ship.position.x)
direction_y = np.sign(directional_choice.y - ship.position.y)
#direction choice is the position in the space not vs the original position
logging.debug("directional Choice x:{} y:{}".format((direction_x),(direction_y)))
return ship.move((direction_x,direction_y))
game.ready("BBCMicroTurtle_v2")
# Now that your bot is initialized, save a message to yourself in the log file with some important information.
# Here, you log here your id, which you can always fetch from the game object by using my_id.
logging.debug("Successfully created bot! My Player ID is {}.".format(game.my_id))
""" <<<Game Loop>>> """
while True:
# This loop handles each turn of the game. The game object changes every turn, and you refresh that state by
# running update_frame().
game.update_frame()
# You extract player metadata and the updated map metadata here for convenience.
command_queue = []
occ_arr, hlt_amt = get_info(game_map, occ_arr, hlt_amt, me)
logging.debug("I have this much halite: {}".format(me.halite_amount))
ship_states = create_ship_states(me,ship_states)
for ship in me.get_ships():
command_queue.append(movement(ship,ship_states,occ_arr,hlt_amt,drops,yard))
logging.debug("command_queue: {}".format(command_queue))
if game.turn_number <= 200 and me.halite_amount >= constants.SHIP_COST and not game_map[me.shipyard].is_occupied:
logging.info("generate ship")
command_queue.append(me.shipyard.spawn())
# making my ships == 2 and competitior shnment, ending this turn.
game.end_turn(command_queue)