Spreading ghosts now handles situations where there is no free slot left
authorHeli Hyvättinen <heli.hyvattinen@kymp.net>
Mon, 6 Jun 2011 10:04:41 +0000 (13:04 +0300)
committerHeli Hyvättinen <heli.hyvattinen@kymp.net>
Mon, 6 Jun 2011 10:04:41 +0000 (13:04 +0300)
seascene.cpp
seascene.h

index eaaef92..963317f 100644 (file)
@@ -41,19 +41,27 @@ void SeaScene::spreadGhosts(int ghosts)
 {
     for (int i=0; i < ghosts; i++)
     {
-        QPointF position = findRandomFreeSlot();
+        QPointF * pPosition = findRandomFreeSlot();
+
+        //If there was no room no point to continue
+        if (pPosition == NULL)
+            return;
 
         QGraphicsPixmapItem * pGhost = addPixmap(QPixmap(":/pix/aave.png"));
         pGhost->setData(0,"ghost");
-        pGhost->setPos(position);
+        pGhost->setPos(*pPosition);
+        delete pPosition;
     }
 }
 
-QPointF SeaScene::findRandomFreeSlot()
+QPointF* SeaScene::findRandomFreeSlot()
 {
+    if (freeTiles_.isEmpty())
+        return NULL;
+
     int index = qrand()%freeTiles_.size();
 
     qDebug()  << index << " index";
-    return freeTiles_.takeAt(index);
+    return new QPointF (freeTiles_.takeAt(index));
 
 }
index bfeb5cb..9abc977 100644 (file)
@@ -19,7 +19,11 @@ public slots:
 
 protected:
 
-    QPointF findRandomFreeSlot();
+    /*! Gives a pointer to a random position if a free one is found. Otherwise returns NULL.
+        The position is the upper left corner of a free 40x40 pixels slot.
+    */
+
+    QPointF* findRandomFreeSlot();
 
     const QString ghostImageFilename_;
     const QString rockImageFilename_;