From 3d6c73c33276ecedba86cb871b4b5a089571629b Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Fri, 18 Oct 2024 12:21:51 +0200 Subject: New interface function nissy_countmoves --- shell/shell.c | 23 ++++++++++++++++++++++- src/core/moves.h | 5 +++-- src/nissy.c | 12 ++++++++++++ src/nissy.h | 13 +++++++++++++ 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 *); static int64_t gendata_exec(args_t *); static int64_t solve_exec(args_t *); static int64_t solve_scramble_exec(args_t *); +static int64_t countmoves_exec(args_t *); static int64_t help_exec(args_t *); static int parse_args(int, char **, args_t *); @@ -202,12 +203,19 @@ struct { "solve_scramble", "solve_scramble " FLAG_SOLVER " SOLVER" "[" FLAG_MINMOVES " n] [" FLAG_MAXMOVES " N] " - FLAG_CUBE " CUBE", + FLAG_MOVES " MOVES", "Solve the given SCRAMBLE using SOLVER, " "using at least n and at most N moves. " INFO_MOVESFORMAT, solve_scramble_exec ), + COMMAND( + "countmoves", + "countmoves " FLAG_MOVES " MOVES", + "Count the given MOVES in HTM. " + INFO_MOVESFORMAT, + countmoves_exec + ), COMMAND( "help", "help [" FLAG_COMMAND " COMMAND]", @@ -487,6 +495,19 @@ solve_scramble_exec(args_t *args) return solve_exec(args); } +static int64_t +countmoves_exec(args_t *args) +{ + long long count; + + count = nissy_countmoves(args->str_moves); + + if (count >= 0) + printf("%lld\n", count); + + return count >= 0 ? 0 : count; +} + static int64_t help_exec(args_t *args) { 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) uint8_t m; int c; - FOREACH_READMOVE(buf, m, c, max, -1, - ret[c] = m; + FOREACH_READMOVE(buf, m, c, max, NISSY_ERROR_INVALID_MOVES, + if (ret != NULL) + ret[c] = m; ) 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 @@ #include +#include #include #include #include @@ -520,6 +521,17 @@ nissy_solve( } } +long long +nissy_countmoves( + const char *moves +) +{ + if (moves == NULL) + return NISSY_ERROR_NULL_POINTER; + + return readmoves(moves, INT_MAX, NULL); +} + long long nissy_setlogger( void (*log)(const char *, ...) diff --git a/src/nissy.h b/src/nissy.h index 8f4f363..7a39927 100644 --- a/src/nissy.h +++ b/src/nissy.h @@ -280,6 +280,19 @@ long long nissy_solve( char sols[sols_size] ); +/* +Parameters: + moves - The moves to be counted. + +Return values: + NISSY_ERROR_INVALID_MOVES - The given moves are invalid. + NISSY_ERROR_NULL_POINTER - The 'moves' argument is NULL. + Any value >= 0 - The number of moves. +*/ +long long nissy_countmoves( + const char *moves +); + /* Set a global logger function used by this library. -- cgit v1.3