summaryrefslogtreecommitdiff
path: root/web/web_match.c
blob: aabea4dee9dee84cece8662e039a19a9f96960a8 (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
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
#include "web_match.h"

#include "match.h"

#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>

struct match_state *state;
struct match_opts *match_opts;

void free_state()
{
	state->legs[1]->n_visits += state->boat_undone; // avoid memory leak
	leg_free(state->legs[0]);
	leg_free(state->legs[1]);
	free(state);
	state = NULL;
}

void match_opts_free()
{
	free(match_opts);
	match_opts = NULL;
}

struct leg *state_active_leg()
{
	return state->legs[state->active_player - 1];
}

int match_num_players()
{
	if (state->mode == M_P)
		return 1;

	return 2;
}

int match_winning_player()
{
	for (int i = 0; i < match_num_players(); ++i) {
		if (state->legs[i]->rem == 0)
			return i + 1;
	}

	return -1;
}

bool match_is_over()
{
	return match_winning_player() != -1;
}

int match_prev_player()
{
	if (state->active_player == 1)
		return match_num_players();
	else
		return state->active_player - 1;
}

int match_next_player()
{
	if (state->active_player == match_num_players())
		return 1;
	else
		return state->active_player + 1;
}

int match_prev_throw_player()
{
	if (match_is_over())
		return state->active_player;

	return match_prev_player();
}

bool match_player_is_comp(int pn)
{
	return state->mode == M_PVC && pn == 2;
}

bool match_first_user_has_thrown()
{
	for (int i = 0, np = match_num_players(); i < np; ++i) {
		int pn = match_opts->throws_first + i;
		if (pn > np)
			pn -= np;

		if (!match_player_is_comp(pn))
			return !!state->legs[pn - 1]->n_visits;
	}

	return false;
}