-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cu
661 lines (558 loc) · 17.3 KB
/
main.cu
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
/*
This project has been released under the MIT License, and it's text is
contained within the accompanying file LICENSE.md.
All future versions of the MIT License automatically apply.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <errno.h>
#include <string.h>
#include <curand.h>
#include <curand_kernel.h>
#include "main.h"
#include "basic_strategy.h"
extern "C" {
#include "fileio.h"
}
/*
__global__ void mykernel() {
}
*/
bool debug_trace = true, psoft = false, dsoft = false;
bool record_allbank = false;
int ndecks, ntrials, houserules, strategy, nbets;
float penet, bank, startbank, minbet, betspread;
char mystring[64];
int shoe[52*8], dealer[21], player[8][21];
float bets[8];
int cards, maxcardpos, cardpos = 0;
int handno, cardno, nhands, ptotal[8], paces[8], dtotal, daces, dcardno;
float *allbank;
// Variables for statistics
float avegain = 0.0, siggain = 0.0, drawavegain = 0.0, drawsiggain = 0.0;
float maxgain = 0.0, mingain = 0.0, maxbank = 0.0, minbank = 0.0;
float maxmidbank = 0.0, minmidbank = 0.0;
float avebet = 0.0, sigbet = 0.0, maxbet = 0.0, minebet = 0.0;
int nwinhands = 0, ndbusts = 0, npbusts = 0;
float fwinhands = 0.0, fdbusts = 0.0, fpbusts = 0.0;
int nsur = 0, nstd = 0, nhits = 0, ndbls = 0, nsplts = 0;
int nbankrupt = 0, totalbets = 0;
float fsur = 0.0, fstd = 0.0, fhits = 0.0, fdbls = 0.0, fsplts = 0.0;
int main(int argc, char* argv[]) {
int i;
int seed = time(NULL);
srand(seed);
read_params(argv[1]);
minebet = minbet;
initialize_shoe(shoe, ndecks);
shuffle(shoe, ndecks);
ncards = 52*8;
maxcardpos = floor(ncards*penet);
allbank = (float*)malloc(sizeof(float)*ntrials*nbets);
startbank = bank;
if(debug_trace) { printf("Playing trials...."); }
for(i = 0; i < ntrials; i++) {
play(i);
}
if(debug_trace) { printf(" done.\n"); }
if(debug_trace) { printf("Computing statistics...."); }
compute_stats();
if(debug_trace) { printf(" done.\n"); }
sprintf(mystring,argv[1]);
strcat(mystring,".stats.dat");
if(debug_trace) { printf("Writing stats...."); }
write_stats(mystring);
if(debug_trace) { printf(" done.\n"); }
if(record_allbank) {
sprintf(mystring,argv[1]);
strcat(mystring,".allbank.dat");
if(debug_trace) {
printf("Writing allbank to %s ....", mystring);
}
write_allbank(mystring, allbank, ntrials, nbets);
if(debug_trace) { printf(" done.\n"); }
}
free(allbank);
}
void play(int trialnum) {
int curbets = 1, playeraction, flag = 0;
float stbank, stavebet;
bank = startbank;
nsur = 0; nstd = 0; ndbusts = 0; nhits = 0; ndbls = 0; nsplts = 0;
while ( !trialover(curbets) ) {
// curbets++;
handno = 0, nhands = 1;
stbank = bank;
stavebet = avebet;
opendraw();
flag = 0;
while( flag == 0 ) {
playeraction = verb(1);
switch(playeraction) {
case 0: // surrender
nsur++;
bank += bets[handno--]/2.0;
if (handno < 0) { flag = 1; } // Last hand surrendered
else { recomputeptotal(); }
break;
case 1: // stand
nstd++;
if (--handno < 0) { flag = 2; } // Last hand stood
else { recomputeptotal(); }
break;
case 2: // hit
nhits++;
hitdeal();
if (handbust()) {
if (--handno < 0) { flag = 3; } // Last hand busted
else { recomputeptotal(); }
}
break;
case 3: // double
ndbls++;
bank -= bets[handno];
avebet = avebet + bets[handno];
sigbet = sigbet + bets[handno]*bets[handno];
bets[handno] = 2*bets[handno];
hitdeal();
if (--handno < 0) { flag = 4; } // Last hand doubled
else { recomputeptotal(); }
break;
case 4: // split
nsplts++;
splitdeal(); // Not checking if split command is valid
break;
default: // do nothing
break;
}
}
printf("dcardno = %d\n", dcardno);
resolvedeal();
printf("dcardno = %d\n", dcardno);
allbank[nbets*trialnum+curbets-1] = bank;
if((bank - stbank) > maxgain) { maxgain = bank - stbank; }
if((bank - stbank) < mingain) { mingain = bank - stbank; }
if(bank > maxmidbank) { maxmidbank = bank; }
if(bank < minmidbank) { minmidbank = bank; }
if((avebet - stavebet) > maxbet) { maxbet = avebet - stavebet; }
if((avebet - stavebet) < minebet) { minebet = avebet - stavebet; }
curbets++;
printhands();
}
if (bank > maxbank) { maxbank = bank; }
if (bank < minbank) { minbank = bank; }
if (bank <= 0.0) { nbankrupt++; }
avegain = avegain + bank - startbank;
siggain = siggain + (bank - startbank)*(bank - startbank);
drawavegain = drawavegain + (bank - startbank)/(curbets-1);
drawsiggain = drawsiggain + (bank - startbank)/(curbets-1)*(bank - startbank)/(curbets-1);
fsur = fsur + 1.0*nsur/(curbets-1);
fstd = fstd + 1.0*nstd/(curbets-1);
fhits = fhits + 1.0*nhits/(curbets-1);
fdbls = fdbls + 1.0*ndbls/(curbets-1);
fsplts = fsplts + 1.0*nsplts/(curbets-1);
totalbets = totalbets + curbets - 1;
}
void printhands() {
int ll, temp;
printf("Dealer %d: ", dcardno);
for(ll = 0; ll < dcardno; ll++) {
printf("%d ", dealer[ll]);
}
printf("\ndtotal = %d ", dtotal);
if (dtotal > 21) { printf("(BUST!)\n"); }
else { printf("\n"); }
for(temp = 0; temp < nhands; temp++) {
printf("Player hand %d: ", temp);
ll = 0;
while(player[temp][ll] != 0) {
printf("%d ", player[temp][ll++]);
} printf("\nptotal = %d ", ptotal[temp]);
if(ptotal[temp] > 21){
printf("(BUST!)\n");
} else if (ptotal[temp] == dtotal) {
printf("(PUSH!)\n");
} else if((ptotal[temp] > dtotal) || (dtotal > 21)) {
printf("(WIN!)\n");
} else {
printf("(LOSS!)\n");
}
}
}
void resolvedeal() {
// Surrender not being accounted for right now
bool allbusted = true;
dcardno = 2;
// Check player busts and finish dealer card draws
for(handno = 0; handno < nhands; handno++) {
recomputeptotal();
if(ptotal[handno] <= 21) { allbusted = false; }
else { npbusts++; }
}
if(!allbusted) {
if(dealer[0] == 1) {
dtotal += 11; dsoft = true; daces++;
} else if (dealer[0] > 10) {
dtotal += 10;
} else { dtotal += dealer[0]; }
if(dtotal > 21) {
while(daces > 0) {
dtotal -= 10;
if(dtotal <= 21) { daces--; break; }
}
if(dtotal > 21) { ndbusts++; }
}
// Modify soft-17 rule based on 'houserules' parameter
while((dtotal < 17) || ((dtotal == 17) && (daces > 0))) {
if( cardpos >= maxcardpos ) {
shuffle(shoe, ndecks);
cardpos = 0;
}
dealer[dcardno++] = shoe[cardpos++];
updatedtotal();
}
for(handno = 0; handno < nhands; handno++) {
if((ptotal[handno] <= 21) && ((ptotal[handno] > dtotal) || (dtotal > 21))) {
nwinhands++;
// Check for Blackjack
// Modify this according to 'houserules' parameter
if((ptotal[handno] == 21) && (player[handno][2] == 0)) {
bank += 2.5*bets[handno];
} else {
bank += 2*bets[handno];
}
}
}
}
}
void splitdeal() {
nhands++;
if( cardpos >= maxcardpos - 2 ) {
shuffle(shoe, ndecks);
cardpos = 0;
}
bank -= bets[handno++];
bets[handno] = bets[handno-1];
avebet = avebet + bets[handno];
sigbet = sigbet + bets[handno]*bets[handno];
player[handno][0] = player[handno-1][1];
player[handno-1][1] = shoe[cardpos++];
player[handno][1] = shoe[cardpos++];
cardno = 2;
if (player[handno][0] == 1) {
ptotal[handno] = 11; psoft = true; paces[handno]++;
} else if (player[handno][0] > 10) {
ptotal[handno] = 10;
} else { ptotal[handno] = player[handno][0]; }
if (player[handno][1] == 1) {
ptotal[handno] += 11; psoft = true; paces[handno]++;
} else if (player[handno][1] > 10) {
ptotal[handno] += 10;
} else { ptotal[handno] += player[handno][1]; }
}
void hitdeal() {
if( cardpos >= maxcardpos ) {
shuffle(shoe, ndecks);
cardpos = 0;
}
player[handno][cardno++] = shoe[cardpos++];
updateptotal();
}
void recomputeptotal() {
cardno = 0; ptotal[handno] = 0; paces[handno] = 0;
while( player[handno][cardno] != 0 ) {
if (player[handno][cardno] == 1) {
ptotal[handno] += 11; psoft = true;
paces[handno]++;
} else if (player[handno][cardno] > 10) {
ptotal[handno] += 10;
} else { ptotal[handno] += player[handno][cardno]; }
cardno++;
}
// cardno--;
if (ptotal[handno] > 21) {
while(paces[handno] > 0) {
ptotal[handno] -= 10; paces[handno]--;
if (ptotal[handno] <= 21) { break; }
}
}
if (paces[handno] == 0) { psoft = false; }
}
void updateptotal() {
if (player[handno][cardno-1] == 1) {
ptotal[handno] += 11; psoft = true; paces[handno]++;
} else if (player[handno][cardno-1] > 10) {
ptotal[handno] += 10;
} else { ptotal[handno] += player[handno][cardno-1]; }
if (ptotal[handno] > 21 ) {
/* for( int ll = 0; ll < cardno; ll++) {
if (player[handno][ll] == 1) { paces[handno]++; }
}
*/
while(paces[handno] > 0) {
ptotal[handno] -= 10; paces[handno]--;
if (ptotal[handno] < 21) { break; }
}
if (paces[handno] == 0) { psoft = false; }
}
}
void updatedtotal() {
if (dealer[dcardno-1] == 1) {
dtotal += 11; dsoft = true; daces++;
} else if (dealer[dcardno-1] > 10) {
dtotal += 10;
} else { dtotal += dealer[dcardno-1]; }
if (dtotal > 21 ) {
/* for( int ll = 0; ll < dcardno; ll++) {
if (dealer[ll] == 1) { daces++; }
}
*/
while(daces > 0) {
dtotal -= 10; daces--;
if (dtotal< 21) { break; }
}
if (daces == 0) { dsoft = false; }
}
}
void opendraw() {
cleartable();
if( cardpos >= maxcardpos - 4 ) {
shuffle(shoe, ndecks);
cardpos = 0;
}
bets[handno] = verb(0);
avebet = avebet + bets[handno];
sigbet = sigbet + bets[handno]*bets[handno];
bank -= bets[handno];
// dealer[0] card is hidden from player
dealer[0] = shoe[cardpos++];
dealer[1] = shoe[cardpos++];
player[handno][0] = shoe[cardpos++];
player[handno][1] = shoe[cardpos++];
cardno = 2;
if (dealer[1] == 1) {
dtotal = 11; dsoft = true; daces++;
} else if (dealer[1] > 10) {
dtotal = 10;
} else { dtotal = dealer[1]; }
if (player[handno][0] == 1) {
ptotal[handno] = 11; psoft = true; paces[handno]++;
} else if (player[handno][0] > 10) {
ptotal[handno] = 10;
} else { ptotal[handno] = player[handno][0]; }
if (player[handno][1] == 1) {
ptotal[handno] += 11; psoft = true; paces[handno]++;
} else if (player[handno][1] > 10) {
ptotal[handno] += 10;
} else { ptotal[handno] += player[handno][1]; }
}
bool handbust() {
if (ptotal[handno] > 21 && paces[handno] == 0) {
return true;
}
return false;
}
int verb(int state) {
// This is the player action function
switch(state) {
case 0: // pre-deal betting cycle, return bet amount
return openbet();
case 1: // cards dealt, return action
return pdecision();
default: // Same as case 0
return openbet();
}
}
int openbet() {
switch(strategy) {
case 0: // Test strategy
return minbet;
default: // Same as case 0
return minbet;
}
}
int pdecision() {
/* return meanings
0 == surrender
1 == stand
2 == hit
3 == double
4 == split
*/
switch(strategy) {
<<<<<<< HEAD
case 999: // Another test strategy
if ((cardno == 2) && (player[handno][0] == player[handno][1])) {
return 4; // Split whenever possible
} else { // Else double if ptotal < 17, else stand
if (ptotal[handno] < 17) {
return 3;
} else {
return 1;
}
}
case 1:
// Does basic action assuming that doubles are allowed, splits allowed, surrenders not allowed.
// This can be easily modified.
return handaction_simple(player[handno], DOUBLE_Y, SPLIT_Y, SURRENDER_N);
break;
=======
case 1:
// Does basic action assuming that doubles are allowed, splits allowed, surrenders not allowed.
// This can be easily modified.
handaction_simple(player[handno], DOUBLE_Y, SPLIT_Y, SURRENDER_N);
break;
>>>>>>> beba92adfee6036752ee8017515969c273d1c6e6
case 0: // Test strategy, hit if ptotal < 17, else stand
default: // Same as case 0
if (ptotal[handno] < 17) {
return 2;
} else {
return 1;
}
}
}
void cleartable() {
// Clean up hands
for(int i = 0; i < 21; i++) {
dealer[i] = 0;
for(int j = 0; j < 8; j++) { player[j][i] = 0; }
}
for(int k = 0; k < 8; k++) {
bets[k] = 0.0; ptotal[k] = 0; paces[k] = 0;
}
dtotal = 0; psoft = false; dsoft = false; daces = 0;
}
bool trialover(int curbetno) {
if (nbets > 0) {
if (curbetno > nbets || bank <= 0) {
return true; } else {return false;}
} else if (nbets < 0) {
if (curbetno > abs(nbets)) { return true; }
else { return false; }
} else {
if (bank <= 0) { return true; } else { return false; }
}
}
void read_params(char* fname) {
sprintf(mystring,"debug_trace");
get_bool_param(fname, mystring, &debug_trace, true);
sprintf(mystring,"ndecks");
get_int_param(fname, mystring, &ndecks, debug_trace);
if (ndecks < 1) { ndecks = 1; }
if (ndecks > 8) { ndecks = 8; }
sprintf(mystring,"ntrials");
get_int_param(fname, mystring, &ntrials, debug_trace);
sprintf(mystring,"houserules");
get_int_param(fname, mystring, &houserules, debug_trace);
sprintf(mystring,"strategy");
get_int_param(fname, mystring, &strategy, debug_trace);
sprintf(mystring,"nbets");
get_int_param(fname, mystring, &nbets, debug_trace);
sprintf(mystring,"penetration");
get_real_param(fname, mystring, &penet, debug_trace);
if (penet < 2.0) { penet = 10.0; }
if (penet > 100.0) { penet = 100.0; }
sprintf(mystring,"bank");
get_real_param(fname, mystring, &bank, debug_trace);
sprintf(mystring,"minbet");
get_real_param(fname, mystring, &minbet, debug_trace);
if (minbet > bank) { minbet = bank; }
sprintf(mystring,"betspread");
get_real_param(fname, mystring, &betspread, debug_trace);
sprintf(mystring,"record_allbank");
get_bool_param(fname, mystring, &record_allbank, debug_trace);
}
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
// Fischer-Yates random permutation of arr[]
// Move this to GPU eventually
void shuffle( int* arr, int ndeck ) {
int ii, jj, kk;
// srand ( time(NULL) );
// Shuffle thrice, just 'cause
for (kk = 0; kk < 3; kk++) {
// Start from the last element and swap one by one. We don't
// need to run for the first element that's why ii > 0
for (ii = ndeck*52-1; ii > 0; ii--){
// Pick a random index from 0 to ii
jj = rand() % (ii+1);
// Swap arr[i] with the element at random index
swap(&arr[ii], &arr[jj]);
}
}
}
void initialize_shoe(int* arr, int ndeck) {
// 1s are aces, J = 11, Q = 12, K = 13
for (int ii = 0; ii < ndeck; ii++) {
for (int jj = 0; jj < 4; jj++) {
for (int kk = 0; kk < 13; kk++) {
arr[ii*52+jj*13+kk] = kk+1;
}
}
}
}
void compute_stats() {
float invntrials;
invntrials = 1.0/ntrials;
avegain = avegain*invntrials;
siggain = sqrt(siggain*invntrials - avegain*avegain);
drawavegain = drawavegain*invntrials;
drawsiggain = sqrt(drawsiggain*invntrials - drawavegain*drawavegain);
avebet = avebet/(totalbets);
sigbet = sqrt(sigbet/totalbets - avebet*avebet);
// nbankrupt = nbankrupt*invntrials;
fwinhands = nwinhands*invntrials;
fpbusts = npbusts*invntrials;
fdbusts = ndbusts*invntrials;
fsur = fsur*invntrials;
fstd = fstd*invntrials;
fhits = fhits*invntrials;
fdbls = fdbls*invntrials;
fsplts = fsplts*invntrials;
}
void write_stats(char* p_file) {
FILE *fp;
if((fp = fopen(p_file, "w")) == NULL) {
printf("Couldn't open file: %s\n", p_file);
exit(1);
}
fprintf(fp, "houserules = \t%d\n", houserules);
fprintf(fp, "strategy = \t%d\n", strategy);
fprintf(fp, "ntrials = \t%d\n", ntrials);
fprintf(fp, "nbets = \t%d\n\n", nbets);
fprintf(fp, "ndecks = \t%d\n", ndecks);
fprintf(fp, "penetration = \t%f\n\n", penet);
fprintf(fp, "bank = \t\t%f\n", startbank);
fprintf(fp, "minbet = \t%f\n", minbet);
fprintf(fp, "betspread = \t%f\n\n", betspread);
fprintf(fp, "Average gain per trial = \t%f\n", avegain);
fprintf(fp, "Sigma gain over trials = \t%f\n", siggain);
fprintf(fp, "Average gain per draw = \t%f\n", drawavegain);
fprintf(fp, "Sigma gain over draws = \t%f\n\n", drawsiggain);
fprintf(fp, "Biggest gain per draw = \t%f\n", maxgain);
fprintf(fp, "Smallest gain per draw = \t%f\n", mingain);
fprintf(fp, "Bank Peak end of trial = \t%f\n", maxbank);
fprintf(fp, "Lowest bank end of trail = \t%f\n", minbank);
fprintf(fp, "Biggest bank peak = \t\t%f\n", maxmidbank);
fprintf(fp, "Lowest bank trough = \t\t%f\n\n", minmidbank);
fprintf(fp, "Average bet size = \t\t%f\n", avebet);
fprintf(fp, "Sigma of bet size = \t\t%f\n", sigbet);
fprintf(fp, "Biggest bet made = \t\t%f\n", maxbet);
fprintf(fp, "Smallest bet made = \t\t%f\n", minebet);
fprintf(fp, "Number of bankruptcies = \t%d\n\n", nbankrupt);
fprintf(fp, "Total number of bets made = \t\t%d\n", totalbets);
fprintf(fp, "Average number of winning hands per trial = \t%f\n", fwinhands);
fprintf(fp, "Average number of player busts per trial = \t%f\n", fpbusts);
fprintf(fp, "Average number of dealer busts per trial = \t%f\n\n", fdbusts);
fprintf(fp, "Average number of surrenders = \t%f\n", fsur);
fprintf(fp, "Average number of stands = \t%f\n", fstd);
fprintf(fp, "Average number of hits = \t%f\n", fhits);
fprintf(fp, "Average number of doubles = \t%f\n", fdbls);
fprintf(fp, "Average number of splits = \t%f\n", fsplts);
fclose(fp);
}