Added "Show Solution"
authorpaul romanchenko <paulaner@gmail.com>
Tue, 21 Sep 2010 05:19:39 +0000 (09:19 +0400)
committerpaul romanchenko <paulaner@gmail.com>
Tue, 21 Sep 2010 05:19:39 +0000 (09:19 +0400)
fieldview.cpp
fieldview.h
mainwindow.cpp
mainwindow.h
mainwindow.ui

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);
+    }
+}
index 197e830..892fdf0 100644 (file)
@@ -64,23 +64,33 @@ public:
         explicit FieldView(QWidget *parent = 0);
         PlayField* setPlayField(PlayField *pf);
 
+        void showSolution();
+        void stop();
+
 private:
         PlayField *playField;
         int selX, selY;
         int moveX, moveY;
         bool moving;
         QList<Animation*> *moves;
+        QTimer *solutionTimer;
 
         void playMoves();
+        void move(int x, int y, int dest_x);
+
+        int solutionMove;
+        bool inSolution;
 signals:
         void solved(int moves);
         void updateMoves(int moves);
+        void animationEnd();
 
 public slots:
         void cellMoved(int w, int h, int wnew, int hnew);
         void cellGone(int w, int h);
         void playMove();
         void undo();
+        void playSolutionMove();
 
 
 protected:
index f90ed54..b78f9f8 100644 (file)
@@ -54,12 +54,15 @@ void MainWindow::reorient()
     ui->buttonsPortrait->removeWidget(ui->nextLevel);
     ui->buttonsPortrait->removeWidget(ui->reload);
     ui->buttonsPortrait->removeWidget(ui->undo);
+    ui->buttonsPortrait->removeWidget(ui->solve);
 
     ui->undoLandscape->removeWidget(ui->undo);
+    ui->solveLandscape->removeWidget(ui->solve);
     ui->buttonsLandscape->removeWidget(ui->prevLevel);
     ui->buttonsLandscape->removeWidget(ui->nextLevel);
     ui->buttonsLandscape->removeWidget(ui->reload);
 
+
     QDesktopWidget* q=QApplication::desktop();
     if(q->height()>q->width())
     {
@@ -69,6 +72,7 @@ void MainWindow::reorient()
         ui->buttonsPortrait->addWidget(ui->reload);
         ui->buttonsPortrait->addWidget(ui->nextLevel);
         ui->buttonsPortrait->addWidget(ui->undo);
+        ui->buttonsPortrait->addWidget(ui->solve);
         //ui->buttonsPortrait->layout();
     } else
     {
@@ -76,6 +80,7 @@ void MainWindow::reorient()
         ui->buttonsLandscape->insertWidget(0,ui->reload,1,Qt::AlignLeft);
         ui->buttonsLandscape->insertWidget(0,ui->prevLevel,1,Qt::AlignLeft);
         ui->undoLandscape->addWidget(ui->undo);
+        ui->solveLandscape->addWidget(ui->solve);
         //ui->buttonsLandscape->layout();
     }
 }
@@ -158,3 +163,17 @@ void MainWindow::loadNextLevel()
         loadLevel();
     }
 }
+
+void MainWindow::solve()
+{
+    // reset current level
+    loadLevel();
+    // and show solution
+    QMessageBox confirm;
+    confirm.setText(tr("Reset the level and show the solution?"));
+    confirm.setInformativeText(tr("If you agree current level will be reloaded and solution will be shown"));
+    confirm.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
+    int confirmation=confirm.exec();
+    if(confirmation==QMessageBox::Yes)
+        ui->playwidget->showSolution();
+}
index 24cd524..8db5b6d 100644 (file)
@@ -37,6 +37,7 @@ public slots:
     void loadLevel();
     void solved(int moves);
     void howToPlay();
+    void solve();
 
     void loadNextLevel();
     void loadPrevLevel()
index 8fda48f..21e7de2 100644 (file)
           </layout>
          </item>
          <item>
+          <layout class="QHBoxLayout" name="solveLandscape">
+         <item>
+          <widget class="QPushButton" name="solve">
+           <property name="text">
+            <string>Solve!</string>
+           </property>
+          </widget>
+         </item>
+         </layout>
+         </item>
+         <item>
           <spacer name="verticalSpacer_2">
            <property name="orientation">
             <enum>Qt::Vertical</enum>
    <slots>
     <signal>solved(int)</signal>
     <signal>updateMoves(int)</signal>
+    <signal>solved()</signal>
     <slot>undo()</slot>
    </slots>
   </customwidget>
    <hints>
     <hint type="sourcelabel">
      <x>697</x>
-     <y>227</y>
+     <y>162</y>
     </hint>
     <hint type="destinationlabel">
      <x>553</x>
    <hints>
     <hint type="sourcelabel">
      <x>765</x>
-     <y>227</y>
+     <y>162</y>
     </hint>
     <hint type="destinationlabel">
      <x>515</x>
    <hints>
     <hint type="sourcelabel">
      <x>731</x>
-     <y>227</y>
+     <y>162</y>
     </hint>
     <hint type="destinationlabel">
      <x>584</x>
    <hints>
     <hint type="sourcelabel">
      <x>765</x>
-     <y>259</y>
+     <y>194</y>
     </hint>
     <hint type="destinationlabel">
      <x>321</x>
     </hint>
    </hints>
   </connection>
+  <connection>
+   <sender>solve</sender>
+   <signal>clicked()</signal>
+   <receiver>MainWindow</receiver>
+   <slot>solve()</slot>
+   <hints>
+    <hint type="sourcelabel">
+     <x>705</x>
+     <y>310</y>
+    </hint>
+    <hint type="destinationlabel">
+     <x>610</x>
+     <y>286</y>
+    </hint>
+   </hints>
+  </connection>
  </connections>
  <slots>
   <slot>loadNextLevel()</slot>
   <slot>reorient()</slot>
   <slot>solved(int)</slot>
   <slot>about()</slot>
+  <slot>solve()</slot>
  </slots>
 </ui>