-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.c
177 lines (129 loc) · 4.57 KB
/
player.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "player.h"
player *createPlayer(char *playerName, int initialFunds) {
int initialSize;
initialSize = sizeof(player);
player *blackjackPlayer;
blackjackPlayer = (player *) malloc(initialSize);
playerHand *playersHand;
playersHand = createPlayerHand();
strcpy(blackjackPlayer->name, playerName);
blackjackPlayer->funds = initialFunds;
blackjackPlayer->howManyHands = 1;
/* blackjackPlayer->insuranceBet = 0; */
blackjackPlayer->playerHands[blackjackPlayer->howManyHands - 1] = playersHand;
blackjackPlayer->bets[0] = 0;
return blackjackPlayer;
}
//TODO: Change function name if I want to get PEGI 13
void killPlayer(player *playerPtr){
for (int i = 0; i < playerPtr->howManyHands; i++) {
removeHand(getSpecificHand(playerPtr, i));
}
free(playerPtr);
}
void giveCardTo(player *playerPtr, card *newCard, int whichHand) {
playerHand *activePlayerHand = getSpecificHand(playerPtr, whichHand);
activePlayerHand = receiveCard(activePlayerHand, newCard);
/* activePlayerHand = resizeHand(activePlayerHand); */
playerPtr->playerHands[whichHand] = activePlayerHand;
/* playerPtr->playerHands[whichHand] = activePlayerHand; */
/* activePlayerHand->hand[activePlayerHand->cardsInHand] = newCard; */
/* activePlayerHand->cardsInHand += 1; */
}
void printCards(player *playerPtr, int whichHand) {
playerHand *activePlayerHand = getSpecificHand(playerPtr, whichHand);
for (int i = 0; i < activePlayerHand->cardsInHand; i++) {
printf("%s's card: ", playerPtr->name);
printf("%d, %d \n", activePlayerHand->hand[i]->rank,
activePlayerHand->hand[i]->suit);
}
}
void makeABet(player *playerPtr, int bet, int whichHand) {
//TODO: Create if statement to check if you have enough funds
playerPtr->bets[whichHand] = bet;
playerPtr->funds -= bet;
}
bool canMakeABet(player *playerPtr, int bet) {
int playersFunds;
playersFunds = getFunds(playerPtr);
bool canMakeSpecificBet;
canMakeSpecificBet = (playersFunds >= bet);
return canMakeSpecificBet;
}
static void resetBet(player *playerPtr, int whichHand) {
playerPtr->bets[whichHand] = 0;
}
//This returns the money to symbolize the dealer taking it
int loseBet(player *playerPtr, int whichHand) {
int lostBet;
lostBet = getBet(playerPtr, whichHand);
resetBet(playerPtr, whichHand);
return lostBet;
}
void winBet(player *playerPtr, int awardedMoney, int whichHand) {
playerPtr->funds += awardedMoney;
resetBet(playerPtr, whichHand);
}
int getBet(player *playerPtr, int whichHand) {
return playerPtr->bets[whichHand];
}
int getNumberOfHands(player *playerPtr) {
return playerPtr->howManyHands;
}
//TODO: Make it return a copy? Maybe
playerHand *getSpecificHand(player *playerPtr, int whichHand) {
return playerPtr->playerHands[whichHand];
}
void increaseBet(player *playerPtr, int increaseAmount, int whichHand) {
makeABet(playerPtr, increaseAmount, whichHand);
}
void splitCards(player *playerPtr, int whichHand) {
playerHand *newHand;
newHand = createPlayerHand();
//TODO: Add if statement to check if the hand is already created
//We remove the last card from the hand you want to split
card *cardForNextHand;
playerHand *handToRemoveFrom;
handToRemoveFrom = playerPtr->playerHands[whichHand];
//cardForNextHand and cardToBeRemoved SHOULD be the same
cardForNextHand = removeSpecificCard(handToRemoveFrom, whichHand);
playerPtr->playerHands[playerPtr->howManyHands] = newHand;
giveCardTo(playerPtr, cardForNextHand, playerPtr->howManyHands);
playerPtr->howManyHands += 1;
}
int getFunds(player *playerPtr) {
return playerPtr->funds;
}
void resetPlayer(player *playerPtr) {
int amountOfHands;
amountOfHands = getNumberOfHands(playerPtr);
for (int currentHand = 0; currentHand < amountOfHands; currentHand++) {
playerPtr->bets[currentHand] = 0;
playerHand *currentPlayerHand;
currentPlayerHand = getSpecificHand(playerPtr, currentHand);
playerHand *resizeHand;
resizeHand = resetHand(currentPlayerHand);
playerPtr->playerHands[currentHand] = resizeHand;
}
playerPtr->howManyHands = 1;
/* playerPtr->insuranceBet = 0; */
}
bool isBroke(player *playerPtr) {
bool actuallyBroke;
actuallyBroke = (playerPtr->funds <= 0);
return actuallyBroke;
}
#define MAXINSURANCEDENOMINATOR 2
void askForInsurance(player *playerPtr) {
int originalBet;
//Insurance will always be on the first hand's bet
originalBet = getBet(playerPtr, 0);
int newInsuranceBet;
newInsuranceBet = originalBet / MAXINSURANCEDENOMINATOR;
makeABet(playerPtr, newInsuranceBet, INSURANCEBET);
/* playerPtr->insuranceBet = newInsuranceBet; */
/* playerPtr->funds -= newInsuranceBet; */
}
int getInsurancePosition() {
return INSURANCEBET;
}