summaryrefslogtreecommitdiff
path: root/src/hand.c
blob: 407fe48d66e3d455e1b344563b6a9dce38a0a8bd (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
/*****************************************************************************
 *                          ___ __  __ ___ _ __                              *
 *                         / _ \\ \/ // _ \ '_ \                             *
 *                        | (_) |>  <|  __/ | | |                            *
 *                         \___//_/\_\\___|_| |_|                            *
 *                                                                           *
 *                             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 "hand.h"
#include <stdlib.h>
#include <assert.h>
#include "card.h"

/**
 * Compares two hands; used for sorting them in hand_sort
 */
static int hand_comparator(const void *a, const void *b)
{
	card c1 = *(card *)a;
	card c2 = *(card *)b;
	
	return c1 - c2;
}

/**
 * Sord the cards in a hand
 * @param[in,out] h Pointer to hand that should be sorted
 */
void hand_sort(hand_t *h)
{
	assert(h != NULL);

	qsort(h->cards, MAX_HAND_CARDS, sizeof(card), hand_comparator);
}

/**
 * Removes a card from a hand by setting it to invalid value 0
 * @param[in,out] h Hand to remove card from
 * @param[in] card_index Index of card to remove
 */
void hand_remove_card(hand_t *h, const uint8_t card_index)
{
	assert(h != NULL);

	h->cards[card_index] = 0;
}

/**
 * Count number of valid cards in hand
 * @param[in] h Hand to count
 * @return Number of valid cards in hand
 */
const uint8_t hand_count_cards(const hand_t* h)
{
	uint8_t count = 0;
	for(int i=0; i<MAX_HAND_CARDS; i++)
		if(h->cards[i] != 0)
			count++;

	return count;
}