-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCube.cpp
640 lines (558 loc) · 19.1 KB
/
Cube.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
/**
* SPDX-License-Identifier: MIT
*
* Copyright (c) 2022 Chuck Wolber
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include <cmath>
#include <vector>
#include "Cube.hpp"
Cube::Cube() {
this->cubeSize = DEFAULT_SIZE;
this->fInitColor = CubieColor::WHITE;
initializeCube();
}
Cube::Cube(CubieColor referenceColor) {
this->cubeSize = DEFAULT_SIZE;
this->fInitColor = referenceColor;
initializeCube();
}
Cube::Cube(CubieColor referenceColor, unsigned int cubeSize) {
if (cubeSize < MIN_SIZE)
cubeSize = MIN_SIZE;
this->cubeSize = cubeSize;
this->fInitColor = referenceColor;
initializeCube();
}
Cube::Cube(const Cube& obj) {
cubeSize = obj.cubeSize;
fInitColor = obj.fInitColor;
initializeCube();
copyCube(obj);
}
Cube::Cube(Cube&& obj) {
copyCubeAttributes(obj);
obj.cube = nullptr;
obj.edges = nullptr;
}
Cube& Cube::operator=(const Cube& rhs) {
if (&rhs != this) {
destroyCube();
cubeSize = rhs.cubeSize;
fInitColor = rhs.fInitColor;
initializeCube();
copyCube(rhs);
}
return *this;
}
Cube& Cube::operator=(Cube&& rhs) {
if (&rhs != this) {
destroyCube();
copyCubeAttributes(rhs);
rhs.cube = nullptr;
rhs.edges = nullptr;
}
return *this;
}
bool Cube::operator==(const Cube& obj) {
if (cubeSize != obj.cubeSize)
return false;
for (unsigned int row=0; row<LAYERS_PER_COL*cubeSize; row++)
for (unsigned int col=0; col<LAYERS_PER_ROW*cubeSize; col++)
if (cube[row][col] != obj.cube[row][col])
return false;
return true;
}
bool Cube::operator!=(const Cube& obj) {
return !(*this == obj);
}
Cube::~Cube() {
destroyCube();
}
void Cube::destroyCube() {
if (cube != nullptr) {
for (unsigned int i=0; i<(LAYERS_PER_COL*cubeSize); i++)
delete[] cube[i];
delete[] cube;
cube = nullptr;
}
if (edges != nullptr) {
delete[] edges;
edges = nullptr;
}
}
/* Copy semantics helper. */
void Cube::copyCube(const Cube& from) {
for (unsigned int row=0; row<LAYERS_PER_COL*cubeSize; row++)
for (unsigned int col=0; col<LAYERS_PER_ROW*cubeSize; col++)
cube[row][col] = from.cube[row][col];
}
/* Move semantics helper. */
void Cube::copyCubeAttributes(const Cube& from) {
cubeSize = from.cubeSize;
fInitColor = from.fInitColor;
cube = from.cube;
edges = from.edges;
fInitColor = from.fInitColor;
uInitColor = from.uInitColor;
dInitColor = from.dInitColor;
lInitColor = from.lInitColor;
rInitColor = from.rInitColor;
bInitColor = from.bInitColor;
fUpperLeft = from.fUpperLeft;
uUpperLeft = from.uUpperLeft;
lUpperLeft = from.lUpperLeft;
rUpperLeft = from.rUpperLeft;
fUpperLeftMax = from.fUpperLeftMax;
uUpperLeftMax = from.uUpperLeftMax;
lUpperLeftMax = from.lUpperLeftMax;
rUpperLeftMax = from.rUpperLeftMax;
}
unsigned int Cube::getCubeSize() {
return cubeSize;
}
std::vector<CubieColor> Cube::getCube() {
std::vector<CubieColor> tmp;
for (unsigned int row=0; row<LAYERS_PER_COL*cubeSize; row++)
for (unsigned int col=0; col<LAYERS_PER_ROW*cubeSize; col++)
tmp.push_back(cube[row][col]);
return tmp;
}
char Cube::cubieColorToChar(CubieColor cubie) {
switch (cubie) {
case CubieColor::BLUE:
return 'b';
break;
case CubieColor::GREEN:
return 'g';
break;
case CubieColor::ORANGE:
return 'o';
break;
case CubieColor::RED:
return 'r';
break;
case CubieColor::WHITE:
return 'w';
break;
case CubieColor::YELLOW:
return 'y';
break;
default:
return ' ';
}
}
/**
* https://puzzling.stackexchange.com/questions/86916/minimum-effort-to-detect-a-solved-rubiks-cube
*
* "Any four solved faces is sufficient to prove the entire cube is solved."
*/
bool Cube::isSolved() {
if (!isSolved(fUpperLeft, fUpperLeftMax))
return false;
if (!isSolved(uUpperLeft, uUpperLeftMax))
return false;
if (!isSolved(lUpperLeft, lUpperLeftMax))
return false;
if (!isSolved(rUpperLeft, rUpperLeftMax))
return false;
return true;
}
bool Cube::isSolved(Coordinate upperLeft, Coordinate upperLeftMax) {
char cubieVal = cube[upperLeft.row][upperLeft.col];
for (unsigned int row=upperLeft.row; row<upperLeftMax.row; row++)
for (unsigned int col=upperLeft.col; col<upperLeftMax.col; col++)
if (cubieVal != cube[row][col])
return false;
return true;
}
void Cube::performAlgorithm(const std::vector<Turn> &algorithm) {
for (const Turn &t : algorithm)
turn(t);
}
void Cube::turn(Turn t) {
switch (t.layer) {
case Layer::F:
rotateLayer(t.layer, t.clockwise);
rotateEdges(Edges::UpFace, t.clockwise);
break;
case Layer::U:
rotateLayer(t.layer, t.clockwise);
rotateEdges(Edges::FaceUp, t.clockwise);
break;
case Layer::R:
rotateLayer(t.layer, t.clockwise);
rotateEdges(Edges::UpRight, t.clockwise);
break;
case Layer::D:
rotateLayer(t.layer, t.clockwise);
rotateEdges(Edges::FaceDown, t.clockwise);
break;
case Layer::L:
rotateLayer(t.layer, t.clockwise);
rotateEdges(Edges::UpLeft, t.clockwise);
break;
case Layer::B:
rotateLayer(t.layer, t.clockwise);
rotateEdges(Edges::UpBack, t.clockwise);
break;
case Layer::M:
turn({Layer::R, t.clockwise});
turn({Layer::L, !t.clockwise});
break;
case Layer::E:
turn({Layer::U, t.clockwise});
turn({Layer::D, !t.clockwise});
break;
case Layer::S:
turn({Layer::F, !t.clockwise});
turn({Layer::B, t.clockwise});
break;
case Layer::NOLAYER:
default:
break;
}
}
/**
* The atomic element of a layer rotation is a four way circular cubie swap. The
* four way swap is iterated over the outer edge of the layer. Then the sublayer
* is reduced by one in each dimension (row and column) to generate a new
* sublayer that needs its outline four way swapped. This continues until we
* reach the middle of the layer.
*/
void Cube::rotateLayer(Layer layer, bool clockwise) {
unsigned int subCubeSize, subLayerMax;
unsigned int ulr, ulc, urr, urc, llr, llc, lrr, lrc;
/**
* We have to set this to a throw-away value because -Ofast optimization
* causes an unassigned variable to emit -Werror=maybe-uninitialized. This
* warning cannot be squelched when using LLVM because of LLVM bug
* https://bugs.llvm.org/show_bug.cgi?id=24979
*/
Coordinate ul = {Layer::U, true};
getLayerUpperLeft(ul, layer);
subLayerMax = (unsigned int)ceil((float)cubeSize/2);
for (unsigned int subLayer=0; subLayer<subLayerMax; subLayer++) {
subCubeSize = cubeSize - 2*subLayer;
ulr = ul.row + subLayer;
urc = ul.col + subLayer + subCubeSize - 1;
llc = ul.col + subLayer;
lrr = ul.row + subLayer + subCubeSize - 1;
for (unsigned int i=0; i<(subCubeSize - 1); i++) {
ulc = ul.col + subLayer + i;
urr = ul.row + subLayer + i;
llr = ul.row + subLayer + subCubeSize - 1 - i;
lrc = ul.col + subLayer + subCubeSize - 1 - i;
fourWayRotate({{ulr, ulc}, // Upper Left
{urr, urc}, // Upper Right
{lrr, lrc}, // Lower Right
{llr, llc}}, // Lower Left
clockwise);
}
}
}
void Cube::rotateEdges(Edges start, bool clockwise) {
unsigned int index0 = start*cubeSize;
unsigned int index1 = index0 + cubeSize;
unsigned int index2 = index1 + cubeSize;
unsigned int index3 = index2 + cubeSize;
for (unsigned int i=0; i<cubeSize; i++)
fourWayRotate({{edges[index0 + i].row, edges[index0 + i].col},
{edges[index1 + i].row, edges[index1 + i].col},
{edges[index2 + i].row, edges[index2 + i].col},
{edges[index3 + i].row, edges[index3 + i].col}},
clockwise);
}
/**
* Clockwise:
* * Move lower right to lower left.
* * Move upper right to lower right.
* * Move upper left to upper right.
* * Restore color to upper left.
*
* Counter-Clockwise:
* * Move upper left to lower left.
* * Move upper right to upper left.
* * Move lower right to upper right.
* * Restore color to lower right.
*/
void Cube::fourWayRotate(Square square, bool clockwise) {
CubieColor tmp = cube[square.ll.row][square.ll.col];
if (clockwise) {
cube[square.ll.row][square.ll.col] = cube[square.lr.row][square.lr.col];
cube[square.lr.row][square.lr.col] = cube[square.ur.row][square.ur.col];
cube[square.ur.row][square.ur.col] = cube[square.ul.row][square.ul.col];
cube[square.ul.row][square.ul.col] = tmp;
} else {
cube[square.ll.row][square.ll.col] = cube[square.ul.row][square.ul.col];
cube[square.ul.row][square.ul.col] = cube[square.ur.row][square.ur.col];
cube[square.ur.row][square.ur.col] = cube[square.lr.row][square.lr.col];
cube[square.lr.row][square.lr.col] = tmp;
}
}
void Cube::initializeCube() {
cube = new CubieColor*[LAYERS_PER_COL*cubeSize];
for (unsigned int i=0; i<(LAYERS_PER_COL*cubeSize); i++)
cube[i] = new CubieColor[LAYERS_PER_ROW*cubeSize];
for (unsigned int row=0; row<LAYERS_PER_COL*cubeSize; row++)
for (unsigned int col=0; col<LAYERS_PER_ROW*cubeSize; col++)
cube[row][col] = CubieColor::NOCOLOR;
initializeLayers();
initializeEdges();
getLayerUpperLeft(fUpperLeft, Layer::F);
fUpperLeftMax.row = fUpperLeft.row + cubeSize;
fUpperLeftMax.col = fUpperLeft.col + cubeSize;
getLayerUpperLeft(uUpperLeft, Layer::U);
uUpperLeftMax.row = uUpperLeft.row + cubeSize;
uUpperLeftMax.col = uUpperLeft.col + cubeSize;
getLayerUpperLeft(lUpperLeft, Layer::L);
lUpperLeftMax.row = lUpperLeft.row + cubeSize;
lUpperLeftMax.col = lUpperLeft.col + cubeSize;
getLayerUpperLeft(rUpperLeft, Layer::R);
rUpperLeftMax.row = rUpperLeft.row + cubeSize;
rUpperLeftMax.col = rUpperLeft.col + cubeSize;
}
void Cube::initializeLayers() {
switch (fInitColor) {
case CubieColor::BLUE:
uInitColor = CubieColor::WHITE;
lInitColor = CubieColor::RED;
rInitColor = CubieColor::ORANGE;
bInitColor = CubieColor::GREEN;
dInitColor = CubieColor::YELLOW;
break;
case CubieColor::GREEN:
uInitColor = CubieColor::WHITE;
lInitColor = CubieColor::ORANGE;
rInitColor = CubieColor::RED;
bInitColor = CubieColor::BLUE;
dInitColor = CubieColor::YELLOW;
break;
case CubieColor::ORANGE:
uInitColor = CubieColor::WHITE;
lInitColor = CubieColor::BLUE;
rInitColor = CubieColor::GREEN;
bInitColor = CubieColor::RED;
dInitColor = CubieColor::YELLOW;
break;
case CubieColor::RED:
uInitColor = CubieColor::WHITE;
lInitColor = CubieColor::GREEN;
rInitColor = CubieColor::BLUE;
bInitColor = CubieColor::ORANGE;
dInitColor = CubieColor::YELLOW;
break;
case CubieColor::WHITE:
uInitColor = CubieColor::GREEN;
lInitColor = CubieColor::RED;
rInitColor = CubieColor::ORANGE;
bInitColor = CubieColor::YELLOW;
dInitColor = CubieColor::BLUE;
break;
case CubieColor::YELLOW:
uInitColor = CubieColor::GREEN;
lInitColor = CubieColor::ORANGE;
rInitColor = CubieColor::RED;
bInitColor = CubieColor::WHITE;
dInitColor = CubieColor::BLUE;
break;
default:
break;
}
initializeLayer(Layer::U, uInitColor);
initializeLayer(Layer::L, lInitColor);
initializeLayer(Layer::F, fInitColor);
initializeLayer(Layer::R, rInitColor);
initializeLayer(Layer::B, bInitColor);
initializeLayer(Layer::D, dInitColor);
}
void Cube::initializeLayer(Layer layer, CubieColor color) {
/**
* We have to set this to a throw-away value because -Ofast optimization
* causes an unassigned variable to emit -Werror=maybe-uninitialized. This
* warning cannot be squelched when using LLVM because of LLVM bug
* https://bugs.llvm.org/show_bug.cgi?id=24979
*/
Coordinate ul = {Layer::U, true};
getLayerUpperLeft(ul, layer);
for (unsigned int r = ul.row; r < (ul.row + cubeSize); r++)
for (unsigned int c = ul.col; c < (ul.col + cubeSize); c++)
cube[r][c] = color;
}
/**
* Edges are filled into the edges array in a clockwise fashion relevant to
* the layer that owns those edges. This mimicks standardized turn logic where
* "clockwise" is relative to the layer you are turning.
*/
void Cube::initializeEdges() {
edges = new Coordinate[NUM_EDGE_TYPES*cubeSize];
initializeFaceEdges();
initializeUpEdges();
initializeLeftEdges();
initializeRightEdges();
initializeDownEdges();
initializeBackEdges();
}
void Cube::initializeFaceEdges() {
Coordinate ul;
getLayerUpperLeft(ul, Layer::F);
for (unsigned int i=0; i<cubeSize; i++) {
Coordinate c1;
c1.row = ul.row - 1;
c1.col = ul.col + i;
edges[i + Edges::UpFace*cubeSize] = c1;
Coordinate c2;
c2.row = ul.row + i;
c2.col = ul.col + cubeSize;
edges[i + Edges::RightFace*cubeSize] = c2;
Coordinate c3;
c3.row = ul.row + cubeSize;
c3.col = ul.col + cubeSize - 1 - i;
edges[i + Edges::DownFace*cubeSize] = c3;
Coordinate c4;
c4.row = ul.row + cubeSize - 1 - i;
c4.col = ul.col - 1;
edges[i + Edges::LeftFace*cubeSize] = c4;
}
}
void Cube::initializeUpEdges() {
Coordinate ul;
getLayerUpperLeft(ul, Layer::U);
for (unsigned int i=0; i<cubeSize; i++) {
Coordinate c1;
c1.row = cubeSize;
c1.col = ul.col + cubeSize - 1 - i;
edges[i + Edges::FaceUp*cubeSize] = c1;
Coordinate c2;
c2.row = cubeSize;
c2.col = ul.col - 1 - i;
edges[i + Edges::LeftUp*cubeSize] = c2;
Coordinate c3;
c3.row = cubeSize;
c3.col = ul.col + cubeSize*3 - 1 - i;
edges[i + Edges::BackUp*cubeSize] = c3;
Coordinate c4;
c4.row = cubeSize;
c4.col = ul.col + cubeSize*2 - 1 - i;
edges[i + Edges::RightUp*cubeSize] = c4;
}
}
void Cube::initializeLeftEdges() {
Coordinate ul;
getLayerUpperLeft(ul, Layer::L);
for (unsigned int i=0; i<cubeSize; i++) {
Coordinate c1;
c1.row = i;
c1.col = cubeSize;
edges[i + Edges::UpLeft*cubeSize] = c1;
Coordinate c2;
c2.row = ul.row + i;
c2.col = cubeSize;
edges[i + Edges::FaceLeft*cubeSize] = c2;
Coordinate c3;
c3.row = ul.row + cubeSize + i;
c3.col = cubeSize;
edges[i + Edges::DownLeft*cubeSize] = c3;
Coordinate c4;
c4.row = ul.row + cubeSize - 1 - i;
c4.col = cubeSize*4 - 1;
edges[i + Edges::BackLeft*cubeSize] = c4;
}
}
void Cube::initializeRightEdges() {
Coordinate ul;
getLayerUpperLeft(ul, Layer::R);
for (unsigned int i=0; i<cubeSize; i++) {
Coordinate c1;
c1.row = ul.row - 1 - i;
c1.col = ul.col - 1;
edges[i + Edges::UpRight*cubeSize] = c1;
Coordinate c2;
c2.row = ul.row + i;
c2.col = ul.col + cubeSize;
edges[i + Edges::BackRight*cubeSize] = c2;
Coordinate c3;
c3.row = ul.row + cubeSize*2 - 1 - i;
c3.col = ul.col - 1;
edges[i + Edges::DownRight*cubeSize] = c3;
Coordinate c4;
c4.row = ul.row + cubeSize - 1 - i;
c4.col = ul.col - 1;
edges[i + Edges::FaceRight*cubeSize] = c4;
}
}
void Cube::initializeDownEdges() {
Coordinate ul;
getLayerUpperLeft(ul, Layer::D);
for (unsigned int i=0; i<cubeSize; i++) {
Coordinate c1;
c1.row = ul.row - 1;
c1.col = ul.col + i;
edges[i + Edges::FaceDown*cubeSize] = c1;
Coordinate c2;
c2.row = ul.row - 1;
c2.col = ul.col + cubeSize + i;
edges[i + Edges::RightDown*cubeSize] = c2;
Coordinate c3;
c3.row = ul.row - 1;
c3.col = ul.col + cubeSize*2 + i;
edges[i + Edges::BackDown*cubeSize] = c3;
Coordinate c4;
c4.row = ul.row - 1;
c4.col = i;
edges[i + Edges::LeftDown*cubeSize] = c4;
}
}
void Cube::initializeBackEdges() {
Coordinate ul;
getLayerUpperLeft(ul, Layer::B);
for (unsigned int i=0; i<cubeSize; i++) {
Coordinate c1;
c1.row = 0;
c1.col = cubeSize*2 - 1 - i;
edges[i + Edges::UpBack*cubeSize] = c1;
Coordinate c2;
c2.row = cubeSize + i;
c2.col = 0;
edges[i + Edges::LeftBack*cubeSize] = c2;
Coordinate c3;
c3.row = cubeSize*3 - 1;
c3.col = cubeSize + i;
edges[i + Edges::DownBack*cubeSize] = c3;
Coordinate c4;
c4.row = ul.row + cubeSize - 1 - i;
c4.col = ul.col - 1;
edges[i + Edges::RightBack*cubeSize] = c4;
}
}
void Cube::getLayerUpperLeft(Coordinate& coord, Layer l) {
/* Layers that are not (yet?) supported. */
if (l == Layer::M || l == Layer::E || l == Layer::S)
return;
if (l == Layer::NOLAYER)
return;
coord.col = (l%LAYERS_PER_ROW)*cubeSize;
if (l < LAYERS_PER_ROW)
coord.row = 0;
else if (l < LAYERS_PER_ROW*2)
coord.row = cubeSize;
else
coord.row = cubeSize*2;
}