diff options
-rw-r--r-- | dartbot.c | 73 |
1 files changed, 71 insertions, 2 deletions
@@ -274,7 +274,8 @@ struct leg *leg_init(int points) void leg_free(struct leg *l) { for (int i = 0; i < l->n_visits; ++i) - free(l->visits[i].darts); + if (l->visits[i].darts) + free(l->visits[i].darts); free(l->visits); free(l); } @@ -366,6 +367,43 @@ void draw(WINDOW *w, struct leg *l) wrefresh(w); } +void user_visit(WINDOW *w, struct leg *l) +{ + struct visit *v = l->visits + l->n_visits++; + + char prompt[] = "Enter points: "; + WINDOW *iw = subwin(w, 1, COLS-4, LINES-2, 2); + waddstr(iw, prompt); + touchwin(w); + wrefresh(iw); + + char buf[100] = { 0 }; + int buflen = 0; + + int c; + while((c = wgetch(iw)) != 10) { + if (c == 127) { + if (buflen > 0) { + buf[--buflen] = 0; + wmove(iw, 0, sizeof(prompt) - 1 + buflen); + wclrtoeol(iw); + touchwin(w); + wrefresh(iw); + } + } else if (c >= (int)'0' && c <= (int)'9') { + buf[buflen++] = c; + wechochar(iw, c); + } + } + + delwin(iw); + + v->points = atoi(buf); + l->rem -= v->points; + v->rem = l->rem; +} + + void test_curses_match(int start_points) { initscr(); @@ -525,6 +563,36 @@ void test_curses_match2(int start_points) endwin(); } +void curses_match(int start_points) +{ + initscr(); + start_color(); + init_colours(); + curs_set(0); + noecho(); + + WINDOW *w = newwin(LINES, COLS, 0, 0); + + struct leg *l1 = leg_init(start_points); + struct leg *l2 = leg_init(start_points); + + draw2(w, l1, l2); + + while (l1->rem > 0 && l2->rem > 0) { + user_visit(w, l1); + if (l1->rem > 0) + test_visit(l2); + + draw2(w, l1, l2); + } + + leg_free(l1); + leg_free(l2); + wgetch(w); + delwin(w); + endwin(); +} + int main() { srand(time(NULL)); @@ -532,7 +600,8 @@ int main() init_centre_dists(); //test_curses_match(501); - test_curses_match2(501); + //test_curses_match2(501); + curses_match(501); test_match(501); return 0; |