diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2024-09-05 17:52:41 +0200 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2024-09-05 17:52:41 +0200 |
| commit | f85ac77efc0feeeab183e509a7baca1c507da3c5 (patch) | |
| tree | 7bc6cee6059b97e41dc58982ef8c9b4c624d9824 /src | |
| parent | 35237ef2621d8e5a2387426067c2ec9a75f03836 (diff) | |
| download | nissy-core-f85ac77efc0feeeab183e509a7baca1c507da3c5.tar.gz nissy-core-f85ac77efc0feeeab183e509a7baca1c507da3c5.zip | |
Added test for invermoves and small refactor
Diffstat (limited to '')
| -rw-r--r-- | src/core/moves.h | 54 |
1 files changed, 40 insertions, 14 deletions
diff --git a/src/core/moves.h b/src/core/moves.h index 5d08a66..3f285fb 100644 --- a/src/core/moves.h +++ b/src/core/moves.h | |||
| @@ -13,9 +13,27 @@ STATIC cube_t premove(cube_t, uint8_t); | |||
| 13 | STATIC uint8_t inverse_move(uint8_t); | 13 | STATIC uint8_t inverse_move(uint8_t); |
| 14 | STATIC void invertmoves(uint8_t *, uint8_t, uint8_t *); | 14 | STATIC void invertmoves(uint8_t *, uint8_t, uint8_t *); |
| 15 | 15 | ||
| 16 | STATIC int readmoves(const char *, int, uint8_t *); | ||
| 16 | STATIC cube_t applymoves(cube_t, const char *); | 17 | STATIC cube_t applymoves(cube_t, const char *); |
| 17 | STATIC cube_t frommoves(const char *); | 18 | STATIC cube_t frommoves(const char *); |
| 18 | 19 | ||
| 20 | #define FOREACH_READMOVE(ARG_BUF, ARG_MOVE, ARG_C, ARG_MAX, \ | ||
| 21 | LABEL_ERROR, ARG_ACTION) \ | ||
| 22 | const char *VAR_B; \ | ||
| 23 | uint8_t VAR_MOVE_NOMOD, VAR_MOD; \ | ||
| 24 | for (VAR_B = ARG_BUF, ARG_C = 0; *VAR_B != '\0'; VAR_B++, ARG_C++) { \ | ||
| 25 | while (*VAR_B == ' ' || *VAR_B == '\t' || *VAR_B == '\n') \ | ||
| 26 | VAR_B++; \ | ||
| 27 | if (*VAR_B == '\0' || ARG_C == ARG_MAX) \ | ||
| 28 | break; \ | ||
| 29 | if ((VAR_MOVE_NOMOD = readmove(*VAR_B)) == UINT8_ERROR) \ | ||
| 30 | goto LABEL_ERROR; \ | ||
| 31 | if ((VAR_MOD = readmodifier(*(VAR_B+1))) != 0) \ | ||
| 32 | VAR_B++; \ | ||
| 33 | ARG_MOVE = VAR_MOVE_NOMOD + VAR_MOD; \ | ||
| 34 | ARG_ACTION \ | ||
| 35 | } | ||
| 36 | |||
| 19 | STATIC bool | 37 | STATIC bool |
| 20 | allowednextmove(uint8_t *moves, uint8_t n) | 38 | allowednextmove(uint8_t *moves, uint8_t n) |
| 21 | { | 39 | { |
| @@ -173,28 +191,36 @@ invertmoves(uint8_t *moves, uint8_t nmoves, uint8_t *ret) | |||
| 173 | ret[i] = inverse_move(moves[nmoves - i - 1]); | 191 | ret[i] = inverse_move(moves[nmoves - i - 1]); |
| 174 | } | 192 | } |
| 175 | 193 | ||
| 194 | STATIC int | ||
| 195 | readmoves(const char *buf, int max, uint8_t *ret) | ||
| 196 | { | ||
| 197 | uint8_t m; | ||
| 198 | int c; | ||
| 199 | |||
| 200 | FOREACH_READMOVE(buf, m, c, max, readmoves_error, | ||
| 201 | ret[c] = m; | ||
| 202 | ) | ||
| 203 | |||
| 204 | return c; | ||
| 205 | |||
| 206 | readmoves_error: | ||
| 207 | LOG("readmoves error\n"); | ||
| 208 | return -1; | ||
| 209 | } | ||
| 210 | |||
| 176 | STATIC cube_t | 211 | STATIC cube_t |
| 177 | applymoves(cube_t cube, const char *buf) | 212 | applymoves(cube_t cube, const char *buf) |
| 178 | { | 213 | { |
| 179 | uint8_t r, m; | 214 | int c; |
| 180 | const char *b; | 215 | uint8_t m; |
| 181 | 216 | ||
| 182 | DBG_ASSERT(isconsistent(cube), ZERO_CUBE, | 217 | DBG_ASSERT(isconsistent(cube), ZERO_CUBE, |
| 183 | "move error: inconsistent cube\n"); | 218 | "move error: inconsistent cube\n"); |
| 184 | 219 | ||
| 185 | for (b = buf; *b != '\0'; b++) { | 220 | FOREACH_READMOVE(buf, m, c, -1, applymoves_error, |
| 186 | while (*b == ' ' || *b == '\t' || *b == '\n') | 221 | cube = move(cube, m); |
| 187 | b++; | 222 | ) |
| 188 | if (*b == '\0') | ||
| 189 | goto applymoves_finish; | ||
| 190 | if ((r = readmove(*b)) == UINT8_ERROR) | ||
| 191 | goto applymoves_error; | ||
| 192 | if ((m = readmodifier(*(b+1))) != 0) | ||
| 193 | b++; | ||
| 194 | cube = move(cube, r + m); | ||
| 195 | } | ||
| 196 | 223 | ||
| 197 | applymoves_finish: | ||
| 198 | return cube; | 224 | return cube; |
| 199 | 225 | ||
| 200 | applymoves_error: | 226 | applymoves_error: |
