diff options
Diffstat (limited to 'src/cube.c')
| -rw-r--r-- | src/cube.c | 126 |
1 files changed, 104 insertions, 22 deletions
| @@ -5,27 +5,61 @@ | |||
| 5 | static int where_is_piece(int piece, int *arr, int n); | 5 | static int where_is_piece(int piece, int *arr, int n); |
| 6 | 6 | ||
| 7 | void | 7 | void |
| 8 | compose(Cube *c2, Cube *c1) | 8 | compose_centers(Cube *c2, Cube *c1) |
| 9 | { | 9 | { |
| 10 | apply_permutation(c2->ep, c1->ep, 12); | 10 | apply_permutation(c2->xp, c1->xp, 6); |
| 11 | apply_permutation(c2->ep, c1->eo, 12); | 11 | } |
| 12 | sum_arrays_mod(c2->eo, c1->eo, 12, 2); | ||
| 13 | 12 | ||
| 13 | void | ||
| 14 | compose_corners(Cube *c2, Cube *c1) | ||
| 15 | { | ||
| 14 | apply_permutation(c2->cp, c1->cp, 8); | 16 | apply_permutation(c2->cp, c1->cp, 8); |
| 15 | apply_permutation(c2->cp, c1->co, 8); | 17 | apply_permutation(c2->cp, c1->co, 8); |
| 16 | sum_arrays_mod(c2->co, c1->co, 8, 3); | 18 | sum_arrays_mod(c2->co, c1->co, 8, 3); |
| 19 | } | ||
| 17 | 20 | ||
| 18 | apply_permutation(c2->xp, c1->xp, 6); | 21 | void |
| 22 | compose_edges(Cube *c2, Cube *c1) | ||
| 23 | { | ||
| 24 | apply_permutation(c2->ep, c1->ep, 12); | ||
| 25 | apply_permutation(c2->ep, c1->eo, 12); | ||
| 26 | sum_arrays_mod(c2->eo, c1->eo, 12, 2); | ||
| 19 | } | 27 | } |
| 20 | 28 | ||
| 21 | void | 29 | void |
| 22 | copy_cube(Cube *src, Cube *dst) | 30 | compose(Cube *c2, Cube *c1) |
| 31 | { | ||
| 32 | compose_centers(c2, c1); | ||
| 33 | compose_corners(c2, c1); | ||
| 34 | compose_edges(c2, c1); | ||
| 35 | } | ||
| 36 | |||
| 37 | void | ||
| 38 | copy_cube_centers(Cube *src, Cube *dst) | ||
| 39 | { | ||
| 40 | memcpy(dst->xp, src->xp, 6 * sizeof(int)); | ||
| 41 | } | ||
| 42 | |||
| 43 | void | ||
| 44 | copy_cube_corners(Cube *src, Cube *dst) | ||
| 23 | { | 45 | { |
| 24 | memcpy(dst->ep, src->ep, 12 * sizeof(int)); | ||
| 25 | memcpy(dst->eo, src->eo, 12 * sizeof(int)); | ||
| 26 | memcpy(dst->cp, src->cp, 8 * sizeof(int)); | 46 | memcpy(dst->cp, src->cp, 8 * sizeof(int)); |
| 27 | memcpy(dst->co, src->co, 8 * sizeof(int)); | 47 | memcpy(dst->co, src->co, 8 * sizeof(int)); |
| 28 | memcpy(dst->xp, src->xp, 6 * sizeof(int)); | 48 | } |
| 49 | |||
| 50 | void | ||
| 51 | copy_cube_edges(Cube *src, Cube *dst) | ||
| 52 | { | ||
| 53 | memcpy(dst->ep, src->ep, 12 * sizeof(int)); | ||
| 54 | memcpy(dst->eo, src->eo, 12 * sizeof(int)); | ||
| 55 | } | ||
| 56 | |||
| 57 | void | ||
| 58 | copy_cube(Cube *src, Cube *dst) | ||
| 59 | { | ||
| 60 | copy_cube_centers(src, dst); | ||
| 61 | copy_cube_corners(src, dst); | ||
| 62 | copy_cube_edges(src, dst); | ||
| 29 | } | 63 | } |
| 30 | 64 | ||
| 31 | bool | 65 | bool |
| @@ -49,25 +83,51 @@ equal(Cube *c1, Cube *c2) | |||
| 49 | } | 83 | } |
| 50 | 84 | ||
| 51 | void | 85 | void |
| 52 | invert_cube(Cube *cube) | 86 | invert_cube_centers(Cube *cube) |
| 53 | { | 87 | { |
| 54 | Cube aux; | ||
| 55 | int i; | 88 | int i; |
| 89 | Cube aux; | ||
| 56 | 90 | ||
| 57 | copy_cube(cube, &aux); | 91 | copy_cube_centers(cube, &aux); |
| 58 | 92 | ||
| 59 | for (i = 0; i < 12; i++) { | 93 | for (i = 0; i < 6; i++) |
| 60 | cube->ep[aux.ep[i]] = i; | 94 | cube->xp[aux.xp[i]] = i; |
| 61 | cube->eo[aux.ep[i]] = aux.eo[i]; | 95 | } |
| 62 | } | 96 | |
| 97 | void | ||
| 98 | invert_cube_corners(Cube *cube) | ||
| 99 | { | ||
| 100 | int i; | ||
| 101 | Cube aux; | ||
| 102 | |||
| 103 | copy_cube_corners(cube, &aux); | ||
| 63 | 104 | ||
| 64 | for (i = 0; i < 8; i++) { | 105 | for (i = 0; i < 8; i++) { |
| 65 | cube->cp[aux.cp[i]] = i; | 106 | cube->cp[aux.cp[i]] = i; |
| 66 | cube->co[aux.cp[i]] = (3 - aux.co[i]) % 3; | 107 | cube->co[aux.cp[i]] = (3 - aux.co[i]) % 3; |
| 67 | } | 108 | } |
| 109 | } | ||
| 68 | 110 | ||
| 69 | for (i = 0; i < 6; i++) | 111 | void |
| 70 | cube->xp[aux.xp[i]] = i; | 112 | invert_cube_edges(Cube *cube) |
| 113 | { | ||
| 114 | int i; | ||
| 115 | Cube aux; | ||
| 116 | |||
| 117 | copy_cube_edges(cube, &aux); | ||
| 118 | |||
| 119 | for (i = 0; i < 12; i++) { | ||
| 120 | cube->ep[aux.ep[i]] = i; | ||
| 121 | cube->eo[aux.ep[i]] = aux.eo[i]; | ||
| 122 | } | ||
| 123 | } | ||
| 124 | |||
| 125 | void | ||
| 126 | invert_cube(Cube *cube) | ||
| 127 | { | ||
| 128 | invert_cube_centers(cube); | ||
| 129 | invert_cube_corners(cube); | ||
| 130 | invert_cube_edges(cube); | ||
| 71 | } | 131 | } |
| 72 | 132 | ||
| 73 | bool | 133 | bool |
| @@ -105,15 +165,37 @@ is_solved(Cube *cube) | |||
| 105 | } | 165 | } |
| 106 | 166 | ||
| 107 | void | 167 | void |
| 108 | make_solved(Cube *cube) | 168 | make_solved_centers(Cube *cube) |
| 169 | { | ||
| 170 | static int sorted[6] = {0, 1, 2, 3, 4, 5}; | ||
| 171 | |||
| 172 | memcpy(cube->xp, sorted, 6 * sizeof(int)); | ||
| 173 | } | ||
| 174 | |||
| 175 | void | ||
| 176 | make_solved_corners(Cube *cube) | ||
| 177 | { | ||
| 178 | static int sorted[8] = {0, 1, 2, 3, 4, 5, 6, 7}; | ||
| 179 | |||
| 180 | memcpy(cube->cp, sorted, 8 * sizeof(int)); | ||
| 181 | memset(cube->co, 0, 8 * sizeof(int)); | ||
| 182 | } | ||
| 183 | |||
| 184 | void | ||
| 185 | make_solved_edges(Cube *cube) | ||
| 109 | { | 186 | { |
| 110 | static int sorted[12] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; | 187 | static int sorted[12] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; |
| 111 | 188 | ||
| 112 | memcpy(cube->ep, sorted, 12 * sizeof(int)); | 189 | memcpy(cube->ep, sorted, 12 * sizeof(int)); |
| 113 | memset(cube->eo, 0, 12 * sizeof(int)); | 190 | memset(cube->eo, 0, 12 * sizeof(int)); |
| 114 | memcpy(cube->cp, sorted, 8 * sizeof(int)); | 191 | } |
| 115 | memset(cube->co, 0, 8 * sizeof(int)); | 192 | |
| 116 | memcpy(cube->xp, sorted, 6 * sizeof(int)); | 193 | void |
| 194 | make_solved(Cube *cube) | ||
| 195 | { | ||
| 196 | make_solved_centers(cube); | ||
| 197 | make_solved_corners(cube); | ||
| 198 | make_solved_edges(cube); | ||
| 117 | } | 199 | } |
| 118 | 200 | ||
| 119 | void | 201 | void |
