Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JCGrant/5ea767bae8a725f738d75649e5f2c91a to your computer and use it in GitHub Desktop.
Save JCGrant/5ea767bae8a725f738d75649e5f2c91a to your computer and use it in GitHub Desktop.
Halite Random Improvement Sample Code (Python 3)
This Gist contains the sample code for improving the sample Halite random bot in Python 3.
# Goes in MyBot.py
from hlt import *
from networking import *
myID, gameMap = getInit()
sendInit("MyPythonBot")
while True:
moves = []
gameMap = getFrame()
for y in range(gameMap.height):
for x in range(gameMap.width):
location = Location(x, y)
if gameMap.getSite(location).owner == myID:
moves.append(Move(location, random.choice(DIRECTIONS)))
sendFrame(moves)
# Goes in MyBot.py
from hlt import *
from networking import *
myID, gameMap = getInit()
sendInit("PythonBot")
def move(location):
site = gameMap.getSite(location)
if site.strength == 0:
return Move(location, STILL)
return Move(location, random.choice(DIRECTIONS))
while True:
moves = []
gameMap = getFrame()
for y in range(gameMap.height):
for x in range(gameMap.width):
location = Location(x, y)
if gameMap.getSite(location).owner == myID:
moves.append(move(location))
sendFrame(moves)
# Goes in MyBot.py
from hlt import *
from networking import *
myID, gameMap = getInit()
sendInit("PythonBot")
def move(location):
site = gameMap.getSite(location)
if site.strength < site.production * 5:
return Move(location, STILL)
return Move(location, random.choice(DIRECTIONS))
while True:
moves = []
gameMap = getFrame()
for y in range(gameMap.height):
for x in range(gameMap.width):
location = Location(x, y)
if gameMap.getSite(location).owner == myID:
moves.append(move(location))
sendFrame(moves)
# Goes in MyBot.py
from hlt import *
from networking import *
myID, gameMap = getInit()
sendInit("PythonBot")
def move(location):
site = gameMap.getSite(location)
if site.strength < site.production * 5:
return Move(location, STILL)
return Move(location, NORTH if random.random() > 0.5 else WEST)
while True:
moves = []
gameMap = getFrame()
for y in range(gameMap.height):
for x in range(gameMap.width):
location = Location(x, y)
if gameMap.getSite(location).owner == myID:
moves.append(move(location))
sendFrame(moves)
# Goes in MyBot.py
from hlt import *
from networking import *
myID, gameMap = getInit()
sendInit("PythonBot")
def move(location):
site = gameMap.getSite(location)
for d in CARDINALS:
neighbour_site = gameMap.getSite(location, d)
if neighbour_site.owner != myID and neighbour_site.strength < site.strength:
return Move(location, d)
if site.strength < site.production * 5:
return Move(location, STILL)
return Move(location, NORTH if random.random() > 0.5 else WEST)
while True:
moves = []
gameMap = getFrame()
for y in range(gameMap.height):
for x in range(gameMap.width):
location = Location(x, y)
if gameMap.getSite(location).owner == myID:
moves.append(move(location))
sendFrame(moves)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment