From: paul romanchenko Date: Tue, 21 Sep 2010 05:19:39 +0000 (+0400) Subject: Added "Show Solution" X-Git-Url: http://git.maemo.org/git/?p=vexed;a=commitdiff_plain;h=6c9fbeb1b4cda4d08076565aa30a3b0bf4d530ec Added "Show Solution" --- diff --git a/fieldview.cpp b/fieldview.cpp index 2ccb757..5940197 100644 --- a/fieldview.cpp +++ b/fieldview.cpp @@ -13,7 +13,8 @@ FieldView::FieldView(QWidget *parent) : moving=false; moves=new QList(); 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(); - 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); + } +} diff --git a/fieldview.h b/fieldview.h index 197e830..892fdf0 100644 --- a/fieldview.h +++ b/fieldview.h @@ -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 *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: diff --git a/mainwindow.cpp b/mainwindow.cpp index f90ed54..b78f9f8 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -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(); +} diff --git a/mainwindow.h b/mainwindow.h index 24cd524..8db5b6d 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -37,6 +37,7 @@ public slots: void loadLevel(); void solved(int moves); void howToPlay(); + void solve(); void loadNextLevel(); void loadPrevLevel() diff --git a/mainwindow.ui b/mainwindow.ui index 8fda48f..21e7de2 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -242,6 +242,17 @@ + + + + + Solve! + + + + + + Qt::Vertical @@ -351,6 +362,7 @@ solved(int) updateMoves(int) + solved() undo() @@ -431,7 +443,7 @@ 697 - 227 + 162 553 @@ -447,7 +459,7 @@ 765 - 227 + 162 515 @@ -463,7 +475,7 @@ 731 - 227 + 162 584 @@ -479,7 +491,7 @@ 765 - 259 + 194 321 @@ -503,6 +515,22 @@ + + solve + clicked() + MainWindow + solve() + + + 705 + 310 + + + 610 + 286 + + + loadNextLevel() @@ -515,5 +543,6 @@ reorient() solved(int) about() + solve()