-
Notifications
You must be signed in to change notification settings - Fork 0
/
Card.c
97 lines (81 loc) · 1.17 KB
/
Card.c
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
#include <Card.h>
const unsigned int CARD_NUM_RANKS = 13;
const unsigned int CARD_NUM_SUITS = 4;
Card Card_create(Suit suit, Rank rank)
{
Card output = { .suit = suit, .rank = rank };
return output;
}
Rank Rank_predecr(Rank* rank)
{
if (*rank == RANK_TWO)
{
*rank = RANK_ACE;
}
else
{
--(*rank);
}
return *rank;
}
Rank Rank_postdecr(Rank* rank)
{
Rank copy = *rank;
if (*rank == RANK_TWO)
{
*rank = RANK_ACE;
}
else
{
--(*rank);
}
return copy;
}
Rank Rank_preincr(Rank* rank)
{
if (*rank == RANK_ACE)
{
*rank = RANK_TWO;
}
else
{
++(*rank);
}
return *rank;
}
Rank Rank_postincr(Rank* rank)
{
Rank copy = *rank;
if (*rank == RANK_ACE)
{
*rank = RANK_TWO;
}
else
{
++(*rank);
}
return copy;
}
bool Card_equals(const Card card1, const Card card2)
{
return card1.rank == card2.rank && card1.suit == card2.suit;
}
int Card_compare(const Card card1, const Card card2)
{
if (Card_equals(card1, card2))
{
return 0;
}
else if (Card_calcValue(card1) > Card_calcValue(card2))
{
return 1;
}
else
{
return -1;
}
}
unsigned int Card_calcValue(const Card card)
{
return (int)card.rank * CARD_NUM_SUITS + (int)card.suit;
}