[gcompris] Add safety counter to prevent endless while-loop



commit fdadb16c682d78d5761242a1022a6949eee38cee
Author: Peter Albrecht <pa-dev gmx de>
Date:   Fri Sep 7 23:49:41 2012 +0200

    Add safety counter to prevent endless while-loop
    
    Keeping the number of decorations at 10, the case of not finding a valid
    position seems to be veeeery unlikely. But if someone modifies game
    parameters like the number of decorations or max decoration scale, we
    don't want the application to freeze, because of running in an endless
    while loop.

 src/mining-activity/mining.py |   12 ++++++++++++
 1 files changed, 12 insertions(+), 0 deletions(-)
---
diff --git a/src/mining-activity/mining.py b/src/mining-activity/mining.py
index ff7cf47..e5ec643 100644
--- a/src/mining-activity/mining.py
+++ b/src/mining-activity/mining.py
@@ -339,6 +339,8 @@ class Placer:
     width_half = int(area.width / 2.0)
     height_half = int(area.height / 2.0)
 
+    safety_counter = 0
+
     while True:
       # get new random position for the item
       x = random.randrange(width_half, gcompris.BOARD_WIDTH - width_half) * self.activity.source_image_scale
@@ -355,6 +357,16 @@ class Placer:
         # ... and end the search for a valid position
         break
 
+      safety_counter += 1
+      if safety_counter > 20:
+        # We tried to place this object many times, but could not find a valid position.
+        # Seems to be very difficult with this amount of objects.
+        # Since an invalid position (= overlapping objects) is way better than a frozen
+        # application, we exit this while loop with an "invalid" position.
+        print("Warning: safety_counter reached maximum!")
+        self.add_blocker(item);
+        break
+
 
   def add_blocker(self, blocking_area):
     """ Add a new blocking area to the internal list of blocking areas """



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]