fork download
  1. // mainWindow.cpp
  2. #include "mainWindow.h"
  3. #include <ctime>
  4.  
  5. const int MainWindow::CARDS_NUM = 7;
  6.  
  7. MainWindow::MainWindow()
  8. {
  9. higherButton = new QPushButton("Higher");
  10. lowerButton = new QPushButton("Lower");
  11. newGameButton = new QPushButton("New Game");
  12.  
  13. highLowLayout = new QBoxLayout(QBoxLayout::LeftToRight);
  14. highLowLayout->addWidget(newGameButton);
  15. highLowLayout->addStretch(1);
  16. highLowLayout->addWidget(higherButton);
  17. highLowLayout->addWidget(lowerButton);
  18. highLowLayout->addStretch(1);
  19. highLowLayout->addWidget(&scoreLabel);
  20.  
  21. scoreLabel.setText( QString::number(score) );
  22.  
  23. buttons.setLayout(highLowLayout);
  24.  
  25. QBoxLayout* mainLayout = new QBoxLayout(QBoxLayout::TopToBottom);
  26.  
  27. mainLayout->addWidget(cards.getWidget());
  28. mainLayout->addWidget(&buttons);
  29. mainLayout->addWidget(&status);
  30.  
  31. mainWindow.setLayout(mainLayout);
  32.  
  33. QObject::connect(newGameButton,SIGNAL(clicked()),SLOT(newGame()));
  34. QObject::connect(higherButton,SIGNAL(clicked()),SLOT(higher()));
  35. QObject::connect(lowerButton,SIGNAL(clicked()),SLOT(lower()));
  36. }
  37.  
  38. MainWindow::~MainWindow()
  39. {
  40. if (currentDeck != 0) delete[] currentDeck;
  41. }
  42.  
  43. int * generateDeck()
  44. {
  45. srand(time(0));
  46. int * deck = new int[52];
  47. for (int i{}; i<52; i++) deck[i] = i;
  48.  
  49. for (int i{51}; i>0; i--)
  50. std::swap( deck[i], deck[ rand() % i ] );
  51.  
  52. return deck;
  53. }
  54.  
  55. int compareCards(int a, int b)
  56. {
  57. int mod_a = a % 13;
  58. int mod_b = b % 13;
  59. if (mod_a > mod_b) return 1;
  60. if (mod_a < mod_b) return -1;
  61. if (a > b) return 1;
  62. else return -1;
  63. return 0;
  64. }
  65.  
  66. void MainWindow::updateScore(int n)
  67. {
  68. score+= n;
  69. scoreLabel.setText( QString::number(score) );
  70. }
  71.  
  72.  
  73. void MainWindow::higher() { newMove(1); }
  74.  
  75. void MainWindow::lower() { newMove(-1); }
  76.  
  77. void MainWindow::newMove(int n)
  78. {
  79. if(!gameRunning) return;
  80. cards.setCard(currentCard,currentDeck[currentCard]);
  81. if (compareCards(currentDeck[currentCard],currentDeck[currentCard - 1]) == n)
  82. {
  83. status.setText("RIGHT!");
  84. updateScore(10);
  85. }
  86. else
  87. {
  88. status.setText("WRONG!");
  89. updateScore(-10);
  90. }
  91. if (++currentCard >= CARDS_NUM) gameRunning = false;
  92. }
  93.  
  94. void MainWindow::newGame()
  95. {
  96. cards.clear();
  97.  
  98. if (currentDeck != 0) delete[] currentDeck;
  99. currentDeck = generateDeck();
  100. cards.setCard(0,currentDeck[0]);
  101. currentCard = 1;
  102.  
  103. gameRunning = true;
  104. updateScore(-35);
  105. status.setText("New game started!");
  106. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:2:24: fatal error: mainWindow.h: No such file or directory
 #include "mainWindow.h"
                        ^
compilation terminated.
stdout
Standard output is empty