diff options
author | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2022-04-09 04:16:36 -0700 |
---|---|---|
committer | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2022-04-09 04:16:36 -0700 |
commit | 5726615d498d9a7cef16151f0923b3115c847f3c (patch) | |
tree | 89224ba1a5ae5c8e689337fe776fa464c38cc51a | |
parent | 622336462dcc3b30f08bb989bd47934b3ecbcdeb (diff) | |
download | dartboat-5726615d498d9a7cef16151f0923b3115c847f3c.tar.gz dartboat-5726615d498d9a7cef16151f0923b3115c847f3c.tar.xz |
pass pointer to visit function for each player
This allows the program to act as a scoreboard between two players
without requiring extra logic.
-rw-r--r-- | dartbot.c | 87 |
1 files changed, 24 insertions, 63 deletions
@@ -297,7 +297,7 @@ void leg_free(struct leg *l) free(l); } -void test_visit(struct leg *l) +void comp_visit(__attribute__((unused)) WINDOW *w, struct leg *l) { struct visit *v = l->visits + l->n_visits++; v->darts = calloc(3, sizeof(*(v->darts))); @@ -329,7 +329,7 @@ void test_match(int start_points) struct leg *l = leg_init(start_points); while(l->rem > 0) - test_visit(l); + comp_visit(NULL, l); printf("(%2d) %3d\n", l->n_visits, l->start); for(int i = 0; i < l->n_visits; ++i) { @@ -420,32 +420,6 @@ void user_visit(WINDOW *w, struct leg *l) v->rem = l->rem; } - -void test_curses_match(int start_points) -{ - initscr(); - curs_set(0); - noecho(); - - WINDOW *w = newwin(LINES, COLS, 0, 0); - - struct leg *l = leg_init(start_points); - - draw(w, l); - - while (l->rem > 0) { - test_visit(l); - - usleep(250000); - draw(w, l); - } - - leg_free(l); - wgetch(w); - delwin(w); - endwin(); -} - void init_colours() { FOREACH_COLOUR(GEN_COLOUR_INIT_PAIR) @@ -482,7 +456,7 @@ void flushbuf(WINDOW *w, char *buf, int *buflen, int col) *buflen = 0; } -void draw2(WINDOW *w, struct leg *l1, struct leg *l2) +void curses_draw(WINDOW *w, struct leg *l1, struct leg *l2) { werase(w); box(w, 0, 0); @@ -542,7 +516,8 @@ void draw2(WINDOW *w, struct leg *l1, struct leg *l2) wrefresh(w); } -void test_curses_match2(int start_points) +void curses_match(int start_points, void (*f1)(WINDOW *, struct leg *), + void (*f2)(WINDOW *, struct leg *)) { initscr(); start_color(); @@ -555,15 +530,16 @@ void test_curses_match2(int start_points) struct leg *l1 = leg_init(start_points); struct leg *l2 = leg_init(start_points); - draw2(w, l1, l2); + curses_draw(w, l1, l2); while (l1->rem > 0 && l2->rem > 0) { - test_visit(l1); + (*f1)(w, l1); + curses_draw(w, l1, l2); + if (l1->rem > 0) - test_visit(l2); + (*f2)(w, l2); - usleep(250000); - draw2(w, l1, l2); + curses_draw(w, l1, l2); } leg_free(l1); @@ -573,34 +549,19 @@ void test_curses_match2(int start_points) endwin(); } -void curses_match(int start_points) +void cvc_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); + curses_match(start_points, comp_visit, comp_visit); +} - draw2(w, l1, l2); - } +void pvc_curses_match(int start_points) +{ + curses_match(start_points, user_visit, comp_visit); +} - leg_free(l1); - leg_free(l2); - wgetch(w); - delwin(w); - endwin(); +void pvp_curses_match(int start_points) +{ + curses_match(start_points, user_visit, user_visit); } int main() @@ -609,9 +570,9 @@ int main() init_sectors(); init_centre_dists(); - //test_curses_match(501); - //test_curses_match2(501); - curses_match(501); + //cvc_curses_match(501); + pvc_curses_match(501); + //pvp_curses_match(501); test_match(501); return 0; |