Commits

Chas Leichner committed 389e832

random mole generation working

  • Participants
  • Parent commits 14513e0

Comments (0)

Files changed (1)

File games/mole/mole.py

         self.time = time.time()
         kwargs.setdefault('matrix_size', (5, 5))
         super(Mole, self).__init__(**kwargs)
-        self.matrix = [[1 for x in xrange(kwargs.get('matrix_size')[0])] for y in xrange(kwargs.get('matrix_size')[1])]
+        self.matrix = self.random_matrix(0.3)
         #TODO
         #self.player_images = (MTWidget(),MTSvg(filename='cross.svg'),MTSvg(filename='circle.svg') )
         self.done = False
             #print i, j
             if self.matrix[i][j]:
                 self.matrix[i][j] = 0
-            '''
-            else:
-                self.matrix[i][j] = 1
-            '''
+
             self.dispatch_event('on_value_change', self.matrix)
             self.dispatch_event('on_press', (i, j, self.matrix[i][j]))
             self.last_tile = (i, j)
 
+    def random_matrix(self, density):
+        def generate_num():
+            if random.random() > density:
+                return 1
+            else:
+                return 0
+        return [[generate_num() for x in xrange(len(self.matrix))] for y in xrange(len(self.matrix[0]))]
+
     def draw(self):
         current_time = time.time()
-        if self.matrix_proportion() < 0.8 or current_time - self.time > 2:
+        if current_time - self.time > 2:
             self.time = current_time
-            self.matrix = [[1 for x in xrange(len(self.matrix))] for y in xrange(len(self.matrix[0]))]
+            self.matrix = self.random_matrix(0.3)
 
         for i in range (self._matrix_size[0]):
             for j in range (self._matrix_size[1]):