Added "Show Solution"
[vexed] / fieldview.cpp
index 2ccb757..5940197 100644 (file)
@@ -13,7 +13,8 @@ FieldView::FieldView(QWidget *parent) :
     moving=false;
     moves=new QList<Animation*>();
     playField=0;
-
+    solutionTimer=0;
+    inSolution=false;
 }
 
 void FieldView::mousePressEvent(QMouseEvent *event)
@@ -42,9 +43,14 @@ void FieldView::mouseReleaseEvent(QMouseEvent *event)
             return;
         }
     }
+    move(selX,selY,moveX);
+}
+
+void FieldView::move(int x, int y, int dest_x)
+{
     delete moves;
     moves=new QList<Animation*>();
-    playField->move(selX, selY, moveX);
+    playField->move(x, y, dest_x);
     playMoves();
     emit updateMoves(playField->moves);
 }
@@ -95,9 +101,13 @@ void FieldView::playMove()
                 connect(a,SIGNAL(finished()),this,SLOT(playMove()));
                 a->start();
                 delete move;
-        } else if(playField->checkSolved())
+        } else
         {
-            emit solved(playField->moves);
+            if(playField->checkSolved() && !inSolution)
+            {
+                emit solved(playField->moves);
+            }
+            emit animationEnd();
         }
 }
 
@@ -148,3 +158,59 @@ void FieldView::updateWidgets()
         }
     }
 }
+
+
+// level should be resetted to show solution!
+// create new widget
+void FieldView::showSolution()
+{
+    stop();
+    inSolution=true;
+    solutionMove=0;
+    solutionTimer=new QTimer(this);
+    connect(solutionTimer,SIGNAL(timeout()),this,SLOT(playSolutionMove()));
+    playSolutionMove();
+}
+
+void FieldView::stop()
+{
+    disconnect(this,SIGNAL(animationEnd()),this,SLOT(playSolutionMove()));
+    if(solutionTimer!=0)
+    {
+        solutionTimer->stop();
+        delete solutionTimer;
+        solutionTimer=0;
+    }
+    inSolution=false;
+}
+
+void FieldView::playSolutionMove()
+{
+    // drawn from original
+    char chX=playField->solution.at(solutionMove * 2).toAscii();
+    char chY=playField->solution.at(solutionMove * 2 + 1).toAscii();
+
+    int x=chX-'a';
+    int dest_x=x+1;
+    if( chX<='Z')
+    {
+        x = chX-'A';
+        dest_x=x-1;
+    }
+
+    int y=chY-'a';
+    if(chY<='Z'){
+       y=chY-'A';
+       dest_x=x+1;
+    }
+
+    if(x<0 || x>PF::FIELD_WIDTH || y<0 || y>PF::FIELD_HEIGHT)
+    {
+        stop();
+    } else
+    {
+        solutionMove++;
+        move(x,y,dest_x);
+        solutionTimer->start(500);
+    }
+}