Advertisement
Guest User

Board ( .h / .cpp )

a guest
Nov 26th, 2012
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.07 KB | None | 0 0
  1. //Board.cpp
  2. #include "Board.h"
  3. #include <QMetaObject>
  4.  
  5. Board::Board(QWidget *parent) : QWidget(parent)
  6. {
  7.     reset();
  8.     layout->addWidget(hhigher,0,0,1,2);
  9.     layout->addWidget(top,1,0,1,2);
  10.     layout->addWidget(higherornot,2,0,1,2);
  11.     layout->addWidget(pts,3,0,1,2);
  12.     layout->addWidget(usernumber,4,0,1,2);
  13.     layout->addWidget(okbutton,5,0);
  14.     layout->addWidget(quitbutton,5,1);
  15.     QObject::connect(okbutton,SIGNAL(clicked()),this,SLOT(verify()));
  16.     QObject::connect(quitbutton,SIGNAL(clicked()),qApp,SLOT(quit()));
  17.     QObject::connect(this,SIGNAL(gameLost()),this,SLOT(replay()));
  18.     setLayout(layout);
  19.     setFixedSize(sizeHint());
  20.     setWindowTitle("Found the mystery number");
  21.     setWindowIcon(QIcon("cm.ico"));
  22. }
  23.  
  24. void Board::verify()
  25. {
  26.     pts--;
  27.     if(usernumber->value() > nombreMystere && nbCoups >= 0)
  28.     {
  29.         QString str("Lower than "+(QString::number(usernumber->value()))+" !");
  30.         QString c(QString::number(pts)+" pts left !");
  31.         points->setText(c);
  32.         higherornot->setText(str);
  33.     }
  34.     else if(usernumber->value() < nombreMystere && nbCoups >= 0)
  35.     {
  36.         QString phrase("Higher thant "+(QString::number(usernumber->value()))+" !");
  37.         QString c(QString::number(pts)+" pts left !");
  38.         points->setText(c);
  39.         higherornot->setText(phrase);
  40.     }
  41.     else if(nbCoups >= 0 && usernumber->value() == nombreMystere)
  42.     {
  43.         points->setText("Winning with "+QString::number(10-pts)+" pts");
  44.         higherornot->setText("Same !");
  45.         emit gameWon();
  46.     }
  47.     else
  48.     {
  49.         points->setText("No points left !");
  50.         higherornot->setText("Lose !");
  51.         emit gameLost();
  52.     }
  53.  
  54.     usernumber->setValue(usernumber->value());
  55. }
  56.  
  57. void Board::replay()
  58. {
  59.     this->close();
  60.     int ret = QMessageBox::information(0,"Replay", "Want to replay ?",QMessageBox::Yes  | QMessageBox::No);
  61.     if(ret == QMessageBox::Yes)
  62.     {
  63.         reset();
  64.         this->update();
  65.         this->show();
  66.     }
  67. }
  68.  
  69. void Board::reset()
  70. {
  71.     pts = 10;
  72.     QTime midnight(0,0,0,0);
  73.     qsrand(midnight.msecsTo(QTime::currentTime()));
  74.     secret = (qrand() % 1000);
  75.     hhigher = new QLabel("Found the mystery number");
  76.     points = new QLabel;
  77.     top = new QLabel("Try to found the mystery number.");
  78.     usernumber = new QSpinBox;
  79.     higherornot = new QLabel;
  80.     usernumber->setRange(0,1000);
  81.     okbutton = new QPushButton("Ok");
  82.     quitbutton = new QPushButton("Close");
  83.     layout = new QGridLayout;
  84. }
  85.  
  86. //Board.h
  87. #ifndef BOARD_H
  88. #define BOARD_H
  89.  
  90. #include <QtGui>
  91. #include <QMetaObject>
  92.  
  93. class Board : public QWidget
  94. {
  95.     Q_OBJECT
  96.  
  97. public:
  98.     FenetrePrincipale(QWidget *parent = 0);
  99.     void reset();
  100. public slots:
  101.     void verify();
  102.     void replay();
  103. signals:
  104.     void gameLost();
  105.     void gameWon();
  106. private:
  107.     int points;
  108.     QLabel *top;
  109.     QLabel *higherornot;
  110.     QSpinBox *usernumber;
  111.     QPushButton *okbutton;
  112.     QPushButton *quitbutton;
  113.     QGridLayout *layout;
  114.     int secret;
  115.     QLabel *pts;
  116.     QLabel *hhigher;
  117. };
  118.  
  119. #endif // BOARD_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement