diff options
| author | Sebastiano Tronto <sebastiano.tronto@gmail.com> | 2021-11-11 22:05:00 +0100 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano.tronto@gmail.com> | 2021-11-11 22:05:00 +0100 |
| commit | 4fb67201414169a2687f41c4056b2e284b4938cb (patch) | |
| tree | a68246e3e21435229541f83f485ab41cfb2ba08a /old/2021-02-18-piecefilter/src/moves.c | |
| parent | 3568412f8f230774d0d11d7ed1c897424f95d3ef (diff) | |
| download | nissy-4fb67201414169a2687f41c4056b2e284b4938cb.tar.gz nissy-4fb67201414169a2687f41c4056b2e284b4938cb.zip | |
Removed old files
Diffstat (limited to 'old/2021-02-18-piecefilter/src/moves.c')
| -rw-r--r-- | old/2021-02-18-piecefilter/src/moves.c | 482 |
1 files changed, 0 insertions, 482 deletions
diff --git a/old/2021-02-18-piecefilter/src/moves.c b/old/2021-02-18-piecefilter/src/moves.c deleted file mode 100644 index 7cccbdf..0000000 --- a/old/2021-02-18-piecefilter/src/moves.c +++ /dev/null | |||
| @@ -1,482 +0,0 @@ | |||
| 1 | #include "moves.h" | ||
| 2 | |||
| 3 | void move_cubearray(Move m, CubeArray *arr, PieceFilter f); | ||
| 4 | Cube move_via_array(Move m, Cube cube, PieceFilter f); | ||
| 5 | void sort_cancel_rotate(NissMove *alg, int n, bool inv, int top, int front); | ||
| 6 | bool read_ttables_file(); | ||
| 7 | bool write_ttables_file(); | ||
| 8 | |||
| 9 | /* Transition tables */ | ||
| 10 | uint16_t epose_ttable[NMOVES][factorial12/factorial8]; | ||
| 11 | uint16_t eposs_ttable[NMOVES][factorial12/factorial8]; | ||
| 12 | uint16_t eposm_ttable[NMOVES][factorial12/factorial8]; | ||
| 13 | uint16_t eofb_ttable[NMOVES][pow2to11]; | ||
| 14 | uint16_t eorl_ttable[NMOVES][pow2to11]; | ||
| 15 | uint16_t eoud_ttable[NMOVES][pow2to11]; | ||
| 16 | uint16_t cp_ttable[NMOVES][factorial8]; | ||
| 17 | uint16_t coud_ttable[NMOVES][pow3to7]; | ||
| 18 | uint16_t cofb_ttable[NMOVES][pow3to7]; | ||
| 19 | uint16_t corl_ttable[NMOVES][pow3to7]; | ||
| 20 | uint16_t cpos_ttable[NMOVES][factorial6]; | ||
| 21 | |||
| 22 | bool commute[NMOVES][NMOVES]; | ||
| 23 | bool possible_next[NMOVES][NMOVES][NMOVES]; | ||
| 24 | Move inverse[NMOVES]; | ||
| 25 | |||
| 26 | char move_string[NMOVES][5] = | ||
| 27 | { "-", | ||
| 28 | "U", "U2", "U\'", "D", "D2", "D\'", "R", "R2", "R\'", | ||
| 29 | "L", "L2", "L\'", "F", "F2", "F\'", "B", "B2", "B\'", | ||
| 30 | "Uw", "Uw2", "Uw\'", "Dw", "Dw2", "Dw\'", "Rw", "Rw2", "Rw\'", | ||
| 31 | "Lw", "Lw2", "Lw\'", "Fw", "Fw2", "Fw\'", "Bw", "Bw2", "Bw\'", | ||
| 32 | "M", "M2", "M\'", "S", "S2", "S\'", "E", "E2", "E\'", | ||
| 33 | "x", "x2", "x\'", "y", "y2", "y\'", "z", "z2", "z\'" }; | ||
| 34 | |||
| 35 | /* For each type of pieces only the effects of U, x and y are described */ | ||
| 36 | int edge_cycle[NMOVES][12] = | ||
| 37 | { [U] = {UR, UF, UL, UB, DF, DL, DB, DR, FR, FL, BL, BR}, | ||
| 38 | [x] = {DF, FL, UF, FR, DB, BL, UB, BR, DR, DL, UL, UR}, | ||
| 39 | [y] = {UR, UF, UL, UB, DR, DF, DL, DB, BR, FR, FL, BL} }; | ||
| 40 | int eofb_flipped[NMOVES][12] = | ||
| 41 | { [x] = { [UF] = 1, [UB] = 1, [DF] = 1, [DB] = 1 }, | ||
| 42 | [y] = { [FR] = 1, [FL] = 1, [BL] = 1, [BR] = 1 } }; | ||
| 43 | int eorl_flipped[NMOVES][12] = | ||
| 44 | { [x] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, | ||
| 45 | [y] = { [FR] = 1, [FL] = 1, [BL] = 1, [BR] = 1 } }; | ||
| 46 | int eoud_flipped[NMOVES][12] = | ||
| 47 | { [U] = { [UF] = 1, [UL] = 1, [UB] = 1, [UR] = 1 }, | ||
| 48 | [x] = { [UF] = 1, [UB] = 1, [DF] = 1, [DB] = 1 }, | ||
| 49 | [y] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } }; | ||
| 50 | int corner_cycle[NMOVES][8] = | ||
| 51 | { [U] = {UBR, UFR, UFL, UBL, DFR, DFL, DBL, DBR}, | ||
| 52 | [x] = {DFR, DFL, UFL, UFR, DBR, DBL, UBL, UBR}, | ||
| 53 | [y] = {UBR, UFR, UFL, UBL, DBR, DFR, DFL, DBL} }; | ||
| 54 | int coud_flipped[NMOVES][8] = | ||
| 55 | { [x] = {[UFR]=2,[UBR]=1,[DBR]=2,[DFR]=1,[UFL]=1,[UBL]=2,[DBL]=1,[DFL]=2} }; | ||
| 56 | int corl_flipped[NMOVES][8] = | ||
| 57 | { [U] = { [UFR] = 1, [UBR] = 2, [UBL] = 1, [UFL] = 2 }, | ||
| 58 | [y] = {[UFR]=1,[UBR]=2,[UBL]=1,[UFL]=2,[DFR]=2,[DBR]=1,[DBL]=2,[DFL]=1} }; | ||
| 59 | int cofb_flipped[NMOVES][8] = | ||
| 60 | { [U] = { [UFR] = 2, [UBR] = 1, [UBL] = 2, [UFL] = 1 }, | ||
| 61 | [x] = {[UFR]=1,[UBR]=2,[DFR]=2,[DBR]=1,[UBL]=2,[UFL]=1,[DBL]=1,[DFL]=2}, | ||
| 62 | [y] = {[UFR]=2,[UBR]=1,[UBL]=2,[UFL]=1,[DFR]=1,[DBR]=2,[DBL]=1,[DFL]=2} }; | ||
| 63 | int center_cycle[NMOVES][6] = | ||
| 64 | { [x] = {F_center, B_center, R_center, L_center, D_center, U_center}, | ||
| 65 | [y] = {U_center, D_center, B_center, F_center, R_center, L_center} }; | ||
| 66 | |||
| 67 | /* Each move is reduced to a combination of U, x and y using this table */ | ||
| 68 | Move equiv_moves[NMOVES][14] = { | ||
| 69 | [U] = { U, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, | ||
| 70 | [U2] = { U, U, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, | ||
| 71 | [U3] = { U, U, U, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, | ||
| 72 | [D] = { x, x, U, x, x, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, | ||
| 73 | [D2] = { x, x, U, U, x, x, 0, 0, 0, 0, 0, 0, 0, 0 }, | ||
| 74 | [D3] = { x, x, U, U, U, x, x, 0, 0, 0, 0, 0, 0, 0 }, | ||
| 75 | [R] = { y, x, U, x, x, x, y, y, y, 0, 0, 0, 0, 0 }, | ||
| 76 | [R2] = { y, x, U, U, x, x, x, y, y, y, 0, 0, 0, 0 }, | ||
| 77 | [R3] = { y, x, U, U, U, x, x, x, y, y, y, 0, 0, 0 }, | ||
| 78 | [L] = { y, y, y, x, U, x, x, x, y, 0, 0, 0, 0, 0 }, | ||
| 79 | [L2] = { y, y, y, x, U, U, x, x, x, y, 0, 0, 0, 0 }, | ||
| 80 | [L3] = { y, y, y, x, U, U, U, x, x, x, y, 0, 0, 0 }, | ||
| 81 | [F] = { x, U, x, x, x, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, | ||
| 82 | [F2] = { x, U, U, x, x, x, 0, 0, 0, 0, 0, 0, 0, 0 }, | ||
| 83 | [F3] = { x, U, U, U, x, x, x, 0, 0, 0, 0, 0, 0, 0 }, | ||
| 84 | [B] = { x, x, x, U, x, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, | ||
| 85 | [B2] = { x, x, x, U, U, x, 0, 0, 0, 0, 0, 0, 0, 0 }, | ||
| 86 | [B3] = { x, x, x, U, U, U, x, 0, 0, 0, 0, 0, 0, 0 }, | ||
| 87 | |||
| 88 | [Uw] = { x, x, U, x, x, y, 0, 0, 0, 0, 0, 0, 0, 0 }, | ||
| 89 | [Uw2] = { x, x, U, U, x, x, y, y, 0, 0, 0, 0, 0, 0 }, | ||
| 90 | [Uw3] = { x, x, U, U, U, x, x, y, y, y, 0, 0, 0, 0 }, | ||
| 91 | [Dw] = { U, y, y, y, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, | ||
| 92 | [Dw2] = { U, U, y, y, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, | ||
| 93 | [Dw3] = { U, U, U, y, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, | ||
| 94 | [Rw] = { y, y, y, x, U, x, x, x, y, x, 0, 0, 0, 0 }, | ||
| 95 | [Rw2] = { y, y, y, x, U, U, x, x, x, y, x, x, 0, 0 }, | ||
| 96 | [Rw3] = { y, y, y, x, U, U, U, y, x, x, x, y, 0, 0 }, | ||
| 97 | [Lw] = { y, x, U, x, x, x, y, y, y, x, x, x, 0, 0 }, | ||
| 98 | [Lw2] = { y, x, U, U, x, x, x, y, y, y, x, x, 0, 0 }, | ||
| 99 | [Lw3] = { y, x, U, U, U, x, x, x, y, y, y, x, 0, 0 }, | ||
| 100 | [Fw] = { x, x, x, U, y, y, y, x, 0, 0, 0, 0, 0, 0 }, | ||
| 101 | [Fw2] = { x, x, x, U, U, y, y, x, 0, 0, 0, 0, 0, 0 }, | ||
| 102 | [Fw3] = { x, x, x, U, U, U, y, x, 0, 0, 0, 0, 0, 0 }, | ||
| 103 | [Bw] = { x, U, y, y, y, x, x, x, 0, 0, 0, 0, 0, 0 }, | ||
| 104 | [Bw2] = { x, U, U, y, y, x, x, x, 0, 0, 0, 0, 0, 0 }, | ||
| 105 | [Bw3] = { x, U, U, U, y, x, x, x, 0, 0, 0, 0, 0, 0 }, | ||
| 106 | |||
| 107 | [M] = { y, x, U, x, x, U, U, U, y, x, y, y, y, 0 }, | ||
| 108 | [M2] = { y, x, U, U, x, x, U, U, x, x, x, y, 0, 0 }, | ||
| 109 | [M3] = { y, x, U, U, U, x, x, U, y, x, x, x, y, 0 }, | ||
| 110 | [S] = { x, U, U, U, x, x, U, y, y, y, x, 0, 0, 0 }, | ||
| 111 | [S2] = { x, U, U, x, x, U, U, y, y, x, 0, 0, 0, 0 }, | ||
| 112 | [S3] = { x, U, x, x, U, U, U, y, x, 0, 0, 0, 0, 0 }, | ||
| 113 | [E] = { U, x, x, U, U, U, x, x, y, y, y, 0, 0, 0 }, | ||
| 114 | [E2] = { U, U, x, x, U, U, x, x, y, y, 0, 0, 0, 0 }, | ||
| 115 | [E3] = { U, U, U, x, x, U, x, x, y, 0, 0, 0, 0, 0 }, | ||
| 116 | |||
| 117 | [x] = { x, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, | ||
| 118 | [x2] = { x, x, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, | ||
| 119 | [x3] = { x, x, x, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, | ||
| 120 | [y] = { y, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, | ||
| 121 | [y2] = { y, y, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, | ||
| 122 | [y3] = { y, y, y, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, | ||
| 123 | [z] = { y, y, y, x, y, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, | ||
| 124 | [z2] = { y, y, x, x, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, | ||
| 125 | [z3] = { y, x, y, y, y, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, | ||
| 126 | }; | ||
| 127 | |||
| 128 | /* Movesets */ | ||
| 129 | bool standard_moveset[NMOVES] = { | ||
| 130 | [U] = true, [U2] = true, [U3] = true, [D] = true, [D2] = true, [D3] = true, | ||
| 131 | [R] = true, [R2] = true, [R3] = true, [L] = true, [L2] = true, [L3] = true, | ||
| 132 | [F] = true, [F2] = true, [F3] = true, [B] = true, [B2] = true, [B3] = true, | ||
| 133 | }; | ||
| 134 | |||
| 135 | bool is_solved_up_to_reorient(Cube cube) { | ||
| 136 | if (is_solved(cube)) | ||
| 137 | return true; | ||
| 138 | |||
| 139 | /*TODO: improve this crap */ | ||
| 140 | |||
| 141 | bool ret = false; | ||
| 142 | for (int i = x; i <= z3; i++) { | ||
| 143 | ret = ret || is_solved(move_cube(i, cube)); | ||
| 144 | for (int j = x; j <= z3; j++) | ||
| 145 | ret = ret || is_solved(move_cube(i, move_cube(j, cube))); | ||
| 146 | } | ||
| 147 | return ret; | ||
| 148 | } | ||
| 149 | |||
| 150 | void move_cubearray(Move m, CubeArray *arr, PieceFilter f) { | ||
| 151 | if (f.epose || f.eposs || f.eposm) | ||
| 152 | apply_permutation(edge_cycle[m], arr->ep, 12); | ||
| 153 | if (f.eofb) { | ||
| 154 | apply_permutation(edge_cycle[m], arr->eofb, 12); | ||
| 155 | sum_arrays_mod(eofb_flipped[m], arr->eofb, 12, 2); | ||
| 156 | } | ||
| 157 | if (f.eorl) { | ||
| 158 | apply_permutation(edge_cycle[m], arr->eorl, 12); | ||
| 159 | sum_arrays_mod(eorl_flipped[m], arr->eorl, 12, 2); | ||
| 160 | } | ||
| 161 | if (f.eoud) { | ||
| 162 | apply_permutation(edge_cycle[m], arr->eoud, 12); | ||
| 163 | sum_arrays_mod(eoud_flipped[m], arr->eoud, 12, 2); | ||
| 164 | } | ||
| 165 | if (f.cp) | ||
| 166 | apply_permutation(corner_cycle[m], arr->cp, 8); | ||
| 167 | if (f.coud) { | ||
| 168 | apply_permutation(corner_cycle[m], arr->coud, 8); | ||
| 169 | sum_arrays_mod(coud_flipped[m], arr->coud, 8, 3); | ||
| 170 | } | ||
| 171 | if (f.corl) { | ||
| 172 | apply_permutation(corner_cycle[m], arr->corl, 8); | ||
| 173 | sum_arrays_mod(corl_flipped[m], arr->corl, 8, 3); | ||
| 174 | } | ||
| 175 | if (f.cofb) { | ||
| 176 | apply_permutation(corner_cycle[m], arr->cofb, 8); | ||
| 177 | sum_arrays_mod(cofb_flipped[m], arr->cofb, 8, 3); | ||
| 178 | } | ||
| 179 | if (f.cpos) | ||
| 180 | apply_permutation(center_cycle[m], arr->cpos, 6); | ||
| 181 | } | ||
| 182 | |||
| 183 | Cube move_via_array(Move m, Cube cube, PieceFilter f) { | ||
| 184 | CubeArray arr = {0}; | ||
| 185 | cube_to_arrays(cube, &arr, f); | ||
| 186 | move_cubearray(m, &arr, f); | ||
| 187 | return arrays_to_cube(arr, f); | ||
| 188 | } | ||
| 189 | |||
| 190 | int copy_alg(NissMove *src, NissMove *dest) { | ||
| 191 | int i; | ||
| 192 | for (i = 0; src[i].m != NULLMOVE; i++) | ||
| 193 | dest[i] = src[i]; | ||
| 194 | dest[i].m = NULLMOVE; | ||
| 195 | return i; | ||
| 196 | } | ||
| 197 | |||
| 198 | /* TODO: all strings start with space?? */ | ||
| 199 | void print_moves(NissMove *alg) { | ||
| 200 | bool niss = false; | ||
| 201 | for (int i = 0; alg[i].m != NULLMOVE; i++) { | ||
| 202 | char *fill = !niss && alg[i].inverse ? " (" : | ||
| 203 | (niss && !alg[i].inverse ? ") " : " "); | ||
| 204 | printf("%s%s", fill, move_string[alg[i].m]); | ||
| 205 | niss = alg[i].inverse; | ||
| 206 | } | ||
| 207 | printf("%s\n", niss ? ")" : ""); | ||
| 208 | } | ||
| 209 | |||
| 210 | int read_moves(char *str, NissMove *alg, int n) { | ||
| 211 | bool niss = false; | ||
| 212 | int c = 0; | ||
| 213 | |||
| 214 | for (int i = 0; str[i] && c < n; i++) { | ||
| 215 | if (str[i] == ' ' || str[i] == '\t' || str[i] == '\n') | ||
| 216 | continue; | ||
| 217 | |||
| 218 | if (str[i] == '(' || str[i] == ')') { | ||
| 219 | if ((niss && str[i] == '(') || (!niss && str[i] == ')')) | ||
| 220 | return -1; | ||
| 221 | niss = !niss; | ||
| 222 | continue; | ||
| 223 | } | ||
| 224 | |||
| 225 | alg[c].inverse = niss; alg[c].m = NULLMOVE; | ||
| 226 | for (Move j = 0; j < NMOVES; j++) { | ||
| 227 | if (str[i] == move_string[j][0]) { | ||
| 228 | alg[c].m = j; | ||
| 229 | if (alg[c].m <= B && str[i+1]=='w') { alg[c].m += Uw - U; i++; } | ||
| 230 | if (str[i+1]=='2') { alg[c].m += 1; i++; } | ||
| 231 | else if (str[i+1]=='\'' || str[i+1]=='3') { alg[c].m += 2; i++; } | ||
| 232 | c++; | ||
| 233 | break; | ||
| 234 | } | ||
| 235 | } | ||
| 236 | } | ||
| 237 | |||
| 238 | alg[c].m = NULLMOVE; | ||
| 239 | return c; | ||
| 240 | } | ||
| 241 | |||
| 242 | /* Helper function for cleanup. alg must contain only basic moves, no 2 or '. | ||
| 243 | top and front describe an admissible orientation of the cube. */ | ||
| 244 | void sort_cancel_rotate(NissMove *alg, int n, bool inv, int top, int front) { | ||
| 245 | int c = 0, i = 0; | ||
| 246 | PieceFilter cpos_only = { .cpos = true }; | ||
| 247 | NissMove aux[n+3]; | ||
| 248 | aux[0].m = NULLMOVE; | ||
| 249 | |||
| 250 | while (i < n && alg[i].m != NULLMOVE) { | ||
| 251 | int j = i; | ||
| 252 | while (j < n && commute[alg[i].m][alg[j].m]) j++; | ||
| 253 | Move base = 6*((alg[i].m-1)/6); | ||
| 254 | int t1 = 0, t2 = 0; | ||
| 255 | for (int k = i; k < j; k++) | ||
| 256 | if (alg[k].m == base+1) t1 = (t1+1)%4; | ||
| 257 | else t2 = (t2+1)%4; | ||
| 258 | if (t1) { aux[c].inverse = inv; aux[c].m = base+t1; c++; } | ||
| 259 | if (t2) { aux[c].inverse = inv; aux[c].m = base+t2+3; c++; } | ||
| 260 | i = j; | ||
| 261 | } | ||
| 262 | aux[c].m = NULLMOVE; | ||
| 263 | |||
| 264 | CubeArray q; | ||
| 265 | cube_to_arrays(blank_cube(), &q, cpos_only); | ||
| 266 | /* First we try to rotate in one move, then we try an x or y rotation | ||
| 267 | followed by a z rotation */ | ||
| 268 | for (int r = x; r <= z3; r++) { | ||
| 269 | move_cubearray(r, &q, cpos_only); | ||
| 270 | if (q.cpos[F_center] == front && q.cpos[U_center] == top) { | ||
| 271 | aux[c].inverse = inv; aux[c].m = r; | ||
| 272 | |||
| 273 | aux[++c].m = NULLMOVE; | ||
| 274 | copy_alg(aux, alg); | ||
| 275 | return; | ||
| 276 | } | ||
| 277 | move_cubearray(inverse[r], &q, cpos_only); | ||
| 278 | } | ||
| 279 | for (int r = x; r <= y3; r++) { | ||
| 280 | move_cubearray(r, &q, cpos_only); | ||
| 281 | if (q.cpos[F_center] == front) { | ||
| 282 | aux[c].inverse = inv; aux[c++].m = r; | ||
| 283 | break; | ||
| 284 | } | ||
| 285 | move_cubearray(inverse[r], &q, cpos_only); | ||
| 286 | } | ||
| 287 | for (int r = z; r <= z3; r++) { | ||
| 288 | move_cubearray(r, &q, cpos_only); | ||
| 289 | if (q.cpos[U_center] == top) { | ||
| 290 | aux[c].inverse = inv; aux[c++].m = r; | ||
| 291 | break; | ||
| 292 | } | ||
| 293 | move_cubearray(inverse[r], &q, cpos_only); | ||
| 294 | } | ||
| 295 | |||
| 296 | aux[c].m = NULLMOVE; | ||
| 297 | copy_alg(aux, alg); | ||
| 298 | } | ||
| 299 | |||
| 300 | /* TODO: does not work with niss + rotations */ | ||
| 301 | void cleanup(NissMove *alg, int n) { | ||
| 302 | int count_n = 0, count_i = 0, *count; | ||
| 303 | PieceFilter cpos_only = { .cpos = true }; | ||
| 304 | NissMove aux_n[n+1], aux_i[n+1], *aux; | ||
| 305 | CubeArray cube_n, cube_i, *cube; | ||
| 306 | cube_to_arrays(blank_cube(), &cube_n, cpos_only); | ||
| 307 | cube_to_arrays(blank_cube(), &cube_i, cpos_only); | ||
| 308 | |||
| 309 | for (int i = 0; count_n + count_i < n && alg[i].m != NULLMOVE; i++) { | ||
| 310 | if (alg[i].inverse) { count = &count_i; aux = aux_i; cube = &cube_i; } | ||
| 311 | else { count = &count_n; aux = aux_n; cube = &cube_n; } | ||
| 312 | |||
| 313 | for (int j = 0; equiv_moves[alg[i].m][j]; j++) { | ||
| 314 | Move m = equiv_moves[alg[i].m][j]; | ||
| 315 | aux[*count].inverse = alg[i].inverse; | ||
| 316 | move_cubearray(m, cube, cpos_only); | ||
| 317 | if (m == U) aux[(*count)++].m = 3 * cube->cpos[0] + 1; | ||
| 318 | } | ||
| 319 | } | ||
| 320 | |||
| 321 | aux_n[count_n].m = NULLMOVE; | ||
| 322 | aux_i[count_i].m = NULLMOVE; | ||
| 323 | sort_cancel_rotate(aux_n, count_n, false, cube_n.cpos[0], cube_n.cpos[4]); | ||
| 324 | sort_cancel_rotate(aux_i, count_i, true, cube_i.cpos[0], cube_n.cpos[4]); | ||
| 325 | copy_alg(aux_n, alg); | ||
| 326 | copy_alg(aux_i, alg+count_n); | ||
| 327 | } | ||
| 328 | |||
| 329 | bool read_ttables_file() { | ||
| 330 | FILE *ttf; | ||
| 331 | if ((ttf = fopen("ttables", "rb")) != NULL) { | ||
| 332 | for (int m = 0; m < NMOVES; m++) { | ||
| 333 | fread(epose_ttable[m], sizeof(uint16_t), factorial12/factorial8, ttf); | ||
| 334 | fread(eposs_ttable[m], sizeof(uint16_t), factorial12/factorial8, ttf); | ||
| 335 | fread(eposm_ttable[m], sizeof(uint16_t), factorial12/factorial8, ttf); | ||
| 336 | fread(eofb_ttable[m], sizeof(uint16_t), pow2to11, ttf); | ||
| 337 | fread(eorl_ttable[m], sizeof(uint16_t), pow2to11, ttf); | ||
| 338 | fread(eoud_ttable[m], sizeof(uint16_t), pow2to11, ttf); | ||
| 339 | fread(cp_ttable[m], sizeof(uint16_t), factorial8, ttf); | ||
| 340 | fread(coud_ttable[m], sizeof(uint16_t), pow3to7, ttf); | ||
| 341 | fread(corl_ttable[m], sizeof(uint16_t), pow3to7, ttf); | ||
| 342 | fread(cofb_ttable[m], sizeof(uint16_t), pow3to7, ttf); | ||
| 343 | fread(cpos_ttable[m], sizeof(uint16_t), factorial6, ttf); | ||
| 344 | } | ||
| 345 | fclose(ttf); | ||
| 346 | return true; | ||
| 347 | } else return false; | ||
| 348 | } | ||
| 349 | |||
| 350 | bool write_ttables_file() { | ||
| 351 | FILE *ttf; | ||
| 352 | if ((ttf = fopen("ttables", "wb")) != NULL) { | ||
| 353 | for (int m = 0; m < NMOVES; m++) { | ||
| 354 | fwrite(epose_ttable[m], sizeof(uint16_t), factorial12/factorial8, ttf); | ||
| 355 | fwrite(eposs_ttable[m], sizeof(uint16_t), factorial12/factorial8, ttf); | ||
| 356 | fwrite(eposm_ttable[m], sizeof(uint16_t), factorial12/factorial8, ttf); | ||
| 357 | fwrite(eofb_ttable[m], sizeof(uint16_t), pow2to11, ttf); | ||
| 358 | fwrite(eorl_ttable[m], sizeof(uint16_t), pow2to11, ttf); | ||
| 359 | fwrite(eoud_ttable[m], sizeof(uint16_t), pow2to11, ttf); | ||
| 360 | fwrite(cp_ttable[m], sizeof(uint16_t), factorial8, ttf); | ||
| 361 | fwrite(coud_ttable[m], sizeof(uint16_t), pow3to7, ttf); | ||
| 362 | fwrite(corl_ttable[m], sizeof(uint16_t), pow3to7, ttf); | ||
| 363 | fwrite(cofb_ttable[m], sizeof(uint16_t), pow3to7, ttf); | ||
| 364 | fwrite(cpos_ttable[m], sizeof(uint16_t), factorial6, ttf); | ||
| 365 | } | ||
| 366 | fclose(ttf); | ||
| 367 | return true; | ||
| 368 | } else return false; | ||
| 369 | } | ||
| 370 | |||
| 371 | void init_ttables(bool read, bool write) { | ||
| 372 | /* Generate all move cycles and flips; I do this regardless */ | ||
| 373 | for (int i = 0; i < NMOVES; i++) { | ||
| 374 | if (i == U || i == x || i == y) | ||
| 375 | continue; | ||
| 376 | |||
| 377 | CubeArray arr = {0}; | ||
| 378 | cube_to_arrays(blank_cube(), &arr, fAll); | ||
| 379 | for (int j = 0; equiv_moves[i][j]; j++) | ||
| 380 | move_cubearray(equiv_moves[i][j], &arr, fAll); | ||
| 381 | |||
| 382 | intarrcopy(arr.ep, edge_cycle[i], 12); | ||
| 383 | intarrcopy(arr.eofb, eofb_flipped[i], 12); | ||
| 384 | intarrcopy(arr.eorl, eorl_flipped[i], 12); | ||
| 385 | intarrcopy(arr.eoud, eoud_flipped[i], 12); | ||
| 386 | intarrcopy(arr.cp, corner_cycle[i], 8); | ||
| 387 | intarrcopy(arr.coud, coud_flipped[i], 8); | ||
| 388 | intarrcopy(arr.corl, corl_flipped[i], 8); | ||
| 389 | intarrcopy(arr.cofb, cofb_flipped[i], 8); | ||
| 390 | intarrcopy(arr.cpos, center_cycle[i], 6); | ||
| 391 | } | ||
| 392 | |||
| 393 | if (read) | ||
| 394 | if (read_ttables_file()) | ||
| 395 | return; | ||
| 396 | |||
| 397 | /* Initialize transition tables */ | ||
| 398 | Cube c = {0}; | ||
| 399 | PieceFilter fe = {.epose=true}, fs = {.eposs=true}, fm = {.eposm=true}; | ||
| 400 | PieceFilter feo = { .eofb = true, .eorl = true, .eoud = true }; | ||
| 401 | PieceFilter fcp = { .cp = true }; | ||
| 402 | PieceFilter fco = { .cofb = true, .corl = true, .coud = true }; | ||
| 403 | PieceFilter fcc = { .cpos = true }; | ||
| 404 | for (int m = 0; m < NMOVES; m++) { | ||
| 405 | for (uint16_t i = 0; i < factorial12/factorial8; i++) { | ||
| 406 | c.epose = i; epose_ttable[m][i] = move_via_array(m, c, fe).epose; | ||
| 407 | c.eposs = i; eposs_ttable[m][i] = move_via_array(m, c, fs).eposs; | ||
| 408 | c.eposm = i; eposm_ttable[m][i] = move_via_array(m, c, fm).eposm; | ||
| 409 | } | ||
| 410 | for (uint16_t i = 0; i < pow2to11; i++ ) { | ||
| 411 | c.eofb = i; eofb_ttable[m][i] = move_via_array(m, c, feo).eofb; | ||
| 412 | c.eorl = i; eorl_ttable[m][i] = move_via_array(m, c, feo).eorl; | ||
| 413 | c.eoud = i; eoud_ttable[m][i] = move_via_array(m, c, feo).eoud; | ||
| 414 | } | ||
| 415 | for (uint16_t i = 0; i < factorial8; i++) { | ||
| 416 | c.cp = i; cp_ttable[m][i] = move_via_array(m, c, fcp).cp; | ||
| 417 | } | ||
| 418 | for (uint16_t i = 0; i < pow3to7; i++) { | ||
| 419 | c.coud = i; coud_ttable[m][i] = move_via_array(m, c, fco).coud; | ||
| 420 | c.corl = i; corl_ttable[m][i] = move_via_array(m, c, fco).corl; | ||
| 421 | c.cofb = i; cofb_ttable[m][i] = move_via_array(m, c, fco).cofb; | ||
| 422 | } | ||
| 423 | for (uint16_t i = 0; i < factorial6; i++) { | ||
| 424 | c.cpos = i; cpos_ttable[m][i] = move_via_array(m, c, fcc).cpos; | ||
| 425 | } | ||
| 426 | } | ||
| 427 | |||
| 428 | if (write) write_ttables_file(); | ||
| 429 | } | ||
| 430 | |||
| 431 | Cube move_cube(Move m, Cube cube) { | ||
| 432 | Cube moved = cube; | ||
| 433 | |||
| 434 | moved.epose = epose_ttable[m][cube.epose]; | ||
| 435 | moved.eposs = eposs_ttable[m][cube.eposs]; | ||
| 436 | moved.eposm = eposm_ttable[m][cube.eposm]; | ||
| 437 | moved.eofb = eofb_ttable[m][cube.eofb]; | ||
| 438 | moved.eorl = eorl_ttable[m][cube.eorl]; | ||
| 439 | moved.eoud = eoud_ttable[m][cube.eoud]; | ||
| 440 | moved.coud = coud_ttable[m][cube.coud]; | ||
| 441 | moved.cofb = cofb_ttable[m][cube.cofb]; | ||
| 442 | moved.corl = corl_ttable[m][cube.corl]; | ||
| 443 | moved.cp = cp_ttable[m][cube.cp]; | ||
| 444 | moved.cpos = cpos_ttable[m][cube.cpos]; | ||
| 445 | |||
| 446 | return moved; | ||
| 447 | } | ||
| 448 | |||
| 449 | Cube apply_alg(NissMove *alg, Cube cube) { | ||
| 450 | Cube ret = {0}; | ||
| 451 | for (int i = 0; alg[i].m != NULLMOVE; i++) | ||
| 452 | if (alg[i].inverse) | ||
| 453 | ret = move_cube(alg[i].m, ret); | ||
| 454 | ret = compose(cube, inverse_cube(ret)); | ||
| 455 | |||
| 456 | for (int i = 0; alg[i].m != NULLMOVE; i++) | ||
| 457 | if (!alg[i].inverse) | ||
| 458 | ret = move_cube(alg[i].m, ret); | ||
| 459 | return ret; | ||
| 460 | } | ||
| 461 | |||
| 462 | void init_aux_tables() { | ||
| 463 | /* Commute */ | ||
| 464 | for (int i = 0; i < NMOVES; i++) | ||
| 465 | for (int j = 0; j < NMOVES; j++) | ||
| 466 | commute[i][j] = equal(move_cube(i, move_cube(j, blank_cube())), | ||
| 467 | move_cube(j, move_cube(i, blank_cube()))); | ||
| 468 | |||
| 469 | /* Possible next (if the sequence i j k is valid) */ | ||
| 470 | for (int i = 0; i < NMOVES; i++) | ||
| 471 | for (int j = 0; j < NMOVES; j++) | ||
| 472 | for (int k = 0; k < NMOVES; k++) | ||
| 473 | possible_next[i][j][k] = | ||
| 474 | (j == 0) || | ||
| 475 | (j != 0 && (j-(j-1)%3) != (k-(k-1)%3) && | ||
| 476 | !(i != 0 && commute[i][j] && (i-(i-1)%3) == (k-(k-1)%3))); | ||
| 477 | |||
| 478 | /* Inverse */ | ||
| 479 | for (int i = 0; i < NMOVES; i++) | ||
| 480 | inverse[i] = i == 0 ? 0 : i + 2 - 2*((i-1)%3); | ||
| 481 | } | ||
| 482 | |||
