summaryrefslogtreecommitdiff
path: root/src/card_stack.c
blob: 3fcfd6957f3c1106685fbb434daabe207c9523a5 (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
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
/*****************************************************************************
 *                          ___ __  __ ___ _ __                              *
 *                         / _ \\ \/ // _ \ '_ \                             *
 *                        | (_) |>  <|  __/ | | |                            *
 *                         \___//_/\_\\___|_| |_|                            *
 *                                                                           *
 *                             The card game                                 *
 *                                                                           *
 *  Copyright (C) 2011, Reiner Herrmann <reiner@reiner-h.de>                 *
 *                      Mario Kilies <MarioKilies@GMX.net>                   *
 *                                                                           *
 *  This program is free software; you can redistribute it and/or modify     *
 *  it under the terms of the GNU General Public License as published by     *
 *  the Free Software Foundation; either version 3 of the License, or        *
 *  (at your option) any later version.                                      *
 *                                                                           *
 *  This program is distributed in the hope that it will be useful,          *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the             *
 *  GNU General Public License for more details.                             *
 *                                                                           *
 *  You should have received a copy of the GNU General Public License        *
 *  along with this program. If not, see <http://www.gnu.org/licenses/>.     *
 *                                                                           *
 *****************************************************************************/

#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;
}