-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenPiece.CC
273 lines (211 loc) · 6.97 KB
/
GenPiece.CC
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
// Greg Kaiser
//
// CS290 with Prof. Francis
// 4D Tetris
//
// GenPiece.C
// Last modified: April 25, 1996
//
// (C) 1996 Board of Trustees University of Illinois
#include "GenPiece.h"
#include <math.h>
#include <iostream>
//#define DEBUG_GENPIECE
extern Board *board;
extern int *DIM;
extern float *LEN;
extern float *botcorner;
// Given: The number of hypercubes in this piece
// Task: Allocate the appropriate number of Hypers for this piece
// Return: Nothing, but the Hypers have been allocated
GenPiece::GenPiece(int cubenum)
{
numcubes = cubenum;
cubes = new Hyper*[numcubes];
}
// Given: Nothing
// Task: Delete the memory used by this piece, but preserving the
// parts that were passed on to the board
// Return: Nothing, but the memory is cleared
GenPiece::~GenPiece()
{
// don't delete the cubes, as they will become part of the board
// after this is done
delete center;
}
// Given: The two axes for the plane of rotation, and the sign of rotation
// Task: Determine if this piece can be rotated in the given plane and
// and the given direction
// Return: A pointer to the new board positions of each of the cubes if
// the given rotation is possible, otherwise, a pointer to NULL
int **GenPiece::CanRotate(int dir, int dude, int sign)
{
#ifdef DEBUG_GENPIECE
cout << "Begin GenPiece::CanRotate(" << dir << ", " << sign << ")" << endl;
#endif
int **newpos = new int*[numcubes];
for (int i = 0; i < numcubes; i++)
newpos[i] = new int[4];
int i = 0;
int boolean = 1;
int dircenter, dudecenter;
if (centerstat < 0) { // center is messed up
dircenter = (int)(floor((((*center)[dir] - botcorner[dir])/LEN[dir])+.5));
dudecenter = (int)(floor((((*center)[dude] - botcorner[dude])/LEN[dude])
+ .5));
} else { // center is (0,0)
dircenter = cubes[centerstat]->GetPos()[dir];
dudecenter = cubes[centerstat]->GetPos()[dude];
}
while (boolean && (i < numcubes)) {
if (centerstat < 0) {
int dircoord = cubes[i]->GetPos()[dir];
int dudecoord = cubes[i]->GetPos()[dude];
int dirchange = ((dircoord >= dircenter) ? 1 : 0);
int dudechange = ((dudecoord >= dudecenter) ? 1 : 0);
dircoord += (-dircenter) + dirchange;
dudecoord += (-dudecenter) + dudechange;
int tempcoord = dircoord;
dircoord = dudecoord * sign;
dudecoord = tempcoord * (-sign);
dirchange = ((dircoord >= 0) ? 1 : 0);
dudechange = ((dudecoord >= 0) ? 1 : 0);
dircoord += dircenter - dirchange;
dudecoord += dudecenter - dudechange;
for (int j = 0; j < 4; j++)
newpos[i][j] = ((j == dir) ? dircoord : ((j == dude) ? dudecoord :
cubes[i]->GetPos()[j]));
} else {
int dircoord = cubes[i]->GetPos()[dir] - dircenter;
int dudecoord = cubes[i]->GetPos()[dude] - dudecenter;
int tempcoord = dircoord;
dircoord = dudecoord * sign;
dudecoord = tempcoord * (-sign);
dircoord += dircenter;
dudecoord += dudecenter;
for (int j = 0; j < 4; j++)
newpos[i][j] = ((j == dir) ? dircoord : ((j == dude) ? dudecoord :
cubes[i]->GetPos()[j]));
}
boolean = board->GoodSpot(newpos[i]);
i++;
}
#ifdef DEBUG_GENPIECE
cout << "End GenPiece::CanRotate(...)" << endl;
#endif
if (boolean)
return newpos;
return NULL;
}
// Given: Two axes of the plane of rotation, and a sign for the direction
// Task: If possible, perform the given rotation
// Return: Nothing, but if the piece could be rotated, then it's been rotated
void GenPiece::Rotate(int dir, int dude, int sign)
{
#ifdef DEBUG_GENPIECE
cout << "Begin GenPiece::Rotate(" << dir << ", " << sign << ")" << endl;
#endif
int **newpos = CanRotate(dir, dude, sign);
if (newpos != NULL) {
for (int i = 0; i < numcubes; i++)
cubes[i]->Rotate(newpos[i], center, dir, dude, sign);
for (int i = 0; i < numcubes; i++)
delete newpos[i];
delete newpos;
}
#ifdef DEBUG_GENPIECE
cout << "End GenPiece::Rotate(...)" << endl;
#endif
}
// Given: A direction and sign for translation
// Task: If possible, perform the given translation on this piece
// Return: 1 if the translation was performed, 0 if it was impossible
int GenPiece::Translate(int dir, int sign)
{
#ifdef DEBUG_GENPIECE
cout << "Begin GenPiece::Translate(" << dir << "," << sign << ")" << endl;
#endif
int edge = ((sign == 1) ? DIM[dir] : -1);
int **newpos = new int*[numcubes];
for (int i = 0; i < numcubes; i++)
newpos[i] = new int[4];
int i = 0;
int boolean = 1;
while (boolean && (i < numcubes))
if ((cubes[i]->GetPos()[dir] + sign) == edge)
boolean = 0;
else {
for (int j = 0; j < 4; j++)
newpos[i][j] = cubes[i]->GetPos()[j] + ((j == dir) ? sign : 0);
boolean = board->GoodSpotShort(newpos[i]);
i++;
}
if (boolean) {
for (int i = 0; i < numcubes; i++)
cubes[i]->Translate(dir, sign);
/*
cout << endl;
for (int i = 0; i < numcubes; i++)
for (int j = 0; j < 4; j++)
cout << "boardpos["<<i<<"]["<<j<<"] = "<<cubes[i]->GetPos()[j] << endl;
*/
float xx = ((dir == VX) ? (float)(sign)*LEN[VX] : 0.0);
float yy = ((dir == VY) ? (float)(sign)*LEN[VY] : 0.0);
float zz = ((dir == VZ) ? (float)(sign)*LEN[VZ] : 0.0);
float ww = ((dir == VW) ? (float)(sign)*LEN[VW] : 0.0);
center->Translate(xx, yy, zz, ww);
}
for (int i = 0; i < numcubes; i++)
delete newpos[i];
delete newpos;
#ifdef DEBUG_GENPIECE
cout << "End GenPiece::Translate(" << dir << "," << sign << ")" << endl;
#endif
return boolean;
}
// Given: Nothing
// Task: Drop this piece as far as possible in the negative VW direction
// Return: Nothing, but the piece has been dropped
void GenPiece::Drop()
{
#ifdef DEBUG_GENPIECE
cout << "Begin GenPiece::Drop()" << endl;
#endif
while (Translate(VW, -1));
#ifdef DEBUG_GENPIECE
cout << "End GenPiece::Drop()" << endl;
#endif
}
// Given: Nothing
// Task: Let the board know that we're done with this piece, basically by
// sending this pieces information to the board, so it can become
// part of the board (resistance is futile. you will be assimilated)
// Return: Nothing, but the piece has been assimilated to the board
int GenPiece::DonePiece()
{
#ifdef DEBUG_GENPIECE
cout << "Begin GenPiece::DonePiece()" << endl;
#endif
return (board->DonePiece(cubes, numcubes));
#ifdef DEBUG_GENPIECE
cout << "End GenPiece::DonePiece()" << endl;
#endif
}
// Given: Three axes for the projection
// Task: Draw this piece in the given projection, plus draw its shadow on
// the floor, if w is in the projection
// Return: Nothing, but the piee has been drawn
void GenPiece::Drawit(int axes)
{
#ifdef DEBUG_GENPIECE
// cout << "Begin GenPiece::Drawit()" << endl;
#endif
for (int i = 0; i < numcubes; i++)
cubes[i]->Drawit(axes);
if (axes != 7)
for (int i = 0; i < numcubes; i++)
cubes[i]->Drawit2D(axes);
#ifdef DEBUG_GENPIECE
// cout << "End GenPiece::Drawit()" << endl;
#endif
}