-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
924 lines (914 loc) · 37.2 KB
/
index.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
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.Poker = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Card = exports.Suits = void 0;
exports.Suits = {
CLUB: "CLUB",
DIAMOND: "DIAMOND",
HEART: "HEART",
SPADE: "SPADE",
};
var SHORT_NAMES = {
c: "CLUB",
d: "DIAMOND",
h: "HEART",
s: "SPADE",
};
var Card = /** @class */ (function () {
function Card(suit, value) {
var s;
if (typeof suit == "string") {
s = suit.length == 1 ? SHORT_NAMES[suit.toLowerCase()] : exports.Suits[suit];
if (!s)
throw new Error('Invalid suit! Supported suits are: CLUB,DIAMOND,HEART,SPADE. You can also use the first letter as well.');
}
else
s = suit;
this.suit = s;
if (value < 2 || value > 14)
throw new Error('Invalid card value, should be 2 to 14 where 11=J, 12=Q, 13=K and 14=A');
this.value = value;
}
return Card;
}());
exports.Card = Card;
},{}],2:[function(require,module,exports){
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Deck = void 0;
var Card_1 = require("./Card");
var Deck = /** @class */ (function () {
function Deck() {
var suits = [Card_1.Suits.CLUB, Card_1.Suits.DIAMOND, Card_1.Suits.HEART, Card_1.Suits.SPADE];
this.cards = Array.apply(null, new Array(52)).map(function (_, v) { return new Card_1.Card(suits[Math.floor(v / 13)], (v % 13) + 2); });
}
Deck.prototype.shuffle = function () {
var currentIndex = this.cards.length, temporaryValue, randomIndex;
while (0 !== currentIndex) { // While there remain elements to shuffle...
randomIndex = Math.floor(Math.random() * currentIndex); // Pick a remaining element...
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = this.cards[currentIndex];
this.cards[currentIndex] = this.cards[randomIndex];
this.cards[randomIndex] = temporaryValue;
}
};
Deck.prototype.getCards = function (count) {
if (count === void 0) { count = 1; }
return this.cards.splice(0, count);
};
return Deck;
}());
exports.Deck = Deck;
},{"./Card":1}],3:[function(require,module,exports){
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Game = void 0;
var deck_1 = require("./deck");
var Holdem_1 = require("./Holdem");
var Game = /** @class */ (function () {
/**
* Inititalizes the Game
*
* @param playerMoney Array of player money to start with, number of players will be of same length
* @param initialBet Minimum betting amount to start with
*/
function Game(playerMoney, initialBet) {
this.pot = 0;
this.players = [];
this.deck = new deck_1.Deck();
this.table = [];
this.round = [];
this.__instance = new Holdem_1.Holdem();
this.__roundStates = [];
this.initialBet = initialBet;
this.newRound(playerMoney);
}
/**
* Starts a new game round
*
* @param playerMoney Money for individual players
*/
Game.prototype.newRound = function (playerMoney) {
var _this = this;
this.pot = 0;
this.deck = new deck_1.Deck();
this.deck.shuffle();
this.table = [];
this.round = [];
this.__roundStates = [];
this.players = playerMoney.map(function (money) {
return {
money: money,
hand: _this.deck.getCards(2),
folded: false,
active: true
};
});
};
/**
* Returns the current game state
*/
Game.prototype.getState = function () {
var _this = this;
return {
communityCards: this.table.slice(0),
pot: this.round.reduce(function (a, c) { return a + c.money; }, this.pot),
players: this.players.map(function (p, i) {
var availableActions = [];
if (p.active) {
if (_this.table.length == 0) {
availableActions.push("bet", "fold");
}
else if (_this.round.length) {
if (_this.round[i].decision != "fold") {
availableActions.push("raise", "call", "check", "fold");
}
}
}
return {
money: p.money,
hand: p.hand,
folded: p.folded,
active: p.active,
currentDecision: (_this.round.length && _this.round[i].decision) || '',
currentBet: (_this.round.length && _this.round[i].money) || 0,
availableActions: availableActions
};
})
};
};
/**
* Starts the round if not started yet
*/
Game.prototype.startRound = function () {
var activePlayers = 0;
for (var i = 0; i < this.players.length; i++) {
if (this.players[i].money < 0)
this.players[i].active = false;
else {
activePlayers++;
}
this.round[i] = {
money: 0, //this.players[i].active?this.initialBet:0
};
}
if (activePlayers == 1) {
this.round = [];
throw new Error("Game cannot continue");
}
};
/**
* Bet the initial betting amount
*
* @param index Player index
*/
Game.prototype.bet = function (index) {
if (!this.round.length)
throw new Error("Game round not started");
if (this.round[index].decision)
throw new Error("Please proceed to next round");
this.round[index].money = this.initialBet;
this.round[index].decision = "bet";
};
/**
* Bet 0 unit of money
*
* @param index Player index
*/
Game.prototype.check = function (index) {
if (!this.round.length)
throw new Error("Game round not started");
if (this.round[index].decision)
throw new Error("Please proceed to next round");
if (this.table.length == 0)
throw new Error("Cannot check in opening round");
this.round[index].money = 0;
this.round[index].decision = "check";
};
/**
* Raise by a player
*
* @param index Player index
* @param money Raise amount
*/
Game.prototype.raise = function (index, money) {
if (!this.round.length)
throw new Error("Game round not started");
if (this.players[index].money < money)
throw new Error('Too much');
if (this.round[index].decision && this.round[index].decision != "raise")
throw new Error("Please proceed to next round");
var raised_money = money;
if (!this.table.length)
raised_money = money + this.initialBet;
this.round[index].money = raised_money;
this.round[index].decision = "raise";
};
/**
* Call by a player
*
* @param index Player index
*/
Game.prototype.call = function (index) {
if (!this.round.length)
throw new Error("Game round not started");
//if(this.round[index].decision) throw new Error("Please proceed to next round");
var max_money = this.round.slice(0).sort(function (a, b) { return b.money - a.money; })[0].money;
if (max_money == 0)
max_money = this.initialBet; //fall back to initial betting amount
this.round[index].money = max_money;
this.round[index].decision = "call";
};
/**
* Fold by a player
*
* @param index Player index
*/
Game.prototype.fold = function (index) {
if (!this.round.length)
throw new Error("Game round not started");
if (this.round[index].decision)
throw new Error("Please proceed to next round");
this.round[index].decision = "fold";
this.players[index].folded = true;
};
/**
* Whether the current round can be ended
*/
Game.prototype.canEndRound = function () {
var last_amount = -1;
for (var i = 0; i < this.round.length; i++) {
if (this.round[i].decision == "fold")
continue;
var money = this.round[i].money;
if (last_amount < 0)
last_amount = money;
else if (money != last_amount)
return false;
}
return true;
};
/**
* Ends the current round.
*/
Game.prototype.endRound = function () {
if (!this.round.length)
throw new Error("Game round not started");
if (this.__roundStates.length == 4)
throw new Error("Round is over, please invoke checkResult");
var last_amount = -1;
var pot = 0;
for (var i = 0; i < this.round.length; i++) {
if (this.round[i].decision == "fold")
continue;
var money = this.round[i].money;
if (last_amount < 0)
last_amount = money;
else if (money != last_amount)
throw new Error("Round is not over yet, please call or raise.");
pot += money;
}
this.pot += pot;
this.__roundStates.push(this.round.slice(0));
if (this.__roundStates.length < 4) {
var communityCardCountForThisRound = 1;
if (this.table.length == 0)
communityCardCountForThisRound = 3;
this.table.push.apply(this.table, this.deck.getCards(communityCardCountForThisRound));
}
};
/**
* Returns the result of the current round
*/
Game.prototype.checkResult = function () {
var result = this.__instance.compareHands(this.players.map(function (m) { return m.hand; }), this.table);
if (result.type == 'win') {
if (result.index != undefined)
this.players[result.index].money += this.pot;
else
throw new Error("This error will never happen");
}
else { //draw
//split the pot among non-folded players
var splitCount = this.players.filter(function (f) { return !f.folded; }).length;
if (splitCount > 0) {
var eachSplit = this.pot / splitCount;
for (var i = 0; i < this.players.length; i++) {
if (!this.players[i].folded)
this.players[i].money += eachSplit;
}
}
}
this.newRound(this.players.map(function (m) { return m.money; }));
return result;
};
/**
* Returns the max possible hand value.
* Note: Community cards are ignored.
*
* @param hand Array of cards
*/
Game.prototype.computeHand = function (hand) {
return this.__instance.computeHand(hand, hand);
};
return Game;
}());
exports.Game = Game;
},{"./Holdem":4,"./deck":6}],4:[function(require,module,exports){
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Holdem = exports.DESC = void 0;
var TieBreaker_1 = require("./TieBreaker");
var DESC = function (a, b) { return b.value - a.value; };
exports.DESC = DESC;
var ASC = function (a, b) { return a.value - b.value; };
var VASC = function (a, b) { return a - b; };
var HAND_HIGHCARD = "High Card";
var HAND_PAIR = "Pair";
var HAND_TWOPAIRS = "Two pairs";
var HAND_THREEOFAKIND = "Three of a kind";
var HAND_STRAIGHT = "Straight";
var HAND_FLUSH = "Flush";
var HAND_FULLHOUSE = "Full House";
var HAND_FOUROFAKIND = "Four of a kind";
var HAND_STRAIGHTFLUSH = "Straight flush";
var HAND_ROYALFLUSH = "Royal flush";
var Holdem = /** @class */ (function () {
function Holdem() {
this.test_cache = {
high_card: {
suit: '',
value: 0,
kicker: {
suit: '',
value: 0,
}
},
pair: {
value: 0,
index: -1
},
two_pairs: {
all_indices: [],
pair_indices: []
},
three_of_a_kind: {
value: 0,
indices: []
},
four_of_a_kind: 0,
straight: { result: false },
flush: { result: false },
straight_flush: false,
};
this.test_summary = {
value: {},
suit: {}
};
this.TestSeries = [
{ name: HAND_HIGHCARD, fn: this.TestHighCard },
{ name: HAND_PAIR, fn: this.TestPair },
{ name: HAND_TWOPAIRS, fn: this.TestTwoPairs },
{ name: HAND_THREEOFAKIND, fn: this.TestThreeOfAKind },
{ name: HAND_STRAIGHT, fn: this.TestStraight },
{ name: HAND_FLUSH, fn: this.TestFlush },
{ name: HAND_FULLHOUSE, fn: this.TestFullHouse },
{ name: HAND_FOUROFAKIND, fn: this.TestFourOfAKind },
{ name: HAND_STRAIGHTFLUSH, fn: this.TestStraightFlush },
{ name: HAND_ROYALFLUSH, fn: this.TestRoyalFlush }
];
/*computeHandAllPartialStat(hand:Array<Card>){
let stat = {
high_card:0,
pair:0,
two_pairs:0,
three_of_a_kind:0,
straight:0,
flush:0,
full_house:0,
four_of_a_kind:0,
straight_flush:0,
royal_flush:0
};
let result = this.computeHand(hand,hand);
}*/
/** TODO
Maximum number of raises
Most fixed-limit games will not allow more than a predefined number of raises in a betting round.
The maximum number of raises depends on the casino house rules, and is usually posted conspicuously
in the card room. Typically, an initial bet plus either three or four raises are allowed.
Consider this example in a $20/$40 game, with a posted limit of a bet and three raises.
During a $20 round with three players, play could proceed as follows:
Player A bets $20.
Player B puts in another bet, raises another $20, making it $40 to play.
Player C puts in a third bet, raising another $20 on that, thus making it $60 to play.
Player A puts in the fourth bet (they are usually said to cap the betting).
Once Player A has made their final bet, Players B and C may only call another two and one
bets (respectively); they may not raise again because the betting is capped.
A common exception in this rule practiced in some card rooms is to allow unlimited raising
when a pot is played heads up (when only two players are in the hand at the start of the
betting round). Usually, this has occurred because all other players have folded, and only
two remain, although it is also practiced when only two players get dealt in. Many card rooms
will permit these two players to continue re-raising each other until one player is all in.
*/
}
//Simple value of the card. Lowest: 2 - Highest: Ace(14)
Holdem.prototype.TestHighCard = function (hand, mainCards) {
var _a = mainCards.slice(0).sort(exports.DESC), high_card = _a[0], kicker = _a[1];
this.test_cache.high_card = {
suit: "".concat(high_card.suit),
value: high_card.value,
kicker: {
suit: "".concat(kicker.suit),
value: kicker.value
}
};
return high_card.value;
};
//Two cards with the same value
Holdem.prototype.TestPair = function (hand, mainCards) {
for (var key in this.test_summary.value)
if (this.test_summary.value[key].length == 2) {
this.test_cache.pair = { value: Number(key), index: this.test_summary.value[key][0] };
return true;
}
return false;
};
//Two times two cards with the same value
Holdem.prototype.TestTwoPairs = function (hand, mainCards) {
var all_indices = [];
var pair_indices = [];
for (var key in this.test_summary.value) {
if (this.test_summary.value[key].length == 2) {
var _a = this.test_summary.value[key], a = _a[0], b = _a[1];
pair_indices.push(a);
all_indices.push(a);
all_indices.push(b);
}
}
if (pair_indices.length == 2) {
this.test_cache.two_pairs = { all_indices: all_indices, pair_indices: pair_indices };
return true;
}
};
//Three cards with the same value
Holdem.prototype.TestThreeOfAKind = function (hand, mainCards) {
for (var key in this.test_summary.value) {
if (this.test_summary.value[key].length == 3) {
this.test_cache.three_of_a_kind.value = Number(key);
this.test_cache.three_of_a_kind.indices = this.test_summary.value[key];
return true;
}
}
};
//Sequence of 5 cards in increasing value (Ace can precede 2 and follow up King)
Holdem.prototype.TestStraight = function (hand, mainCards) {
var seq_count = 0;
var seq_start = -1;
var max_seq_count = 0;
var max_seq_start = -1;
var seq = hand.slice(0).sort(ASC);
for (var i = 0; i < seq.length - 1; i++) {
if (seq[i].value === seq[i + 1].value - 1) {
if (seq_start < 0)
seq_start = i;
seq_count++;
}
else if (seq_count < 4) {
// save max progress
if (max_seq_count < seq_count) {
max_seq_count = seq_count;
max_seq_start = seq_start;
}
seq_count = 0;
seq_start = -1;
}
}
// restore max progress
if (max_seq_count > seq_count) {
seq_count = max_seq_count;
seq_start = max_seq_start;
}
// sequence count is always one less because we check next card
var result = seq_count >= 4 || (seq_count == 3 && seq_start == 0 && seq[seq.length - 1].value == 14);
this.test_cache.straight.result = result;
if (result) {
this.test_cache.straight.start = hand.findIndex(function (f) { return f.suit == seq[seq_start].suit && f.value == seq[seq_start].value; });
}
return result;
};
//5 cards of the same suit
Holdem.prototype.TestFlush = function (hand, mainCards) {
for (var key in this.test_summary.suit) {
if (this.test_summary.suit[key].length == 5) {
this.test_cache.flush = { suit: key, result: true };
return true;
}
}
this.test_cache.flush = {
result: false
};
};
//Combination of three of a kind and a pair
Holdem.prototype.TestFullHouse = function (hand, mainCards) {
if (this.test_cache.three_of_a_kind.indices.length == 3 && this.test_cache.pair.value >= 2) {
return this.test_cache.three_of_a_kind.value !== this.test_cache.pair.value;
}
};
//Four cards of the same value
Holdem.prototype.TestFourOfAKind = function (hand, mainCards) {
for (var key in this.test_summary.value) {
if (this.test_summary.value[key].length == 4) {
this.test_cache.four_of_a_kind = Number(key);
return true;
}
}
};
//Straight of the same suit
Holdem.prototype.TestStraightFlush = function (hand, mainCards) {
if (this.test_cache.straight.result && this.test_cache.straight.start !== undefined && this.test_cache.flush.result) {
if (hand[this.test_cache.straight.start].suit === this.test_cache.flush.suit) {
this.test_cache.straight_flush = true;
return true;
}
}
};
//Straight flush from Ten to Ace
Holdem.prototype.TestRoyalFlush = function (hand, mainCards) {
return this.test_cache.straight_flush && this.test_cache.straight.start !== undefined && hand[this.test_cache.straight.start].value == 10;
};
Holdem.prototype.computeHand = function (allcards, mainCards) {
var _this = this;
this.test_cache = {
high_card: { suit: '', value: 0, kicker: { suit: '', value: 0 } },
pair: { value: 0, index: -1 },
two_pairs: {
all_indices: [],
pair_indices: []
},
three_of_a_kind: {
value: 0,
indices: []
},
four_of_a_kind: 0,
straight: { result: false },
flush: { result: false },
straight_flush: false,
};
this.test_summary = {
value: {},
suit: {}
};
for (var i = 0; i < allcards.length; i++) {
var suit = allcards[i].suit + '';
var value = allcards[i].value;
if (!this.test_summary.suit[suit])
this.test_summary.suit[suit] = [i];
else
this.test_summary.suit[suit].push(i);
if (!this.test_summary.value[value])
this.test_summary.value[value] = [i];
else
this.test_summary.value[value].push(i);
}
var sorted_summary = this.TestSeries.map(function (t, i) { return i == 0 ? t.fn.call(_this, allcards, mainCards) : (t.fn.call(_this, allcards, mainCards) ? (i + 14) : 0); }).sort(VASC);
var result = sorted_summary.pop();
return {
name: this.TestSeries[Math.max(result - 14, 0)].name,
value: result
};
};
Holdem.prototype.compareHands = function (hands, community) {
var _this = this;
var ranks = hands.map(function (f, index) {
var hand = f.concat(community);
return __assign(__assign({}, _this.computeHand(hand, f)), { index: index, cache: Object.assign({}, _this.test_cache), hand: hand });
}).sort(exports.DESC);
if (ranks[0].value > ranks[1].value) {
var result = { type: "win", index: ranks[0].index, name: ranks[0].name };
if (result.name == HAND_HIGHCARD)
result.suit = ranks[0].cache.high_card.suit;
return result;
}
else {
//console.log(JSON.stringify(ranks, null, 1));
var highest_rank_name_1 = ranks[0].name;
var conflict = TieBreaker_1.TieBreaker[highest_rank_name_1](ranks.filter(function (r) { return r.name == highest_rank_name_1; }));
var result = __assign(__assign({}, conflict), {
//put the highest rank name
name: highest_rank_name_1 });
return result;
}
};
return Holdem;
}());
exports.Holdem = Holdem;
},{"./TieBreaker":5}],5:[function(require,module,exports){
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TieBreaker = void 0;
var highCardTie = function (ranks, high) {
if (high === void 0) { high = true; }
var max_card_index = -1;
var max_card_value = 0;
var max_card_map = {};
for (var i = 0; i < ranks.length; i++) {
var value = high ? ranks[i].cache.high_card.value : ranks[i].cache.high_card.kicker.value;
if (max_card_value <= value) {
max_card_value = value;
max_card_index = i;
if (!max_card_map[max_card_value])
max_card_map[max_card_value] = 1;
else
max_card_map[max_card_value]++;
}
}
if (max_card_index >= 0) {
if (max_card_map[max_card_value] == 1) {
return {
type: 'win',
index: ranks[max_card_index].index,
value: max_card_value,
tieBreak: max_card_value
};
}
}
return { type: 'draw' };
};
var BreakTieUsingHighCard = function (ranks) {
var result = highCardTie(ranks);
if (result.type === "draw") {
result = highCardTie(ranks, false);
}
return result;
};
exports.TieBreaker = {
/**
* When No Player Has Even A Pair, Then The Highest Card Wins. When Both Players Have Identical High Cards,
* The Next Highest Card Wins, And So On Until Five Cards Have Been Used. In The Unusual Circumstance
* That Two Players Hold The Identical Five Cards, The Pot Would Be Split.
*
*/
"High Card": BreakTieUsingHighCard,
/**
* If Two Or More Players Hold A Single Pair Then Highest Pair Wins. If The Pairs Are Of The Same Value,
* The Highest Kicker Card Determines The Winner. A Second And Even Third Kicker Can Be Used If Necessary
*/
"Pair": function (ranks) {
var max_pair_index = -1;
var max_pair_val = 0;
var max_pair_counter = {};
for (var i = 0; i < ranks.length; i++) {
if (max_pair_val <= ranks[i].cache.pair.value) {
max_pair_val = ranks[i].cache.pair.value;
max_pair_index = i;
if (!max_pair_counter['max' + max_pair_val])
max_pair_counter['max' + max_pair_val] = [i];
else
max_pair_counter['max' + max_pair_val].push(i);
}
}
if (max_pair_index >= 0) {
if (max_pair_counter['max' + max_pair_val].length == 1) {
return { type: 'win', index: ranks[max_pair_index].index, value: max_pair_val };
}
var candidates = max_pair_counter['max' + max_pair_val].map(function (m) {
var pair_value = ranks[m].cache.pair.value;
return __assign(__assign({}, ranks[m]), { hand: ranks[m].hand.filter(function (f) { return f.value != pair_value; }) });
});
return exports.TieBreaker["High Card"](candidates);
}
return { type: 'draw' };
},
/**
* The Highest Pair Is Used To Determine The Winner. If Two Or More Players Have The Same Highest Pair,
* Then The Highest Of The Second Pair Determines The Winner. If Both Players Hold Identical Two Pairs,
* The Fifth Card Is Used To Break The Tie.
*/
"Two pairs": function (ranks) {
for (var check_highest = 0; check_highest < 2; check_highest++) {
var max_pair_index = -1;
var max_pair_val = 0;
var max_pair_counter = {};
for (var i = 0; i < ranks.length; i++) {
var _a = ranks[i].cache.two_pairs.pair_indices, pa = _a[0], pb = _a[1];
var my_highest_pair_index = void 0;
if (ranks[i].hand[pa].value < ranks[i].hand[pb].value) {
my_highest_pair_index = check_highest == 0 ? pb : pa;
}
else {
my_highest_pair_index = check_highest == 0 ? pa : pb;
}
if (max_pair_val <= ranks[i].hand[my_highest_pair_index].value) {
max_pair_val = ranks[i].hand[my_highest_pair_index].value;
max_pair_index = i;
if (!max_pair_counter['max' + max_pair_val])
max_pair_counter['max' + max_pair_val] = [i];
else
max_pair_counter['max' + max_pair_val].push(i);
}
}
if (max_pair_index >= 0) {
if (max_pair_counter['max' + max_pair_val].length == 1) {
return { type: 'win', index: ranks[max_pair_index].index, value: max_pair_val };
}
}
}
//compare the last card
var max_hand_value = 0;
var max_hand_index = -1;
var max_hand_counter = {};
var _loop_1 = function (i) {
var hand = ranks[i].hand;
ranks[i].cache.two_pairs.all_indices.sort(function (a, b) { return b - a; }).forEach(function (f) { return hand.splice(f, 1); });
if (max_hand_value <= hand[0].value) {
max_hand_index = i;
max_hand_value = hand[0].value;
if (!max_hand_counter['max' + max_hand_value])
max_hand_counter['max' + max_hand_value] = 1;
else
max_hand_counter['max' + max_hand_value]++;
}
};
for (var i = 0; i < ranks.length; i++) {
_loop_1(i);
}
if (max_hand_index >= 0) {
if (max_hand_counter['max' + max_hand_value] == 1)
return { type: 'win', index: ranks[max_hand_index].index, value: max_hand_value };
}
return { type: 'draw' };
},
/**
* If More Than One Player Holds Three Of A Kind, Then The Higher Value Of The Cards Used To Make
* The Three Of A Kind Determines The Winner. If Two Or More Players Have The Same Three Of A Kind,
* Then A Fourth Card (And A Fifth If Necessary) Can Be Used As Kickers To Determine The Winner.
*/
"Three of a kind": function (ranks) {
var max_pair_index = -1;
var max_pair_val = 0;
var max_pair_counter = {};
for (var i = 0; i < ranks.length; i++) {
var val = ranks[i].cache.three_of_a_kind.value;
if (max_pair_val <= val) {
max_pair_val = val;
max_pair_index = i;
if (!max_pair_counter['max' + max_pair_val])
max_pair_counter['max' + max_pair_val] = [i];
else
max_pair_counter['max' + max_pair_val].push(i);
}
}
if (max_pair_index >= 0) {
if (max_pair_counter['max' + max_pair_val].length == 1) {
return { type: 'win', index: ranks[max_pair_index].index, value: max_pair_val };
}
var candidates = max_pair_counter['max' + max_pair_val].map(function (m) {
var pair_value = ranks[m].cache.three_of_a_kind.value;
return __assign(__assign({}, ranks[m]), { hand: ranks[m].hand.filter(function (f) { return f.value != pair_value; }) });
});
return exports.TieBreaker["High Card"](candidates);
}
return { type: 'draw' };
},
/**
* A Straight Is Any Five Cards In Sequence, But Not Necessarily Of The Same Suit. If More Than
* One Player Has A Straight, The Straight Ending In The Card Wins. If Both Straights End In A Card
* Of The Same Strength, The Hand Is Tied.
*/
"Straight": BreakTieUsingHighCard,
/**
* A Flush Is Any Hand With Five Cards Of The Same Suit. If Two Or More Players Hold A Flush,
* The Flush With The Highest Card Wins. If More Than One Player Has The Same Strength High
* Card, Then The Strength Of The Second Highest Card Held Wins. This Continues Through The Five
* Highest Cards In The Player's Hands.
*/
"Flush": BreakTieUsingHighCard,
/**
* When Two Or More Players Have Full Houses, We Look First At The Strength Of The Three Of A
* Kind To Determine The Winner. For Example, Aces Full Of Deuces (AAA22) Beats Kings Full Of
* Jacks (KKKJJ). If There Are Three Of A Kind On The Table (Community Cards) In A Texas Holdem
* Game That Are Used By Two Or More Players To Make A Full House, Then We Would Look At The
* Strength Of The Pair To Determine A Winner.
*/
"Full House": function (ranks) {
for (var check_3k = 0; check_3k < 2; check_3k++) {
var max_3k_index = -1;
var max_3k_val = -1;
var max_3k_counter = {};
for (var i = 0; i < ranks.length; i++) {
var val = ranks[i].cache[check_3k == 0 ? 'three_of_a_kind' : 'pair'].value;
if (max_3k_val <= val) {
max_3k_index = i;
if (!max_3k_counter[max_3k_val])
max_3k_counter[max_3k_val] = 1;
else
max_3k_counter[max_3k_val]++;
}
}
if (max_3k_index >= 0) {
if (max_3k_counter[max_3k_val] == 1) {
return { type: 'win', index: ranks[max_3k_index].index, value: max_3k_val };
}
}
}
return { type: 'draw' };
},
/**
* This One Is Simple. Four Aces Beats Any Other Four Of A Kind, Four Kings Beats Four Queens
* Or Less And So On. The Only Tricky Part Of A Tie Breaker With Four Of A Kind Is When The Four
* Falls On The Table In A Game Of Texas Holdem And Is Therefore Shared Between Two (Or More)
* Players. A Kicker Can Be Used, However, If The Fifth Community Card Is Higher Than Any Card
* Held By Any Player Still In The Hand, Then The Hand Is Considered A Tie And The Pot Is Split.
*/
"Four of a kind": function (ranks) {
for (var check_4k = 0; check_4k < 2; check_4k++) {
var max_4k_index = -1;
var max_4k_val = 0;
var max_4k_counter = {};
var _loop_2 = function (i) {
var val = ranks[i].cache.four_of_a_kind;
if (check_4k == 1) {
try {
val = ranks[i].hand.filter(function (f) { return f.value != val; })[0].value;
}
catch (e) {
return "break";
}
}
if (max_4k_val <= val) {
max_4k_val = val;
max_4k_index = i;
if (!max_4k_counter[max_4k_val])
max_4k_counter[max_4k_val] = 1;
else
max_4k_counter[max_4k_val]++;
}
};
for (var i = 0; i < ranks.length; i++) {
var state_1 = _loop_2(i);
if (state_1 === "break")
break;
}
if (max_4k_index >= 0) {
if (max_4k_counter[max_4k_val] == 1)
return { type: 'win', index: ranks[max_4k_index].index, value: max_4k_val };
}
}
return { type: 'draw' };
},
/**
* Straight Flushes Come In Varying Strengths From Five High To A King High. A King High Straight
* Flush Loses Only To A Royal. If More Than One Player Has A Straight Flush, The Winner Is The
* Player With The Highest Card Used In The Straight. A Queen High Straight Flush Beats A Jack
* High And A Jack High Beats A Ten High And So On. The Suit Never Comes Into Play I.E. A Seven
* High Straight Flush Of Diamonds Will Split The Pot With A Seven High Straight Flush Of Hearts.
*/
"Straight flush": BreakTieUsingHighCard,
/**
* An Ace-High Straight Flush Is Called Royal Flush. A Royal Flush Is The Highest Hand In Poker.
* Between Two Royal Flushes, There Can Be No Tie Breaker. If Two Players Have Royal Flushes,
* They Split The Pot.
*/
"Royal flush": function (ranks) {
return { type: 'draw' };
}
};
},{}],6:[function(require,module,exports){
arguments[4][2][0].apply(exports,arguments)
},{"./Card":1,"dup":2}],7:[function(require,module,exports){
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Suits = exports.Card = exports.Deck = exports.Game = void 0;
var Game_1 = require("./Game");
Object.defineProperty(exports, "Game", { enumerable: true, get: function () { return Game_1.Game; } });
var Deck_1 = require("./Deck");
Object.defineProperty(exports, "Deck", { enumerable: true, get: function () { return Deck_1.Deck; } });
var Card_1 = require("./Card");
Object.defineProperty(exports, "Card", { enumerable: true, get: function () { return Card_1.Card; } });
Object.defineProperty(exports, "Suits", { enumerable: true, get: function () { return Card_1.Suits; } });
},{"./Card":1,"./Deck":2,"./Game":3}]},{},[7])(7)
});