1
0
mirror of git://projects.qi-hardware.com/openwrt-packages.git synced 2024-11-26 03:06:15 +02:00

Tile: check if the puzzle is solved

This commit is contained in:
kyak 2011-01-14 19:24:57 +03:00
parent bd53759d02
commit ca4ebe424a
3 changed files with 61 additions and 26 deletions

View File

@ -6,6 +6,8 @@
#include <QtGui/QWSServer> #include <QtGui/QWSServer>
#endif #endif
bool isRunning = 0;
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
QApplication a(argc, argv); QApplication a(argc, argv);

View File

@ -159,6 +159,20 @@ void Tile::swapButtons(QPushButton *button, QPushButton *button_neighbour) {
button->setText("16"); button->setText("16");
button_neighbour->show(); button_neighbour->show();
button_neighbour->setFocus(); button_neighbour->setFocus();
//qDebug() << isSolved();
if (isRunning && isSolved()) {
switch (QMessageBox::information(this,
"Solved!",
"Hooray, you solved it!\nShuffle again?",
"&Yes","&No"))
{
case 0:
Shuffle();
default:
isRunning = 0;
break;
}
}
} }
void Tile::Reset() void Tile::Reset()
@ -203,6 +217,8 @@ void Tile::Shuffle()
} }
if (!isSolvable()) { if (!isSolvable()) {
Shuffle(); Shuffle();
} else {
isRunning = 1;
} }
} }
@ -323,7 +339,8 @@ bool Tile::eventFilter(QObject *obj, QEvent *event)
return result; return result;
}//eventFilter }//eventFilter
void Tile::keyUp(QPushButton *button) { void Tile::keyUp(QPushButton *button)
{
int id = button->property("id").toInt(); int id = button->property("id").toInt();
QPushButton *button_up; QPushButton *button_up;
if (id < 5) { if (id < 5) {
@ -338,7 +355,8 @@ void Tile::keyUp(QPushButton *button) {
} }
} }
void Tile::keyDown(QPushButton *button) { void Tile::keyDown(QPushButton *button)
{
int id = button->property("id").toInt(); int id = button->property("id").toInt();
QPushButton *button_down; QPushButton *button_down;
if (id > 12) { if (id > 12) {
@ -352,3 +370,15 @@ void Tile::keyDown(QPushButton *button) {
button_down->setFocus(); button_down->setFocus();
} }
} }
bool Tile::isSolved()
{
QPushButton *button;
for (int i = 1; i < 17; i++) {
button = idtoButton(i);
if (button->text() != button->property("id")) {
return 0;
}
}
return 1;
}

View File

@ -8,6 +8,8 @@
#include <QMessageBox> #include <QMessageBox>
#include <QKeyEvent> #include <QKeyEvent>
extern bool isRunning;
namespace Ui { namespace Ui {
class Tile; class Tile;
} }
@ -32,6 +34,7 @@ public slots:
bool eventFilter(QObject *obj, QEvent *event); bool eventFilter(QObject *obj, QEvent *event);
void keyUp(QPushButton *button); void keyUp(QPushButton *button);
void keyDown(QPushButton *button); void keyDown(QPushButton *button);
bool isSolved();
protected: protected:
void changeEvent(QEvent *e); void changeEvent(QEvent *e);