summaryrefslogtreecommitdiff
path: root/src/main_stack.c
blob: 76bee04085115c85fabc3e27319d1d8b7351b66f (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
/*****************************************************************************
 *                          ___ __  __ ___ _ __                              *
 *                         / _ \\ \/ // _ \ '_ \                             *
 *                        | (_) |>  <|  __/ | | |                            *
 *                         \___//_/\_\\___|_| |_|                            *
 *                                                                           *
 *                             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 "main_stack.h"
#include <stdlib.h>

/**
 * Initialize main stack: Assign valid cards and shuffle it
 * @param[in,out] m Pointer to main stack
 */
void main_stack_init(main_stack_t *m)
{
	assert(m != NULL);

	// assign card values to main stack
	for(int i = 0; i < MAX_MAIN_STACK_CARDS; i++)
		m->cards[i] = i + 1;

	// shuffle stack
	for(int i = 0; i < 3 * MAX_MAIN_STACK_CARDS; i++)
	{
		uint32_t x = rand() % MAX_MAIN_STACK_CARDS;
		uint32_t y = rand() % MAX_MAIN_STACK_CARDS;
		card tmp = m->cards[x];
		m->cards[x] = m->cards[y];
		m->cards[y] = tmp;
	}
}

/**
 * Draw card on top of main stack and mark it as removed
 * @param[in,out] m Pointer to main stack
 * @return Card on top of stack
 */
card main_stack_remove_card(main_stack_t *m)
{
	assert(m != NULL);

	for(int i = 0; i < MAX_MAIN_STACK_CARDS; i++)
	{
		card c = m->cards[i];
		if(c == 0)
			continue;
		m->cards[i] = 0;
		return c;
	}
	return 0; // stack empty
}

/**
 * Returns number of cards remaining in main stack
 * @param[in] m Pointer to main stack
 * @return Number of cards in stack
 */
uint8_t main_stack_size(const main_stack_t *m)
{
	assert(m != NULL);

	uint8_t count = 0;

	for(uint8_t i = 0; i < MAX_MAIN_STACK_CARDS; i++)
	{
		if(m->cards[i] == 0)
			continue;
		count++;
	}

	return count;
}