1
0
mirror of git://projects.qi-hardware.com/openwrt-packages.git synced 2024-12-22 14:10:48 +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>
#endif
bool isRunning = 0;
int main(int argc, char *argv[])
{
QApplication a(argc, argv);

View File

@ -159,6 +159,20 @@ void Tile::swapButtons(QPushButton *button, QPushButton *button_neighbour) {
button->setText("16");
button_neighbour->show();
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()
@ -203,6 +217,8 @@ void Tile::Shuffle()
}
if (!isSolvable()) {
Shuffle();
} else {
isRunning = 1;
}
}
@ -323,32 +339,46 @@ bool Tile::eventFilter(QObject *obj, QEvent *event)
return result;
}//eventFilter
void Tile::keyUp(QPushButton *button) {
int id = button->property("id").toInt();
QPushButton *button_up;
if (id < 5) {
button_up = idtoButton(id+12);
} else {
button_up = idtoButton(id-4);
}
if (button_up->isHidden()) {
keyUp(button_up);
} else {
button_up->setFocus();
}
void Tile::keyUp(QPushButton *button)
{
int id = button->property("id").toInt();
QPushButton *button_up;
if (id < 5) {
button_up = idtoButton(id+12);
} else {
button_up = idtoButton(id-4);
}
if (button_up->isHidden()) {
keyUp(button_up);
} else {
button_up->setFocus();
}
}
void Tile::keyDown(QPushButton *button) {
int id = button->property("id").toInt();
QPushButton *button_down;
if (id > 12) {
button_down = idtoButton(id-12);
} else {
button_down = idtoButton(id+4);
}
if (button_down->isHidden()) {
keyDown(button_down);
} else {
button_down->setFocus();
}
void Tile::keyDown(QPushButton *button)
{
int id = button->property("id").toInt();
QPushButton *button_down;
if (id > 12) {
button_down = idtoButton(id-12);
} else {
button_down = idtoButton(id+4);
}
if (button_down->isHidden()) {
keyDown(button_down);
} 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 <QKeyEvent>
extern bool isRunning;
namespace Ui {
class Tile;
}
@ -32,6 +34,7 @@ public slots:
bool eventFilter(QObject *obj, QEvent *event);
void keyUp(QPushButton *button);
void keyDown(QPushButton *button);
bool isSolved();
protected:
void changeEvent(QEvent *e);