diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2025-07-31 09:47:57 +0200 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2025-07-31 09:47:57 +0200 |
| commit | 992de12cc081e7dde16c3c231f83bfa77c67825a (patch) | |
| tree | af0b2d51b1a6cd92ae2cd6a32d581d752ddce8ea /src/core/moves.h | |
| parent | e2b154c40acaac4e7a7b3e379ada0404519c3750 (diff) | |
| download | nissy-core-992de12cc081e7dde16c3c231f83bfa77c67825a.tar.gz nissy-core-992de12cc081e7dde16c3c231f83bfa77c67825a.zip | |
Added move sequence comparison function
Diffstat (limited to '')
| -rw-r--r-- | src/core/moves.h | 69 |
1 files changed, 66 insertions, 3 deletions
diff --git a/src/core/moves.h b/src/core/moves.h index d3ff4a3..9c8cc90 100644 --- a/src/core/moves.h +++ b/src/core/moves.h | |||
| @@ -3,8 +3,12 @@ | |||
| 3 | 3 | ||
| 4 | STATIC uint8_t readmove(char); | 4 | STATIC uint8_t readmove(char); |
| 5 | STATIC int64_t readmoves(const char *, | 5 | STATIC int64_t readmoves(const char *, |
| 6 | size_t, size_t, uint64_t *, uint64_t *, uint8_t *, uint8_t *); | 6 | size_t, size_t, size_t *, size_t *, uint8_t *, uint8_t *); |
| 7 | STATIC int64_t readmoves_struct(const char *, moves_struct_t [static 1]); | ||
| 7 | STATIC int64_t countmoves(const char *); | 8 | STATIC int64_t countmoves(const char *); |
| 9 | STATIC bool moves_struct_equal( | ||
| 10 | const moves_struct_t [static 1], const moves_struct_t [static 1]); | ||
| 11 | STATIC long long comparemoves(const char *, const char *); | ||
| 8 | STATIC uint8_t readmodifier(char); | 12 | STATIC uint8_t readmodifier(char); |
| 9 | STATIC int64_t writemoves(size_t, const uint8_t *, size_t, char *); | 13 | STATIC int64_t writemoves(size_t, const uint8_t *, size_t, char *); |
| 10 | 14 | ||
| @@ -124,8 +128,8 @@ readmoves( | |||
| 124 | const char *buf, | 128 | const char *buf, |
| 125 | size_t nsize, | 129 | size_t nsize, |
| 126 | size_t invsize, | 130 | size_t invsize, |
| 127 | uint64_t *n, | 131 | size_t *n, |
| 128 | uint64_t *i, | 132 | size_t *i, |
| 129 | uint8_t *normal, | 133 | uint8_t *normal, |
| 130 | uint8_t *inverse | 134 | uint8_t *inverse |
| 131 | ) | 135 | ) |
| @@ -154,6 +158,13 @@ readmoves( | |||
| 154 | } | 158 | } |
| 155 | 159 | ||
| 156 | STATIC int64_t | 160 | STATIC int64_t |
| 161 | readmoves_struct(const char *moves, moves_struct_t ret[static 1]) | ||
| 162 | { | ||
| 163 | return readmoves(moves, MOVES_STRUCT_MAXLEN, MOVES_STRUCT_MAXLEN, | ||
| 164 | &ret->nnormal, &ret->ninverse, ret->normal, ret->inverse); | ||
| 165 | } | ||
| 166 | |||
| 167 | STATIC int64_t | ||
| 157 | countmoves(const char *buf) | 168 | countmoves(const char *buf) |
| 158 | { | 169 | { |
| 159 | uint8_t m; | 170 | uint8_t m; |
| @@ -168,6 +179,58 @@ countmoves(const char *buf) | |||
| 168 | return count; | 179 | return count; |
| 169 | } | 180 | } |
| 170 | 181 | ||
| 182 | STATIC bool | ||
| 183 | moves_struct_equal( | ||
| 184 | const moves_struct_t ms1[static 1], | ||
| 185 | const moves_struct_t ms2[static 1] | ||
| 186 | ) | ||
| 187 | { | ||
| 188 | size_t i; | ||
| 189 | |||
| 190 | if (ms1->nnormal != ms2->nnormal || ms1->ninverse != ms2->ninverse) | ||
| 191 | return false; | ||
| 192 | |||
| 193 | for (i = 0; i < ms1->nnormal; i++) | ||
| 194 | if (ms1->normal[i] != ms2->normal[i]) | ||
| 195 | return false; | ||
| 196 | |||
| 197 | for (i = 0; i < ms1->ninverse; i++) | ||
| 198 | if (ms1->inverse[i] != ms2->inverse[i]) | ||
| 199 | return false; | ||
| 200 | |||
| 201 | return true; | ||
| 202 | } | ||
| 203 | |||
| 204 | STATIC long long | ||
| 205 | comparemoves(const char *moves1, const char *moves2) | ||
| 206 | { | ||
| 207 | int64_t err; | ||
| 208 | moves_struct_t ms1, ms2; | ||
| 209 | |||
| 210 | if ((err = readmoves_struct(moves1, &ms1)) < 0) | ||
| 211 | return err; | ||
| 212 | sortparallel_moves(ms1.nnormal, ms1.normal); | ||
| 213 | sortparallel_moves(ms1.ninverse, ms1.inverse); | ||
| 214 | |||
| 215 | if ((err = readmoves_struct(moves2, &ms2)) < 0) | ||
| 216 | return err; | ||
| 217 | sortparallel_moves(ms2.nnormal, ms2.normal); | ||
| 218 | sortparallel_moves(ms2.ninverse, ms2.inverse); | ||
| 219 | |||
| 220 | if (moves_struct_equal(&ms1, &ms2)) | ||
| 221 | return NISSY_COMPARE_MOVES_EQUAL; | ||
| 222 | |||
| 223 | /* | ||
| 224 | TODO: more types of move comparison | ||
| 225 | - up to moving rotations around | ||
| 226 | - up to rotation | ||
| 227 | - up transformation (including mirror or not including it) | ||
| 228 | - ... | ||
| 229 | */ | ||
| 230 | |||
| 231 | return NISSY_COMPARE_MOVES_DIFFERENT; | ||
| 232 | } | ||
| 233 | |||
| 171 | STATIC int64_t | 234 | STATIC int64_t |
| 172 | writemoves( | 235 | writemoves( |
| 173 | size_t nmoves, | 236 | size_t nmoves, |
