1
0
mirror of git://projects.qi-hardware.com/openwrt-packages.git synced 2024-10-01 13:07:36 +03: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,32 +339,46 @@ 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(); {
QPushButton *button_up; int id = button->property("id").toInt();
if (id < 5) { QPushButton *button_up;
button_up = idtoButton(id+12); if (id < 5) {
} else { button_up = idtoButton(id+12);
button_up = idtoButton(id-4); } else {
} button_up = idtoButton(id-4);
if (button_up->isHidden()) { }
keyUp(button_up); if (button_up->isHidden()) {
} else { keyUp(button_up);
button_up->setFocus(); } else {
} button_up->setFocus();
}
} }
void Tile::keyDown(QPushButton *button) { void Tile::keyDown(QPushButton *button)
int id = button->property("id").toInt(); {
QPushButton *button_down; int id = button->property("id").toInt();
if (id > 12) { QPushButton *button_down;
button_down = idtoButton(id-12); if (id > 12) {
} else { button_down = idtoButton(id-12);
button_down = idtoButton(id+4); } else {
} button_down = idtoButton(id+4);
if (button_down->isHidden()) { }
keyDown(button_down); if (button_down->isHidden()) {
} else { keyDown(button_down);
button_down->setFocus(); } else {
} 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);