summaryrefslogtreecommitdiff
path: root/src/card_stack.c
blob: b5cfa8afb381fca250cc7f0f12df83d2e9166343 (plain)
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
#include "card_stack.h"
#include <assert.h>
#include <stdlib.h>

/**
 * Calculates the points of a card stack. This will be the sum of the points of all cards contained in the card stack.
 * @param[in] cs The card stack fir which the points will be calculated
*/
uint32_t card_stack_get_points(const card_stack_t *cs)
{
	assert(cs != NULL);

	uint32_t points = 0;

	for(uint8_t i = 0; i < MAX_CARD_STACK_SIZE; i++)
	{
		if(cs->cards[i] > 0)
			points += card_get_points(cs->cards[i]);
	}

	return points;
}

/**
 * Determines the uppermost card on a card stack. The card will not be removed from the stack.
 * @param[in] cs The card stack from which the uppermost card will be retrieved
*/
const card card_stack_top(const card_stack_t *cs)
{
	assert(cs != NULL);

	for(int i = 0; i < MAX_CARD_STACK_SIZE; i++)
	{
		card cur = cs->cards[MAX_CARD_STACK_SIZE-1-i];
		if(cur != 0)
			return cur;
	}

	return 0;
}

/**
 * Places a card on top of a card stack.
 * @param[in] cs The card stack to place the card on
 * @param[in] c The card to place
*/
void card_stack_push(card_stack_t *cs, const card c)
{
	assert(cs != NULL);

	for(int i = 0; i < MAX_CARD_STACK_SIZE; i++)
	{
		if(cs->cards[i] != 0)
			continue;
		cs->cards[i] = c;
		break;
	}
}

/**
 * Replaces a card stack with a single card. All cards within the card stack will be removed and the first card will be set to a given card.
 * @param[in] cs The card stack to replace
 * @param[in] new_card The new first card
*/
void card_stack_replace(card_stack_t *cs, const card new_card)
{
	assert(cs != NULL);

	for(int i = 0; i < MAX_CARD_STACK_SIZE; i++)
		cs->cards[i] = 0;

	cs->cards[0] = new_card;
}