Advertisement
Guest User

Сorrected game

a guest
Nov 26th, 2012
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Board.pro
  2.  
  3. QT       += core gui
  4. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
  5. TARGET = Board
  6. TEMPLATE = app
  7. SOURCES += main.cpp\
  8.         Board.cpp
  9. HEADERS  += Board.h
  10.  
  11. //Board.h
  12.  
  13. #ifndef BOARD_H
  14. #define BOARD_H
  15.  
  16. #include <QWidget>
  17. #include <QLabel>
  18. #include <QSpinBox>
  19. #include <QPushButton>
  20. #include <QGridLayout>
  21.  
  22. class Board : public QWidget
  23. {
  24.     Q_OBJECT
  25.  
  26. public:
  27.     Board(QWidget *parent = 0);
  28.  
  29. public slots:
  30.     void reset();
  31.     void verify();
  32.     void replay();
  33. signals:
  34.     void gameLost();
  35.     void gameWon();
  36. private:
  37.     void setupUi();
  38.  
  39.     int pts;
  40.     QLabel *top;
  41.     QLabel *higherornot;
  42.     QSpinBox *usernumber;
  43.     QPushButton *okbutton;
  44.     QPushButton *quitbutton;
  45.     QGridLayout *layout;
  46.     int secret;
  47.     QLabel *points;
  48.     QLabel *hhigher;
  49. };
  50.  
  51. #endif // BOARD_H
  52.  
  53. //Board.cpp
  54.  
  55. #include "Board.h"
  56.  
  57. #include <QApplication>
  58. #include <QMessageBox>
  59. #include <QTime>
  60.  
  61. Board::Board(QWidget *parent) : QWidget(parent)
  62. {
  63.     reset();
  64.  
  65.     setupUi();
  66.  
  67.     QObject::connect(okbutton,SIGNAL(clicked()),this,SLOT(verify()));
  68.     QObject::connect(quitbutton,SIGNAL(clicked()),qApp,SLOT(quit()));
  69.     QObject::connect(this,SIGNAL(gameLost()),this,SLOT(replay()));
  70.     // New connection
  71.     QObject::connect(this,SIGNAL(gameWon()),this,SLOT(reset()));
  72. }
  73.  
  74. void Board::verify()
  75. {
  76.     pts--;
  77.  
  78.     int nbCoups = pts;
  79.     int nombreMystere = secret;
  80.  
  81.     if(usernumber->value() > nombreMystere && nbCoups >= 0)
  82.     {
  83.         QString str("Lower than "+(QString::number(usernumber->value()))+" !");
  84.         QString c(QString::number(pts)+" pts left !");
  85.         points->setText(c);
  86.         higherornot->setText(str);
  87.     }
  88.     else if(usernumber->value() < nombreMystere && nbCoups >= 0)
  89.     {
  90.         QString phrase("Higher thant "+(QString::number(usernumber->value()))+" !");
  91.         QString c(QString::number(pts)+" pts left !");
  92.         points->setText(c);
  93.         higherornot->setText(phrase);
  94.     }
  95.     else if(nbCoups >= 0 && usernumber->value() == nombreMystere)
  96.     {
  97.         points->setText("Winning with "+QString::number(10-pts)+" pts");
  98.         higherornot->setText("Same !");
  99.         emit gameWon();
  100.     }
  101.     else
  102.     {
  103.         points->setText("No points left !");
  104.         higherornot->setText("Lose !");
  105.         emit gameLost();
  106.     }
  107. }
  108.  
  109. void Board::replay()
  110. {
  111.     this->close();
  112.     int ret = QMessageBox::information(0,"Replay", "Want to replay ?",QMessageBox::Yes  | QMessageBox::No);
  113.     if(ret == QMessageBox::Yes)
  114.     {
  115.         reset();
  116.         points->setText("");
  117.         higherornot->setText("");
  118.         this->show();
  119.     }
  120. }
  121.  
  122. void Board::reset()
  123. {
  124.     pts = 10;
  125.     QTime midnight(0,0,0,0);
  126.     qsrand(midnight.msecsTo(QTime::currentTime()));
  127.     secret = (qrand() % 1000);
  128. }
  129.  
  130.  
  131. void Board::setupUi()
  132. {
  133.     hhigher = new QLabel("Found the mystery number");
  134.     points = new QLabel;
  135.     top = new QLabel("Try to found the mystery number.");
  136.     usernumber = new QSpinBox;
  137.     usernumber->setRange(0,1000);
  138.     higherornot = new QLabel;
  139.     okbutton = new QPushButton("Ok");
  140.     quitbutton = new QPushButton("Close");
  141.     layout = new QGridLayout;
  142.  
  143.     layout->addWidget(hhigher,0,0,1,2);
  144.     layout->addWidget(top,1,0,1,2);
  145.     layout->addWidget(higherornot,2,0,1,2);
  146.     layout->addWidget(points,3,0,1,2);
  147.     layout->addWidget(usernumber,4,0,1,2);
  148.     layout->addWidget(okbutton,5,0);
  149.     layout->addWidget(quitbutton,5,1);
  150.  
  151.     setLayout(layout);
  152.     setFixedSize(sizeHint());
  153.     setWindowTitle("Found the mystery number");
  154.     setWindowIcon(QIcon("cm.ico"));
  155. }
  156.  
  157. // main.cpp
  158.  
  159. #include "Board.h"
  160.  
  161. #include <QApplication>
  162.  
  163. int main(int argc, char *argv[])
  164. {
  165.     QApplication a(argc, argv);
  166.  
  167.     Board w;
  168.     w.show();
  169.    
  170.     return a.exec();
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement