h48

A prototype for an optimal Rubik's cube solver, work in progress.
git clone https://git.tronto.net/h48
Download | Log | Files | Refs | README | LICENSE

commit f85ac77efc0feeeab183e509a7baca1c507da3c5
parent 35237ef2621d8e5a2387426067c2ec9a75f03836
Author: Sebastiano Tronto <sebastiano@tronto.net>
Date:   Thu,  5 Sep 2024 17:52:41 +0200

Added test for invermoves and small refactor

Diffstat:
Msrc/core/moves.h | 54++++++++++++++++++++++++++++++++++++++++--------------
Atest/032_invertmoves/001_onemove.in | 1+
Atest/032_invertmoves/001_onemove.out | 1+
Atest/032_invertmoves/002_20moves.in | 1+
Atest/032_invertmoves/002_20moves.out | 1+
Atest/032_invertmoves/invertmoves_tests.c | 21+++++++++++++++++++++
6 files changed, 65 insertions(+), 14 deletions(-)

diff --git a/src/core/moves.h b/src/core/moves.h @@ -13,9 +13,27 @@ STATIC cube_t premove(cube_t, uint8_t); STATIC uint8_t inverse_move(uint8_t); STATIC void invertmoves(uint8_t *, uint8_t, uint8_t *); +STATIC int readmoves(const char *, int, uint8_t *); STATIC cube_t applymoves(cube_t, const char *); STATIC cube_t frommoves(const char *); +#define FOREACH_READMOVE(ARG_BUF, ARG_MOVE, ARG_C, ARG_MAX, \ + LABEL_ERROR, ARG_ACTION) \ + const char *VAR_B; \ + uint8_t VAR_MOVE_NOMOD, VAR_MOD; \ + for (VAR_B = ARG_BUF, ARG_C = 0; *VAR_B != '\0'; VAR_B++, ARG_C++) { \ + while (*VAR_B == ' ' || *VAR_B == '\t' || *VAR_B == '\n') \ + VAR_B++; \ + if (*VAR_B == '\0' || ARG_C == ARG_MAX) \ + break; \ + if ((VAR_MOVE_NOMOD = readmove(*VAR_B)) == UINT8_ERROR) \ + goto LABEL_ERROR; \ + if ((VAR_MOD = readmodifier(*(VAR_B+1))) != 0) \ + VAR_B++; \ + ARG_MOVE = VAR_MOVE_NOMOD + VAR_MOD; \ + ARG_ACTION \ + } + STATIC bool allowednextmove(uint8_t *moves, uint8_t n) { @@ -173,28 +191,36 @@ invertmoves(uint8_t *moves, uint8_t nmoves, uint8_t *ret) ret[i] = inverse_move(moves[nmoves - i - 1]); } +STATIC int +readmoves(const char *buf, int max, uint8_t *ret) +{ + uint8_t m; + int c; + + FOREACH_READMOVE(buf, m, c, max, readmoves_error, + ret[c] = m; + ) + + return c; + +readmoves_error: + LOG("readmoves error\n"); + return -1; +} + STATIC cube_t applymoves(cube_t cube, const char *buf) { - uint8_t r, m; - const char *b; + int c; + uint8_t m; DBG_ASSERT(isconsistent(cube), ZERO_CUBE, "move error: inconsistent cube\n"); - for (b = buf; *b != '\0'; b++) { - while (*b == ' ' || *b == '\t' || *b == '\n') - b++; - if (*b == '\0') - goto applymoves_finish; - if ((r = readmove(*b)) == UINT8_ERROR) - goto applymoves_error; - if ((m = readmodifier(*(b+1))) != 0) - b++; - cube = move(cube, r + m); - } + FOREACH_READMOVE(buf, m, c, -1, applymoves_error, + cube = move(cube, m); + ) -applymoves_finish: return cube; applymoves_error: diff --git a/test/032_invertmoves/001_onemove.in b/test/032_invertmoves/001_onemove.in @@ -0,0 +1 @@ +F diff --git a/test/032_invertmoves/001_onemove.out b/test/032_invertmoves/001_onemove.out @@ -0,0 +1 @@ +F' diff --git a/test/032_invertmoves/002_20moves.in b/test/032_invertmoves/002_20moves.in @@ -0,0 +1 @@ +B2 R D2 B2 R D2 F2 L2 F2 R' F2 D' L D2 L U2 L B2 F2 U diff --git a/test/032_invertmoves/002_20moves.out b/test/032_invertmoves/002_20moves.out @@ -0,0 +1 @@ +U' F2 B2 L' U2 L' D2 L' D F2 R F2 L2 F2 D2 R' B2 D2 R' B2 diff --git a/test/032_invertmoves/invertmoves_tests.c b/test/032_invertmoves/invertmoves_tests.c @@ -0,0 +1,21 @@ +#include "../test.h" + +#define MAXMOVES 20 + +int64_t readmoves(const char *, int, uint8_t *); +void writemoves(uint8_t *, int, char *); +void invertmoves(uint8_t *, uint8_t, uint8_t *); + +void run(void) { + char movestr[STRLENMAX], outstr[STRLENMAX]; + uint8_t moves[MAXMOVES], ret[MAXMOVES]; + int c; + + fgets(movestr, STRLENMAX, stdin); + c = readmoves(movestr, MAXMOVES, moves); + + invertmoves(moves, c, ret); + writemoves(ret, c, outstr); + + printf("%s\n", outstr); +}