1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
#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)
{
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, "Dartbot 1", comp_visit, "Dartbot 2", comp_visit);
}
void pvc_curses_match(int start_points)
{
curses_match(start_points, "David", user_visit, "Dartbot", comp_visit);
}
void pvp_curses_match(int start_points)
{
curses_match(start_points, "David", user_visit, "Davidn't", user_visit);
}
int main()
{
srand(time(NULL));
init_board();
//cvc_curses_match(501);
pvc_curses_match(501);
//pvp_curses_match(501);
test_match(501);
return 0;
}
|