From: Heli Hyvättinen Date: Mon, 6 Jun 2011 10:04:41 +0000 (+0300) Subject: Spreading ghosts now handles situations where there is no free slot left X-Git-Tag: v0.1.0~26 X-Git-Url: https://vcs.maemo.org/git/?a=commitdiff_plain;h=6caa3c8220a274cfc6a0d4afa9e3ae212a413a80;p=ghostsoverboard Spreading ghosts now handles situations where there is no free slot left --- diff --git a/seascene.cpp b/seascene.cpp index eaaef92..963317f 100644 --- a/seascene.cpp +++ b/seascene.cpp @@ -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)); } diff --git a/seascene.h b/seascene.h index bfeb5cb..9abc977 100644 --- a/seascene.h +++ b/seascene.h @@ -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_;