This repository has been archived by the owner on Jul 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cards.c
executable file
·124 lines (103 loc) · 3.38 KB
/
cards.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include "blackjack.h"
/**
* Fill the deck with CARDS cards
*/
void fillDeck(Card * const wDeck){
int i;
for(i=0; i<CARDS; i++){
wDeck[i].face = i%(CARDS/4);
wDeck[i].suit = (unsigned int)i/(CARDS/4);
wDeck[i].color = (unsigned int)i/(CARDS/2);
}
}
/**
* Shuffle the deck with the Knuth Shuffle
*/
void shuffleDeck(Card * const wDeck){
int n = CARDS;
Card swappingCard;
unsigned int randomCardIndex;
while(n > 1){
randomCardIndex = rrand(n--);
swappingCard = wDeck[randomCardIndex];
wDeck[randomCardIndex] = wDeck[n];
wDeck[n] = swappingCard;
}
}
/**
* Pick the given number of card from the top of the deck
*/
void serveCard(Player *player, Card deck[], enum WHO who, int numberOfCards){
int i;
for(i=0; i<HANDSIZE; ++i){
if(numberOfCards == 0)
break;
if(player->hand[i].face == EMPTY){
player->hand[i] = drawCard(deck);
numberOfCards--;
}
}
showHand(player->hand, who);
}
/**
* Calculate the number of point of the given hand
*
* Return: Points
*/
int handPoints(Card hand[]){
int points=0, i, ace=0;
for(i=0; i<HANDSIZE; ++i){
if(hand[i].face != EMPTY){
if (hand[i].face == 0) /* If the card is an ace, we store if for later */
ace++;
else if (hand[i].face < 10) /* If the card is between 2 and 10, points = facial value */
points += hand[i].face + 1;
else /* If it is a face card, points = 10 */
points += 10;
}
}
if(ace > 0 && (points + 11) <= 21){ /* If +11 isn't to much */
points += 11;
ace--;
}
points += ace; /* every other ace = 1 */
return points;
}
/**
* Calculate the number of point of the given hand
* (if the hand is 17 with a ace worth 11, we return 7)
*
* Return: Points
*/
int soft17HandPoints(Card hand[]){
int points=0, i, ace=0;
for(i=0; i<HANDSIZE; ++i){
if(hand[i].face != EMPTY){
if (hand[i].face == 0) /* If the card is an ace, we store if for later */
ace++;
else if (hand[i].face < 10) /* If the card is between 2 and 10, points = facial value */
points += hand[i].face + 1;
else /* If it is a face card, points = 10 */
points += 10;
}
}
if(ace > 0 && (points + 11) <= 21 && (points + 11) != 11){ /* If +11 isn't to much */
points += 11;
ace--;
}
points += ace; /* every other ace = 1 */
return points;
}
/**
* Take the card at the top of the deck
*/
Card drawCard(Card dk[]){
Card temp;
int i;
for(i=0; i<CARDS; i++)
if(dk[i].face != EMPTY)
break;
temp = dk[i];
dk[i].face = EMPTY;
return temp;
}