moving=false;
moves=new QList<Animation*>();
playField=0;
-
+ solutionTimer=0;
+ inSolution=false;
}
void FieldView::mousePressEvent(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);
}
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();
}
}
}
}
}
+
+
+// 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);
+ }
+}
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:
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())
{
ui->buttonsPortrait->addWidget(ui->reload);
ui->buttonsPortrait->addWidget(ui->nextLevel);
ui->buttonsPortrait->addWidget(ui->undo);
+ ui->buttonsPortrait->addWidget(ui->solve);
//ui->buttonsPortrait->layout();
} else
{
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();
}
}
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();
+}
void loadLevel();
void solved(int moves);
void howToPlay();
+ void solve();
void loadNextLevel();
void loadPrevLevel()
</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>