What's going on and welcome to part 2 of the Halite III tutorial series. In the previous tutorial we build some ships and they aimlessly went about collecting halite.
The most immediate issue is that our ships are crashing into eachother, and they arent ever trying to drop off halite. Let's solve that next.
Code up to this point:
# Python 3.6
import hlt #main halite stuff
from hlt import constants # halite constants
from hlt.positionals import Direction # helper for moving
import random # randomly picking a choice for now.
import logging # logging stuff to console
game = hlt.Game() # game object
# Initializes the game
game.ready("Sentdebot")
while True:
# This loop handles each turn of the game. The game object changes every turn, and you refresh that state by
game.update_frame()
# You extract player metadata and the updated map metadata here for convenience.
me = game.me
'''comes from game, game comes from before the loop, hlt.Game points to networking, which is where you will
find the actual Game class (hlt/networking.py). From here, GameMap is imported from hlt/game_map.py.
open that file to seee all the things we do with game map.'''
game_map = game.game_map # game map data. Recall game is
# A command queue holds all the commands you will run this turn. You build this list up and submit it at the
# end of the turn.
command_queue = []
for ship in me.get_ships():
command_queue.append(
ship.move(
random.choice([ Direction.North, Direction.South, Direction.East, Direction.West ])))
# ship costs 1000, dont make a ship on a ship or they both sink
if me.halite_amount >= 1000 and not game_map[me.shipyard].is_occupied:
command_queue.append(me.shipyard.spawn())
# Send your moves back to the game environment, ending this turn.
game.end_turn(command_queue)
Now, let's learn some things about our choices. We can only move once in the north, south, east or west direction, so we can have a pretty basic bot that just checks these. Code for doing this has already been made for us, and we can do something like:
for ship in me.get_ships():
choices = ship.position.get_surrounding_cardinals()
if game.turn_number == 15:
logging.info(choices)
If you run this, then check the log at turn 15:
INFO:root:=============== TURN 014 ================
INFO:root:=============== TURN 015 ================
INFO:root:[Position(11, 17), Position(11, 19), Position(12, 18), Position(10, 18)]
INFO:root:[Position(8, 16), Position(8, 18), Position(9, 17), Position(7, 17)]
INFO:root:[Position(8, 14), Position(8, 16), Position(9, 15), Position(7, 15)]
INFO:root:=============== TURN 016 ================
INFO:root:=============== TURN 017 ================
So we can see these are lists of these Position objects. Each list corresponds to each ship. In this case, none of the possible moves overlap eachother, but we'd definitely want to remember to watch out for that.
Next, we can ask the game map itself how much halite a given square has, with something like:
game_map[position].halite_amount
With that, we can begin to decide which direction our ship should travel, which is what we'll be working on in the next tutorial.