diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2025-04-22 14:17:03 +0200 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2025-04-22 14:17:03 +0200 |
| commit | ac652bb6fb45099008e862e3ced650299dc3ebb7 (patch) | |
| tree | 12fb1c902c271fbeac41c4ee416cf1cd76b16fd7 /src/core/cube.h | |
| parent | 71e104d84b3538e15f27df79b151e6b49cbad1eb (diff) | |
| parent | e391c4624055138f075c0e5772ccaf8ede094b5a (diff) | |
| download | nissy-core-ac652bb6fb45099008e862e3ced650299dc3ebb7.tar.gz nissy-core-ac652bb6fb45099008e862e3ced650299dc3ebb7.zip | |
Merge branch 'master' into extend_moves
Diffstat (limited to 'src/core/cube.h')
| -rw-r--r-- | src/core/cube.h | 178 |
1 files changed, 178 insertions, 0 deletions
diff --git a/src/core/cube.h b/src/core/cube.h index ce8a6b8..a045e55 100644 --- a/src/core/cube.h +++ b/src/core/cube.h | |||
| @@ -174,3 +174,181 @@ getcube(int64_t ep, int64_t eo, int64_t cp, int64_t co) | |||
| 174 | 174 | ||
| 175 | return cubefromarray(carr, earr); | 175 | return cubefromarray(carr, earr); |
| 176 | } | 176 | } |
| 177 | |||
| 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 | ||
| 194 | readco(const char *str) | ||
| 195 | { | ||
| 196 | if (*str == '0') | ||
| 197 | return 0; | ||
| 198 | if (*str == '1') | ||
| 199 | return CTWIST_CW; | ||
| 200 | if (*str == '2') | ||
| 201 | return CTWIST_CCW; | ||
| 202 | |||
| 203 | LOG("Error reading CO\n"); | ||
| 204 | return UINT8_ERROR; | ||
| 205 | } | ||
| 206 | |||
| 207 | STATIC uint8_t | ||
| 208 | readcp(const char *str) | ||
| 209 | { | ||
| 210 | uint8_t c; | ||
| 211 | |||
| 212 | for (c = 0; c < 8; c++) | ||
| 213 | if (!strncmp(str, cornerstr[c], 3) || | ||
| 214 | !strncmp(str, cornerstralt[c], 3)) | ||
| 215 | return c; | ||
| 216 | |||
| 217 | LOG("Error reading CP\n"); | ||
| 218 | return UINT8_ERROR; | ||
| 219 | } | ||
| 220 | |||
| 221 | STATIC uint8_t | ||
| 222 | readeo(const char *str) | ||
| 223 | { | ||
| 224 | if (*str == '0') | ||
| 225 | return 0; | ||
| 226 | if (*str == '1') | ||
| 227 | return EFLIP; | ||
| 228 | |||
| 229 | LOG("Error reading EO\n"); | ||
| 230 | return UINT8_ERROR; | ||
| 231 | } | ||
| 232 | |||
| 233 | STATIC uint8_t | ||
| 234 | readep(const char *str) | ||
| 235 | { | ||
| 236 | uint8_t e; | ||
| 237 | |||
| 238 | for (e = 0; e < 12; e++) | ||
| 239 | if (!strncmp(str, edgestr[e], 2)) | ||
| 240 | return e; | ||
| 241 | |||
| 242 | LOG("Error reading EP\n"); | ||
| 243 | return UINT8_ERROR; | ||
| 244 | } | ||
| 245 | |||
| 246 | STATIC cube_t | ||
| 247 | readcube(const char *buf) | ||
| 248 | { | ||
| 249 | int i; | ||
| 250 | uint8_t c[8], e[12]; | ||
| 251 | |||
| 252 | for (i = 0; i < 8; i++) { | ||
| 253 | c[i] = b32tocorner(buf[i]); | ||
| 254 | if (c[i] == UINT8_ERROR) { | ||
| 255 | LOG("Error reading corner %d ", i); | ||
| 256 | if (buf[i] == 0) { | ||
| 257 | LOG("(string terminated early)\n"); | ||
| 258 | } else { | ||
| 259 | LOG("(char '%c')\n", buf[i]); | ||
| 260 | } | ||
| 261 | return ZERO_CUBE; | ||
| 262 | } | ||
| 263 | } | ||
| 264 | |||
| 265 | if (buf[8] != '=') { | ||
| 266 | LOG("Error reading separator: a single '=' " | ||
| 267 | "must be used to separate edges and corners\n"); | ||
| 268 | return ZERO_CUBE; | ||
| 269 | } | ||
| 270 | |||
| 271 | for (i = 0; i < 12; i++) { | ||
| 272 | e[i] = b32toedge(buf[i+9]); | ||
| 273 | if (e[i] == UINT8_ERROR) { | ||
| 274 | LOG("Error reading edge %d ", i); | ||
| 275 | if (buf[i+9] == 0) { | ||
| 276 | LOG("(string terminated early)\n"); | ||
| 277 | } else { | ||
| 278 | LOG("(char '%c')\n", buf[i+9]); | ||
| 279 | } | ||
| 280 | return ZERO_CUBE; | ||
| 281 | } | ||
| 282 | } | ||
| 283 | |||
| 284 | return cubefromarray(c, e); | ||
| 285 | } | ||
| 286 | |||
| 287 | STATIC int64_t | ||
| 288 | writecube(cube_t cube, size_t buf_size, char buf[buf_size]) | ||
| 289 | { | ||
| 290 | int i; | ||
| 291 | uint8_t corner[8], edge[12]; | ||
| 292 | |||
| 293 | if (buf_size < NISSY_SIZE_CUBE) { | ||
| 294 | LOG("Cannot write cube: buffer size must be at least %u " | ||
| 295 | "bytes, but the provided one is %zu bytes.\n", | ||
| 296 | NISSY_SIZE_CUBE, buf_size); | ||
| 297 | return NISSY_ERROR_BUFFER_SIZE; | ||
| 298 | } | ||
| 299 | |||
| 300 | pieces(&cube, corner, edge); | ||
| 301 | |||
| 302 | for (i = 0; i < 8; i++) | ||
| 303 | buf[i] = cornertob32(corner[i]); | ||
| 304 | |||
| 305 | buf[8] = '='; | ||
| 306 | |||
| 307 | for (i = 0; i < 12; i++) | ||
| 308 | buf[i+9] = edgetob32(edge[i]); | ||
| 309 | |||
| 310 | /* TODO */ | ||
| 311 | buf[21] = '='; | ||
| 312 | buf[22] = 'A'; | ||
| 313 | buf[23] = '\0'; | ||
| 314 | |||
| 315 | return NISSY_OK; | ||
| 316 | } | ||
| 317 | |||
| 318 | STATIC uint8_t | ||
| 319 | b32toedge(char c) | ||
| 320 | { | ||
| 321 | if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'f'))) | ||
| 322 | return UINT8_ERROR; | ||
| 323 | |||
| 324 | return c <= 'Z' ? (uint8_t)(c - 'A') : (uint8_t)(c - 'a') + 26; | ||
| 325 | } | ||
| 326 | |||
| 327 | STATIC uint8_t | ||
| 328 | b32tocorner(char c) { | ||
| 329 | uint8_t val; | ||
| 330 | |||
| 331 | if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'f'))) | ||
| 332 | return UINT8_ERROR; | ||
| 333 | |||
| 334 | val = c <= 'Z' ? (uint8_t)(c - 'A') : (uint8_t)(c - 'a') + 26; | ||
| 335 | |||
| 336 | return (val & 7) | ((val & 24) << 2); | ||
| 337 | } | ||
| 338 | |||
| 339 | STATIC char | ||
| 340 | edgetob32(uint8_t edge) | ||
| 341 | { | ||
| 342 | return edge < 26 ? 'A' + (char)edge : 'a' + (char)(edge - 26); | ||
| 343 | } | ||
| 344 | |||
| 345 | STATIC char | ||
| 346 | cornertob32(uint8_t corner) | ||
| 347 | { | ||
| 348 | uint8_t val; | ||
| 349 | |||
| 350 | val = (corner & 7) | ((corner & 96) >> 2); | ||
| 351 | |||
| 352 | return val < 26 ? 'A' + (char)val : 'a' + (char)(val - 26); | ||
| 353 | } | ||
| 354 | /******************************************************************************/ | ||
