diff options
Diffstat (limited to 'src/core/moves.h')
| -rw-r--r-- | src/core/moves.h | 162 |
1 files changed, 162 insertions, 0 deletions
diff --git a/src/core/moves.h b/src/core/moves.h index 7da6405..726a452 100644 --- a/src/core/moves.h +++ b/src/core/moves.h | |||
| @@ -1,8 +1,23 @@ | |||
| 1 | /* probably these can be placed in constants file */ | ||
| 2 | #define NONISS 0x00 | ||
| 3 | #define NISS 0x01 | ||
| 4 | #define INVERSEBRANCH 0x03 | ||
| 5 | #define BRANCH 0x02 | ||
| 6 | #define ALLMOVES 0x3FFFF | ||
| 7 | #define NOHALFTURNS 0x2DB6D | ||
| 8 | |||
| 1 | _static_inline bool allowednextmove(uint8_t *, uint8_t); | 9 | _static_inline bool allowednextmove(uint8_t *, uint8_t); |
| 10 | _static uint32_t allowednextmoveH48(uint8_t *, uint8_t, uint32_t); | ||
| 11 | |||
| 2 | _static_inline uint8_t inverse_trans(uint8_t); | 12 | _static_inline uint8_t inverse_trans(uint8_t); |
| 3 | _static_inline uint8_t movebase(uint8_t); | 13 | _static_inline uint8_t movebase(uint8_t); |
| 4 | _static_inline uint8_t moveaxis(uint8_t); | 14 | _static_inline uint8_t moveaxis(uint8_t); |
| 5 | 15 | ||
| 16 | _static cube_t move(cube_t, uint8_t); | ||
| 17 | _static cube_t premove(cube_t, uint8_t); | ||
| 18 | _static uint8_t inverse_move(uint8_t); | ||
| 19 | _static uint8_t* invertpremoves(uint8_t *, uint8_t); | ||
| 20 | |||
| 6 | _static bool | 21 | _static bool |
| 7 | allowednextmove(uint8_t *moves, uint8_t n) | 22 | allowednextmove(uint8_t *moves, uint8_t n) |
| 8 | { | 23 | { |
| @@ -28,6 +43,40 @@ allowednextmove(uint8_t *moves, uint8_t n) | |||
| 28 | return axis[1] != axis[2] || base[0] != base[2]; | 43 | return axis[1] != axis[2] || base[0] != base[2]; |
| 29 | } | 44 | } |
| 30 | 45 | ||
| 46 | static uint32_t | ||
| 47 | disable_moves(uint32_t current_result, uint8_t base_index) | ||
| 48 | { | ||
| 49 | return current_result & ~((1 << base_index) | (1 << (base_index + 1)) | (1 << (base_index + 2))); | ||
| 50 | } | ||
| 51 | |||
| 52 | _static uint32_t | ||
| 53 | allowednextmoveH48(uint8_t *moves, uint8_t n, uint32_t h48branch) | ||
| 54 | { | ||
| 55 | uint32_t result = ALLMOVES; | ||
| 56 | if (h48branch & BRANCH) | ||
| 57 | result &= NOHALFTURNS; | ||
| 58 | if (n < 1) | ||
| 59 | return result; | ||
| 60 | |||
| 61 | uint8_t base1 = movebase(moves[n-1]); | ||
| 62 | uint8_t axis1 = moveaxis(moves[n-1]); | ||
| 63 | |||
| 64 | result = disable_moves(result, base1 * 3); | ||
| 65 | if (base1 >= 9) | ||
| 66 | result = disable_moves(result, (base1 * 3) - 9); | ||
| 67 | |||
| 68 | if (n == 1) | ||
| 69 | return result; | ||
| 70 | |||
| 71 | uint8_t base2 = movebase(moves[n-2]); | ||
| 72 | uint8_t axis2 = moveaxis(moves[n-2]); | ||
| 73 | |||
| 74 | if(axis1 == axis2) | ||
| 75 | result = disable_moves(result, base2 * 3); | ||
| 76 | |||
| 77 | return result; | ||
| 78 | } | ||
| 79 | |||
| 31 | _static_inline uint8_t | 80 | _static_inline uint8_t |
| 32 | inverse_trans(uint8_t t) | 81 | inverse_trans(uint8_t t) |
| 33 | { | 82 | { |
| @@ -45,3 +94,116 @@ moveaxis(uint8_t move) | |||
| 45 | { | 94 | { |
| 46 | return move / 6; | 95 | return move / 6; |
| 47 | } | 96 | } |
| 97 | |||
| 98 | _static cube_t | ||
| 99 | move(cube_t c, uint8_t m) | ||
| 100 | { | ||
| 101 | switch (m) { | ||
| 102 | case _move_U: | ||
| 103 | return _move(U, c); | ||
| 104 | case _move_U2: | ||
| 105 | return _move(U2, c); | ||
| 106 | case _move_U3: | ||
| 107 | return _move(U3, c); | ||
| 108 | case _move_D: | ||
| 109 | return _move(D, c); | ||
| 110 | case _move_D2: | ||
| 111 | return _move(D2, c); | ||
| 112 | case _move_D3: | ||
| 113 | return _move(D3, c); | ||
| 114 | case _move_R: | ||
| 115 | return _move(R, c); | ||
| 116 | case _move_R2: | ||
| 117 | return _move(R2, c); | ||
| 118 | case _move_R3: | ||
| 119 | return _move(R3, c); | ||
| 120 | case _move_L: | ||
| 121 | return _move(L, c); | ||
| 122 | case _move_L2: | ||
| 123 | return _move(L2, c); | ||
| 124 | case _move_L3: | ||
| 125 | return _move(L3, c); | ||
| 126 | case _move_F: | ||
| 127 | return _move(F, c); | ||
| 128 | case _move_F2: | ||
| 129 | return _move(F2, c); | ||
| 130 | case _move_F3: | ||
| 131 | return _move(F3, c); | ||
| 132 | case _move_B: | ||
| 133 | return _move(B, c); | ||
| 134 | case _move_B2: | ||
| 135 | return _move(B2, c); | ||
| 136 | case _move_B3: | ||
| 137 | return _move(B3, c); | ||
| 138 | default: | ||
| 139 | LOG("move error, unknown move\n"); | ||
| 140 | return zero; | ||
| 141 | } | ||
| 142 | } | ||
| 143 | |||
| 144 | _static cube_t | ||
| 145 | premove(cube_t c, uint8_t m) | ||
| 146 | { | ||
| 147 | switch (m) { | ||
| 148 | case _move_U: | ||
| 149 | return _premove(U3, c); | ||
| 150 | case _move_U2: | ||
| 151 | return _premove(U2, c); | ||
| 152 | case _move_U3: | ||
| 153 | return _premove(U, c); | ||
| 154 | case _move_D: | ||
| 155 | return _premove(D3, c); | ||
| 156 | case _move_D2: | ||
| 157 | return _premove(D2, c); | ||
| 158 | case _move_D3: | ||
| 159 | return _premove(D, c); | ||
| 160 | case _move_R: | ||
| 161 | return _premove(R3, c); | ||
| 162 | case _move_R2: | ||
| 163 | return _premove(R2, c); | ||
| 164 | case _move_R3: | ||
| 165 | return _premove(R, c); | ||
| 166 | case _move_L: | ||
| 167 | return _premove(L3, c); | ||
| 168 | case _move_L2: | ||
| 169 | return _premove(L2, c); | ||
| 170 | case _move_L3: | ||
| 171 | return _premove(L, c); | ||
| 172 | case _move_F: | ||
| 173 | return _premove(F3, c); | ||
| 174 | case _move_F2: | ||
| 175 | return _premove(F2, c); | ||
| 176 | case _move_F3: | ||
| 177 | return _premove(F, c); | ||
| 178 | case _move_B: | ||
| 179 | return _premove(B3, c); | ||
| 180 | case _move_B2: | ||
| 181 | return _premove(B2, c); | ||
| 182 | case _move_B3: | ||
| 183 | return _premove(B, c); | ||
| 184 | default: | ||
| 185 | LOG("move error, unknown move\n"); | ||
| 186 | return zero; | ||
| 187 | } | ||
| 188 | } | ||
| 189 | |||
| 190 | _static uint8_t | ||
| 191 | inverse_move(uint8_t m) | ||
| 192 | { | ||
| 193 | return m - 2 * (m % 3) + 2; | ||
| 194 | } | ||
| 195 | |||
| 196 | _static uint8_t* | ||
| 197 | invertpremoves(uint8_t *moves, uint8_t nmoves) | ||
| 198 | { | ||
| 199 | uint8_t i; | ||
| 200 | uint8_t *ret = malloc(nmoves * sizeof(uint8_t)); | ||
| 201 | |||
| 202 | for (i = 0; i < nmoves; i++) | ||
| 203 | ret[i] = inverse_move(moves[i]); | ||
| 204 | |||
| 205 | // invert elements in the array | ||
| 206 | for (i = 0; i < nmoves / 2; i++) | ||
| 207 | _swap(ret[i], ret[nmoves - i - 1]); | ||
| 208 | return ret; | ||
| 209 | } | ||
