//--------------------------------------- mainWnd.h ------------------------
#ifndef MAINWND_H
#define MAINWND_H
#include <QtGui>
#include <QMainWindow>
class MainWnd;
class QBtn: public QPushButton
{
Q_OBJECT
public:
int ind;
MainWnd *Wnd;
QBtn(int n, MainWnd * parent);
private slots:
void funBtn();
};
//class MainWnd;
//
class MainWnd : public QMainWindow
{
Q_OBJECT
public:
QBtn* btn[16];
MainWnd( QWidget * parent = 0, Qt::WindowFlags f = 0 );
void SetBtns();
void funSetBtn(int n);
private:
void setupWidgets();
void setNumbers();
bool newGame();
bool endGame();
};
#endif // MAINWND_H
//--------------------------------------- mainWnd.cpp ------------------------
#include "mainWnd.h"
#define SIZE 50
int nums[ 16 ] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 0, 15};
void makeNew()
{
srand ( clock() );
for (int i = 0; i < 50; i++)
{
int t = rand();
int n1 = (t >> 1) & 0xF;
t = rand() + i;
int n2 = t & 0xF;
int n = nums[ n1 ];
nums[ n1 ] = nums[ n2 ];
nums[ n2 ] = n;
}
}
QBtn::QBtn( int n, MainWnd * parent )
{ ind = n;
Wnd = parent;
setParent( Wnd );
connect( this, SIGNAL(released()), this, SLOT(funBtn() ) );
};
//
void QBtn::funBtn()
{
Wnd->funSetBtn( ind );
}
MainWnd::MainWnd( QWidget * , Qt::WFlags )
{ setMinimumSize( SIZE * 4, SIZE * 4 );
setMaximumSize( SIZE * 4, SIZE * 4 );
QFont font;
font.setPointSize( 16 );
setFont( font );
setObjectName( "MainWnd" );
setWindowTitle( "15" );
setupWidgets();
setNumbers();
}
void MainWnd::setupWidgets()
{
QFrame * frame = new QFrame();
for ( int i = 0; i < 16; i++ )
{
btn[ i ] = new QBtn( i, this );
btn[ i ]->setMinimumSize(SIZE,SIZE );
btn[ i ]->setMaximumSize(SIZE,SIZE );
if ( i == 15 )
btn[ i ]->setFlat( true );
}
QGridLayout * grbox = new QGridLayout( frame );
grbox->setSpacing( 2 ); grbox->setContentsMargins( 3, 3, 3, 3 );
for ( int i = 0; i < 4; i++ )
for ( int j = 0; j < 4; j++ )
{
int k = i * 4 + j;
grbox->addWidget(btn[k], i, j);
}
setCentralWidget( frame );
btn[ 0 ]->setFocus();
}
//
void MainWnd::setNumbers()
{
for ( int i = 0; i < 16 ; i++ )
{
char s[ 8 ]= "";
if (nums[ i ] > 0)
{
sprintf( s , "%d", nums[ i ] );
btn[ i ]->setFlat( false );
} else
{
btn[ i ]->setFlat( true );
}
btn[ i ]->setText( s );
}
}
//
void MainWnd::funSetBtn( int n )
{
if ( nums[ n ] == 0 ) return;
int x = n / 4;
int y = n % 4;
int nn = -1;
if ( x > 0 && nums[ n - 4 ] == 0 ) nn = n - 4;
if ( nn < 0 && x < 3 && nums[ n + 4 ] == 0 ) nn = n + 4;
if ( nn < 0 && y > 0 && nums[ n - 1 ] == 0 ) nn = n - 1;
if ( nn < 0 && y < 3 && nums[ n + 1 ] == 0 ) nn = n + 1;
if ( nn >=0)
{
nums[ nn ] = nums[ n ]; nums[ n ] = 0;
}
setNumbers();
if ( endGame() )
{
if ( !newGame() ) close();
else
{
makeNew();
setNumbers();
}
}
}
//
bool MainWnd::newGame()
{
QMessageBox dlg;
dlg.setText( "New game" );
dlg.setInformativeText( "Do you want to start new game?" );
dlg.setStandardButtons( QMessageBox::Cancel | QMessageBox::Ok );
dlg.setDefaultButton( QMessageBox::Ok );
return dlg.exec() == QMessageBox::Ok;
}
//
bool MainWnd::endGame()
{
bool res = false;
if ( nums[0] == 0 )
return res;
for( int i = 0; i < 14; i++ )
{
if ( nums[i+1]-nums[i] != 1 )
break;
if ( i == 13 )
res = true;
}
return res;
}
//--------------------------------------- main.cpp ------------------------
#include <QApplication>
#include "mainWnd.h"
//
int main(int argc, char ** argv)
{
QApplication app( argc, argv );
MainWnd win;
win.show();
app.connect( &app, SIGNAL( lastWindowClosed() ), &app, SLOT( quit() ) );
return app.exec();
}