diff options
author | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2022-05-24 19:58:04 -0700 |
---|---|---|
committer | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2022-05-24 19:58:43 -0700 |
commit | 9a02c0367351c311734f42ab2afe2b377ca12908 (patch) | |
tree | 845b652771bb82a7ffaef39919dc56f08e2f207b /checkouts.c | |
parent | 14dd3ef4196af7c2269d80931dfcdcd1c3068857 (diff) | |
download | dartboat-9a02c0367351c311734f42ab2afe2b377ca12908.tar.gz dartboat-9a02c0367351c311734f42ab2afe2b377ca12908.tar.xz |
display 2-dart checkout if different than 3-dart checkout
Diffstat (limited to 'checkouts.c')
-rw-r--r-- | checkouts.c | 38 |
1 files changed, 29 insertions, 9 deletions
diff --git a/checkouts.c b/checkouts.c index a0bfaf2..0099aa5 100644 --- a/checkouts.c +++ b/checkouts.c @@ -4,25 +4,45 @@ #include <stdlib.h> #include <string.h> -char *checkouts_suggested(int rem) +int checkouts_suggested(char *buf, int rem, int darts_in_hand) { + buf[0] = 0; if (rem < 2 || rem > 170) - return NULL; + return 0; - char *str = NULL, *p = NULL; + char *p = buf; char *target; - int i = 3, trem = rem; + int i = darts_in_hand, trem = rem; while (i && trem && (target = CHECKOUTS[--i][trem - 1])) { - if (i == 2) - p = str = malloc(3 * SEGMENT_MAX_LEN); - else + if (i < darts_in_hand - 1) *p++ = '-'; p = stpcpy(p, target); - trem -= segment_points(segment_from_name(target)); } - return str; + return p - buf; +} + +#define CHECKOUT_2_PRE " (" +#define CHECKOUT_2_POST ")" +// buf must be at least 32 bytes +int checkouts_suggested_3_2(char *buf, int rem) +{ + int len = checkouts_suggested(buf, rem, 3); + if (!len) + return 0; + + char *buf2 = buf + len + sizeof(CHECKOUT_2_PRE) - 1; + int len2 = checkouts_suggested(buf2, rem, 2); + if (!len2 || + (len == len2 && !strncmp(buf, buf2, len > len2 ? len : len2))) + return len; + + memcpy(buf + len, CHECKOUT_2_PRE, sizeof(CHECKOUT_2_PRE) - 1); + memcpy(buf2 + len2, CHECKOUT_2_POST, sizeof(CHECKOUT_2_POST)); + len += sizeof(CHECKOUT_2_PRE) + len2 + sizeof(CHECKOUT_2_POST) - 2; + + return len; } char *CHECKOUTS[][170] = { |