-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcubeface.cpp
279 lines (231 loc) · 6.07 KB
/
cubeface.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
#include "cubeface.h"
#include "color.h"
#include <string>
using namespace std;
//give a cubeface its defining color
CubeFace::CubeFace(Color clr) {
faceColor = clr;
for (int i = 0; i < 3; i++) {
vector<Color> row;
for (int j = 0; j < 3; j++) {
row.push_back(clr);
}
face.push_back(row);
}
}
//check if all colors in the face of the CubeFace is of this CubeFace's color
bool CubeFace::completedFace() {
for ( vector<Color> col : face ) {
for ( Color clr : col ) {
if (clr != faceColor) {
return false;
}
}
}
return true;
}
//add the given CubeFaces to this CubeFace's neighbor fields
void CubeFace::addNeighbors(CubeFace* btm, CubeFace* top, CubeFace* right, CubeFace* left) {
bottomFace = btm;
topFace = top;
rightFace = right;
leftFace = left;
}
//get the defining color of this cubeface
Color CubeFace::getFaceColor() {
return faceColor;
}
//get a specific color at the given coordinate of this face
Color CubeFace::getFace(int row, int col) {
return face[row][col];
}
//set the defining color of this cubeface
void CubeFace::setColor(Color clr) {
faceColor = clr;
}
//set the color at the given coordinate in the face to the given color
void CubeFace::setFace(int row, int col, Color clr) {
face[row][col] = clr;
}
//print the cubeface and with the colors in each part of the face
void CubeFace::printCubeFace() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << "[";
Color clr = face[i][j];
switch (clr) {
case 0:
cout << "\033[1mW\033[0m";
break;
case 1:
cout << "\033[1;31mR\033[0m";
break;
case 2:
cout << "\033[1;38;5;10mG\033[0m";
break;
case 3:
cout << "\033[1;38;5;202mO\033[0m";
break;
case 4:
cout << "\033[1;38;5;12mB\033[0m";
break;
case 5:
cout << "\033[1;38;5;11mY\033[0m";
break;
default:
cout << "?";
break;
}
cout << "]";
}
cout << endl;
}
cout << endl;
}
//transform the colors of this CubeFace's face clockwise
void CubeFace::turnClockWise() {
vector< vector<Color> > turnedFace;
for (int i = 0; i < 3; i++) {
vector<Color> row;
for (int j = 0; j < 3; j++) {
row.push_back(face[3 - j - 1].at(i));
}
turnedFace.push_back(row);
}
face = turnedFace;
}
//transform the colors of this CubeFace's face counter-clockwise
void CubeFace::turnCounterClockWise() {
vector< vector<Color> > turnedFace;
for (int i = 0; i < 3; i++) {
vector<Color> row;
for (int j = 0; j < 3; j++) {
row.push_back(face[j].at(3 - i - 1));
}
turnedFace.push_back(row);
}
face = turnedFace;
}
//return a vector of the topmost row of this face
vector<Color> CubeFace::getTop() {
vector<Color> temp;
for (int i = 0; i < 3; i++) {
temp.push_back(face[0][i]);
}
return temp;
}
//return a vector of the bottommost row of this face
vector<Color> CubeFace::getBottom() {
vector<Color> temp;
for (int i = 0; i < 3; i++) {
temp.push_back(face[2][i]);
}
return temp;
}
//return a vector of the leftmost column of this face
vector<Color> CubeFace::getLeft() {
vector<Color> temp;
for (int i = 0; i < 3; i++) {
temp.push_back(face[i][0]);
}
return temp;
}
//return a vector of the rightmost column of this face
vector<Color> CubeFace::getRight() {
vector<Color> temp;
for (int i = 0; i < 3; i++) {
temp.push_back(face[i][2]);
}
return temp;
}
//set the leftmost column of this face to the colors in the given vector
void CubeFace::setLeft(vector<Color> colors) {
if (colors.size() == 3) {
for (int i = 0; i < 3; i++) {
face[i][0] = colors[i];
}
}
}
//set the rightmost column of this face to the colors in the given vector
void CubeFace::setRight(vector<Color> colors) {
if (colors.size() == 3) {
for (int i = 0; i < 3; i++) {
face[i][2] = colors[i];
}
}
}
//set the topmost row of this face to the colors in the given vector
void CubeFace::setTop(vector<Color> colors) {
if (colors.size() == 3) {
for (int i = 0; i < 3; i++) {
face[0][i] = colors[i];
}
}
}
//set the bottommost row of this face to the colors in the given vector
void CubeFace::setBottom(vector<Color> colors) {
if (colors.size() == 3) {
for (int i = 0; i < 3; i++) {
face[2][i] = colors[i];
}
}
}
//swap the colors of the neighbors of this CubeFace clockwise given an Extract
void CubeFace::swapSidesClockWise(const Extract &e) {
vector<Color> temp = e.extractLeftFace(leftFace, true);
e.setLeftFace(leftFace, e.extractBottomFace(bottomFace, true));
e.setBottomFace(bottomFace, e.extractRightFace(rightFace, true));
e.setRightFace(rightFace, e.extractTopFace(topFace, true));
e.setTopFace(topFace, temp);
}
//swap the colors of the neighbors of this CubeFace counter-clockwise given an Extract
void CubeFace::swapSidesCounterClockWise(const Extract &e) {
vector<Color> temp = e.extractLeftFace(leftFace, false);
e.setLeftFace(leftFace, e.extractTopFace(topFace, false));
e.setTopFace(topFace, e.extractRightFace(rightFace, false));
e.setRightFace(rightFace, e.extractBottomFace(bottomFace, false));
e.setBottomFace(bottomFace, temp);
}
//rotate this CubeFace clockwise given an Extract
void CubeFace::rotateClockWise(const Extract &e) {
turnClockWise();
swapSidesClockWise(e);
}
//rotate this CubeFace counter-clockwise given an Extract
void CubeFace::rotateCounterClockWise(const Extract &e) {
turnCounterClockWise();
swapSidesCounterClockWise(e);
}
//rotate this CubeFace given an Extract and a direction
void CubeFace::rotate(const Extract &e, bool clockwise) {
if (clockwise)
rotateClockWise(e);
else
rotateCounterClockWise(e);
}
void CubeFace::orientClockWise() {
turnClockWise();
CubeFace* temp = leftFace;
leftFace = bottomFace;
bottomFace = rightFace;
rightFace = topFace;
topFace = temp;
}
void CubeFace::orientCounterClockWise() {
turnCounterClockWise();
CubeFace* temp = leftFace;
leftFace = topFace;
topFace = rightFace;
rightFace = bottomFace;
bottomFace = temp;
}
void CubeFace::totalReOrientation() {
turnClockWise();
turnClockWise();
CubeFace* tempOne = topFace;
topFace = bottomFace;
bottomFace = tempOne;
CubeFace* tempTwo = leftFace;
leftFace = rightFace;
rightFace = tempTwo;
}