-
Notifications
You must be signed in to change notification settings - Fork 0
/
Memory.cpp
311 lines (281 loc) · 8.74 KB
/
Memory.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#include "Memory.h"
#include <QApplication>
#include <QAction>
#include <QActionGroup>
#include <QDialog>
#include <QGridLayout>
#include <QLocale>
#include <QMenu>
#include <QMenuBar>
#include <QMessageBox>
#include <QPushButton>
#include <QTextEdit>
#include <QTimer>
#include <QTranslator>
#include <QVBoxLayout>
#ifndef _WIN32
#include <unistd.h>
#include <sys/time.h>
#else
#include <windows.h>
#endif
void ssleep(unsigned int secs)
{
#ifdef _WIN32
Sleep(secs*1000);
#else
struct timespec req;
req.tv_sec = secs;
req.tv_nsec = 0;
nanosleep(&req, NULL);
#endif
}
Memory::Memory()
: m_iGridSize(3), m_bPlaying(false)
{
setWindowTitle(tr("Memory"));
{
QMenu *file = menuBar()->addMenu(tr("&Game"));
QAction *new_game = file->addAction(tr("&New game..."));
connect(new_game, SIGNAL(triggered()), this, SLOT(newGame()));
QAction *quit = file->addAction(tr("&Quit"));
connect(quit, SIGNAL(triggered()), qApp, SLOT(quit()));
}
{
QMenu *diff = menuBar()->addMenu(tr("&Difficulty"));
QActionGroup *dg = new QActionGroup(diff);
QAction *easy = dg->addAction(tr("&Easy"));
easy->setCheckable(true); diff->addAction(easy); easy->setChecked(true);
connect(easy, SIGNAL(triggered()), this, SLOT(setEasy()));
QAction *normal = dg->addAction(tr("&Normal"));
normal->setCheckable(true); diff->addAction(normal);
connect(normal, SIGNAL(triggered()), this, SLOT(setNormal()));
QAction *hard = dg->addAction(tr("&Hard"));
hard->setCheckable(true); diff->addAction(hard);
connect(hard, SIGNAL(triggered()), this, SLOT(setHard()));
}
{
QMenu *help = menuBar()->addMenu(tr("&Help"));
QAction *rules = help->addAction(tr("Game &rules..."));
connect(rules, SIGNAL(triggered()), this, SLOT(showRules()));
QAction *aboutQt = help->addAction(tr("About &Qt"));
connect(aboutQt, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
QAction *about = help->addAction(tr("&About Memory"));
connect(about, SIGNAL(triggered()), this, SLOT(about()));
}
show();
m_Rules = new QDialog(this);
m_Rules->setWindowTitle(tr("About Memory"));
QVBoxLayout *l = new QVBoxLayout;
QTextEdit *text = new QTextEdit;
text->setReadOnly(true);
text->setHtml(tr("<h2>Rules</h2>"
"<p>This game is a <em>remake</em> of the classic game published by "
"Ravensburger in 1959. The game shows pairs of identical cards placed "
"randomly and showing their hidden side. You can reveal a card by "
"clicking on it. When you reveal two cards, if they are not identical, "
"they are hidden again.</p>"
"<h2>Goal</h2>"
"<p>The goal is to reveal all the cards (as quickly as possible). "
"In order to win, you have to remember the position of every card to "
"be able to associate the pair when you discover the other card.</p>"));
l->addWidget(text);
l->setSizeConstraint(QLayout::SetFixedSize);
m_Rules->setModal(true);
m_Rules->setLayout(l);
}
void Memory::newGame()
{
m_bPlaying = true;
PlayingGrid *game = new PlayingGrid(m_iGridSize);
connect(game, SIGNAL(gameWon(int)), this, SLOT(gameWon(int)));
setCentralWidget(game);
resize(20 + 100 * m_iGridSize, 40 + 120 * (m_iGridSize-1));
}
void Memory::setEasy()
{
if(m_bPlaying && m_iGridSize != 3)
{
m_iGridSize = 3;
QMessageBox::StandardButton b = QMessageBox::question(this, tr("Abort "
"current game?"), tr("Do you wish to restart in easy mode?"),
QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
if(b == QMessageBox::Yes)
newGame();
}
else
m_iGridSize = 3;
}
void Memory::setNormal()
{
if(m_bPlaying && m_iGridSize != 4)
{
m_iGridSize = 4;
QMessageBox::StandardButton b = QMessageBox::question(this, tr("Abort "
"current game?"), tr("Do you wish to restart in normal mode?"),
QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
if(b == QMessageBox::Yes)
newGame();
}
else
m_iGridSize = 4;
}
void Memory::setHard()
{
if(m_bPlaying && m_iGridSize != 5)
{
m_iGridSize = 5;
QMessageBox::StandardButton b = QMessageBox::question(this, tr("Abort "
"current game?"), tr("Do you wish to restart in hard mode?"),
QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
if(b == QMessageBox::Yes)
newGame();
}
else
m_iGridSize = 5;
}
void Memory::gameWon(int time)
{
QMessageBox::information(this, tr("Victory"),
tr("You won the game in %1 seconds.").arg(time));
}
void Memory::showRules()
{
m_Rules->show();
}
void Memory::about()
{
QMessageBox::information(this, tr("About Memory..."),
tr("Memory is a game created by Rémi RAMPIN, aka remram44 "
"<remirampin@gmail.com>\n"
"It is not believed to be useful, it was written for educationnal "
"purpose."));
}
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QTranslator translator;
translator.load(QString("memory_") + QLocale::system().name());
app.installTranslator(&translator);
new Memory;
return app.exec();
}
PlayingGrid::PlayingGrid(int size)
: m_iSize(size), m_iSelX(-1), m_iSelY(0)
{
QGridLayout *layout = new QGridLayout;
setLayout(layout);
int n;
for(n = 0; n < size*(size-1); n++)
{
// Choose the position of the card
int r = (int)( ((double)rand()) / RAND_MAX * (size*(size-1)-n) );
// Looks for the r-th empty cell
int p = 0;
while(r >= 0)
{
if(layout->itemAtPosition(p/size, p%size) == NULL)
{
r--;
if(r < 0)
{
Card *c = new Card(p/size, p%size, n/2);
connect(c, SIGNAL(selected(int, int, int)),
this, SLOT(cardSelected(int, int, int)));
layout->addWidget(c, p/size, p%size);
}
}
p++;
}
}
m_Wait = new QTimer;
m_Wait->setSingleShot(true);
connect(m_Wait, SIGNAL(timeout()), this, SLOT(hideCards()));
m_Time.start();
}
#define GET_CARD(i, j) static_cast<Card*>(grid->itemAtPosition((i), (j)) \
->widget())
void PlayingGrid::cardSelected(int x, int y, int n)
{
// Waiting period during which two different cards stay visible (they are
// then hidden again)
if(m_Wait->isActive())
return ;
QGridLayout *grid = static_cast<QGridLayout*>(layout());
GET_CARD(x, y)->setMode(Card::ACTIVE);
if(m_iSelX == -1)
{
m_iSelX = x;
m_iSelY = y;
}
else
{
if(GET_CARD(m_iSelX, m_iSelY)->getN() == n)
{
// TODO : play a sound or something
GET_CARD(m_iSelX, m_iSelY)->setMode(Card::SHOWN);
GET_CARD(x, y)->setMode(Card::SHOWN);
m_iSelX = -1;
int i, j;
for(i = 0; i < m_iSize-1; i++)
{
for(j = 0; j < m_iSize; j++)
{
if(GET_CARD(i, j)->getMode() == Card::HIDDEN)
return ;
}
}
emit gameWon(m_Time.elapsed()/1000);
}
else
{
m_iSelX = -1;
m_Wait->start(2000);
}
}
}
void PlayingGrid::hideCards()
{
QGridLayout *grid = static_cast<QGridLayout*>(layout());
int i, j;
for(i = 0; i < m_iSize-1; i++)
{
for(j = 0; j < m_iSize; j++)
{
if(GET_CARD(i, j)->getMode() == Card::ACTIVE)
GET_CARD(i, j)->setMode(Card::HIDDEN);
}
}
}
Card::Card(int i, int j, int n)
: x(i), y(j), N(n), m_eMode(Card::HIDDEN)
{
// TODO : graphics
m_Button = new QPushButton("-", this);
connect(m_Button, SIGNAL(clicked()),
this, SLOT(clicked()));
}
void Card::clicked()
{
if(m_eMode == Card::HIDDEN)
emit selected(x, y, N);
}
void Card::setMode(EMode e)
{
m_eMode = e;
switch(e)
{
case Card::HIDDEN:
m_Button->setText("-");
break;
case Card::SHOWN:
m_Button->setText(QString::number(N));
break;
case Card::ACTIVE:
m_Button->setText(QString::number(N));
break;
}
QFont f = font();
f.setBold(m_eMode == Card::ACTIVE);
setFont(f);
}