diff options
Diffstat (limited to 'dartboat.c')
-rw-r--r-- | dartboat.c | 168 |
1 files changed, 168 insertions, 0 deletions
diff --git a/dartboat.c b/dartboat.c new file mode 100644 index 0000000..aa59630 --- /dev/null +++ b/dartboat.c @@ -0,0 +1,168 @@ +#include "board.h" +#include "checkouts.h" +#include "comp.h" +#include "curses.h" +#include "match.h" + +#include <ncurses.h> +#include <stdio.h> +#include <stdlib.h> +#include <time.h> + +void test_match(int start_points) +{ + struct leg *l = leg_init(start_points, NULL); + + while (l->rem > 0) + comp_visit(l); + + printf("(%2d) %3d\n", 0, l->start); + for (int i = 0; i < l->n_visits; ++i) { + struct visit *v = l->visits + i; + printf("(%2d) %3d %3d", i+1, v->points, v->rem); + + for (int j = 0; j < v->n_darts; ++j) { + char *n = segment_name(v->darts[j]); + printf(j == 0 ? " %4s" :" %4s", n); + free(n); + } + printf("\n"); + } + + leg_free(l); +} + +void user_visit(struct leg *l) +{ + if (l->n_visits == l->size_visits) + leg_grow_visits(l); + struct visit *v = l->visits + l->n_visits++; + + char status[100]; + int len = sprintf(status, " %s has %d remaining", l->name, l->rem); + if (l->rem <= 170) { + char *target = CHECKOUTS[2][l->rem-1]; + if (target) { + int trem = l->rem - segment_points(segment_from_name(target)); + len += sprintf(status + len, " (%s", target); + if (trem) { + target = CHECKOUTS[1][trem-1]; + len += sprintf(status + len, "-%s", target); + trem = trem - segment_points(segment_from_name(target)); + + if (trem) { + target = CHECKOUTS[0][trem-1]; + len += sprintf(status + len, "-%s", target); + } + } + len += sprintf(status + len, ")"); + } + } + char prompt[] = " enter points> "; + curses_status(status); + curses_prompt(prompt); + + char buf[100] = { 0 }; + int buflen = 0; + + int c; + while((c = wgetch(promptw)) != 10) { + if (c == 127) { + if (buflen > 0) { + buf[--buflen] = 0; + wmove(promptw, 0, sizeof(prompt) - 1 + buflen); + wclrtoeol(promptw); + wrefresh(promptw); + } + } else if (c >= '0' && c <= '9') { + buf[buflen++] = c; + wechochar(promptw, c); + } + } + + curses_prompt(""); + v->points = atoi(buf); + l->rem -= v->points; + v->rem = l->rem; +} + +void curses_match(int start_points, char *n1, void (*f1)(struct leg *), + char *n2, void (*f2)(struct leg *)) +{ + init_curses(); + + struct leg *l1 = leg_init(start_points, n1); + struct leg *l2 = leg_init(start_points, n2); + + curses_draw(l1, l2); + + while (l1->rem > 0 && l2->rem > 0) { + (*f1)(l1); + curses_draw(l1, l2); + + if (l1->rem > 0) + (*f2)(l2); + + curses_draw(l1, l2); + } + + char status[100]; + sprintf(status, " %s wins", l1->rem <= 0 ? l1->name : l2->name); + curses_status(status); + + leg_free(l1); + leg_free(l2); + wgetch(w); + free_curses(); +} + +void cvc_curses_match(int start_points) +{ + curses_match(start_points, "Dartboat 1", comp_visit, "Dartboat 2", + comp_visit); +} + +void pvc_curses_match(int start_points) +{ + curses_match(start_points, "David", user_visit, "Dartboat", comp_visit); +} + +void pvp_curses_match(int start_points) +{ + curses_match(start_points, "David", user_visit, "Davidn't", user_visit); +} + +void test_averages() +{ + int rounds = 100000; + + for (int stdev = 4; stdev < 25; ++stdev) { + horizontal_stdev = vertical_stdev = stdev; + + int darts = 0; + for (int i = 0; i < rounds; ++i) { + struct leg *l = leg_init(501, NULL); + while (l->rem > 0) + comp_visit(l); + leg_free(l); + + darts += (l->n_visits-1)*3 + l->visits[l->n_visits-1].n_darts; + } + + printf("%d %f\n", stdev, (double)(501*rounds)/darts*3); + } +} + +int main() +{ + srand(time(NULL)); + init_board(); + + //cvc_curses_match(501); + pvc_curses_match(501); + //pvp_curses_match(501); + test_match(501); + //test_averages(); + + return 0; +} |