-
Notifications
You must be signed in to change notification settings - Fork 0
/
craps.js
1279 lines (1130 loc) · 36.5 KB
/
craps.js
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
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
let logs = [];
function log(message) {
console.log(message);
logs.push(message);
}
class Player {
/*
Player standing at the craps table
Parameters
----------
bankroll : float
Starting amount of cash for the player, will be updated during play
bettingStrategy : function(table, player, unit=5)
A function that implements a particular betting strategy. See betting_strategies.py
name : string, optional (default = "Player")
Name of the player
Attributes
----------
betsOnTable : list
List of betting objects for the player
totalBetAmount : int
Sum of bet value for the player
*/;
constructor(bankroll, bettingStrategy=null, name='Player') {
this.bankroll = bankroll;
this.bettingStrategy = bettingStrategy;
this.name = name;
this.betsOnTable = [];
this.totalBetAmount = 0;
this.startingBankroll = bankroll;
this.strategyInfo = {};
}
bet(bet) {
if (this.bankroll < bet.betAmount) {
log("not enough cash");
return;
}
if (this.name == 'Odds'
&& !this.getBet("Come", this.winning_numbers[0])
&& !this.getBet("Passline", this.winning_numbers[0])
) {
log("invalid bet");
return;
}
this.bankroll -= (bet.betAmount);
this.betsOnTable.push(bet)
// TODO: make sure this only happens if that bet isn't on the table, otherwise wager amount gets updated
this.totalBetAmount += bet.betAmount;
}
createBet(name, amount, number) {
number = parseInt(number);
amount = parseInt(amount);
let current = this.getBet(name, number);
if (!current)
this.bet(new bets[name](amount, number));
else {
this.removeBetByObject(current);
this.bet(new bets[name](amount + current.betAmount, number));
}
}
removeBetByObject(bet) {
// TODO: add bet attribute for whether a bet can be removed and put condition in here
let idx = this.betsOnTable.indexOf(bet);
if (idx > -1) {
this.bankroll += bet.betAmount;
this.totalBetAmount -= (bet.betAmount);
this.betsOnTable.splice(idx, 1);
}
}
getBet(name, subname='') {
return this.betsOnTable.find(bet=>bet.name == name
&& ((!subname && !bet.subname) || subname == bet.subname));
}
/* returns the total number of bets in this.betsOnTable that match bets_to_check */
countBets(name) {
return this.betsOnTable.filter(b=>b.name == name).length;
}
remove(name, subname='') {
let betsToRemove = this.betsOnTable.filter(b=>b.name == name && (!subname || subname == b.subname));
betsToRemove.forEach(b=>this.removeBetByObject(b));
}
removeBet(name, subname='') {
let betsToRemove = this.betsOnTable.filter(b=>b.name == name && (!subname || subname == b.subname));
betsToRemove.forEach(b=>this.removeBetByObject(b));
}
addStrategyBets(table, unit) {
/* Implement the given betting strategy */
return this.bettingStrategy.update(this, table, unit);
}
updateBets(table, dice, verbose=false) {
let info = {};
this.strategyInfo.lastRollBets = {won: [], lost: [], pushed: []}
for (let i = this.betsOnTable.length - 1; i >= 0; i--) {
let b = this.betsOnTable[i];
let betStatus, winAmount;
[betStatus, winAmount] = b.updateBet(table, dice);
if (betStatus == 'win') {
b.winAmount = winAmount;
this.bankroll += winAmount + b.betAmount - b.commissionAmount;
this.totalBetAmount -= b.betAmount;
this.betsOnTable.splice(i, 1);
this.strategyInfo.currentShooter.betsWon.push(b);
this.strategyInfo.lastRollBets.won.push(b);
if (verbose) {
log(`${this.name} won ${winAmount} on ${b.name}${b.subname} bet!`);
}
} else if (betStatus == 'lose') {
this.totalBetAmount -= b.betAmount;
this.betsOnTable.splice(i, 1);
this.strategyInfo.currentShooter.betsLost.push(b);
this.strategyInfo.lastRollBets.lost.push(b);
if (verbose) {
log(`${this.name} lost ${b.betAmount} on ${b.name}${b.subname} bet.`);
}
} else if (betStatus == 'push') {
this.bankroll += b.betAmount;
this.totalBetAmount -= b.betAmount;
this.betsOnTable.splice(i, 1);
this.strategyInfo.currentShooter.betsPush.push(b);
this.strategyInfo.lastRollBets.pushed.push(b);
if (verbose) {
log(`${this.name} pushed ${b.betAmount} on ${b.name}${b.subname} bet.`);
}
}
info[b.name] = {'betStatus': betStatus, 'winAmount': winAmount};
};
return info;
}
}
class Dice {
/**
*
* Simulate the rolling of a dice
Parameters
----------
NONE
Attributes
----------
numberOfRolls : int
Number of rolls for the dice
result : array, shape = [2]
Most recent outcome of the roll of two dice
total : int
Sum of dice outcome
*/
numberOfRolls = 0
result = [];
total = 0;
nextDie() {
return Math.floor(Math.random() * 6) + 1;
}
roll() {
this.numberOfRolls++;
this.result = [this.nextDie(), this.nextDie()];
this.total = this.result[0] + this.result[1];
}
}
class FakeDice extends Dice {
/**
* Instantiate with a list of lists of 2 numbers
* Those numbers will be used. When they run out, will revert to rolling randomly
*
*/
constructor(rolls) {
super();
this.rolls = rolls || [];
}
roll() {
if (this.rolls.length) {
this.numberOfRolls++;
this.result = this.rolls.shift();
this.total = this.result[0] + this.result[1];
} else {
super.roll();
}
}
}
class Table {
/**
* Craps Table that contains Dice, Players, the Players' bets, and updates
them accordingly. Main method is run() which should simulate a craps
table until a specified number of rolls plays out or all players run out
of money.
Parameters
----------
NONE
Attributes
----------
players : list
List of player objects at the table
totalPlayerCash : float
Sum of all players bankroll and bets on table
point : string
The point for the table. It is either "Off" when point is off or "On"
when point is on.
pointNumber : int
The point number when point is "On" and None when point is "Off"
playerHasBets : bool
Boolean value for whether any player has a bet on the table.
betUpdateInfo : dictionary
Contains information from updating bets, for given player and a bet
name, this is status of last bet (win/loss), and win amount.
*/
constructor(dice) {
this.players = [];
this.playerHasBets = false;
this.point = null;
this.dice = dice || new Dice();
this.betUpdateInfo = null;
this.payouts = {"fielddouble": [2, 12], "fieldtriple": []};
this.passRolls = 0;
this.lastRoll = null;
this.numberOfShooters = 1;
this.shooterNaturals = 0;
this.shooterPoints = 0;
this.shooterRolls = 0;
this.rollDistribution = Array(13).fill(0);
}
static POINTS = [4, 5, 6, 8, 9, 10];
hasPoint() {
return this.point > 0;
}
setPayouts(name, value) {
this.payouts[name] = value;
}
addPlayer(player) {
// Add player object to the table
if (this.players.includes(player)) {
return;
}
this.players.push(player);
}
run(maxRolls, maxShooters=Infinity, verbose=true) {
/**
* Runs the craps table until a stopping condition is met.
Parameters
----------
maxRolls : int
Maximum number of rolls to run for
verbose : bool
If true, print results from table during each roll
*
*/
if (verbose)
log("Welcome to the Craps Table!");
// make sure at least one player is at table
if (this.players.length == 0)
this.addPlayer(new Player(500, "Player1"));
if (verbose) {
log("Initial strategies:");
this.players.forEach(p=>log(p.name));
}
// maybe wrap this into update table or something
this.totalPlayerCash = 0;
this.players.forEach(p=>{
this.totalPlayerCash += parseInt(p.totalBetAmount) + parseInt(p.bankroll)
p.strategyInfo.currentShooter = {betsWon: [], betsLost: [], betsPush: []};
});
let continueRolling = true;
while (continueRolling) {
// players make their bets
this.addPlayerBets();
if (verbose) {
log(`*** Preparing for next roll.. adding strategy bets -- point is ${this.point ? "(" + this.point + ")" : "Off"}`);
this.players.forEach(p=>{
let totalBetsOnTable = 0;
p.betsOnTable.forEach(b=>totalBetsOnTable += b.betAmount);
log("Strategy:" + p.name + ' Bankroll:' + p.bankroll
+ ' Bets:' + p.betsOnTable.sort((a,b)=>(a.subname + a.name).localeCompare(b.subname + b.name)).map(b=>`${b.name}${b.subname}:${b.betAmount}`).join(' ')
+ ' Total Bets:' + totalBetsOnTable);
});
}
this.dice.roll();
this.rollDistribution[this.dice.total] += 1;
if (verbose) {
log("Dice out! -- " + (!this.hasPoint() ? "Come out roll" : `Point is ${this.point}`) + ` -- Shooter rolled ${this.dice.total} ${this.dice.result}`);
}
this.updatePlayerBets(this.dice, verbose);
this.updateTable(this.dice);
// evaluate the stopping condition
continueRolling =
this.numberOfShooters <= maxShooters
&& (maxRolls == 0 || this.dice.numberOfRolls < maxRolls)
&& this.totalPlayerCash > 0
&& this.players.filter(p=>!p.strategyInfo.stopped).length > 0;
}
if (verbose) {
log("Player ending bankrolls: " + this.players.map(p=>p.name + ':' + p.bankroll + ' ').join(' '))
log("Roll Distribution:" + this.rollDistribution.splice(2));
}
};
addPlayerBets() {
/**
* """ Implement each player's betting strategy """
""" TODO: restrict bets that shouldn't be possible based on table"""
""" TODO: Make the unit parameter specific to each player, and make it more general """
*/
this.players.forEach(p=>{
if (!p.strategyInfo.stopped)
p.addStrategyBets(this, 5);
});
}
updatePlayerBets(dice, verbose=false) {
/**
* check bets for wins/losses, payout wins to their bankroll, remove bets that have resolved
*/
this.betUpdateInfo = {};
this.players.forEach(p=>{
this.betUpdateInfo[p] = p.updateBets(this, dice, verbose);
});
}
updateTable(dice) {
/**
* update table attributes based on previous dice roll
*/
this.passRolls += 1;
this.shooterRolls += 1;
if (this.hasPoint()) {
if (dice.total == 7) {
this.numberOfShooters += 1;
this.shooterRolls = 0;
this.shooterNaturals = 0;
this.shooterPoints = 0;
this.passRolls = 0;
} else if (dice.total == this.point) {
this.shooterPoints += 1;
this.passRolls = 0;
}
}
if (this.hasPoint() && [7, this.point].includes(dice.total)) {
this.point = null;
if (7 == dice.total) {
this.players.forEach(p=>p.strategyInfo.currentShooter = {betsWon: [], betsLost: [], betsPush: []});
}
} else if (!this.hasPoint() && Table.POINTS.includes(dice.total)) {
this.point = dice.total;
}
this.totalPlayerCash = 0;
this.playerHasBets = false;
this.players.forEach(p=>{
this.totalPlayerCash += p.totalBetAmount + p.bankroll;
this.playerHasBets |= p.betsOnTable.length;
});
this.lastRoll = dice.total;
}
getPlayer(name) {
return this.players.find(p=>p.name == name);
}
}
function calculateOddsBet(number, unit=5, win_mult=1) {
number = parseInt(number)
if (win_mult == '345') {
if ([4, 10].includes(number)) {
amount = unit * 3;
} else if ([5, 9].includes(number)) {
amount = unit * 4;
} else if ([6, 8].includes(number)) {
amount = unit * 5;
}
} else {
amount = unit * win_mult;
}
force_multiple = 1;
if ([5, 9].includes(number)) {
force_multiple = 2;
} else if ([6, 8].includes(number)) {
force_multiple = 5;
}
return amount + (amount % force_multiple);
}
function calculateLayOddsBet(number, unit=5, win_mult=1) {
// Lay odds for don't pass
if (win_mult == '345') {
if ([4, 10].includes(number)) {
mult = 3;
} else if ([5, 9].includes(number)) {
mult = 4;
} else if ([6, 8].includes(number)) {
mult = 5;
}
} else {
mult = win_mult;
}
if ([4, 10].includes(number)) {
mult = 2 * mult;
} else if ([5, 9].includes(number)) {
mult = 3 / 2 * mult;
} else if ([6, 8].includes(number)) {
mult = 6 / 5 * mult;
}
force_multiple = 2;
if ([5, 9].includes(number)) {
force_multiple = 3;
} else if ([6, 8].includes(number)) {
force_multiple = 6;
}
amount = mult * unit;
return amount + (amount % force_multiple);
}
function calculatePlaceBet(number, unit=5) {
let factor = 5;
if ([6,8].includes(number)) {
factor = 6;
}
if (unit % factor == 0) {
return unit;
}
return unit + (factor - unit % factor);
}
ALL_NUMBERS = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
class Bet {
/*
A generic bet for the craps table
Parameters
----------
betAmount : float
Wagered amount for the bet
Attributes
----------
name : string
Name for the bet
subname : string
Subname, usually denotes number for a come/don't come bet
winning_numbers : list
Numbers to roll for this bet to win
losing_numbers : list
Numbers to roll that cause this bet to lose
payoutratio : float
Ratio that bet pays out on a win
offOnComeOut : boolean
The bet is not working on the come out roll
comeOutRollPush : boolean
If bet is off, and would have won or lost on come out roll, push the bet.
This is necessary for odds bets where the underlying bet is taken down.
If this is true, off on come out must be true
*/;
name = null;
subname = '';
winning_numbers = [];
losing_numbers = [];
payoutratio = 1;
offOnComeOut = false;
comeOutRollPush = false;
commissionAmount = 0;
// TODO: add whether bet can be removed
constructor(betAmount) {
this.betAmount = parseInt(betAmount);
}
// Use this method to set commission
setCommission(percent) {
this.commissionAmount = this.commission <= 0 ? 0 : Math.max(Math.floor(this.betAmount * percent), 1);
}
updateBet(table, dice) {
let status = null;
let win_amount = 0;
if (this.offOnComeOut && !table.hasPoint()) {
if (this.comeOutRollPush && (this.winning_numbers.includes(dice.total) || this.losing_numbers.includes(dice.total))) {
status = 'push';
}
return [status, win_amount];
}
if (this.winning_numbers.includes(dice.total)) {
status = 'win';
win_amount = this.payoutratio * (this.betAmount - this.commissionAmount);
} else if (this.losing_numbers.includes(dice.total)) {
status = 'lose';
}
return [status, Math.floor(win_amount)];
}
}
/*
Passline and Come bets
*/
class Pass extends Bet {
// TODO: make this require that table_object.point = "Off",
// probably better in the player module
constructor(betAmount) {
super(betAmount);
this.name = 'Pass';
this.winning_numbers = [7, 11];
this.losing_numbers = [2, 3, 12];
this.payoutratio = 1.0;
this.prepoint = true;
}
updateBet(table_object, dice_object) {
let status, win_amount;
[status, win_amount] = [null, 0];
if (this.winning_numbers.includes(dice_object.total)) {
status = 'win';
win_amount = this.payoutratio * this.betAmount;
} else if (this.losing_numbers.includes(dice_object.total)) {
status = 'lose';
} else if (this.prepoint) {
this.winning_numbers = [dice_object.total];
this.losing_numbers = [7];
this.prepoint = false;
}
return [status, win_amount];
}
}
class Come extends Pass {
constructor(betAmount) {
super(betAmount);
this.name = 'Come';
}
updateBet(table_object, dice_object) {
let status, win_amount;
[status, win_amount] = super.updateBet(table_object, dice_object);
if (!this.prepoint && this.subname == '') {
this.subname = [this.winning_numbers].join('');
}
return [status, win_amount];
}
}
class Odds extends Bet {
constructor(betAmount, number) {
super(betAmount);
number = parseInt(number);
this.name = 'Odds';
this.subname = number;
this.winning_numbers = [number];
this.losing_numbers = [7];
this.offOnComeOut = true;
this.comeOutRollPush = true;
switch (parseInt(number)) {
case 4:
case 10:
this.payoutratio = 2 / 1;
break;
case 5:
case 9:
this.payoutratio = 3 / 2;
break;
case 6:
case 8:
this.payoutratio = 6 / 5;
break;
}
}
// updateBet(table, dice) {
// if (this.offOnComeOut && !table.hasPoint() && (
// this.winning_numbers.includes(dice.total) || this.losing_numbers.includes(dice.total)
// )) {
// return ['push', 0];
// }
// return super.updateBet(table, dice);
// }
}
class LayOdds extends Bet {
constructor(betAmount, number) {
super(betAmount);
number = parseInt(number);
this.name = 'LayOdds';
this.subname = number;
this.winning_numbers = [7];
this.losing_numbers = [number];
this.offOnComeOut = true;
switch (parseInt(number)) {
case 4:
case 10:
this.payoutratio = 1 / 2;
break;
case 5:
case 9:
this.payoutratio = 2 / 3;
break;
case 6:
case 8:
this.payoutratio = 5 / 6;
break;
}
}
}
class Buy extends Bet {
constructor(betAmount, number) {
super(betAmount);
this.name = 'Buy';
this.subname = parseInt(number);
this.winning_numbers = [this.subname];
this.losing_numbers = [7];
this.offOnComeOut = true;
if (number == 4 || number == 10) {
this.payoutratio = 2 / 1;
} else if (number == 5 || number == 9) {
this.payoutratio = 3 / 2;
} else if (number == 6 || number == 8) {
this.payoutratio = 6 / 5;
}
this.setCommission(0.05);
}
updateBet(table, dice_object) {
// buy bets are inactive when point is "Off"
if (table.hasPoint()) {
return super.updateBet(table, dice_object);
} else {
return [null, 0];
}
}
}
class Lay extends Bet {
constructor(betAmount, number) {
super(betAmount);
this.name = 'Lay';
this.subname = parseInt(number);
this.winning_numbers = [7];
this.losing_numbers = [this.subname];
this.offOnComeOut = true;
if (number == 4 || number == 10) {
this.payoutratio = 1 / 2;
} else if (number == 5 || number == 9) {
this.payoutratio = 2 / 3;
} else if (number == 6 || number == 8) {
this.payoutratio = 5 / 6;
}
this.setCommission(0.05);
}
updateBet(table, dice_object) {
// buy bets are inactive when point is "Off"
if (table.hasPoint()) {
return super.updateBet(table, dice_object);
} else {
return [null, 0];
}
}
}
class Place extends Bet {
constructor(betAmount, number) {
super(betAmount);
this.name = "Place";
this.subname = parseInt(number);
this.offOnComeOut = true;
this.losing_numbers = [7];
this.winning_numbers = [this.subname];
if ([4, 10].includes(number)) {
this.payoutratio = 9 / 5;
} else if ([5, 9].includes(number)) {
this.payoutratio = 7 / 5;
} else if ([6, 8].includes(number)) {
this.payoutratio = 7 / 6;
}
}
}
class Field extends Bet {
/*
Parameters
----------
double : list
Set of numbers that pay double on the field bet (default = [2,12])
triple : list
Set of numbers that pay triple on the field bet (default = [])
*/
constructor(betAmount) {
super(betAmount);
this.name = 'Field';
this.double_winning_numbers = [2,12];
this.triple_winning_numbers = [];
this.winning_numbers = [2, 3, 4, 9, 10, 11, 12];
this.losing_numbers = [5, 6, 7, 8];
}
updateBet(table_object, dice_object) {
let status = null;
let win_amount = 0;
if (this.triple_winning_numbers.includes(dice_object.total)) {
status = 'win';
win_amount = 3 * this.betAmount;
} else if (this.double_winning_numbers.includes(dice_object.total)) {
status = 'win';
win_amount = 2 * this.betAmount;
} else if (this.winning_numbers.includes(dice_object.total)) {
status = 'win';
win_amount = 1 * this.betAmount;
} else if (this.losing_numbers.includes(dice_object.total)) {
status = 'lose';
}
return [status, win_amount];
}
}
/*
Don't pass and Don't come bets
*/
class DontPass extends Bet {
// TODO: make this require that table_object.point = "Off",
// probably better in the player module
constructor(betAmount) {
super(betAmount);
this.name = 'DontPass';
this.winning_numbers = [2, 3];
this.losing_numbers = [7, 11];
this.push_numbers = [12];
this.payoutratio = 1.0;
this.prepoint = true;
}
updateBet(table_object, dice_object) {
let status = null;
let win_amount = 0;
if (this.winning_numbers.includes(dice_object.total)) {
status = 'win';
win_amount = this.payoutratio * this.betAmount;
} else if (this.losing_numbers.includes(dice_object.total)) {
status = 'lose';
} else if (this.push_numbers.includes(dice_object.total)) {
status = 'push';
} else if (this.prepoint) {
this.winning_numbers = [7];
this.losing_numbers = [dice_object.total];
this.push_numbers = [];
this.prepoint = false;
}
return [status, win_amount];
}
}
class DontCome extends DontPass {
constructor(betAmount) {
super(betAmount);
this.name = 'DontCome';
}
updateBet(table_object, dice_object) {
let result = super.updateBet(table_object, dice_object);
let status = result[0];
let win_amount = result[1];
if (!this.prepoint && this.subname == '') {
this.subname = this.losing_numbers.join('');
return [status, win_amount];
}
return [status, win_amount];
}
}
class AnyCraps extends Bet {
constructor(betAmount) {
super(betAmount);
this.name = 'AnyCraps';
this.subname = '';
this.winning_numbers = [2, 3, 12];
this.losing_numbers = ALL_NUMBERS.filter(x=>!this.winning_numbers.includes(x));
this.payoutratio = 7;
}
}
class AnySeven extends Bet {
constructor(betAmount) {
super(betAmount);
this.name = 'AnySeven';
this.subname = '';
this.winning_numbers = [7];
this.losing_numbers = ALL_NUMBERS.filter(x=>!this.winning_numbers.includes(x));
this.payoutratio = 4;
}
}
class Horn12 extends Bet {
constructor(betAmount) {
super(betAmount);
this.name = 'Horn12';
this.subname = '';
this.winning_numbers = [12];
this.losing_numbers = ALL_NUMBERS.filter(x=>!this.winning_numbers.includes(x));
this.payoutratio = 30;
}
}
class Horn2 extends Bet {
constructor(betAmount) {
super(betAmount);
this.name = 'Horn2';
this.subname = '';
this.winning_numbers = [2];
this.losing_numbers = ALL_NUMBERS.filter(x=>!this.winning_numbers.includes(x));
this.payoutratio = 30;
}
}
class Horn3 extends Bet {
constructor(betAmount) {
super(betAmount);
this.name = 'Horn3';
this.subname = '';
this.winning_numbers = [3];
this.losing_numbers = ALL_NUMBERS.filter(x=>!this.winning_numbers.includes(x));
this.payoutratio = 15;
}
}
class Horn11 extends Bet {
constructor(betAmount) {
super(betAmount);
this.name = 'Horn11';
this.subname = '';
this.winning_numbers = [11];
this.losing_numbers = ALL_NUMBERS.filter(x=>!this.winning_numbers.includes(x));
this.payoutratio = 15;
}
}
class Hard extends Bet {
constructor(betAmount, number) {
number = parseInt(number);
super(betAmount);
if (number % 2 != 0)
throw new Error('Bad value for hardway:' + number);
this.name = 'Hard';
this.subname = number;
this.winning_result = [number/2, number/2].join('');
this.losing_results = [number, 7];
this.payoutratio = (number == 4 || number == 10) ? 7 : 9;
this.offOnComeOut = true;
}
updateBet(table, dice) {
let status = null;
let win_amount = 0;
if (this.offOnComeOut && !table.hasPoint()) {
return [status, win_amount];
}
let result = dice.result.join('');
if (result == this.winning_result) {
status = 'win';
win_amount = this.payoutratio * this.betAmount;
} else if (this.losing_results.includes(dice.total)) {
status = 'lose';
}
return [status, Math.floor(win_amount)];
}
}
class Hop extends Bet {
constructor(betAmount, twodice) {
super(betAmount);
this.name = 'Hop';
this.subname = twodice;
twodice = "" + twodice;
this.winning_results = [twodice, twodice.split('').reverse().join('')];
this.payoutratio = (twodice[0] == twodice[1]) ? 30 : 15;
}
updateBet(table, dice) {
let status = null;
let win_amount = 0;
let result = dice.result.join('');
if (this.winning_results.includes(result)) {
status = 'win';
win_amount = this.payoutratio * this.betAmount;
} else {
status = 'lose';
}
return [status, Math.floor(win_amount)];
}
}
class Simulation {
constructor(numOfRuns=5000, maxRolls=200, maxShooters=20, bankroll=200, strategies=[passline]) {
this.numOfRuns = numOfRuns;
this.maxRolls = maxRolls;
this.maxShooters = maxShooters;
this.bankroll = bankroll;
this.strategies = strategies;
this.logs = [];
logs = this.logs;
}
printout(dice) {
let table = new Table(dice);
this.strategies.forEach(s=>{
table.addPlayer(new Player(this.bankroll, s, s.name || s.constructor.name));
})
table.run(this.maxRolls, this.maxShooters);
}
run() {
let output = {};
for (let i = 0; i < this.numOfRuns; i++) {
let table = new Table();
this.strategies.forEach(s=>{
table.addPlayer(new Player(this.bankroll, s, s.name || s.constructor.name));
})
table.run(this.maxRolls, this.maxShooters, false);
table.players.forEach(player=>{
// log(`${i},${player.name},${player.bankroll},${this.bankroll},${table.dice.numberOfRolls}`);
output[player.name] = output[player.name] || [];
output[player.name].push([i, player.bankroll, this.bankroll, table.dice.numberOfRolls]);
})
}
return output;
}
}
class Strategy {
info = {};
unit = 5;
maxRolls = 0; // 0 for no limit
maxShooters = 0; // 0 for no limit
maxBankroll = 0; // 0 for no limit
stopped = false;
rules = [];
static fromObj(obj) {
let strategy = new this();
strategy.rules = obj.rules;
strategy.name = obj.name;
return strategy;
}
description() {
return 'Not set';
}