aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2024-10-18 12:21:51 +0200
committerSebastiano Tronto <sebastiano@tronto.net>2024-10-18 12:21:51 +0200
commit3d6c73c33276ecedba86cb871b4b5a089571629b (patch)
tree8a68bb982d5d0bde2a8886af9aa146d3c16a8ba4
parent4f0403d24067c1f69b21cd29c5f9e5949e64c635 (diff)
downloadnissy-core-3d6c73c33276ecedba86cb871b4b5a089571629b.tar.gz
nissy-core-3d6c73c33276ecedba86cb871b4b5a089571629b.zip
New interface function nissy_countmoves
-rw-r--r--shell/shell.c23
-rw-r--r--src/core/moves.h5
-rw-r--r--src/nissy.c12
-rw-r--r--src/nissy.h13
4 files changed, 50 insertions, 3 deletions
diff --git a/shell/shell.c b/shell/shell.c
index 24ae243..42b3ccd 100644
--- a/shell/shell.c
+++ b/shell/shell.c
@@ -67,6 +67,7 @@ static int64_t datasize_exec(args_t *);
67static int64_t gendata_exec(args_t *); 67static int64_t gendata_exec(args_t *);
68static int64_t solve_exec(args_t *); 68static int64_t solve_exec(args_t *);
69static int64_t solve_scramble_exec(args_t *); 69static int64_t solve_scramble_exec(args_t *);
70static int64_t countmoves_exec(args_t *);
70static int64_t help_exec(args_t *); 71static int64_t help_exec(args_t *);
71 72
72static int parse_args(int, char **, args_t *); 73static int parse_args(int, char **, args_t *);
@@ -202,13 +203,20 @@ struct {
202 "solve_scramble", 203 "solve_scramble",
203 "solve_scramble " FLAG_SOLVER " SOLVER" 204 "solve_scramble " FLAG_SOLVER " SOLVER"
204 "[" FLAG_MINMOVES " n] [" FLAG_MAXMOVES " N] " 205 "[" FLAG_MINMOVES " n] [" FLAG_MAXMOVES " N] "
205 FLAG_CUBE " CUBE", 206 FLAG_MOVES " MOVES",
206 "Solve the given SCRAMBLE using SOLVER, " 207 "Solve the given SCRAMBLE using SOLVER, "
207 "using at least n and at most N moves. " 208 "using at least n and at most N moves. "
208 INFO_MOVESFORMAT, 209 INFO_MOVESFORMAT,
209 solve_scramble_exec 210 solve_scramble_exec
210 ), 211 ),
211 COMMAND( 212 COMMAND(
213 "countmoves",
214 "countmoves " FLAG_MOVES " MOVES",
215 "Count the given MOVES in HTM. "
216 INFO_MOVESFORMAT,
217 countmoves_exec
218 ),
219 COMMAND(
212 "help", 220 "help",
213 "help [" FLAG_COMMAND " COMMAND]", 221 "help [" FLAG_COMMAND " COMMAND]",
214 "If no COMMAND is specified, prints some generic information " 222 "If no COMMAND is specified, prints some generic information "
@@ -488,6 +496,19 @@ solve_scramble_exec(args_t *args)
488} 496}
489 497
490static int64_t 498static int64_t
499countmoves_exec(args_t *args)
500{
501 long long count;
502
503 count = nissy_countmoves(args->str_moves);
504
505 if (count >= 0)
506 printf("%lld\n", count);
507
508 return count >= 0 ? 0 : count;
509}
510
511static int64_t
491help_exec(args_t *args) 512help_exec(args_t *args)
492{ 513{
493 int i; 514 int i;
diff --git a/src/core/moves.h b/src/core/moves.h
index d07bc19..e3a9bc1 100644
--- a/src/core/moves.h
+++ b/src/core/moves.h
@@ -223,8 +223,9 @@ readmoves(const char *buf, int max, uint8_t *ret)
223 uint8_t m; 223 uint8_t m;
224 int c; 224 int c;
225 225
226 FOREACH_READMOVE(buf, m, c, max, -1, 226 FOREACH_READMOVE(buf, m, c, max, NISSY_ERROR_INVALID_MOVES,
227 ret[c] = m; 227 if (ret != NULL)
228 ret[c] = m;
228 ) 229 )
229 230
230 return c; 231 return c;
diff --git a/src/nissy.c b/src/nissy.c
index fe2747b..986f27f 100644
--- a/src/nissy.c
+++ b/src/nissy.c
@@ -1,4 +1,5 @@
1#include <inttypes.h> 1#include <inttypes.h>
2#include <limits.h>
2#include <pthread.h> 3#include <pthread.h>
3#include <stdatomic.h> 4#include <stdatomic.h>
4#include <stdarg.h> 5#include <stdarg.h>
@@ -521,6 +522,17 @@ nissy_solve(
521} 522}
522 523
523long long 524long long
525nissy_countmoves(
526 const char *moves
527)
528{
529 if (moves == NULL)
530 return NISSY_ERROR_NULL_POINTER;
531
532 return readmoves(moves, INT_MAX, NULL);
533}
534
535long long
524nissy_setlogger( 536nissy_setlogger(
525 void (*log)(const char *, ...) 537 void (*log)(const char *, ...)
526) 538)
diff --git a/src/nissy.h b/src/nissy.h
index 8f4f363..7a39927 100644
--- a/src/nissy.h
+++ b/src/nissy.h
@@ -281,6 +281,19 @@ long long nissy_solve(
281); 281);
282 282
283/* 283/*
284Parameters:
285 moves - The moves to be counted.
286
287Return values:
288 NISSY_ERROR_INVALID_MOVES - The given moves are invalid.
289 NISSY_ERROR_NULL_POINTER - The 'moves' argument is NULL.
290 Any value >= 0 - The number of moves.
291*/
292long long nissy_countmoves(
293 const char *moves
294);
295
296/*
284Set a global logger function used by this library. 297Set a global logger function used by this library.
285 298
286Parameters: 299Parameters:

Generated with cgit - Back to sebastiano.tronto.net