Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Board.cpp
- #include "Board.h"
- #include <QMetaObject>
- Board::Board(QWidget *parent) : QWidget(parent)
- {
- reset();
- layout->addWidget(hhigher,0,0,1,2);
- layout->addWidget(top,1,0,1,2);
- layout->addWidget(higherornot,2,0,1,2);
- layout->addWidget(pts,3,0,1,2);
- layout->addWidget(usernumber,4,0,1,2);
- layout->addWidget(okbutton,5,0);
- layout->addWidget(quitbutton,5,1);
- QObject::connect(okbutton,SIGNAL(clicked()),this,SLOT(verify()));
- QObject::connect(quitbutton,SIGNAL(clicked()),qApp,SLOT(quit()));
- QObject::connect(this,SIGNAL(gameLost()),this,SLOT(replay()));
- setLayout(layout);
- setFixedSize(sizeHint());
- setWindowTitle("Found the mystery number");
- setWindowIcon(QIcon("cm.ico"));
- }
- void Board::verify()
- {
- pts--;
- 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();
- }
- usernumber->setValue(usernumber->value());
- }
- void Board::replay()
- {
- this->close();
- int ret = QMessageBox::information(0,"Replay", "Want to replay ?",QMessageBox::Yes | QMessageBox::No);
- if(ret == QMessageBox::Yes)
- {
- reset();
- this->update();
- this->show();
- }
- }
- void Board::reset()
- {
- pts = 10;
- QTime midnight(0,0,0,0);
- qsrand(midnight.msecsTo(QTime::currentTime()));
- secret = (qrand() % 1000);
- hhigher = new QLabel("Found the mystery number");
- points = new QLabel;
- top = new QLabel("Try to found the mystery number.");
- usernumber = new QSpinBox;
- higherornot = new QLabel;
- usernumber->setRange(0,1000);
- okbutton = new QPushButton("Ok");
- quitbutton = new QPushButton("Close");
- layout = new QGridLayout;
- }
- //Board.h
- #ifndef BOARD_H
- #define BOARD_H
- #include <QtGui>
- #include <QMetaObject>
- class Board : public QWidget
- {
- Q_OBJECT
- public:
- FenetrePrincipale(QWidget *parent = 0);
- void reset();
- public slots:
- void verify();
- void replay();
- signals:
- void gameLost();
- void gameWon();
- private:
- int points;
- QLabel *top;
- QLabel *higherornot;
- QSpinBox *usernumber;
- QPushButton *okbutton;
- QPushButton *quitbutton;
- QGridLayout *layout;
- int secret;
- QLabel *pts;
- QLabel *hhigher;
- };
- #endif // BOARD_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement