Skip to content

Instantly share code, notes, and snippets.

@theepicsnail
Created January 12, 2014 07:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theepicsnail/8381948 to your computer and use it in GitHub Desktop.
Save theepicsnail/8381948 to your computer and use it in GitHub Desktop.
import rg, random
def toward((sr, sc), (tr, tc)):
"""Less dumb towards function
returns a position that is 1 step from (s)ource
towards (t)arget.
"""
#Delta row/col
dr = tr - sr
dc = tc - sc
if dc == 0:
#print "<Vert>",
return (sr + cmp(dr,0), sc)
if dr == 0:
#print "<Horz>",
return (sr, sc + cmp(dc,0))
#print "<Ang:", cmp(dr,0),",",cmp(dc,0),">",
return random.choice([
(sr + cmp(dr,0), sc),
(sr, sc + cmp(dc,0))
])
class Robot:
def find_closest_enemy(self, game):
closest = None
adjacent_enemies = 0
for loc, bot in game.robots.iteritems():
if bot.player_id != self.player_id:
dist = rg.wdist(self.location, bot.location)
#print "Enemy at", loc, dist
if dist == 1:
adjacent_enemies += 1
if (closest is None) or (closest[0] > dist):
closest = [dist, bot.location]
#print "Closest found:", closest
return closest, adjacent_enemies
def act(self, game):
#print "Start", self
closest, adjacent_enemies = self.find_closest_enemy(game)
#print "Closest", closest
if closest is None:
#print "No enemy on field. Guarding."
return ['guard']
if closest[0] == 1:
#print "Enemy adjacent to me. Attacking", closest[1]
if self.hp < 10 * adjacent_enemies:
return ['suicide']
return ['attack', closest[1]]
#print "Moving."
#print "Toward (", self.location, ",", closest[1],") = ",
pos = toward(self.location, closest[1])
#print pos,
pos2 = rg.toward(self.location, closest[1])
#print " rg.toward:",(pos2)
return ['move', pos]
#rg.toward(self.location, closest[1])]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment