From 9a02c0367351c311734f42ab2afe2b377ca12908 Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Tue, 24 May 2022 19:58:04 -0700 Subject: display 2-dart checkout if different than 3-dart checkout --- checkouts.c | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) (limited to 'checkouts.c') diff --git a/checkouts.c b/checkouts.c index a0bfaf2..0099aa5 100644 --- a/checkouts.c +++ b/checkouts.c @@ -4,25 +4,45 @@ #include #include -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] = { -- cgit v1.2.3-70-g09d2