-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayModifiedBetting.cs
355 lines (302 loc) · 13.7 KB
/
PlayModifiedBetting.cs
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace BlackjackBacktest
{
public class PlayModifiedBetting : PlayerBehavior
{
public class ModifiedBettingHand : Hand
{
public virtual Hand HandleSplit(Dealer dealer, BettingBehavior betting)
{
// create an additional hand, using one of the cards from this hand
Hand secondHand = new Hand();
secondHand.IsSplit = true;
dealer.PlayerSplitHand = secondHand;
Card secondHandFirstCard = PrepareForSplit();
secondHand.AddCardShowing(secondHandFirstCard);
// let the logic set the IsBust property as needed
Card cardDealt = dealer.DealCard(this);
dealer.Statistics.DiscardCard(cardDealt);
// replace the second card for the primary hand
cardDealt = dealer.DealCard(secondHand);
dealer.Statistics.DiscardCard(cardDealt);
// split on split is not allowed
secondHand.IsSplittable = false;
// set the default bet
Bet = betting.Bet;
secondHand.Bet = betting.Bet;
// now apply the hit logic
// determine if the bet should be modified
if (betting.DetermineDoubleDown(dealer.DealerHand, this, dealer.Statistics))
{
IsDoubledDown = true;
// okay to double the bet
Bet = betting.Bet * 2;
// deal one card only
// let the logic set the IsBust property as needed
cardDealt = dealer.DealCard(this);
dealer.Statistics.DiscardCard(cardDealt);
}
else
{
// we'll keep hitting till the sum is at least X
while ((SumCards < 16 &&
(dealer.Statistics.TensIndex > 4)) ||
(SumCards < 12))
{
// break from the loop if the proportion of >= 10 cards is too low
if (SumCards > 11 && dealer.Statistics.TensIndex < 2)
break;
// don't try if we have a run of small cards
if (_cards.Count > 4 && SumCards > 11)
break;
// let the logic set the IsBust property as needed
cardDealt = dealer.DealCard(this);
dealer.Statistics.DiscardCard(cardDealt);
if (IsBust)
break;
}
}
// determine if the bet should be modified for the second hand
if (betting.DetermineDoubleDown(dealer.DealerHand, this, dealer.Statistics))
{
secondHand.IsDoubledDown = true;
// okay to double the bet
secondHand.Bet = betting.Bet * 2;
// deal one card only
// let the logic set the IsBust property as needed
cardDealt = dealer.DealCard(secondHand);
dealer.Statistics.DiscardCard(cardDealt);
}
else
{
// we'll keep hitting till the sum is at least X
while ((secondHand.SumCards < 15 && dealer.DealerHand.CardShowing.Face > 6) ||
(secondHand.SumCards < 12) )
{
// break from the loop if the proportion of >= 10 cards is too low
if (secondHand.SumCards > 11 && dealer.Statistics.TensIndex < 3)
break;
// don't try if we have a run of small cards
if (secondHand.Cards.Count > 4 && secondHand.SumCards > 11)
break;
// let the logic set the IsBust property as needed
cardDealt = dealer.DealCard(secondHand);
dealer.Statistics.DiscardCard(cardDealt);
if (secondHand.IsBust)
break;
}
}
return secondHand;
}
// this is how to play, with this type of behavior
public override void Hit(Dealer dealer, BettingBehavior betting)
{
if (IsBlackJack || IsBust)
return;
if(betting.DetermineSplit(dealer.DealerHand, this, dealer.Statistics))
{
HandleSplit(dealer, betting);
// at this point, the two bets were set, and the cards have been
// dealt. The dealer knows about the hands
return;
}
// set the default bet
Bet = betting.Bet;
// modify the bet based on the last few cards (~12)
{
float[] tensFactor =
{
0.7176587594f,
0.7482172244f,
1.008457417f,
0.8508591232f,
0.8222382664f,
0.8221459353f,
0.8482251933f,
0.9152756165f,
1.034960453f,
1.161035422f,
1.577427822f,
2.4375f,
2.0f
};
if(dealer.Statistics.TensIndex > 7)
{
int newbet = Bet;
newbet *= 1000;
Bet = newbet;
}
}
// Intentionally not adding Insurance, because it's not material
// determine if the bet should be modified
if (betting.DetermineDoubleDown(dealer.DealerHand, this, dealer.Statistics))
{
IsDoubledDown = true;
// okay to double the bet
Bet = betting.Bet * 2;
// deal one card only
// let the logic set the IsBust property as needed
Card cardDealt = dealer.DealCard(this);
dealer.Statistics.DiscardCard(cardDealt);
}
else
{
// we'll keep hitting till the sum is at least 16
while ((SumCards < 16 && (dealer.Statistics.TensIndex > 1)) ||
// increase the threshold to 18 if player has an ace showing
(HasAce && _cards.Count < 3 && SumCards < 18) ||
// always hit if we're at 11 or below
SumCards < 12)
{
// let the logic set the IsBust property as needed
Card cardDealt = dealer.DealCard(this);
dealer.Statistics.DiscardCard(cardDealt);
if (IsBust)
break;
}
}
}
}
// this method will play a series of hands, using a specific behavior
public override void PlayHands(int iterations, StatisticsMgr stats)
{
Dealer dlr = new Dealer(stats);
BettingBehavior betting = new BettingBehavior();
// this is done like this to avoid re-allocating hands
ModifiedBettingHand bettingHand = new ModifiedBettingHand();
// doing this here to avoid allocations
Hand[] playerHands = new Hand[2];
while (--iterations >= 0)
{
betting.DetermineBet(stats);
bettingHand.Reset();
dlr.DealHands(bettingHand);
// tweak the stats before the doubledown bet
foreach (Card playerCard in dlr.PlayerHand.Cards)
{
// this records the card displayed, emulating a user that is
// counting the last 12 cards or so. Counting more cards would
// result in better performance.
stats.DiscardCard(playerCard);
}
stats.DiscardCard(dlr.DealerHand.Cards[1]);
stats.BlackJacks += (dlr.PlayerHand.IsBlackJack) ? 1 : 0;
// apply the hit logic to the player's hand. This will also
// split if necessary
dlr.PlayerHand.Hit(dlr, betting);
// dealer only hits if the player doesn't go bust
if (!dlr.PlayerHand.IsBust ||
(dlr.PlayerSplitHand != null && !dlr.PlayerSplitHand.IsBust))
dlr.DealerHand.Hit(dlr);
if (iterations % 1000000 == 0)
{
Debug.WriteLine("\tProfits: {0} Bet: {1}", stats.Profit, betting.Bet);
}
playerHands[0] = dlr.PlayerHand;
if(null != dlr.PlayerSplitHand)
{
// if the hand was split, add the split hand results as well
playerHands[1] = dlr.PlayerSplitHand;
} else
{
playerHands[1] = null;
}
foreach (var playerHand in playerHands)
{
if (null == playerHand)
continue;
// keep track of the total amount bet since it's dynamic
stats.CumulativeBet += playerHand.Bet;
if (playerHand.IsBust)
{
stats.Profit -= playerHand.Bet;
stats.RecordLoss();
stats.LossesByBust++;
if (playerHand.IsDoubledDown)
{
stats.LossesByDoubleDown++;
}
if(playerHand.IsSplit)
{
stats.LossesBySplit++;
}
}
else if (playerHand.IsBlackJack && !dlr.DealerHand.IsBlackJack)
{
// blackjack pays 3 to 2
stats.Profit += (int)(1.5 * playerHand.Bet);
stats.RecordWin(iterations);
if(playerHand.IsSplit)
{
stats.WinsBySplit++;
}
} else if(!playerHand.IsBlackJack && dlr.DealerHand.IsBlackJack)
{
stats.Profit -= playerHand.Bet;
stats.RecordLoss();
if (playerHand.IsDoubledDown)
{
stats.LossesByDoubleDown++;
}
if (playerHand.IsSplit)
{
stats.LossesBySplit++;
}
}
else if (dlr.DealerHand.IsBust)
{
stats.Profit += playerHand.Bet;
stats.RecordWin(iterations);
stats.WinsByBust++;
if (playerHand.IsDoubledDown)
{
stats.WinsByDoubleDown++;
}
if(playerHand.IsSplit)
{
stats.WinsBySplit++;
}
}
else if (playerHand.SumCards > dlr.DealerHand.SumCards)
{
stats.Profit += playerHand.Bet;
stats.RecordWin(iterations);
if (playerHand.IsDoubledDown)
{
stats.WinsByDoubleDown++;
}
if (playerHand.IsSplit)
{
stats.WinsBySplit++;
}
}
else if (dlr.DealerHand.SumCards > playerHand.SumCards)
{
stats.Profit -= playerHand.Bet;
stats.RecordLoss();
if (playerHand.IsDoubledDown)
{
stats.LossesByDoubleDown++;
}
if (playerHand.IsSplit)
{
stats.LossesBySplit++;
}
}
}
// now keep track of the last few discarded cards from both players
int index = 0;
foreach(Card dlrCard in dlr.DealerHand.Cards)
{
// skip the card that was initially shown because it was counted
// earlier
if (index++ == 1)
continue;
stats.DiscardCard(dlrCard);
}
}
}
}
}