h48

A prototype for an optimal Rubik's cube solver, work in progress.
git clone https://git.tronto.net/h48
Download | Log | Files | Refs | README | LICENSE

commit c89c5a1832354d656757c5aff49018d0313838e0
parent 2ec81876eb59bb3e7c544774aa222988c1f574c4
Author: Sebastiano Tronto <sebastiano@tronto.net>
Date:   Tue, 11 Jun 2024 07:48:58 +0200

Cleaned up cube.h

Diffstat:
MTODO.txt | 6+++---
Msrc/cube.h | 18+++++++++++++-----
Msrc/cube_public.h | 111+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------
Msrc/io_cube.h | 4++--
4 files changed, 110 insertions(+), 29 deletions(-)

diff --git a/TODO.txt b/TODO.txt @@ -1,8 +1,7 @@ Refactoring: remove cube_fast_t and add b32 format - - see TODO comments in cube.h - fix utility code in utils/*.c - replace h48_to_lst with convert.c - fix invert.c + replace h48_to_lst with convert.c (use convert from cube.h) + fix invert.c (use inverse from cube.h) fix utils/*.sh scripts to use the new convert convert all utils/cubes/*.txt files in b32 format - switch to b32 by default @@ -20,6 +19,7 @@ Solver - write a solver (how many tricks? some, but not all are needed) More utilities for tables (in cube.h) + - cleanup solve and gendata in cube.h and cube_public.h - for tables, a "dryrun" function that only tells you the size needed - check hash of generated data diff --git a/src/cube.h b/src/cube.h @@ -1,6 +1,7 @@ /* -All the functions below return 0 in case of success and a positive number -in case of error. See (TODO: documentation) for details. +All the functions below return 0 in case of success and a positive +number in case of error, unless otherwise specified. See (TODO: +documentation) for details. */ int nissy_compose( @@ -43,13 +44,21 @@ int nissy_writecube( char *result ); -int nissy_gendata( +int nissy_convertcube( + const char *format_in, + const char *format_out, + const char *cube_string, + char *result +); + +/* Returns the number of bytes written, or -1 in case of error */ +int64_t nissy_gendata( const char *solver, const char *options, void *generated_data, - int64_t *generated_bytes ); +/* Returns the number of solutions found, or -1 in case of error */ int nissy_solve( const char cube[static 22], const char *solver, @@ -61,5 +70,4 @@ int nissy_solve( int8_t optimal, const void *data, char *solutions, - int *n_solutions ); diff --git a/src/cube_public.h b/src/cube_public.h @@ -1,13 +1,15 @@ -int check_error(cube_t, char [static 22]); +int write_result(cube_t, char [static 22]); int -check_error(cube_t cube, char result[static 22]) +write_result(cube_t cube, char result[static 22]) { if (!isconsistent(cube)) { writecube("B32", zero, result); return 2; } + writecube("B32", cube, result); + return issolvable(cube) ? 0 : 1; } @@ -22,54 +24,121 @@ nissy_compose( c = readcube("B32", cube); p = readcube("B32", permutation); - res = compose(c, p); - return check_error(res, result); + return write_result(res, result); } int nissy_inverse( const char cube[static 22], char result[static 22] -); +) +{ + cube_t c, res; + + c = readcube("B32", cube); + res = inverse(c); + + return write_result(res, result); +} -int nissy_applymoves( +int +nissy_applymoves( const char cube[static 22], const char *moves, char result[static 22] -); +) +{ + cube_t c, res; -int nissy_applytrans( + c = readcube("B32", cube); + res = applymoves(c, moves); + + return write_result(res, result); +} + +int +nissy_applytrans( const char cube[static 22], const char *transformation, char result[static 22] -); +) +{ + cube_t c, res; -int nissy_frommoves( + c = readcube("B32", cube); + res = applytrans(c, transformation); + + return write_result(res, result); +} + +int +nissy_frommoves( const char *moves, char result[static 22] -); +) +{ + cube_t res; + + res = applymoves(solved, moves); -int nissy_readcube( + return write_result(res, result); +} + +int +nissy_readcube( const char *format, const char *cube_string, char result[static 22] -); +) +{ + cube_t res; + + res = readcube(format, cube_string); + + return write_result(res, result); +} + +int +nissy_convertcube( + const char *format_in, + const char *format_out, + const char *cube_string, + char *result +) +{ + cube_t c; -int nissy_writecube( + c = readcube(format_in, cube_string); + writecube(format_out, c, result); + + return isconsistent(c) ? 0 : 2; +} + +int +nissy_writecube( const char *format, const char cube[static 22], char *result -); +) +{ + return nissy_convertcube("B32", format, cube, result); +} -int64_t nissy_gendata( +int64_t +nissy_gendata( const char *solver, const char *options, void *data -); +) +{ + /* TODO: move gendata here? */ + return gendata(solver, options, data); +} -int64_t nissy_solve( +int64_t +nissy_solve( const char cube[static 22], const char *solver, const char *options, @@ -80,4 +149,8 @@ int64_t nissy_solve( int8_t optimal, const void *data, char *solutions -); +) +{ + /* TODO: move solve_generic here? */ + return -1; +} diff --git a/src/io_cube.h b/src/io_cube.h @@ -60,7 +60,7 @@ writecube(const char *format, cube_t cube, char *buf) size_t len; if (!isconsistent(cube)) { - errormsg = "ERROR: cannot write inconsistent cube"; + errormsg = "ERROR: inconsistent"; goto writecube_error; } @@ -73,7 +73,7 @@ writecube(const char *format, cube_t cube, char *buf) } } - errormsg = "ERROR: cannot write cube in the given format"; + errormsg = "ERROR: format"; writecube_error: DBG_LOG("writecube error, see stdout for details\n");