From 9e698917cb5b332cdbf1969ddfe3840239106c8d Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Thu, 5 Oct 2023 09:31:02 +0200 Subject: Implemented compose; some cleanup --- src/constants.h | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/cube.h | 51 +++++----------------------------------- src/cube_array.c | 27 +++++++++++++++++++++ src/cube_array.h | 6 +++++ 4 files changed, 110 insertions(+), 45 deletions(-) create mode 100644 src/constants.h create mode 100644 src/cube_array.h (limited to 'src') diff --git a/src/constants.h b/src/constants.h new file mode 100644 index 0000000..6a7e3f9 --- /dev/null +++ b/src/constants.h @@ -0,0 +1,71 @@ +/* Standard moves */ +#define U 0U +#define U2 1U +#define U3 2U +#define D 3U +#define D2 4U +#define D3 5U +#define R 6U +#define R2 7U +#define R3 8U +#define L 9U +#define L2 10U +#define L3 11U +#define F 12U +#define F2 13U +#define F3 14U +#define B 15U +#define B2 16U +#define B3 17U + +/* Regular transformations (rotations) */ +#define UF_i 0 +#define UL_i 1 +#define UB_i 2 +#define UR_i 3 +#define DF_i 4 +#define DL_i 5 +#define DB_i 6 +#define DR_i 7 +#define RU_i 8 +#define RF_i 9 +#define RD_i 10 +#define RB_i 11 +#define LU_i 12 +#define LF_i 13 +#define LD_i 14 +#define LB_i 15 +#define FU_i 16 +#define FR_i 16 +#define FD_i 18 +#define FL_i 19 +#define BU_i 20 +#define BR_i 21 +#define BD_i 22 +#define BL_i 23 + +/* Mirrored transformations */ +#define UF_m 24 +#define UL_m 25 +#define UB_m 26 +#define UR_m 27 +#define DF_m 28 +#define DL_m 29 +#define DB_m 30 +#define DR_m 31 +#define RU_m 32 +#define RF_m 33 +#define RD_m 34 +#define RB_m 35 +#define LU_m 36 +#define LF_m 37 +#define LD_m 38 +#define LB_m 39 +#define FU_m 40 +#define FR_m 41 +#define FD_m 42 +#define FL_m 43 +#define BU_m 44 +#define BR_m 45 +#define BD_m 46 +#define BL_m 47 diff --git a/src/cube.h b/src/cube.h index e5434e3..fcc76dc 100644 --- a/src/cube.h +++ b/src/cube.h @@ -1,52 +1,13 @@ -typedef enum { - U = 0, U2, U3, D, D2, D3, - R, R2, R3, L, L2, L3, - F, F2, F3, B, B2, B3 -} move_t; +/* TODO: ifdef for different implementations */ +#include "cube_array.h" -typedef enum { - UF_i = 0, UL_i, UB_i, UR_i, DF_i, DL_i, DB_i, DR_i, - UF_m, UL_m, UB_m, UR_m, DF_m, DL_m, DB_m, DR_m, - RU_i, RF_i, RD_i, RB_i, LU_i, LF_i, LD_i, LB_i, - RU_m, RF_m, RD_m, RB_m, LU_m, LF_m, LD_m, LB_m, - FU_i, FR_i, FD_i, FL_i, BU_i, BR_i, BD_i, BL_i, - FU_m, FR_m, FD_m, FL_m, BU_m, BR_m, BD_m, BL_m -} trans_t; - -typedef struct { - uint8_t c[8]; - uint8_t e[12]; -} cube_t; +/* For moves and transformations, see constants.h */ +typedef uint8_t move_t; +typedef uint8_t trans_t; extern cube_t solvedcube; -/* -The functions readcube() and writecube() use the following format. - -Each edge is represented by two letters denoting the sides it belongs to -and one number denoting its orientation (0 oriented, 1 mis-oriented). -Similarly, each corner is represented by three letters and a number -(0 oriented, 1 twisted clockwise, 2 twisted counter-clockwise). -Edge orientation is relative to the F / B axis, corner orientation is -relative to the U / D axis. - -The pieces are ordered such that the solved cube looks like this: - -UF0 UB0 DB0 DF0 UR0 UL0 DL0 DR0 FR0 FL0 BL0 BR0 -UFR0 UBL0 DFL0 DBR0 UFL0 UBR0 DFR0 DBL0 - -Whitespace (including newlines) between pieces is ignored when reading -the cube, and a single whitespace character is added between pieces -when writing. - -The cube after the moves R'U'F looks like this: - -FL1 BR0 DB0 UR1 UF0 UB0 DL0 FR0 UL1 DF1 BL0 DR0 -UBL1 DBR1 UFR2 DFR2 DFL2 UBL2 UFL2 DBL0 - -More formats might be supported in the future. -*/ - +/* For the textual representation of the cube, see utils/FORMAT.txt */ cube_t readcube(char *); void writecube(cube_t, char *); diff --git a/src/cube_array.c b/src/cube_array.c index a051610..b0b5e1e 100644 --- a/src/cube_array.c +++ b/src/cube_array.c @@ -25,6 +25,7 @@ The third bit is needed because x+y+1 can exceed 4. #include #endif +#include "constants.h" #include "cube.h" #define _c_ufr 0U @@ -713,6 +714,32 @@ inverse_inconsistent: cube_t compose(cube_t c1, cube_t c2) { + uint8_t i, piece, orien, aux, auy; + cube_t ret = {0}; + +#ifdef DEBUG + if (!isconsistent(c1) || !isconsistent(c2)) + goto compose_inconsistent; +#endif + + for (i = 0; i < 12; i++) { + piece = c2.e[i] & _pbits; + orien = (c2.e[i] ^ c1.e[piece]) & _eobit; + ret.e[i] = (c1.e[piece] & _pbits) | orien; + } + + for (i = 0; i < 8; i++) { + piece = c2.c[i] & _pbits; + aux = (c2.c[i] & _cobits) + (c1.c[piece] & _cobits); + auy = (aux + _ctwist_cw) >> 2U; + orien = (aux + auy) & _cobits2; + ret.c[i] = (c1.c[piece] & _pbits) | orien; + } + + return ret; + +compose_inconsistent: + fprintf(stderr, "compose error, inconsistent cube\n"); return errorcube; } diff --git a/src/cube_array.h b/src/cube_array.h new file mode 100644 index 0000000..6d9603b --- /dev/null +++ b/src/cube_array.h @@ -0,0 +1,6 @@ +/* Public properties specific to the array implementation */ + +typedef struct { + uint8_t c[8]; + uint8_t e[12]; +} cube_t; -- cgit v1.3