-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasicStrategy.c
More file actions
166 lines (130 loc) · 5.09 KB
/
Copy pathbasicStrategy.c
File metadata and controls
166 lines (130 loc) · 5.09 KB
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
#include "basicStrategy.h"
#include "playerDecision.h"
#include "blackjackRules.h"
//This macro makes the table smaller, just aesthetics
#define DD DoubleDown
#define ST Stand
#define SP Split
#define HT Hit
const playerDecision hardHands[17][10] = {
// 2 3 4 5 6 7 8 9 10 A
/* 5*/{HT, HT, HT, HT, HT, HT, HT, HT, HT, HT},
/* 6*/{HT, HT, HT, HT, HT, HT, HT, HT, HT, HT},
/* 7*/{HT, HT, HT, HT, HT, HT, HT, HT, HT, HT},
/* 8*/{HT, HT, HT, HT, HT, HT, HT, HT, HT, HT},
/* 9*/{HT, DD, DD, DD, DD, HT, HT, HT, HT, HT},
/*10*/{DD, DD, DD, DD, DD, DD, DD, DD, HT, HT},
/*11*/{DD, DD, DD, DD, DD, DD, DD, DD, DD, DD},
/*12*/{HT, HT, ST, ST, ST, HT, HT, HT, HT, HT},
/*13*/{ST, ST, ST, ST, ST, HT, HT, HT, HT, HT},
/*14*/{ST, ST, ST, ST, ST, HT, HT, HT, HT, HT},
/*15*/{ST, ST, ST, ST, ST, HT, HT, HT, HT, HT},
/*16*/{ST, ST, ST, ST, ST, HT, HT, HT, HT, HT},
/*17*/{ST, ST, ST, ST, ST, ST, ST, ST, ST, ST},
/*18*/{ST, ST, ST, ST, ST, ST, ST, ST, ST, ST},
/*19*/{ST, ST, ST, ST, ST, ST, ST, ST, ST, ST},
/*20*/{ST, ST, ST, ST, ST, ST, ST, ST, ST, ST},
/*21*/{ST, ST, ST, ST, ST, ST, ST, ST, ST, ST},
};
const playerDecision softHands[8][10] = {
// 2 3 4 5 6 7 8 9 10 A
/*A,2*/{HT, HT, HT, DD, DD, HT, HT, HT, HT, HT},
/*A,3*/{HT, HT, HT, DD, DD, HT, HT, HT, HT, HT},
/*A,4*/{HT, HT, DD, DD, DD, HT, HT, HT, HT, HT},
/*A,5*/{HT, HT, DD, DD, DD, HT, HT, HT, HT, HT},
/*A,6*/{HT, DD, DD, DD, DD, HT, HT, HT, HT, HT},
/*A,7*/{ST, DD, DD, DD, DD, ST, ST, HT, HT, HT},
/*A,8*/{ST, ST, ST, ST, ST, ST, ST, ST, ST, ST},
/*A,9*/{ST, ST, ST, ST, ST, ST, ST, ST, ST, ST},
};
const playerDecision splitHands[10][10] = {
// 2 3 4 5 6 7 8 9 10 A
/*2,2*/{SP, SP, SP, SP, SP, SP, HT, HT, HT, HT},
/*3,3*/{SP, SP, SP, SP, SP, SP, HT, HT, HT, HT},
/*4,4*/{HT, HT, HT, SP, SP, HT, HT, HT, HT, HT},
/*5,5*/{DD, DD, DD, DD, DD, DD, DD, DD, HT, HT},
/*6,6*/{SP, SP, SP, SP, SP, HT, HT, HT, HT, HT},
/*7,7*/{SP, SP, SP, SP, SP, SP, HT, HT, HT, HT},
/*8,8*/{SP, SP, SP, SP, SP, SP, SP, SP, SP, SP},
/*9,9*/{SP, SP, SP, SP, SP, ST, SP, SP, SP, SP},
/*T,T*/{ST, ST, ST, ST, ST, ST, ST, ST, ST, ST},
/*A,A*/{SP, SP, SP, SP, SP, SP, SP, SP, SP, SP},
};
//The array starts at 2
#define HORIZONTALOFFSET 2
#define VERTICALOFFSET 2
#define VERTICALOFFSETHARD 5
static inline playerDecision isCorrectSplit(card *playersCard[], card *dealersCard) {
int cardRrank = getBlackjackValue(playersCard[0]);
if (cardRrank == 1) {
//A is 11 in the chart
cardRrank = 11;
}
int dealersRank = getBlackjackValue(dealersCard);
if (dealersRank == 1) {
//A is 11 in the chart
dealersRank = 11;
}
int horizontalPosition = dealersRank - HORIZONTALOFFSET;
int verticalPosition = cardRrank - VERTICALOFFSET;
playerDecision correctDecision = splitHands[verticalPosition][horizontalPosition];
return correctDecision;
}
static inline playerDecision isCorrectHard(card *playersCard[], int amountOfCards, card *dealersCard) {
/* int cardRrank = playersCard[0]->rank; */
int cardSum = sumCards(playersCard, amountOfCards);
int dealersRank = getBlackjackValue(dealersCard);
if (dealersRank == 1) {
//A is 11 in the chart
dealersRank = 11;
}
int horizontalPosition = dealersRank - HORIZONTALOFFSET;
int verticalPosition = cardSum - VERTICALOFFSETHARD;
playerDecision correctDecision = hardHands[verticalPosition][horizontalPosition];
return correctDecision;
}
//If this functions is called it means that THERE IS 1 ace. If not, the
//function won't work (Why would you use it otherwise? :P)
static inline playerDecision isCorrectSoft(card *playersCard[], int amountOfCards, card *dealersCard) {
int cardSum = sumCards(playersCard, amountOfCards);
//We remove 11 because the table is represented in the form of A,1; A,2
//and so on.
cardSum -= 11;
int dealersRank = getBlackjackValue(dealersCard);
if (dealersRank == 1) {
//A is 11 in the chart
dealersRank = 11;
}
int horizontalPosition = dealersRank - HORIZONTALOFFSET;
int verticalPosition = cardSum - VERTICALOFFSET;
playerDecision correctDecision = softHands[verticalPosition][horizontalPosition];
return correctDecision;
}
playerDecision getCorrectChoice(playerHand *playersHand, card *dealersCard) {
playerDecision correctDecision;
card **playersCard;
playersCard = getCards(playersHand);
int amountOfCards;
amountOfCards = getAmountOfCards(playersHand);
bool canSplit;
canSplit = checkForSplit(playersCard, amountOfCards);
//This will compute the boolean even when it's not needed. However
//it makes the code cleaner and the isSoftHand function is pretty
//light
bool isSoft;
isSoft = isSoftHand(playersCard, amountOfCards);
if (canSplit == true) {
correctDecision = isCorrectSplit(playersCard, dealersCard);
} else if (isSoft == true) {
correctDecision = isCorrectSoft(playersCard, amountOfCards, dealersCard);
} else {
correctDecision = isCorrectHard(playersCard, amountOfCards, dealersCard);
}
//If you can't DoubleDown, then the best thing is to Hit
bool canDoubleDown;
if (correctDecision == DD) {
canDoubleDown = checkForDoubleDown(amountOfCards);
correctDecision = (canDoubleDown == true) ? DD : HT;
}
return correctDecision;
}