summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2022-04-09 01:17:51 -0700
committerDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2022-04-09 01:17:51 -0700
commit5574d28897f28a5be34cce74a95fd07eceb5fe9b (patch)
treecd76404497a488c9d91206cf86f945bf52cec2c4
parentda769df0a9613300c8e4ec8a2832c65e052d6e22 (diff)
downloaddartboat-5574d28897f28a5be34cce74a95fd07eceb5fe9b.tar.gz
dartboat-5574d28897f28a5be34cce74a95fd07eceb5fe9b.tar.xz
user can now play against computer
-rw-r--r--dartbot.c73
1 files changed, 71 insertions, 2 deletions
diff --git a/dartbot.c b/dartbot.c
index dd48d1b..8348d6c 100644
--- a/dartbot.c
+++ b/dartbot.c
@@ -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;