-
Notifications
You must be signed in to change notification settings - Fork 1
/
cube.c
110 lines (92 loc) · 2.24 KB
/
cube.c
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
#include "cube.h"
void initCube(Cube *c) {
size_t i;
*c = safeMalloc(sizeof(struct cubestruct));
for (i = 0; i < NCORNER; i++) {
(*c)->corner[i] = i;
}
for (i = 0; i < NEDGE; i++) {
(*c)->edge[i] = i;
}
}
void freeCube(Cube c) { free(c); }
// Only used for debugging
void printCube(Cube c) {
printf("Corners:");
for (int i = 0; i < NCORNER; i++) {
printf(" %d", c->corner[i]);
}
printf("\n");
printf("Edges:");
for (int i = 0; i < NEDGE; i++) {
printf(" %d", c->edge[i]);
}
printf("\n");
}
// Only used for debugging
Cube copyCube(Cube c) {
Cube newC;
newC = safeMalloc(sizeof(struct cubestruct));
for (int i = 0; i < NCORNER; i++) {
newC->corner[i] = c->corner[i];
}
for (int i = 0; i < NEDGE; i++) {
newC->edge[i] = c->edge[i];
}
return newC;
}
// Only used for debugging
void setCube(Cube cfrom, Cube cto) {
for (int i = 0; i < NCORNER; i++) {
cto->corner[i] = cfrom->corner[i];
}
for (int i = 0; i < NEDGE; i++) {
cto->edge[i] = cfrom->edge[i];
}
}
// Checks if cube is solved.
// Cube is solved if corners and edges are in correct order
int isSolved(Cube c) {
for (int i = 0; i < NCORNER; i++) {
if (c->corner[i] != i) {
return 0;
}
}
for (int i = 0; i < NEDGE; i++) {
if (c->edge[i] != i) {
return 0;
}
}
return 1;
}
// Scrambles the cube, back and forth moves are possible
void scrambleCube(Cube c, int numberMoves, int action[NACTION][SWAP]) {
for (int i = 0; i < numberMoves; i++) {
turn(c, action[rand() % NACTION]);
}
}
// Order of 180 degree actions: F,U,R,L,D,B
void initActions(int action[NACTION][SWAP]) {
int actionh[NACTION][SWAP] = {
{0, 2, 6, 4, 0, 2, 4, 5}, {2, 3, 6, 7, 2, 3, 10, 11},
{1, 2, 4, 7, 5, 7, 9, 11}, {0, 3, 5, 6, 4, 6, 8, 10},
{0, 1, 4, 5, 0, 1, 8, 9}, {1, 3, 5, 7, 1, 3, 6, 7},
};
for (size_t i = 0; i < NACTION; i++) {
for (size_t j = 0; j < SWAP; j++) {
action[i][j] = actionh[i][j];
}
}
}
void swap(int *ap, int *bp) {
int h;
h = *ap;
*ap = *bp;
*bp = h;
}
void turn(Cube c, int a[SWAP]) {
swap(&c->corner[a[0]], &c->corner[a[1]]);
swap(&c->corner[a[2]], &c->corner[a[3]]);
swap(&c->edge[a[4]], &c->edge[a[5]]);
swap(&c->edge[a[6]], &c->edge[a[7]]);
}