aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2024-04-13 15:57:23 +0200
committerSebastiano Tronto <sebastiano@tronto.net>2024-04-13 15:57:23 +0200
commita4598b740fe4b8350b7d09a575c793113c8fde2b (patch)
tree157cdb108564389ecdf1badae2501f890fc2a126
parent3ab2f98170908277f70191846e84e5dde185a056 (diff)
downloadcubecore-a4598b740fe4b8350b7d09a575c793113c8fde2b.tar.gz
cubecore-a4598b740fe4b8350b7d09a575c793113c8fde2b.zip
Some renaming, moved stuff around
-rw-r--r--README.md56
-rw-r--r--cube.c36
-rw-r--r--cube.h60
-rw-r--r--test/020_io_H48_read_write/io_H48_tests.c4
-rw-r--r--test/023_io_LST_write/io_LST_tests.c4
-rw-r--r--test/024_io_LST_read/io_LST_tests.c4
-rw-r--r--test/030_move/move_tests.c4
-rw-r--r--test/040_inverse_cube/inverse_tests.c4
-rw-r--r--test/050_compose/compose_tests.c6
-rw-r--r--test/060_transform/transform_tests.c4
-rw-r--r--test/071_coord_eo/coord_eo_tests.c2
-rw-r--r--test/072_coord_co/coord_co_tests.c2
-rw-r--r--utils/h48_to_lst.c4
-rw-r--r--utils/invert.c4
14 files changed, 97 insertions, 97 deletions
diff --git a/README.md b/README.md
index b49cefd..041b8d3 100644
--- a/README.md
+++ b/README.md
@@ -3,3 +3,59 @@
3A simple set of basic routines for working with a 3x3x3 Rubik's Cube. 3A simple set of basic routines for working with a 3x3x3 Rubik's Cube.
4 4
5Work in progress. 5Work in progress.
6
7## How to use CubeCore
8
9TODO
10
11## The cube
12
13Each piece is represented by an (unsigned) 8-bit integer. The 4
14least-significant bits determine which piece it is, the other 4 determine
15the orientation.
16
17Edges are numbered as follows (see also cube.c):
18UF=0 UB=1 DB=2 DF=3 UR=4 UL=5 DL=6 DR=7 FR=8 FL=9 BL=10 BR=11
19
20Corners are numbered as follows:
21UFR=0 UBL=1 DFL=2 DBR=3 UFL=4 UBR=5 DFR=6 DBL=7
22
23The orientation of the edges is with respect to F/B, the orientation of
24corners is with respect to U/D.
25
26The permutation of the center pieces is not stored. This means that the
27cube is assumed to be in a fixed orientation.
28
29## I/O format
30
31Reading and writing is not done directly via stdin / stdout, but via an
32array of char (called buf in the prototypes below).
33
34Multiple representations of the cube as text are supported:
35
36- H48: a human-readable format.
37 Each edge is represented by two letters denoting the sides it
38 belongs to and one number denoting its orientation (0 oriented, 1
39 mis-oriented). Similarly, each corner is represented by three letters and
40 a number (0 oriented, 1 twisted clockwise, 2 twisted counter-clockwise).
41
42 The solved cube looks like this:
43
44 UF0 UB0 DB0 DF0 UR0 UL0 DL0 DR0 FR0 FL0 BL0 BR0
45 UFR0 UBL0 DFL0 DBR0 UFL0 UBR0 DFR0 DBL0
46
47 The cube after the moves R'U'F looks like this:
48
49 FL1 BR0 DB0 UR1 UF0 UB0 DL0 FR0 UL1 DF1 BL0 DR0
50 UBL1 DBR1 UFR2 DFR2 DFL2 UBL2 UFL2 DBL0
51
52 Whitespace (including newlines) between pieces is ignored when reading the
53 cube. A single whitespace character is added between pieces when writing.
54
55- SRC: format used to generate code for internal use.
56 If OUT is the output in SRC format, one can use `cube_t cube = OUT` to
57 declare a new cube object.
58
59- LST: a format for internal use and generating code.
60 The cube is printed as a comma-separated list of 20 integers, as they appear
61 in cube_t. Corners come first, followed by edge (unlike H48).
diff --git a/cube.c b/cube.c
index f04d27d..ba65580 100644
--- a/cube.c
+++ b/cube.c
@@ -35,12 +35,12 @@ _static uint8_t readco(char *);
35_static uint8_t readcp(char *); 35_static uint8_t readcp(char *);
36_static uint8_t readeo(char *); 36_static uint8_t readeo(char *);
37_static uint8_t readep(char *); 37_static uint8_t readep(char *);
38_static cube_t readcube_H48(char *); 38_static cube_t read_H48(char *);
39_static uint8_t readpiece_LST(char **); 39_static uint8_t readpiece_LST(char **);
40_static cube_t readcube_LST(char *); 40_static cube_t read_LST(char *);
41_static int writepiece_LST(uint8_t, char *); 41_static int writepiece_LST(uint8_t, char *);
42_static void writecube_H48(cube_t, char *); 42_static void write_H48(cube_t, char *);
43_static void writecube_LST(cube_t, char *); 43_static void write_LST(cube_t, char *);
44_static uint8_t readmove(char); 44_static uint8_t readmove(char);
45_static uint8_t readmodifier(char); 45_static uint8_t readmodifier(char);
46_static uint8_t readtrans(char *); 46_static uint8_t readtrans(char *);
@@ -322,14 +322,14 @@ cube_coord_eo(cube_t c)
322} 322}
323 323
324cube_t 324cube_t
325readcube(char *format, char *buf) 325cube_read(char *format, char *buf)
326{ 326{
327 cube_t cube; 327 cube_t cube;
328 328
329 if (!strcmp(format, "H48")) { 329 if (!strcmp(format, "H48")) {
330 cube = readcube_H48(buf); 330 cube = read_H48(buf);
331 } else if (!strcmp(format, "LST")) { 331 } else if (!strcmp(format, "LST")) {
332 cube = readcube_LST(buf); 332 cube = read_LST(buf);
333 } else { 333 } else {
334 DBG_LOG("Cannot read cube in the given format\n"); 334 DBG_LOG("Cannot read cube in the given format\n");
335 cube = zero; 335 cube = zero;
@@ -339,29 +339,29 @@ readcube(char *format, char *buf)
339} 339}
340 340
341void 341void
342writecube(char *format, cube_t cube, char *buf) 342cube_write(char *format, cube_t cube, char *buf)
343{ 343{
344 char *errormsg; 344 char *errormsg;
345 size_t len; 345 size_t len;
346 346
347 if (!cube_consistent(cube)) { 347 if (!cube_consistent(cube)) {
348 errormsg = "ERROR: cannot write inconsistent cube"; 348 errormsg = "ERROR: cannot write inconsistent cube";
349 goto writecube_error; 349 goto write_error;
350 } 350 }
351 351
352 if (!strcmp(format, "H48")) { 352 if (!strcmp(format, "H48")) {
353 writecube_H48(cube, buf); 353 write_H48(cube, buf);
354 } else if (!strcmp(format, "LST")) { 354 } else if (!strcmp(format, "LST")) {
355 writecube_LST(cube, buf); 355 write_LST(cube, buf);
356 } else { 356 } else {
357 errormsg = "ERROR: cannot write cube in the given format"; 357 errormsg = "ERROR: cannot write cube in the given format";
358 goto writecube_error; 358 goto write_error;
359 } 359 }
360 360
361 return; 361 return;
362 362
363writecube_error: 363write_error:
364 DBG_LOG("writecube error, see stdout for details\n"); 364 DBG_LOG("cube_write error, see stdout for details\n");
365 len = strlen(errormsg); 365 len = strlen(errormsg);
366 memcpy(buf, errormsg, len); 366 memcpy(buf, errormsg, len);
367 buf[len] = '\n'; 367 buf[len] = '\n';
@@ -435,7 +435,7 @@ readep(char *str)
435} 435}
436 436
437_static cube_t 437_static cube_t
438readcube_H48(char *buf) 438read_H48(char *buf)
439{ 439{
440 int i; 440 int i;
441 uint8_t piece, orient; 441 uint8_t piece, orient;
@@ -488,7 +488,7 @@ readpiece_LST(char **b)
488} 488}
489 489
490_static cube_t 490_static cube_t
491readcube_LST(char *buf) 491read_LST(char *buf)
492{ 492{
493 int i; 493 int i;
494 cube_t ret = {0}; 494 cube_t ret = {0};
@@ -526,7 +526,7 @@ writepiece_LST(uint8_t piece, char *buf)
526} 526}
527 527
528_static void 528_static void
529writecube_H48(cube_t cube, char *buf) 529write_H48(cube_t cube, char *buf)
530{ 530{
531 uint8_t piece, perm, orient; 531 uint8_t piece, perm, orient;
532 int i; 532 int i;
@@ -555,7 +555,7 @@ writecube_H48(cube_t cube, char *buf)
555} 555}
556 556
557_static void 557_static void
558writecube_LST(cube_t cube, char *buf) 558write_LST(cube_t cube, char *buf)
559{ 559{
560 int i, ptr; 560 int i, ptr;
561 uint8_t piece; 561 uint8_t piece;
diff --git a/cube.h b/cube.h
index 77c6c21..5ef23a8 100644
--- a/cube.h
+++ b/cube.h
@@ -1,23 +1,3 @@
1/******************************************************************************
2Cube type definition
3
4Each piece is represented by an (unsigned) 8-bit integer. The 4
5least-significant bits determine which piece it is, the other 4 determine
6the orientation.
7
8Edges are numbered as follows (see also cube.c):
9UF=0 UB=1 DB=2 DF=3 UR=4 UL=5 DL=6 DR=7 FR=8 FL=9 BL=10 BR=11
10
11Corners are numbered as follows:
12UFR=0 UBL=1 DFL=2 DBR=3 UFL=4 UBR=5 DFR=6 DBL=7
13
14The orientation of the edges is with respect to F/B, the orientation of
15corners is with respect to U/D.
16
17The permutation of the center pieces is not stored. This means that the
18cube is assumed to be in a fixed orientation.
19******************************************************************************/
20
21typedef struct { 1typedef struct {
22 uint8_t corner[8]; 2 uint8_t corner[8];
23 uint8_t edge[12]; 3 uint8_t edge[12];
@@ -36,41 +16,5 @@ cube_t cube_inverse(cube_t);
36int64_t cube_coord_co(cube_t); 16int64_t cube_coord_co(cube_t);
37int64_t cube_coord_eo(cube_t); 17int64_t cube_coord_eo(cube_t);
38 18
39/****************************************************************************** 19cube_t cube_read(char *format, char *buf);
40Read / write utilities 20void cube_write(char *format, cube_t cube, char *buf);
41
42Reading and writing is not done directly via stdin / stdout, but via an
43array of char (called buf in the prototypes below).
44
45Multiple representations of the cube as text are supported:
46
47- H48: a human-readable format.
48 Each edge is represented by two letters denoting the sides it
49 belongs to and one number denoting its orientation (0 oriented, 1
50 mis-oriented). Similarly, each corner is represented by three letters and
51 a number (0 oriented, 1 twisted clockwise, 2 twisted counter-clockwise).
52
53 The solved cube looks like this:
54
55 UF0 UB0 DB0 DF0 UR0 UL0 DL0 DR0 FR0 FL0 BL0 BR0
56 UFR0 UBL0 DFL0 DBR0 UFL0 UBR0 DFR0 DBL0
57
58 The cube after the moves R'U'F looks like this:
59
60 FL1 BR0 DB0 UR1 UF0 UB0 DL0 FR0 UL1 DF1 BL0 DR0
61 UBL1 DBR1 UFR2 DFR2 DFL2 UBL2 UFL2 DBL0
62
63 Whitespace (including newlines) between pieces is ignored when reading the
64 cube. A single whitespace character is added between pieces when writing.
65
66- SRC: format used to generate code for internal use.
67 If OUT is the output in SRC format, one can use `cube_t cube = OUT` to
68 declare a new cube object.
69
70- LST: a format for internal use and generating code.
71 The cube is printed as a comma-separated list of 20 integers, as they appear
72 in cube_t. Corners come first, followed by edge (unlike H48).
73******************************************************************************/
74
75cube_t readcube(char *format, char *buf);
76void writecube(char *format, cube_t cube, char *buf);
diff --git a/test/020_io_H48_read_write/io_H48_tests.c b/test/020_io_H48_read_write/io_H48_tests.c
index 222e4d4..db471d8 100644
--- a/test/020_io_H48_read_write/io_H48_tests.c
+++ b/test/020_io_H48_read_write/io_H48_tests.c
@@ -9,14 +9,14 @@ int main(void) {
9 while (*aux != '\n') 9 while (*aux != '\n')
10 aux++; 10 aux++;
11 11
12 cube = readcube("H48", str); 12 cube = cube_read("H48", str);
13 13
14 if (cube_error(cube)) { 14 if (cube_error(cube)) {
15 printf("Error reading cube\n"); 15 printf("Error reading cube\n");
16 } else if (!cube_solvable(cube)) { 16 } else if (!cube_solvable(cube)) {
17 printf("Cube is not solvable\n"); 17 printf("Cube is not solvable\n");
18 } else { 18 } else {
19 writecube("H48", cube, str); 19 cube_write("H48", cube, str);
20 printf("%s\n", str); 20 printf("%s\n", str);
21 } 21 }
22 22
diff --git a/test/023_io_LST_write/io_LST_tests.c b/test/023_io_LST_write/io_LST_tests.c
index 8a3ce22..1fa7531 100644
--- a/test/023_io_LST_write/io_LST_tests.c
+++ b/test/023_io_LST_write/io_LST_tests.c
@@ -9,14 +9,14 @@ int main(void) {
9 while (*aux != '\n') 9 while (*aux != '\n')
10 aux++; 10 aux++;
11 11
12 cube = readcube("H48", str); 12 cube = cube_read("H48", str);
13 13
14 if (cube_error(cube)) { 14 if (cube_error(cube)) {
15 printf("Error reading cube\n"); 15 printf("Error reading cube\n");
16 } else if (!cube_solvable(cube)) { 16 } else if (!cube_solvable(cube)) {
17 printf("Cube is not solvable\n"); 17 printf("Cube is not solvable\n");
18 } else { 18 } else {
19 writecube("LST", cube, str); 19 cube_write("LST", cube, str);
20 printf("%s\n", str); 20 printf("%s\n", str);
21 } 21 }
22 22
diff --git a/test/024_io_LST_read/io_LST_tests.c b/test/024_io_LST_read/io_LST_tests.c
index 81d4c54..59cf89a 100644
--- a/test/024_io_LST_read/io_LST_tests.c
+++ b/test/024_io_LST_read/io_LST_tests.c
@@ -9,14 +9,14 @@ int main(void) {
9 while (*aux != '\n') 9 while (*aux != '\n')
10 aux++; 10 aux++;
11 11
12 cube = readcube("LST", str); 12 cube = cube_read("LST", str);
13 13
14 if (cube_error(cube)) { 14 if (cube_error(cube)) {
15 printf("Error reading cube\n"); 15 printf("Error reading cube\n");
16 } else if (!cube_solvable(cube)) { 16 } else if (!cube_solvable(cube)) {
17 printf("Cube is not solvable\n"); 17 printf("Cube is not solvable\n");
18 } else { 18 } else {
19 writecube("H48", cube, str); 19 cube_write("H48", cube, str);
20 printf("%s\n", str); 20 printf("%s\n", str);
21 } 21 }
22 22
diff --git a/test/030_move/move_tests.c b/test/030_move/move_tests.c
index 39458ba..497ac6c 100644
--- a/test/030_move/move_tests.c
+++ b/test/030_move/move_tests.c
@@ -8,7 +8,7 @@ int main(void) {
8 8
9 fgets(movestr, STRLENMAX, stdin); 9 fgets(movestr, STRLENMAX, stdin);
10 fgets(cubestr, STRLENMAX, stdin); 10 fgets(cubestr, STRLENMAX, stdin);
11 cube = readcube("H48", cubestr); 11 cube = cube_read("H48", cubestr);
12 12
13 cube = applymoves(cube, movestr); 13 cube = applymoves(cube, movestr);
14 14
@@ -17,7 +17,7 @@ int main(void) {
17 } else if (!cube_solvable(cube)) { 17 } else if (!cube_solvable(cube)) {
18 printf("Moved cube is not solvable\n"); 18 printf("Moved cube is not solvable\n");
19 } else { 19 } else {
20 writecube("H48", cube, cubestr); 20 cube_write("H48", cube, cubestr);
21 printf("%s\n", cubestr); 21 printf("%s\n", cubestr);
22 } 22 }
23 23
diff --git a/test/040_inverse_cube/inverse_tests.c b/test/040_inverse_cube/inverse_tests.c
index ca7e481..59ce84e 100644
--- a/test/040_inverse_cube/inverse_tests.c
+++ b/test/040_inverse_cube/inverse_tests.c
@@ -5,7 +5,7 @@ int main(void) {
5 cube_t cube, inv; 5 cube_t cube, inv;
6 6
7 fgets(str, STRLENMAX, stdin); 7 fgets(str, STRLENMAX, stdin);
8 cube = readcube("H48", str); 8 cube = cube_read("H48", str);
9 inv = cube_inverse(cube); 9 inv = cube_inverse(cube);
10 10
11 if (cube_error(inv)) { 11 if (cube_error(inv)) {
@@ -13,7 +13,7 @@ int main(void) {
13 } else if (!cube_solvable(inv)) { 13 } else if (!cube_solvable(inv)) {
14 printf("Inverted cube is not solvable\n"); 14 printf("Inverted cube is not solvable\n");
15 } else { 15 } else {
16 writecube("H48", inv, str); 16 cube_write("H48", inv, str);
17 printf("%s\n", str); 17 printf("%s\n", str);
18 } 18 }
19 19
diff --git a/test/050_compose/compose_tests.c b/test/050_compose/compose_tests.c
index 3bf6c34..5383733 100644
--- a/test/050_compose/compose_tests.c
+++ b/test/050_compose/compose_tests.c
@@ -5,9 +5,9 @@ int main(void) {
5 cube_t c1, c2, c3; 5 cube_t c1, c2, c3;
6 6
7 fgets(str, STRLENMAX, stdin); 7 fgets(str, STRLENMAX, stdin);
8 c1 = readcube("H48", str); 8 c1 = cube_read("H48", str);
9 fgets(str, STRLENMAX, stdin); 9 fgets(str, STRLENMAX, stdin);
10 c2 = readcube("H48", str); 10 c2 = cube_read("H48", str);
11 11
12 c3 = cube_compose(c1, c2); 12 c3 = cube_compose(c1, c2);
13 13
@@ -16,7 +16,7 @@ int main(void) {
16 } else if (!cube_solvable(c3)) { 16 } else if (!cube_solvable(c3)) {
17 printf("Composed cube is not solvable\n"); 17 printf("Composed cube is not solvable\n");
18 } else { 18 } else {
19 writecube("H48", c3, str); 19 cube_write("H48", c3, str);
20 printf("%s\n", str); 20 printf("%s\n", str);
21 } 21 }
22 22
diff --git a/test/060_transform/transform_tests.c b/test/060_transform/transform_tests.c
index 8680a0b..454bc22 100644
--- a/test/060_transform/transform_tests.c
+++ b/test/060_transform/transform_tests.c
@@ -8,7 +8,7 @@ int main(void) {
8 8
9 fgets(transtr, STRLENMAX, stdin); 9 fgets(transtr, STRLENMAX, stdin);
10 fgets(cubestr, STRLENMAX, stdin); 10 fgets(cubestr, STRLENMAX, stdin);
11 cube = readcube("H48", cubestr); 11 cube = cube_read("H48", cubestr);
12 12
13 cube = applytrans(cube, transtr); 13 cube = applytrans(cube, transtr);
14 14
@@ -17,7 +17,7 @@ int main(void) {
17 } else if (!cube_solvable(cube)) { 17 } else if (!cube_solvable(cube)) {
18 printf("Transformed cube is not solvable\n"); 18 printf("Transformed cube is not solvable\n");
19 } else { 19 } else {
20 writecube("H48", cube, cubestr); 20 cube_write("H48", cube, cubestr);
21 printf("%s\n", cubestr); 21 printf("%s\n", cubestr);
22 } 22 }
23 23
diff --git a/test/071_coord_eo/coord_eo_tests.c b/test/071_coord_eo/coord_eo_tests.c
index 5951042..a0b868a 100644
--- a/test/071_coord_eo/coord_eo_tests.c
+++ b/test/071_coord_eo/coord_eo_tests.c
@@ -6,7 +6,7 @@ int main(void) {
6 int64_t result; 6 int64_t result;
7 7
8 fgets(str, STRLENMAX, stdin); 8 fgets(str, STRLENMAX, stdin);
9 cube = readcube("H48", str); 9 cube = cube_read("H48", str);
10 10
11 result = cube_coord_eo(cube); 11 result = cube_coord_eo(cube);
12 12
diff --git a/test/072_coord_co/coord_co_tests.c b/test/072_coord_co/coord_co_tests.c
index aa4136b..2924996 100644
--- a/test/072_coord_co/coord_co_tests.c
+++ b/test/072_coord_co/coord_co_tests.c
@@ -6,7 +6,7 @@ int main(void) {
6 int64_t result; 6 int64_t result;
7 7
8 fgets(str, STRLENMAX, stdin); 8 fgets(str, STRLENMAX, stdin);
9 cube = readcube("H48", str); 9 cube = cube_read("H48", str);
10 10
11 result = cube_coord_co(cube); 11 result = cube_coord_co(cube);
12 12
diff --git a/utils/h48_to_lst.c b/utils/h48_to_lst.c
index 21615d5..e51628a 100644
--- a/utils/h48_to_lst.c
+++ b/utils/h48_to_lst.c
@@ -11,8 +11,8 @@ int main(void) {
11 cube_t cube; 11 cube_t cube;
12 12
13 fgets(str, STRLENMAX, stdin); 13 fgets(str, STRLENMAX, stdin);
14 cube = readcube("H48", str); 14 cube = cube_read("H48", str);
15 writecube("LST", cube, str); 15 write_cube("LST", cube, str);
16 fputs(str, stdout); 16 fputs(str, stdout);
17 17
18 return 0; 18 return 0;
diff --git a/utils/invert.c b/utils/invert.c
index ff01bee..c68b1c4 100644
--- a/utils/invert.c
+++ b/utils/invert.c
@@ -11,8 +11,8 @@ int main(void) {
11 cube_t cube; 11 cube_t cube;
12 12
13 fgets(str, STRLENMAX, stdin); 13 fgets(str, STRLENMAX, stdin);
14 cube = readcube("H48", str); 14 cube = cube_read("H48", str);
15 writecube("H48", inverse(cube), str); 15 cube_write("H48", cube_inverse(cube), str);
16 fputs(str, stdout); 16 fputs(str, stdout);
17 17
18 return 0; 18 return 0;

Generated with cgit - Back to sebastiano.tronto.net