diff options
Diffstat (limited to 'src/core/cube.h')
| -rw-r--r-- | src/core/cube.h | 99 |
1 files changed, 56 insertions, 43 deletions
diff --git a/src/core/cube.h b/src/core/cube.h index a045e55..76c3ff1 100644 --- a/src/core/cube.h +++ b/src/core/cube.h | |||
| @@ -1,17 +1,33 @@ | |||
| 1 | STATIC cube_t solvedcube(void); | 1 | #define ZERO_ORIENTED_CUBE ((oriented_cube_t) {0}) |
| 2 | #define SOLVED_ORIENTED_CUBE \ | ||
| 3 | ((oriented_cube_t) { .cube = SOLVED_CUBE, .orientation = 0 }) | ||
| 4 | |||
| 5 | STATIC oriented_cube_t solvedcube(void); | ||
| 2 | STATIC cube_t cubefromarray(uint8_t [static 8], uint8_t [static 12]); | 6 | STATIC cube_t cubefromarray(uint8_t [static 8], uint8_t [static 12]); |
| 3 | STATIC bool isconsistent(cube_t); | 7 | STATIC bool isconsistent(oriented_cube_t); |
| 4 | STATIC bool issolvable(cube_t); | 8 | STATIC bool issolvable(oriented_cube_t); |
| 5 | STATIC bool issolved(cube_t); | 9 | STATIC bool issolved(oriented_cube_t); |
| 6 | STATIC bool iserror(cube_t); | 10 | STATIC bool iserror(oriented_cube_t); |
| 7 | STATIC void getcube_fix(long long *, long long *, long long *, long long *); | 11 | STATIC void getcube_fix(long long *, long long *, long long *, long long *); |
| 8 | STATIC cube_t getcube(int64_t, int64_t, int64_t, int64_t); | 12 | STATIC cube_t getcube(int64_t, int64_t, int64_t, int64_t); |
| 9 | 13 | ||
| 10 | /* This is used only in tests, use SOLVED_CUBE directly everywhere else */ | 14 | STATIC oriented_cube_t readcube(const char *); |
| 11 | STATIC cube_t | 15 | STATIC int64_t writecube(oriented_cube_t, size_t n, char [n]); |
| 16 | STATIC uint8_t readco(const char *); | ||
| 17 | STATIC uint8_t readcp(const char *); | ||
| 18 | STATIC uint8_t readeo(const char *); | ||
| 19 | STATIC uint8_t readep(const char *); | ||
| 20 | |||
| 21 | STATIC uint8_t b32toedge(char); | ||
| 22 | STATIC uint8_t b32tocorner(char); | ||
| 23 | STATIC char edgetob32(uint8_t); | ||
| 24 | STATIC char cornertob32(uint8_t); | ||
| 25 | |||
| 26 | /* This is used only in tests, use SOLVED_ORIENTED_CUBE everywhere else */ | ||
| 27 | STATIC oriented_cube_t | ||
| 12 | solvedcube(void) | 28 | solvedcube(void) |
| 13 | { | 29 | { |
| 14 | return SOLVED_CUBE; | 30 | return SOLVED_ORIENTED_CUBE; |
| 15 | } | 31 | } |
| 16 | 32 | ||
| 17 | STATIC cube_t | 33 | STATIC cube_t |
| @@ -24,12 +40,12 @@ cubefromarray(uint8_t c[static 8], uint8_t e[static 12]) | |||
| 24 | } | 40 | } |
| 25 | 41 | ||
| 26 | STATIC bool | 42 | STATIC bool |
| 27 | isconsistent(cube_t cube) | 43 | isconsistent(oriented_cube_t cube) |
| 28 | { | 44 | { |
| 29 | uint8_t i, p, e, piece, corner[8], edge[12]; | 45 | uint8_t i, p, e, piece, corner[8], edge[12]; |
| 30 | bool found[12]; | 46 | bool found[12]; |
| 31 | 47 | ||
| 32 | pieces(&cube, corner, edge); | 48 | pieces(&cube.cube, corner, edge); |
| 33 | 49 | ||
| 34 | for (i = 0; i < 12; i++) | 50 | for (i = 0; i < 12; i++) |
| 35 | found[i] = false; | 51 | found[i] = false; |
| @@ -63,25 +79,29 @@ isconsistent(cube_t cube) | |||
| 63 | if (!found[i]) | 79 | if (!found[i]) |
| 64 | goto inconsistent_cp; | 80 | goto inconsistent_cp; |
| 65 | 81 | ||
| 82 | if (cube.orientation >= 24) | ||
| 83 | goto inconsistent_orientation; | ||
| 84 | |||
| 66 | return true; | 85 | return true; |
| 67 | 86 | ||
| 68 | inconsistent_ep: | 87 | inconsistent_ep: |
| 69 | inconsistent_cp: | 88 | inconsistent_cp: |
| 70 | inconsistent_eo: | 89 | inconsistent_eo: |
| 71 | inconsistent_co: | 90 | inconsistent_co: |
| 72 | /* We used to do more logging here, hence the 4 different labels */ | 91 | inconsistent_orientation: |
| 92 | /* We used to do more logging here, hence the different labels */ | ||
| 73 | return false; | 93 | return false; |
| 74 | } | 94 | } |
| 75 | 95 | ||
| 76 | STATIC bool | 96 | STATIC bool |
| 77 | issolvable(cube_t cube) | 97 | issolvable(oriented_cube_t cube) |
| 78 | { | 98 | { |
| 79 | uint8_t i, eo, co, piece, edge[12], corner[8], ep[12], cp[8]; | 99 | uint8_t i, eo, co, piece, edge[12], corner[8], ep[12], cp[8]; |
| 80 | 100 | ||
| 81 | DBG_ASSERT(isconsistent(cube), false, | 101 | DBG_ASSERT(isconsistent(cube), false, |
| 82 | "issolvable: cube is inconsistent\n"); | 102 | "issolvable: cube is inconsistent\n"); |
| 83 | 103 | ||
| 84 | pieces(&cube, corner, edge); | 104 | pieces(&cube.cube, corner, edge); |
| 85 | for (i = 0; i < 12; i++) | 105 | for (i = 0; i < 12; i++) |
| 86 | ep[i] = edge[i] & PBITS; | 106 | ep[i] = edge[i] & PBITS; |
| 87 | for (i = 0; i < 8; i++) | 107 | for (i = 0; i < 8; i++) |
| @@ -120,15 +140,15 @@ issolvable_co: | |||
| 120 | } | 140 | } |
| 121 | 141 | ||
| 122 | bool | 142 | bool |
| 123 | issolved(cube_t cube) | 143 | issolved(oriented_cube_t cube) |
| 124 | { | 144 | { |
| 125 | return equal(cube, SOLVED_CUBE); | 145 | return equal(cube.cube, SOLVED_CUBE); |
| 126 | } | 146 | } |
| 127 | 147 | ||
| 128 | bool | 148 | bool |
| 129 | iserror(cube_t cube) | 149 | iserror(oriented_cube_t cube) |
| 130 | { | 150 | { |
| 131 | return equal(cube, ZERO_CUBE); | 151 | return equal(cube.cube, ZERO_CUBE); |
| 132 | } | 152 | } |
| 133 | 153 | ||
| 134 | STATIC void | 154 | STATIC void |
| @@ -175,21 +195,6 @@ getcube(int64_t ep, int64_t eo, int64_t cp, int64_t co) | |||
| 175 | return cubefromarray(carr, earr); | 195 | return cubefromarray(carr, earr); |
| 176 | } | 196 | } |
| 177 | 197 | ||
| 178 | |||
| 179 | /******************************************************************************/ | ||
| 180 | |||
| 181 | STATIC cube_t readcube(const char *); | ||
| 182 | STATIC int64_t writecube(cube_t, size_t n, char [n]); | ||
| 183 | STATIC uint8_t readco(const char *); | ||
| 184 | STATIC uint8_t readcp(const char *); | ||
| 185 | STATIC uint8_t readeo(const char *); | ||
| 186 | STATIC uint8_t readep(const char *); | ||
| 187 | |||
| 188 | STATIC uint8_t b32toedge(char); | ||
| 189 | STATIC uint8_t b32tocorner(char); | ||
| 190 | STATIC char edgetob32(uint8_t); | ||
| 191 | STATIC char cornertob32(uint8_t); | ||
| 192 | |||
| 193 | STATIC uint8_t | 198 | STATIC uint8_t |
| 194 | readco(const char *str) | 199 | readco(const char *str) |
| 195 | { | 200 | { |
| @@ -243,11 +248,11 @@ readep(const char *str) | |||
| 243 | return UINT8_ERROR; | 248 | return UINT8_ERROR; |
| 244 | } | 249 | } |
| 245 | 250 | ||
| 246 | STATIC cube_t | 251 | STATIC oriented_cube_t |
| 247 | readcube(const char *buf) | 252 | readcube(const char *buf) |
| 248 | { | 253 | { |
| 249 | int i; | 254 | int i; |
| 250 | uint8_t c[8], e[12]; | 255 | uint8_t c[8], e[12], orientation; |
| 251 | 256 | ||
| 252 | for (i = 0; i < 8; i++) { | 257 | for (i = 0; i < 8; i++) { |
| 253 | c[i] = b32tocorner(buf[i]); | 258 | c[i] = b32tocorner(buf[i]); |
| @@ -258,14 +263,14 @@ readcube(const char *buf) | |||
| 258 | } else { | 263 | } else { |
| 259 | LOG("(char '%c')\n", buf[i]); | 264 | LOG("(char '%c')\n", buf[i]); |
| 260 | } | 265 | } |
| 261 | return ZERO_CUBE; | 266 | return ZERO_ORIENTED_CUBE; |
| 262 | } | 267 | } |
| 263 | } | 268 | } |
| 264 | 269 | ||
| 265 | if (buf[8] != '=') { | 270 | if (buf[8] != '=') { |
| 266 | LOG("Error reading separator: a single '=' " | 271 | LOG("Error reading separator: a single '=' " |
| 267 | "must be used to separate edges and corners\n"); | 272 | "must be used to separate edges and corners\n"); |
| 268 | return ZERO_CUBE; | 273 | return ZERO_ORIENTED_CUBE; |
| 269 | } | 274 | } |
| 270 | 275 | ||
| 271 | for (i = 0; i < 12; i++) { | 276 | for (i = 0; i < 12; i++) { |
| @@ -277,15 +282,25 @@ readcube(const char *buf) | |||
| 277 | } else { | 282 | } else { |
| 278 | LOG("(char '%c')\n", buf[i+9]); | 283 | LOG("(char '%c')\n", buf[i+9]); |
| 279 | } | 284 | } |
| 280 | return ZERO_CUBE; | 285 | return ZERO_ORIENTED_CUBE; |
| 281 | } | 286 | } |
| 282 | } | 287 | } |
| 283 | 288 | ||
| 284 | return cubefromarray(c, e); | 289 | orientation = (uint8_t)(buf[22] - 'A'); |
| 290 | if (orientation >= 24) { | ||
| 291 | LOG("Error reading orientation: impossible value %" PRIu8 | ||
| 292 | " (%c)\n", orientation, buf[22]); | ||
| 293 | return ZERO_ORIENTED_CUBE; | ||
| 294 | } | ||
| 295 | |||
| 296 | return (oriented_cube_t) { | ||
| 297 | .cube = cubefromarray(c, e), | ||
| 298 | .orientation = orientation | ||
| 299 | }; | ||
| 285 | } | 300 | } |
| 286 | 301 | ||
| 287 | STATIC int64_t | 302 | STATIC int64_t |
| 288 | writecube(cube_t cube, size_t buf_size, char buf[buf_size]) | 303 | writecube(oriented_cube_t cube, size_t buf_size, char buf[buf_size]) |
| 289 | { | 304 | { |
| 290 | int i; | 305 | int i; |
| 291 | uint8_t corner[8], edge[12]; | 306 | uint8_t corner[8], edge[12]; |
| @@ -297,7 +312,7 @@ writecube(cube_t cube, size_t buf_size, char buf[buf_size]) | |||
| 297 | return NISSY_ERROR_BUFFER_SIZE; | 312 | return NISSY_ERROR_BUFFER_SIZE; |
| 298 | } | 313 | } |
| 299 | 314 | ||
| 300 | pieces(&cube, corner, edge); | 315 | pieces(&cube.cube, corner, edge); |
| 301 | 316 | ||
| 302 | for (i = 0; i < 8; i++) | 317 | for (i = 0; i < 8; i++) |
| 303 | buf[i] = cornertob32(corner[i]); | 318 | buf[i] = cornertob32(corner[i]); |
| @@ -307,9 +322,8 @@ writecube(cube_t cube, size_t buf_size, char buf[buf_size]) | |||
| 307 | for (i = 0; i < 12; i++) | 322 | for (i = 0; i < 12; i++) |
| 308 | buf[i+9] = edgetob32(edge[i]); | 323 | buf[i+9] = edgetob32(edge[i]); |
| 309 | 324 | ||
| 310 | /* TODO */ | ||
| 311 | buf[21] = '='; | 325 | buf[21] = '='; |
| 312 | buf[22] = 'A'; | 326 | buf[22] = (char)cube.orientation + 'A'; |
| 313 | buf[23] = '\0'; | 327 | buf[23] = '\0'; |
| 314 | 328 | ||
| 315 | return NISSY_OK; | 329 | return NISSY_OK; |
| @@ -351,4 +365,3 @@ cornertob32(uint8_t corner) | |||
| 351 | 365 | ||
| 352 | return val < 26 ? 'A' + (char)val : 'a' + (char)(val - 26); | 366 | return val < 26 ? 'A' + (char)val : 'a' + (char)(val - 26); |
| 353 | } | 367 | } |
| 354 | /******************************************************************************/ | ||
