Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Board.pro
- QT += core gui
- greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
- TARGET = Board
- TEMPLATE = app
- SOURCES += main.cpp\
- Board.cpp
- HEADERS += Board.h
- //Board.h
- #ifndef BOARD_H
- #define BOARD_H
- #include <QWidget>
- #include <QLabel>
- #include <QSpinBox>
- #include <QPushButton>
- #include <QGridLayout>
- class Board : public QWidget
- {
- Q_OBJECT
- public:
- Board(QWidget *parent = 0);
- public slots:
- void reset();
- void verify();
- void replay();
- signals:
- void gameLost();
- void gameWon();
- private:
- void setupUi();
- int pts;
- QLabel *top;
- QLabel *higherornot;
- QSpinBox *usernumber;
- QPushButton *okbutton;
- QPushButton *quitbutton;
- QGridLayout *layout;
- int secret;
- QLabel *points;
- QLabel *hhigher;
- };
- #endif // BOARD_H
- //Board.cpp
- #include "Board.h"
- #include <QApplication>
- #include <QMessageBox>
- #include <QTime>
- Board::Board(QWidget *parent) : QWidget(parent)
- {
- reset();
- setupUi();
- QObject::connect(okbutton,SIGNAL(clicked()),this,SLOT(verify()));
- QObject::connect(quitbutton,SIGNAL(clicked()),qApp,SLOT(quit()));
- QObject::connect(this,SIGNAL(gameLost()),this,SLOT(replay()));
- // New connection
- QObject::connect(this,SIGNAL(gameWon()),this,SLOT(reset()));
- }
- void Board::verify()
- {
- pts--;
- int nbCoups = pts;
- int nombreMystere = secret;
- if(usernumber->value() > nombreMystere && nbCoups >= 0)
- {
- QString str("Lower than "+(QString::number(usernumber->value()))+" !");
- QString c(QString::number(pts)+" pts left !");
- points->setText(c);
- higherornot->setText(str);
- }
- else if(usernumber->value() < nombreMystere && nbCoups >= 0)
- {
- QString phrase("Higher thant "+(QString::number(usernumber->value()))+" !");
- QString c(QString::number(pts)+" pts left !");
- points->setText(c);
- higherornot->setText(phrase);
- }
- else if(nbCoups >= 0 && usernumber->value() == nombreMystere)
- {
- points->setText("Winning with "+QString::number(10-pts)+" pts");
- higherornot->setText("Same !");
- emit gameWon();
- }
- else
- {
- points->setText("No points left !");
- higherornot->setText("Lose !");
- emit gameLost();
- }
- }
- void Board::replay()
- {
- this->close();
- int ret = QMessageBox::information(0,"Replay", "Want to replay ?",QMessageBox::Yes | QMessageBox::No);
- if(ret == QMessageBox::Yes)
- {
- reset();
- points->setText("");
- higherornot->setText("");
- this->show();
- }
- }
- void Board::reset()
- {
- pts = 10;
- QTime midnight(0,0,0,0);
- qsrand(midnight.msecsTo(QTime::currentTime()));
- secret = (qrand() % 1000);
- }
- void Board::setupUi()
- {
- hhigher = new QLabel("Found the mystery number");
- points = new QLabel;
- top = new QLabel("Try to found the mystery number.");
- usernumber = new QSpinBox;
- usernumber->setRange(0,1000);
- higherornot = new QLabel;
- okbutton = new QPushButton("Ok");
- quitbutton = new QPushButton("Close");
- layout = new QGridLayout;
- layout->addWidget(hhigher,0,0,1,2);
- layout->addWidget(top,1,0,1,2);
- layout->addWidget(higherornot,2,0,1,2);
- layout->addWidget(points,3,0,1,2);
- layout->addWidget(usernumber,4,0,1,2);
- layout->addWidget(okbutton,5,0);
- layout->addWidget(quitbutton,5,1);
- setLayout(layout);
- setFixedSize(sizeHint());
- setWindowTitle("Found the mystery number");
- setWindowIcon(QIcon("cm.ico"));
- }
- // main.cpp
- #include "Board.h"
- #include <QApplication>
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- Board w;
- w.show();
- return a.exec();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement