commit 3d6c73c33276ecedba86cb871b4b5a089571629b
parent 4f0403d24067c1f69b21cd29c5f9e5949e64c635
Author: Sebastiano Tronto <sebastiano@tronto.net>
Date: Fri, 18 Oct 2024 12:21:51 +0200
New interface function nissy_countmoves
Diffstat:
4 files changed, 50 insertions(+), 3 deletions(-)
diff --git 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,13 +203,20 @@ 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]",
"If no COMMAND is specified, prints some generic information "
@@ -488,6 +496,19 @@ solve_scramble_exec(args_t *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)
{
int i;
diff --git 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
@@ -1,4 +1,5 @@
#include <inttypes.h>
+#include <limits.h>
#include <pthread.h>
#include <stdatomic.h>
#include <stdarg.h>
@@ -521,6 +522,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
@@ -281,6 +281,19 @@ long long nissy_solve(
);
/*
+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.
Parameters: