Skip to content

Instantly share code, notes, and snippets.

@wraithan
Created December 8, 2011 09:55
Show Gist options
  • Select an option

  • Save wraithan/1446602 to your computer and use it in GitHub Desktop.

Select an option

Save wraithan/1446602 to your computer and use it in GitHub Desktop.
def visible(self, loc):
' determine which squares are visible to the given player '
if self.vision == None:
if not hasattr(self, 'vision_offsets_2'):
# precalculate squares around an ant to set as visible
self.vision_offsets_2 = []
mx = int(sqrt(self.viewradius2))
for d_row in range(-mx,mx+1):
for d_col in range(-mx,mx+1):
d = d_row**2 + d_col**2
if d <= self.viewradius2:
self.vision_offsets_2.append((
d_row%self.rows-self.rows,
d_col%self.cols-self.cols
))
# set all spaces as not visible
# loop through ants and set all squares around ant as visible
self.vision = [[False]*self.cols for row in range(self.rows)]
for ant in self.my_ants():
a_row, a_col = ant
for v_row, v_col in self.vision_offsets_2:
self.vision[a_row+v_row][a_col+v_col] = True
row, col = loc
return self.vision[row][col]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment