summaryrefslogtreecommitdiff
path: root/checkouts.c
diff options
context:
space:
mode:
Diffstat (limited to 'checkouts.c')
-rw-r--r--checkouts.c38
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] = {