-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathManager (PieceFunctionality).cpp
323 lines (254 loc) · 7.7 KB
/
Manager (PieceFunctionality).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
/******************************************************************************************
PIECE FUNCTIONALITY
******************************************************************************************/
/************************************************************************
Filename: Manager (PieceFuctionality).cpp
Description: Is used to keep the function implamentations
that handle pieces in Manager.h seperate from the general
fucntions that the Game uses.
************************************************************************/
#include "Manager.h"
#include <mmsystem.h>
/*
Description: Flips the tetronimoe data wise
Assumption: The piece exists.
PostCondition: Flips if it can within the contstaints
of the field and other pieces surrounding
it
*/
void Manager::flip()
{
//For each bit check if interfere
bool isEmpty = false;
int rowToCheck, colToCheck;
Piece* flipped = 0;
//Get flipped of active
if( pInAction_->getType() != 'O' )
{
flipped = giveFlipped(*pInAction_);
isEmpty = true;
}
for(int i = 0; i < 4 && isEmpty; i++)
{
rowToCheck = (*flipped)[i].r_;
colToCheck = (*flipped)[i].c_;
if( rowToCheck < 0 || rowToCheck >= playField_.getRows() )
isEmpty = false;
if( colToCheck < 0 || colToCheck >= playField_.getColumns() )
isEmpty = false;
if( isEmpty && playField_[rowToCheck][colToCheck] != EMPTY)
isEmpty = isAPartOfActive(rowToCheck, colToCheck);
}
//If isEmpty is true, set the *pInAction_ to its flipped
if(isEmpty)
{
delete pInAction_;
//Reset inAction piece
pInAction_ = flipped;
//Reset container's pointer to inAction piece
int numAction = container_.getNumPieces()-1;
//HHOOOOOOLLLYYYY SHIIIIIIIIIIT!!!!!!!!!!!!!
container_.setPointerAt(numAction, pInAction_);
}
}
/*
Description: moves the piece right if can
Assumption: The piece exists.
PostCondition: Moves the piece right within the contstaints
of the field and other pieces surrounding
it
*/
void Manager::moveRight()
{
//For each bit check if interfere
bool isEmpty = true;
int rowToCheck, colToCheck;
for(int i = 0; i < 4; i++)
{
rowToCheck = (((*pInAction_)[i].r_));
colToCheck = (((*pInAction_)[i].c_)+1);
if( rowToCheck < 0 || rowToCheck >= playField_.getRows() )
isEmpty = false;
if( colToCheck < 0 || colToCheck >= playField_.getColumns() )
isEmpty = false;
if( isEmpty && playField_[rowToCheck][colToCheck] != EMPTY )
isEmpty = isAPartOfActive(rowToCheck, colToCheck);
}
//If isEmpty is true, set the *pInAction_ to its flipped
if(isEmpty)
{
for(int i = 0; i < 4; i++)
{
rowToCheck = (((*pInAction_)[i].r_));
colToCheck = (((*pInAction_)[i].c_)+1);
(*pInAction_)[i].r_ = rowToCheck;
(*pInAction_)[i].c_ = colToCheck;
}
}
}
/*
Description: moves the piece right if can
Assumption: The piece exists.
PostCondition: Moves the piece right within the contstaints
of the field and other pieces surrounding
it
*/
void Manager::moveLeft()
{
//For each bit check if interfere
bool isEmpty = true;
int rowToCheck, colToCheck;
for(int i = 0; i < 4; i++)
{
rowToCheck = (((*pInAction_)[i].r_));
colToCheck = (((*pInAction_)[i].c_)-1);
if( rowToCheck < 0 || rowToCheck >= playField_.getRows() )
isEmpty = false;
if( colToCheck < 0 || colToCheck >= playField_.getColumns() )
isEmpty = false;
if( isEmpty && playField_[rowToCheck][colToCheck] != EMPTY )
isEmpty = isAPartOfActive(rowToCheck, colToCheck);
}
//If isEmpty is true, set the *pInAction_ to its flipped
if(isEmpty)
{
for(int i = 0; i < 4; i++)
{
rowToCheck = (((*pInAction_)[i].r_));
colToCheck = (((*pInAction_)[i].c_)-1);
(*pInAction_)[i].r_ = rowToCheck;
(*pInAction_)[i].c_ = colToCheck;
}
}
}
/*
Description: moves the piece down
Assumption: The piece exists.
PostCondition: Moves the piece right within the contstaints
of the field and other pieces surrounding
it
Returns: true if it can't move down (hitRockBottom)
*/
bool Manager::gravity()
{
//For each bit check if interfere
bool isEmpty = true;
int rowToCheck, colToCheck;
//Occupied?
for(int i = 0; i < 4 && isEmpty; i++)
{
rowToCheck = (((*pInAction_)[i].r_)+1);
colToCheck = (((*pInAction_)[i].c_));
//In row bounds?
if( rowToCheck < 0 || rowToCheck >= playField_.getRows() )
isEmpty = false;
//In col bounds?
if( colToCheck < 0 || colToCheck >= playField_.getColumns() )
isEmpty = false;
if( isEmpty && playField_[rowToCheck][colToCheck] != EMPTY )
isEmpty = isAPartOfActive(rowToCheck, colToCheck);
}
//If isEmpty is true, set the *pInAction_ to its dropped
if(isEmpty)
{
for(int i = 0; i < 4; i++)
{
rowToCheck = (((*pInAction_)[i].r_)+1);
(*pInAction_)[i].r_ = rowToCheck;
}
}
//Return is empty
return !isEmpty;
}
/*
Description: checks to see if the particular coordinate
is a part of the active piece in motion. This
is to combat the glitching behavior that cause
a pieces components to not want to move into
itself.
Assumption: The piece exists.
PostCondition: Moves the piece right within the contstaints
of the field and other pieces surrounding
it
Returns: true if it can't move down (hitRockBottom)
*/
bool Manager::isAPartOfActive(int row, int col)
{
bool isAPartOfMe = false;
for(int bitNum = 0; bitNum < 4; bitNum++)
{
if( (*pInAction_)[bitNum].r_ == row && (*pInAction_)[bitNum].c_ == col)
isAPartOfMe = true;
}
return isAPartOfMe;
}
/*
Purpose: Is used to shift Pieces that are
moving into activity into the
play field. If they can move in
due to realestate conflicts, then
alert whomever called me.
Assumptions: The Piece must be created such that
all of its bits are in negative rows
above the center of the playField.
Resultant: The tetronimoe is ready to be moved
Output: Returns the condition of
cannotMoveToField
*/
bool Manager::moveActiveToPlayField(Piece &p)
{
//Bool conditions
bool isEmpty = true;
bool inPlayField = true;
//contants
int zeroBitRowPlusOne, oneBitRowPlusOne,twoBitRowPlusOne, threeBitRowPlusOne;
//Check Ints
int rowToCheck, colToCheck;
//In playField?
for(int i = 0; i < 4; i++)
{
if( p[i].r_ < 0 || p[i].r_ >= playField_.getRows() || p[i].c_ < 0 || p[i].c_ >= playField_.getColumns() )
inPlayField = false;
}
//Occupied?
for(int i = 0; i < 4 && isEmpty; i++)
{
rowToCheck = (((*pInAction_)[i].r_)+1);
colToCheck = (((*pInAction_)[i].c_));
if(rowToCheck >= 0 && rowToCheck>= 0 && playField_[rowToCheck][colToCheck] != EMPTY )
isEmpty = isAPartOfActive(rowToCheck, colToCheck);
}
while( !(inPlayField) && (isEmpty) )
{
//Occupied?
for(int i = 0; i < 4 && isEmpty; i++)
{
rowToCheck = (((*pInAction_)[i].r_)+1);
colToCheck = (((*pInAction_)[i].c_));
if(rowToCheck >= 0 && rowToCheck>= 0 && playField_[rowToCheck][colToCheck] != EMPTY )
isEmpty = isAPartOfActive(rowToCheck, colToCheck);
}
//Get next locations
zeroBitRowPlusOne = (p[0].r_)+1;
oneBitRowPlusOne = (p[1].r_)+1;
twoBitRowPlusOne = (p[2].r_)+1;
threeBitRowPlusOne = (p[3].r_)+1;
//If empty then move
if(isEmpty)
{
p[0].r_ = zeroBitRowPlusOne;
p[1].r_ = oneBitRowPlusOne;
p[2].r_ = twoBitRowPlusOne;
p[3].r_ = threeBitRowPlusOne;
}
//In playField?
inPlayField = true;
for(int i = 0; i < 4; i++)
{
if( p[i].r_ < 0 || p[i].r_ >= playField_.getRows() || p[i].c_ < 0 || p[i].c_ >= playField_.getColumns() )
inPlayField = false;
}
}
//Return (cannotMoveToField)
return !isEmpty;
}