aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortslil <tslil@posteo.de>2020-12-29 19:33:00 -0500
committertslil <tslil@posteo.de>2026-08-28 19:37:41 +0100
commite57746530836b24b6d8d20e41239e955fa9e20d4 (patch)
tree1118f87c57de6cc3798f1b29a5cefb982084b379
parent4a8e687b14b50e3a2a4a3632f6553872f4c301d5 (diff)
Output PTN as well
-rw-r--r--include/ptn.c50
-rw-r--r--include/ptn.h10
-rw-r--r--src/ctaklm.c10
3 files changed, 65 insertions, 5 deletions
diff --git a/include/ptn.c b/include/ptn.c
index 1a333d7..66b4ad8 100644
--- a/include/ptn.c
+++ b/include/ptn.c
@@ -42,7 +42,7 @@ parse_move(const uint8_t board_size, char *ptn,
ASSERT_NONEMPTY;
- uint8_t picked_up = 0;
+ uint8_t picked_up = 1;
if ( (*ptn >= '1') && (*ptn <= '0' + board_size)) {
picked_up = *ptn - '0';
@@ -68,10 +68,10 @@ parse_move(const uint8_t board_size, char *ptn,
}
ptn++;
- // Handle the case '<column><row><direction>' as
+ // Handle the case '[1]<column><row><direction>' as
// '1<column><row><direction>1' for convenience
if (*ptn == 0) {
- if (picked_up == 0) {
+ if (picked_up == 1) {
*out_steps = 1;
out_drops[0] = 1;
} else {
@@ -94,7 +94,49 @@ parse_move(const uint8_t board_size, char *ptn,
ptn++;
}
- if ( (picked_up > 0) && (total != picked_up) ) return PTN_INVALID;
+ if ( total != picked_up ) return PTN_INVALID;
return PTN_VALID;
}
+
+void
+generate_place(const uint8_t board_size, const uint8_t in_location,
+ const enum STONE_VARIANT in_stone, char out_ptn[4]) {
+ switch (in_stone) {
+ case STONE_FLAT: { break; }
+ case STONE_STANDING: { *out_ptn = 'S'; out_ptn++; break; }
+ case STONE_CAPSTONE: { *out_ptn = 'C'; out_ptn++; break; }
+ }
+ *out_ptn = 'a' + (in_location % board_size); out_ptn++;
+ *out_ptn = '1' + (in_location / board_size); out_ptn++;
+ *out_ptn = 0;
+}
+
+void
+generate_move(const uint8_t board_size, const uint8_t in_location,
+ const enum MOVE_DIRECTION in_direction,
+ const uint8_t in_steps, const uint8_t in_drops[5],
+ char out_ptn[10]) {
+ uint8_t total = 0;
+ for (uint8_t k = 0; k<in_steps; k++) total+=in_drops[k];
+ if (total > 1) {
+ *out_ptn = '0' + total; out_ptn++;
+ }
+
+ *out_ptn = 'a' + (in_location % board_size); out_ptn++;
+ *out_ptn = '1' + (in_location / board_size); out_ptn++;
+
+ switch (in_direction) {
+ case M_UP: { *out_ptn = '+'; break; }
+ case M_DOWN: { *out_ptn = '-'; break; }
+ case M_LEFT: { *out_ptn = '<'; break; }
+ case M_RIGHT: { *out_ptn = '>'; break; }
+ }; out_ptn++;
+
+
+ for (uint8_t k = 0; (total > 1) && (k < in_steps); k++) {
+ *out_ptn = '0' + in_drops[k]; out_ptn++;
+ }
+
+ *out_ptn = 0;
+}
diff --git a/include/ptn.h b/include/ptn.h
index e780a70..0baed01 100644
--- a/include/ptn.h
+++ b/include/ptn.h
@@ -11,3 +11,13 @@ enum PTN_PARSE_RESULT
parse_move(const uint8_t board_size, char *ptn,
uint8_t *out_location, enum MOVE_DIRECTION *out_direction,
uint8_t *out_steps, uint8_t out_drops[5]);
+
+void
+generate_place(const uint8_t board_size, const uint8_t in_location,
+ const enum STONE_VARIANT in_stone, char out_ptn[4]);
+
+void
+generate_move(const uint8_t board_size, const uint8_t in_location,
+ const enum MOVE_DIRECTION in_direction,
+ const uint8_t in_steps, const uint8_t in_drops[5],
+ char out_ptn[10]);
diff --git a/src/ctaklm.c b/src/ctaklm.c
index f6ccfe8..d7fd7c1 100644
--- a/src/ctaklm.c
+++ b/src/ctaklm.c
@@ -20,9 +20,17 @@ int main(int argc, char **argv) {
enum MOVE_DIRECTION direction;
uint8_t steps;
uint8_t drops[5];
- pr = parse_move(5, "c2+", &location, &direction, &steps, drops);
+ pr = parse_move(5, "3c2+12", &location, &direction, &steps, drops);
printf("Move <%d>: from (%d,%d) step %d sequence [%d,%d,%d,%d,%d]\n",
pr,location%5,location/5,
steps,drops[0],drops[1],drops[2],drops[3],drops[4]);
+
+ char buf[10];
+ generate_move(5, 2+5*1, M_UP, 1, drops, buf);
+ printf("Generated move: %s\n",buf);
+
+ generate_place(5,1+5*2, STONE_CAPSTONE, buf);
+ printf("Generated place: %s\n",buf);
+
}