#include "board.h" #include "checkouts.h" #include "comp.h" #include "curses.h" #include "match.h" #include #include #include #include #include void test_match(int start_points) { struct leg *l = leg_init(start_points); 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) printf(j == 0 ? " %4s" : " %4s", segment_name(v->darts[j])); printf("\n"); } leg_free(l); } void user_visit(struct leg *l, char *name) { 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", name, l->rem); int len_co = checkouts_suggested(status + len + 2, l->rem, 3); if (len_co) { memcpy(status + len, " (", 2); memcpy(status + len + 2 + len_co, ")", 2); } 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; } static void player_visit(struct match *m, int pn) { if (m->players[pn - 1].type == PT_USER) user_visit(m->legs[pn - 1], m->players[pn - 1].name); else comp_visit(m->legs[pn - 1]); } void curses_match(struct match *m) { init_curses(); struct leg *l1 = m->legs[0]; struct leg *l2 = m->legs[1]; curses_draw(l1, l2); while (l1->rem > 0 && l2->rem > 0) { player_visit(m, 1); curses_draw(l1, l2); if (l1->rem > 0) { player_visit(m, 2); curses_draw(l1, l2); } } char status[100]; sprintf(status, " %s wins", m->players[match_winning_player(m) - 1].name); curses_status(status); wgetch(w); free_curses(); } void cvc_curses_match(int start_points) { struct match *m = match_init(); match_add_player(m, PT_COMP, "Computer 1", start_points); match_add_player(m, PT_COMP, "Computer 2", start_points); curses_match(m); match_free(m); } void pvc_curses_match(int start_points) { struct match *m = match_init(); match_add_player(m, PT_USER, "Player", start_points); match_add_player(m, PT_COMP, "Computer", start_points); curses_match(m); match_free(m); } void pvp_curses_match(int start_points) { struct match *m = match_init(); match_add_player(m, PT_USER, "Player 1", start_points); match_add_player(m, PT_USER, "Player 2", start_points); curses_match(m); match_free(m); } 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); 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; }