-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglass.cpp
401 lines (276 loc) · 7.6 KB
/
glass.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#include "glass.h"
#include <QDebug>
#include <QPainter>
#include<QKeyEvent>
#include<QMap>
#include<QMessageBox>
glass::glass(QWidget *parent) : QWidget(parent)
{
timerInterval=0;
gameOn=false;
score=0;
m_rows=0;
m_columns=0;
cur=new figure(W);
next=new figure(W);
qDebug()<<"next figure x: "<<next->getUpX();
QObject::connect(this, SIGNAL(signalGlassInit()), SLOT(slotGlassInit()), Qt::QueuedConnection);
emit signalGlassInit();
qDebug()<<"Init glass";
}
//
// clear the glass
//
void glass::clearGlass(){
for(int i=0; i<m_rows; i++){glassArray[i].fill(emptyCell, m_columns);}
// foreach(QVector<QColor> c, glassArray){c.fill(QColor(0,1,0), c.size());} // foreach не работает!!!!!!!!!!!!!!!!!!!!!!!!
}
//
// creating the glass
//
void glass::slotGlassInit(){
glassArray.resize(m_rows);
for(int i=0; i<m_rows; i++){glassArray[i].resize(m_columns);}
clearGlass();
QSize s(W*m_columns, W*m_rows);
setFixedSize(s);
timerInterval=500;
repaint();
}
void glass::paintEvent(QPaintEvent*event){
uint x=1, y=1;
QPainter painter(this);
foreach(QVector<QColor> c, glassArray){
foreach(QColor col, c){
painter.fillRect(x,y, W-1,W-1,col);
x+=W;
}
x=1;
y+=W;
}
if(gameOn) // moving the figure down
{
cur->paintFigure(painter);
}
};
//
// start new game
//
void glass::slotNewGame(){
gameOn=true;
clearGlass();
cur->setUpIndex(m_columns/2, 0);
cur->MakeRandomColors();
next->MakeRandomColors();
emit(signalNextNewFigure(next));
idTimer=startTimer(timerInterval);
setFocus();
repaint();
}
//
// recording of new colors into array
//
void glass::acceptColors(int i, int j){
glassArray[j-2][i]=cur->getColor()[0];
glassArray[j-1][i]=cur->getColor()[1];
glassArray[j][i]=cur->getColor()[2];
};
//
// adding of new figure, analysing and dropping the sets
//
void glass::currentStepComplete(int x, int y){
acceptColors(x,y);
check=true;
figure*tmp=cur;
cur=next;
next=tmp;
cur->setUpIndex(m_columns/2, 0);
next->setUpIndex(0, 0);
next->MakeRandomColors();
emit(signalNextNewFigure(next));
};
void glass::timerEvent(QTimerEvent*event){
if(check){CheckGlass();}
else{
if(glassArray[2][m_columns/2]!=emptyCell){ // stop the game if there is no space left for fugure to fall
killTimer(idTimer);
QMessageBox::information(this,"Information","Game over");
return;
}
int x=cur->getUpX();
int y=cur->getDownY();
if(y==m_rows-1||glassArray[y+1][x]!=emptyCell){// glassArray[y+1] - не будет выхода за границы, т.к. при достижении низа - первое условие будет true и второе не будет вычисляться
currentStepComplete(x, y);}
else{
cur->goDown();
}
}
repaint();
};
//
//check the glass and drop the sets
//
void glass::CheckGlass() {
bool b=false;
QMap<int, QList<int>> m;
QMap<int, QList<int>> m1;
{qDebug()<<"VERTICAL";
for(int k=0; k<=m_columns-1; k++){
int start=0;
int end=1;
while(end<m_rows) {
if(glassArray[start][k]==emptyCell){
if(end==m_rows-1&&glassArray[end][k]!=emptyCell)
{m[k].append(m_rows-1);
}
start++; end++;
}
else{
if (glassArray[start][k]==glassArray[end][k]&&end<m_rows-1){end++;}
else{
int delta=end-start;
if(end==m_rows-1&&glassArray[start][k]==glassArray[end][k]){delta++; end++;}
if(end==m_rows-1&&glassArray[start][k]!=glassArray[end][k])
{
m[k].append(m_rows-1);}
if(delta<=2)
{ for(int i=start; i<end; i++){
m[k].append(i);
}}
else{b=true; score+=delta; }
start=end;
end=start+1;
}
}
};
}
}
{ qDebug()<<"HORIZONTAL";
for(int k=m_rows-1; k>=0; k--){
int start=0;
int end=1;
while(end<m_columns) {
if(glassArray[k][start]==emptyCell){
if(end==m_columns-1&&glassArray[k][end]!=emptyCell)
{
if (m.contains(m_columns-1)){
if( m[m_columns-1].contains(k))
{
m1[m_columns-1].append(k);
}
}
}
start++; end++;}
else{
if (glassArray[k][start]==glassArray[k][end]&&end<m_columns-1){
end++;}
else{
int delta=end-start;
if(end==m_columns-1&&glassArray[k][start]==glassArray[k][end]){delta++; end++;}
if(end==m_columns-1&&glassArray[k][start]!=glassArray[k][end]){
if (m.contains(m_columns-1)){
if( m[m_columns-1].contains(k))
{
m1[m_columns-1].append(k);
}
}
}
if(delta<=2)
{
for(int i=start; i<end; i++){
if (m.contains(i)){
if( m[i].contains(k))
{
m1[i].append(k);
}
}
}
}
else{b=true; score+=delta; }
start=end;
end=start+1;
}
};
}
}
}
if(b){
for (int i=0; i<m_columns;i++){
if (!m1.contains(i)){
for(int j=0; j<m_rows; j++)
{
glassArray[j][i]=emptyCell;
}
}
}
auto it1=m1.begin(), it2=m1.end();
int i=0;
while(it1!=it2)
{
std::sort(std::begin(it1.value()), std::end(it1.value()));
auto it1_set=it1.value().rbegin(), it2_set=it1.value().rend();
i=0;
while(it1_set!=it2_set)
{
glassArray[m_rows-1-i][it1.key()]=glassArray[*it1_set][it1.key()];
it1_set++;
i++;
}
int empty=m_rows-it1.value().size();
for(int j=0; j<empty; j++){
glassArray[j][it1.key()] =emptyCell;
}
it1++;
}
emit (signalSetLCD(score));
}
else {
if(glassArray[0][m_columns/2]!=emptyCell){ // stop the game if there is no space left for fugure to fall
killTimer(idTimer);
QMessageBox::information(this,"Information","Game over");
return;
}
else{check=false;}
}
};
void glass::keyPressEvent(QKeyEvent*event){
if(gameOn)
{
// if game is on
switch(event->key()) {//code of the key pressed
case Qt::Key_Left:
if(cur->getUpX()>0&&glassArray[cur->getDownY()][cur->getUpX()-1]==emptyCell)
{ cur->toLeft();
repaint(); qDebug()<<"Repaint in keyPressEvent";}
break;
case Qt::Key_Right:
if(cur->getUpX()<m_columns-1&&glassArray[cur->getDownY()][cur->getUpX()+1]==emptyCell)
{
cur->toRight();
}
break;
case Qt::Key_Down:
cur->rotateColors(false);
break;
case Qt::Key_Up:
cur->rotateColors(true);
repaint(); qDebug()<<"Repaint in keyPressEvent";
break;
case Qt::Key_Space:
{
int i=m_rows-1, j=cur->getUpX();
while(glassArray[i][j]!=emptyCell){
i--;
}
cur->setUpIndex(j,i-2);
check=true;
}
break;
default:
QWidget::keyPressEvent(event);
}
}
else{
QWidget::keyPressEvent(event);
}
}