-
Notifications
You must be signed in to change notification settings - Fork 0
/
PField.cpp
260 lines (202 loc) · 6.56 KB
/
PField.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
//
// Created by Douglas Wilcox on 2019-03-30.
//
#include "PField.h"
using namespace std;
//constructor
PField::PField(QGraphicsScene *scene, int sizeX, int sizeY, int gridsize)
: myGridSize(gridsize), myFieldWidth(sizeX), myFieldHeight(sizeY), myScene(scene)
{
int size = myFieldHeight * myFieldWidth;
myClearTimer = new QTimer;
connect(myClearTimer, SIGNAL(timeout()), this, SLOT(ClearFilledRows()));
myIntField.reserve(size);
myIntField.resize(size);
tempIntField.reserve(size);
tempIntField.resize(size);
myTetrinoField.reserve(size);
myTetrinoField.resize(size);
}
//destructor
PField::~PField() {
delete myClearTimer;
}
void PField::PFieldInit() {
myIntField.clear();
tempIntField.clear();
myFilledRows.clear();
for(int i = 0; i < myTetrinoField.size(); i++)
{
if (myTetrinoField[i].get() != nullptr)
{
if (myTetrinoField[i].get()->scene() != NULL)
{
myScene->removeItem(myTetrinoField[i].get());
}
}
}
//create walls and draw them in dark blue
for(int y = 0; y < myFieldHeight; y++)
{
for(int x = 0; x < myFieldWidth; x++)
{
int index = y * myFieldWidth + x;
myIntField[index] = (x==0 || x == myFieldWidth -1 || y == myFieldHeight - 1) ? 1 : 0;
if (myIntField[index] == 1)
{
//create a block at this spot
myTetrinoField[index] = make_unique<QGraphicsRectItem>();
myTetrinoField[index]->setRect(0, 0, myGridSize, myGridSize);//sets UL corner, then offset for LR corner
myTetrinoField[index]->setBrush(Qt::darkBlue); //setBrush for color
myTetrinoField[index]->setPos(x*myGridSize, y*myGridSize);
myScene->addItem(myTetrinoField[index].get());
}
}
}
ResetTempIntField();
}
int PField::GetFieldWidth() {
return myFieldWidth;
}
int PField::CheckIntField(int index) {
return myIntField[index];
}
void PField::PutPieceToField(int index, QColor color) {
//set this position to 2 to distinguish from wall
myIntField[index] = 2;
//how create and draw a rect at this location;
myTetrinoField[index] = make_unique<QGraphicsRectItem>();
myTetrinoField[index]->setRect(0, 0, myGridSize, myGridSize);//sets UL corner, then offset for LR corner
myTetrinoField[index]->setBrush(color); //setBrush for color
int fieldPosX = index % myFieldWidth;
int fieldPosY = index / myFieldWidth;
myTetrinoField[index]->setPos(fieldPosX*myGridSize, fieldPosY*myGridSize);
myScene->addItem(myTetrinoField[index].get());
}
void PField::CheckForFilledRow() {
// vector<int> filledRows;
int row = 0;
myFilledRows.clear();
//find filled rows
for(int y = 0; y < myFieldHeight - 1; y++) //y steps down through rows (ignoring last)
{
bool filled = true; //assume this row is filled
for(int x = 0; x < myFieldWidth; x++) //x steps across the row checking each pos
{
int index = y * myFieldWidth + x;
if( myIntField[index] == 0 ) //empty positions are 0
{
//this row is not filled, move to next row
filled = false;
break;
}
}
if (filled)
{
myFilledRows.push_back(row);
}
row++;
}
//turn this row yellow
for (int thisrow : myFilledRows)
{
int y = thisrow;
for(int x = 0; x < myFieldWidth; x++) //x steps across the row
{
int index = y * myFieldWidth + x;
if (myIntField[index] != 1)
{
myTetrinoField[index]->setBrush(Qt::yellow);
}
myClearTimer->start(500);
}
}
}
void PField::ClearFilledRows() {
myClearTimer->stop();
ResetTempIntField();
//first, remove blocks and set ints to zero
for(auto thisrow : myFilledRows)
{
//clear and remove any QGraphicsItems in this row
int y = thisrow;
for(int x = 0; x < myFieldWidth; x++) //x steps across the row
{
int index = y * myFieldWidth + x;
if (myIntField[index] != 1) //do not remove walls
{
if (myIntField[index] == 2)
{
myScene->removeItem( myTetrinoField[index].get() );
myTetrinoField[index] = nullptr;
myIntField[index] = 0;
}
}
}
}
//now move blocks above down
for(int thisrow : myFilledRows)
{
for(int y = thisrow; y > 0; y-- )
{
for(int x = 0; x < myFieldWidth; x++) //x steps across this row
{
int index = y * myFieldWidth + x;
if (myIntField[index] != 1) //do not move wall pieces
{
//move any blocks above down to this row
if (myTetrinoField[index - myFieldWidth] != nullptr)
{
// cout << "row/block: " << y << "/" << x << endl;
myTetrinoField[index - myFieldWidth]->setPos(x*myGridSize, y*myGridSize);
}
//and update their indices
myTetrinoField[index] = myTetrinoField[index - myFieldWidth];
myTetrinoField[index - myFieldWidth] = nullptr;
myIntField[index] = myIntField[index - myFieldWidth]; //set int to row above
}
}
}
}
}
int PField::GetNumFilledRows() {
return myFilledRows.size();
}
void PField::ResetNumFilledRows() {
myFilledRows.clear();
}
//DEBUGGING / TESTING
void PField::PrintField() {
cout << "_____INT FIELD_____" << endl;
for(int y = 0; y < myFieldHeight; y++)
{
for (int x = 0; x < myFieldWidth; x++)
{
int index = y * myFieldWidth + x;
cout << myIntField[index];
}
cout << endl;
}
cout << "___________________" << endl << endl;
}
void PField::ResetTempIntField() {
for (int x = 0; x < myIntField.size(); x++)
{
tempIntField[x] = myIntField[x];
}
}
void PField::PrintTempIntField() {
for(int y = 0; y < myFieldHeight; y++)
{
for (int x = 0; x < myFieldWidth; x++)
{
int index = y * myFieldWidth + x;
cout << tempIntField[index];
}
cout << endl;
}
cout << "___________" << endl << endl;
}
void PField::PutTempIntField(int index) {
tempIntField[index] = 1;
}