Skip to content

Instantly share code, notes, and snippets.

@benjaminfspector
Last active November 13, 2016 04:25
Show Gist options
  • Save benjaminfspector/73cd76d10de7e5147d7e0b49eb65f288 to your computer and use it in GitHub Desktop.
Save benjaminfspector/73cd76d10de7e5147d7e0b49eb65f288 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:
movedPiece = False
if not movedPiece and gameMap.getSite(Location(y, x)).strength == 0:
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), 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)
@drryd
Copy link

drryd commented Nov 13, 2016

Minor Fix to Revision1.py in my fork -- Fix coordinate from (y, x) to (x, y) and remove unnecessary variable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment