summaryrefslogtreecommitdiff
path: root/match.c
blob: d8f8a23e95d3132acc9251bc336ba9526786643e (plain) (blame)
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
#include "match.h"

#include <stdlib.h>
#include <string.h>

struct leg *leg_init(int points, char *name)
{
	struct leg *l = calloc(1, sizeof(*l));
	l->name = name;
	l->start = l->rem = points;
	l->size_visits = 16;
	l->visits = calloc(l->size_visits, sizeof(*(l->visits)));

	return l;
}

void leg_free(struct leg *l)
{
	for (int i = 0; i < l->n_visits; ++i) {
		if (l->visits[i].darts)
			free(l->visits[i].darts);
		if (l->visits[i].ccoords)
			free(l->visits[i].ccoords);
	}
	free(l->visits);
	free(l);
}

void leg_grow_visits(struct leg *l)
{

	size_t bytes = l->size_visits * sizeof(*(l->visits));
	l->size_visits *= 2;
	l->visits = realloc(l->visits, 2 * bytes);
	memset((char *)l->visits + bytes, 0, bytes);
}

bool is_points_valid(int pts, int rem)
{
	return pts <= rem &&
		rem - pts != 1 &&
		pts >= 0 &&
		pts <= 180 &&
		pts != 179 &&
		pts != 178 &&
		pts != 176 &&
		pts != 175 &&
		pts != 173 &&
		pts != 172 &&
		pts != 169 &&
		pts != 166 &&
		pts != 163 &&
		(rem - pts != 0 ||
			(pts <= 170 &&
				pts != 168 &&
				pts != 165 &&
				pts != 162 &&
				pts != 159));
}