Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save drryd/a8dd2ac1827f863b5fba78bc84746c4c to your computer and use it in GitHub Desktop.
Save drryd/a8dd2ac1827f863b5fba78bc84746c4c 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):
if gameMap.getSite(Location(x, y)).owner == myID:
moves.append(Move(Location(x, y), int(random.random() * 5)))
sendFrame(moves)
# Goes in MyBot.py
from hlt import *
from networking import *
myID, gameMap = getInit()
sendInit("PythonBot")
while True:
moves = []
gameMap = getFrame()
for y in range(gameMap.height):
for x in range(gameMap.width):
if gameMap.getSite(Location(x, y)).owner == myID:
if gameMap.getSite(Location(x, y)).strength == 0:
moves.append(Move(Location(x, y), STILL))
else:
moves.append(Move(Location(x, y), int(random.random() * 5)))
sendFrame(moves)
# Goes in MyBot.py
from hlt import *
from networking import *
myID, gameMap = getInit()
sendInit("PythonBot")
while True:
moves = []
gameMap = getFrame()
for y in range(gameMap.height):
for x in range(gameMap.width):
if gameMap.getSite(Location(x, y)).owner == myID:
movedPiece = False
if not movedPiece and gameMap.getSite(Location(x, y)).strength < gameMap.getSite(Location(x, y)).production * 5:
moves.append(Move(Location(x, y), STILL))
movedPiece = True
if not movedPiece:
moves.append(Move(Location(x, y), int(random.random() * 5)))
movedPiece = True
sendFrame(moves)
# Goes in MyBot.py
from hlt import *
from networking import *
myID, gameMap = getInit()
sendInit("PythonBot")
while True:
moves = []
gameMap = getFrame()
for y in range(gameMap.height):
for x in range(gameMap.width):
if gameMap.getSite(Location(x, y)).owner == myID:
movedPiece = False
if not movedPiece and gameMap.getSite(Location(x, y)).strength < gameMap.getSite(Location(x, y)).production * 5:
moves.append(Move(Location(x, y), STILL))
movedPiece = True
if not movedPiece:
moves.append(Move(Location(x, y), NORTH if bool(int(random.random() * 2)) else WEST))
movedPiece = True
sendFrame(moves)
# Goes in MyBot.py
from hlt import *
from networking import *
myID, gameMap = getInit()
sendInit("PythonBot")
while True:
moves = []
gameMap = getFrame()
for y in range(gameMap.height):
for x in range(gameMap.width):
if gameMap.getSite(Location(x, y)).owner == myID:
movedPiece = False
for d in CARDINALS:
if gameMap.getSite(Location(x, y), d).owner != myID and gameMap.getSite(Location(x, y), d).strength < gameMap.getSite(Location(x, y)).strength:
moves.append(Move(Location(x, y), d))
movedPiece = True
break
if not movedPiece and gameMap.getSite(Location(x, y)).strength < gameMap.getSite(Location(x, y)).production * 5:
moves.append(Move(Location(x, y), STILL))
movedPiece = True
if not movedPiece:
moves.append(Move(Location(x, y), NORTH if bool(int(random.random() * 2)) else WEST))
movedPiece = True
sendFrame(moves)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment