-
Notifications
You must be signed in to change notification settings - Fork 1
/
MyBot.py
97 lines (81 loc) · 4.77 KB
/
MyBot.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
"""
Welcome to your first Halite-II bot!
This bot's name is Settler. It's purpose is simple (don't expect it to win complex games :) ):
1. Initialize game
2. If a ship is not docked and there are unowned planets
2.a. Try to Dock in the planet if close enough
2.b If not, go towards the planet
Note: Please do not place print statements here as they are used to communicate with the Halite engine. If you need
to log anything use the logging module.
"""
# Let's start by importing the Halite Starter Kit so we can interface with the Halite engine
import hlt
# Then let's import the logging module so we can print out information
import logging
# GAME START
# Here we define the bot's name as Settler and initialize the game, including communication with the Halite engine.
game = hlt.Game("player2")
# Then we print our start message to the logs
logging.info("Starting my Settler bot!")
# Versuch, dass nicht alle Raumschiffe in die selbe Richtung fliegen
planned_planets = [] # hinzugefügt
while True:
# TURN START
# Update the map for the new turn and get the latest version
game_map = game.update_map()
# Here we define the set of commands to be sent to the Halite engine at the end of the turn
command_queue = []
# For every ship that I control
for ship in game_map.get_me().all_ships():
# If the ship is docked
if ship.docking_status != ship.DockingStatus.UNDOCKED:
# Skip this ship
continue
# For each planet in the game (only non-destroyed planets are included)
for planet in game_map.all_planets():
# If the planet is owned
if planet.is_owned():
# Skip this planet
continue
# If we can dock, let's (try to) dock. If two ships try to dock at once, neither will be able to.
if ship.can_dock(planet):
# We add the command by appending it to the command_queue
command_queue.append(ship.dock(planet))
else:
if planet in planned_planets:
continue
else:
# If we can't dock, we move towards the closest empty point near this planet (by using closest_point_to)
# with constant speed. Don't worry about pathfinding for now, as the command will do it for you.
# We run this navigate command each turn until we arrive to get the latest move.
# Here we move at half our maximum speed to better control the ships
# In order to execute faster we also choose to ignore ship collision calculations during navigation.
# This will mean that you have a higher probability of crashing into ships, but it also means you will
# make move decisions much quicker. As your skill progresses and your moves turn more optimal you may
# wish to turn that option off.
navigate_command = ship.navigate(
ship.closest_point_to(planet),
game_map,
speed=int(hlt.constants.MAX_SPEED),
ignore_ships=True)
# If the move is possible, add it to the command_queue (if there are too many obstacles on the way
# or we are trapped (or we reached our destination!), navigate_command will return null;
# don't fret though, we can run the command again the next turn)
if navigate_command: # Prüfe, ob die if Schleife die Richtige Einrückung hat
command_queue.append(navigate_command)
planned_planets.append(planet)
break
# Send our set of commands to the Halite engine for this turn
game.send_command_queue(command_queue)
# TURN END
# GAME END
"""The idea here is that first we get all of our ships, then we're looking for any ships not currently docked.
Next, we iterate through all of the planets, looking for a planet that doesn't have docked ships at it (making them "owned").
If our ship can dock, we attempt to dock, entering this action into a "command queue."
The idea here with the command queue is that you might have many actions you want to take per "turn."
You are controlling many ships. If we cannot dock, then we want to start looking for a new planet again, and begin navigating to it as our command.
In order to "dock" at a planet, you need to be within close proximity. Now, each game turn takes place within the while True loop.
So you send all of your commands ONCE per iteration of the main game loop (the top-level while True).
Once the game ends, the while True will be broken and the game is over.
https://pythonprogramming.net/introduction-halite-ii-artificial-intelligence-competition/
"""