-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameGrid.cpp
195 lines (151 loc) · 4.4 KB
/
GameGrid.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include "gamegrid.h"
#include "QtWidgets/qlabel.h"
#include <QThread>
#include <QCoreApplication>
#include <random>
GameGrid::GameGrid() : QWidget(),lastSelectedCard(nullptr)
{
m_gridLayout = new QGridLayout();
m_attemptLabel = new QLabel("No. of Tries Remaining: 50");
m_scoreLabel = new QLabel("Score: 0");
shuffleStrings();
initializeCards();
}
void GameGrid::clickHandler(CardButton* button){
if(button ->isFaceUp() || wait)
return;
button->flip();
if(!lastSelectedCard)
lastSelectedCard = button;
else{
++attempt_count;
m_attemptLabel->setText("No. of Tries Remaining: " + QString::number(50 - attempt_count));
m_scoreLabel->setText("Score: " + QString::number(matched_counter));
checkMatch(button);
allMatched();
if (attempt_count == 50) {
lostGame();
}
}
}
void GameGrid::shuffleStrings(){
// duplicate object because each should appear twice
strings = {
"Elephant",
"Rainbow",
"Bicycle",
"Sunflower",
"Chocolate",
"Telescope",
"Penguin",
"Watermelon",
"Butterfly",
"Balloon",
"Jellyfish",
"Guitar",
"Fireworks",
"Pineapple",
"Moonlight"
};
std::vector<QString> duplicatedStrings;
duplicatedStrings.reserve(strings.size() * 2);
for (const auto& str : strings) {
duplicatedStrings.push_back(str);
duplicatedStrings.push_back(str);
}
// shuffle the order of object
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::shuffle(duplicatedStrings.begin(), duplicatedStrings.end(), std::default_random_engine(seed));
// assign the shuffled vector to original one
strings = duplicatedStrings;
}
void GameGrid::initializeCards(){
for (int row = 0; row < 5; row++) {
for (int col = 0; col < 6; col++) {
CardButton *cardButton = new CardButton(strings[row*6+col]);
connect(cardButton, &CardButton::cardClicked, this, &GameGrid::clickHandler);
m_gridLayout->addWidget(cardButton, row, col);
}
}
}
void GameGrid::checkMatch(CardButton* button){
if(button->key == lastSelectedCard->key){
button->setMatched();
lastSelectedCard->setMatched();
matched_counter++;
m_scoreLabel->setText("Score: " + QString::number(matched_counter));
}
else{
QElapsedTimer timer;
timer.start();
wait = true;
while (timer.elapsed() < 1000) {
// Wait for 1 seconds
QCoreApplication::processEvents();
}
button->flip();
lastSelectedCard->flip();
wait = false;
}
lastSelectedCard = nullptr;
}
void GameGrid:: allMatched(){
// check the all matches
if(matched_counter == 15){
winGame();
}
}
void GameGrid::startNewGame() {
// Clear the existing cards from the layout
if (wait)
return;
QLayoutItem* child;
while ((child = m_gridLayout->takeAt(0)) != nullptr)
{
delete child->widget();
delete child;
}
m_attemptLabel->setText("No. of Tries Remaining: 50");
m_scoreLabel->setText("Score: 0");
// Reset game variables
attempt_count = 0;
matched_counter = 0;
lastSelectedCard = nullptr;
// Shuffle the strings and initialize new cards
shuffleStrings();
initializeCards();
// Update the layout
m_gridLayout->update();
m_attemptLabel->update();
m_scoreLabel->update();
}
void GameGrid::lostGame() {
//lost game function to use when the attempts are finished
QMessageBox msgBox;
msgBox.setText("YOU LOST!");
msgBox.setInformativeText("Do you want to start a new game?");
msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
int ret = msgBox.exec();
if (ret == QMessageBox::Yes) {
startNewGame();
}
if (ret == QMessageBox::No)
{
qApp->exit();
}
}
void GameGrid::winGame() {
// check function when the all pairs are matched
QMessageBox msgBox;
msgBox.setText("YOU WIN!");
msgBox.setInformativeText("Do you want to start a new game?");
msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
int ret = msgBox.exec();
if (ret == QMessageBox::Yes) {
startNewGame();
}
if (ret == QMessageBox::No)
{
qApp->exit();
}
}