Lars Yencken avatar Lars Yencken committed c13e0df

Use a matrix to represent the map.

Comments (0)

Files changed (1)

 import time
 from collections import defaultdict
 from math import sqrt
+import numpy as np
 
 MY_ANT = 0
 ANTS = 0
                     self.spawnradius2 = int(tokens[1])
                 elif key == 'turns':
                     self.turns = int(tokens[1])
-        self.map = [[LAND for col in range(self.cols)]
-                    for row in range(self.rows)]
+        self.map = np.ones((self.rows, self.cols))*LAND
 
     def update(self, data):
         'parse engine input and update the game state'
         # clear hill, ant and food data
         self.hill_list = {}
         for row, col in self.ant_list.keys():
-            self.map[row][col] = LAND
+            self.map[row, col] = LAND
         self.ant_list = {}
         for row, col in self.dead_list.keys():
-            self.map[row][col] = LAND
+            self.map[row, col] = LAND
         self.dead_list = defaultdict(list)
         for row, col in self.food_list:
-            self.map[row][col] = LAND
+            self.map[row, col] = LAND
         self.food_list = []
         
         # update map and create new ant and food lists
                     row = int(tokens[1])
                     col = int(tokens[2])
                     if tokens[0] == 'w':
-                        self.map[row][col] = WATER
+                        self.map[row, col] = WATER
                     elif tokens[0] == 'f':
-                        self.map[row][col] = FOOD
+                        self.map[row, col] = FOOD
                         self.food_list.append((row, col))
                     else:
                         owner = int(tokens[3])
                         if tokens[0] == 'a':
-                            self.map[row][col] = owner
+                            self.map[row, col] = owner
                             self.ant_list[(row, col)] = owner
                         elif tokens[0] == 'd':
                             # food could spawn on a spot where an ant just died
                             # don't overwrite the space unless it is land
-                            if self.map[row][col] == LAND:
-                                self.map[row][col] = DEAD
+                            if self.map[row, col] == LAND:
+                                self.map[row, col] = DEAD
                             # but always add to the dead list
                             self.dead_list[(row, col)].append(owner)
                         elif tokens[0] == 'h':
     def passable(self, loc):
         'true if not water'
         row, col = loc
-        return self.map[row][col] != WATER
+        return self.map[row, col] != WATER
     
     def unoccupied(self, loc):
         'true if no ants are at the location'
         row, col = loc
-        return self.map[row][col] in (LAND, DEAD)
+        return self.map[row, col] in (LAND, DEAD)
 
     def destination(self, loc, direction):
         'calculate a new location given the direction and wrap correctly'
 
     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
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.