-
Notifications
You must be signed in to change notification settings - Fork 0
/
piece.h
298 lines (281 loc) · 8.37 KB
/
piece.h
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
class Piece
{
private:
int type; // I J L O S T Z
bool grid[4][4]; // The shape of the piece on the board
// 0,0 is bottom left corner of piece
int loc[2]; // coordinate of bottom middle of x,y
int size; // the size of the grid that the object is using
int rotateamount; // how much to rotate a pic
// Colours - Red, Green, Blue, Purple, Yellow, Orange, White
public:
Piece();
void Set_Active();
void Rotate(int direction);
void Move_Down();
void Move_Over(int direction);
int Get_Rotate();
int Get_Type();
bool In_Spot(int x, int y); // If the piece occupies a spot of grid array
void Get_Loc(int& x, int& y); // Collecting the x,y coordinates
BITMAP* pic;
};
Piece::Piece () // Initialize
{
type = rand() % 7; // Type of piece
rotateamount = 0;
/////////////////////////////////////////////
for (int i = 0; i < 4; i++) // Assigning Grid
for (int j = 0; j < 4; j++)
grid[j][i] = 0;
if (type == 0) // I
{
for (int i = 0; i < 4; i++)
grid[0][i] = 1;
size = 4;
pic=load_bmp("pics/I.bmp",NULL);
}
else if (type == 1) // J
{
for (int i = 0; i < 3; i++)
grid[0][i] = 1;
grid[1][2] = 1;
size = 3;
pic=load_bmp("pics/J.bmp",NULL);
}
else if (type == 2) // L
{
for (int i = 0; i < 3; i++)
grid[1][i] = 1;
grid[0][2] = 1;
size = 3;
pic=load_bmp("pics/L.bmp",NULL);
}
else if (type == 3) // O
{
for (int i = 0; i < 2; i++)
{
grid[1][i] = 1;
grid[0][i] = 1;
}
size = 2;
pic=load_bmp("pics/O.bmp",NULL);
}
else if (type == 4) // S
{
for (int i = 0; i < 2; i++)
{
grid[1][i] = 1;
grid[0][i+1] = 1;
}
size = 3;
pic=load_bmp("pics/S.bmp",NULL);
}
else if (type == 5) // T
{
for (int i = 0; i < 3; i++)
grid[0][i] = 1;
grid[1][1] = 1;
size = 3;
pic=load_bmp("pics/T.bmp",NULL);
}
else if (type == 6) // Z
{
for (int i = 0; i < 2; i++)
{
grid[0][i] = 1;
grid[1][i+1] = 1;
}
size = 3;
pic=load_bmp("pics/Z.bmp",NULL);
}
/////////////////////////////////////////////
}
void Piece::Set_Active()
{
loc[0] = 7;
loc[1] = 25;
}
void Piece::Rotate(int direction)
{
//size = 4;
bool ok = true;
bool rotation[size][size];
if (direction == 1) // clockwise
{
if (loc[0] < 1) // colliding with left edge
{
for (int i = loc[0]; i < 1; i++)
for (int j = 0; j < 4; j++)
if (grid[j][i-loc[0]])
ok = false;
}
else if (loc[0] > 12) // right edge
for (int i = loc[0]; i > 12; i--)
for (int j = 0; j < 4; j++)
if (grid[j][3-(i-loc[0])])
ok = false;
if (loc[1] < 0) // colliding with bottom
{
for (int i = loc[1]; i < 0; i++)
for (int j = 0; j < 4; j++)
if (grid[3-(i-loc[1])][j])
ok = false;
}
if (ok)
{
for (int i = 0; i < size; i++) // rotating
for (int j = 0; j < size; j++)
rotation[i][size-j-1] = grid[j][i];
for (int i = 0; i < size; i++)
for (int j = 0; j < size; j++)
grid[i][j] = rotation[i][j];
for (int i = 0; i < 4; i++) // collide with other pieces
for (int j = 0; j < 4; j++)
if (board[loc[0]+i][loc[1]+j] && In_Spot(i,j))
ok=false;
if (ok)
rotateamount+=64;
else
{
for (int i = 0; i < size; i++) // unrotate
for (int j = 0; j < size; j++)
rotation[j][i] = grid[i][size-j-1];
for (int i = 0; i < size; i++)
for (int j = 0; j < size; j++)
grid[i][j] = rotation[i][j];
}
}
}
else if (direction == 2) // counter clockwise
{
if (loc[0] < 1) // left edge collision
{
for (int i = loc[0]; i < 1; i++)
for (int j = 0; j < 4; j++)
if (grid[j][3-(i-loc[0])])
ok = false;
}
else if (loc[0] > 12) // right edge
for (int i = loc[0]; i >= 12; i--)
for (int j = 0; j < 4; j++)
if (grid[j][i-loc[0]])
ok = false;
if (loc[1] < 0) // colliding with bottom
{
for (int i = loc[1]; i < 0; i++)
for (int j = 0; j < 4; j++)
if (grid[i-loc[1]][j])
ok = false;
}
if (ok)
{
for (int i = 0; i < size; i++) // rotate
for (int j = 0; j < size; j++)
rotation[j][i] = grid[i][size-j-1];
for (int i = 0; i < size; i++)
for (int j = 0; j < size; j++)
grid[i][j] = rotation[i][j];
for (int i = 0; i < 4; i++) // check against other pieces
for (int j = 0; j < 4; j++)
if (board[loc[0]+i][loc[1]+j] && In_Spot(i,j))
ok=false;
if (ok)
rotateamount-=64;
else
{
for (int i = 0; i < size; i++) // rotate back
for (int j = 0; j < size; j++)
rotation[i][size-j-1] = grid[j][i];
for (int i = 0; i < size; i++)
for (int j = 0; j < size; j++)
grid[i][j] = rotation[i][j];
}
}
}
bool empty;
for (int i = 0; i < 4; i++)
{
empty = true;
for (int j = 0; j < 4; j++)
if (grid[0][j])
empty = false;
if (empty)
{
for (int k = 1; k < 4; k++)
for (int j = 0; j < 4; j++)
grid[k-1][j] = grid[k][j];
for (int j = 0; j < 4; j++)
grid[3][j] = false;
}
}
for (int i = 0; i < 4; i++)
{
empty = true;
for (int j = 0; j < 4; j++)
if (grid[j][0])
empty = false;
if (empty)
{
for (int k = 1; k < 4; k++)
for (int j = 0; j < 4; j++)
grid[j][k-1] = grid[j][k];
for (int j = 0; j < 4; j++)
grid[j][3] = false;
}
}
}
void Piece::Move_Down ()
{
loc[1]--;
}
void Piece::Move_Over (int direction)
{
bool ok = true;
int min=4,max=-1; // find edges to prevent going outside walls
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
if (grid[i][j])
{
if (i > max)
max = i;
if (i < min)
min = i;
}
if (direction == 1 && loc[0]+min > 1) // Left
{
for (int i = 0; i < 4; i++) // check against other pieces
for (int j = 0; j < 4; j++)
if (board[loc[0]+i-1][loc[1]+j] && In_Spot(i,j))
ok=false;
if (ok)
loc[0]--;
}
else if (direction == 2 && loc[0]+max < 15) // Right
{
for (int i = 0; i < 4; i++) // check against other pieces
for (int j = 0; j < 4; j++)
if (board[loc[0]+i+1][loc[1]+j] && In_Spot(i,j))
ok = false;
if (ok)
loc[0]++;
}
}
int Piece::Get_Rotate ()
{
return rotateamount%256;
//return rotateamount;
}
int Piece::Get_Type ()
{
return type;
}
bool Piece::In_Spot (int x, int y)
{
return grid[x][y];
}
void Piece::Get_Loc (int& x, int& y)
{
x = loc[0];
y = loc[1];
}