[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    extragear/plasma/applets/fifteenPuzzle/src
From:       Chani Armitage <chanika () gmail ! com>
Date:       2007-12-12 8:38:37
Message-ID: 1197448717.017496.16246.nullmailer () svn ! kde ! org
[Download RAW message or body]

SVN commit 747528 by chani:

improved puzzle generation (guaranteed to not run forever)
CCBUG: 153524


 M  +24 -16    fifteen.cpp  


--- trunk/extragear/plasma/applets/fifteenPuzzle/src/fifteen.cpp #747527:747528
@@ -50,28 +50,36 @@
 
 void Fifteen::shuffle()
 {
-  bool solvable = false;
   qsrand(time(0));
-  while (!solvable)
+  clearPieces();
+  m_pieces.fill(NULL);
+  for (int i=0; i<16; ++i)
   {
-    clearPieces();
-    m_pieces.fill(NULL);
-    for (int i=0; i<16; ++i)
-    {
       int rand = qrand() % 16;
-     
+
       while (m_pieces.at(rand) != NULL)
-        rand = qrand() % 16;
-      
-        m_pieces[rand] = new Piece(SIZE, i, this);
-        m_pieces[rand]->hide();
-        QObject::connect(m_pieces[rand], SIGNAL(pressed(QGraphicsItem*)), this, \
                SLOT(piecePressed(QGraphicsItem*))); 
-      
-        if (i == 0)
+          rand = qrand() % 16;
+
+      m_pieces[rand] = new Piece(SIZE, i, this);
+      m_pieces[rand]->hide();
+      QObject::connect(m_pieces[rand], SIGNAL(pressed(QGraphicsItem*)), this, \
SLOT(piecePressed(QGraphicsItem*)));  +
+      if (i == 0)
           m_blank = m_pieces[rand];
-    }
-    solvable = isSolvable();
   }
+
+  if (!isSolvable()) {
+      //this is where I would rip out two pieces and swap them around, if it were a \
plastic puzzle +      int a = 1;
+      int b = 2;
+      if (m_pieces[a] == m_blank) { //you can't pull out a hole!
+          a = 3;
+      } else if (m_pieces[b] == m_blank) {
+          b = 0;
+      }
+      qSwap(m_pieces[a], m_pieces[b]);
+  }
+
   updatePixmaps();
   updateNumerals();
   drawPieces();


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic