-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPokerGame.java
More file actions
995 lines (908 loc) · 35.9 KB
/
PokerGame.java
File metadata and controls
995 lines (908 loc) · 35.9 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
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
import java.util.*;
class PokerGame {
private static final ThreadLocal<PokerGame> ACTIVE_GAME = new ThreadLocal<>();
private PokerDeck p = new PokerDeck();
private PokerPlayer[] players;
private int blinds = 20; // current blinds size
private int hands; // original first player, used for blind increasing
private int lastPlayer; // keeps track of the last player to act
private int[] currAction; // keeps track of current players action
private Scanner sc = Player.sc;
private PokerPot pot; // a pokerpot object which keeps track of player contributions and the pot
private int currBet; // current bet for round
private int[] currConts; // current contributions this round
private PlayerStat mp; // keeps track of players stats for the game
private PokerPlayer mainPlayer; // original player
private boolean skipMode = false; // keeps track of whether we are fast-forwarding
private int preflopAggressorIndex = -1;
private int lastRaise = 0; // Tracks the minimum legal increment for the current round
private static final double PREFLOP_EMA_ALPHA = 0.35; // >80% weight in roughly last 5 actions
private static final double POSTFLOP_EMA_ALPHA = 0.35; // >80% weight in roughly last 5 actions
private boolean[] preflopVPIPFlags;
private boolean[] preflopPFRFlags;
private int[][] postflopAggressionActions;
private int[][] postflopAggressionOpportunities;
private boolean[] sawFlopThisHand;
private boolean[] foldToCbetOpportunity;
private boolean[] foldedToCbet;
private boolean cbetFiredOnFlop = false;
private int cbetAggressorIndex = -1;
// Smart HU leak tracking (live table path): mirrors simulator observations.
private boolean huSmartGod = false;
private String huSmartName = null;
private int huSmartIdx = -1;
private int huGodIdx = -1;
private int huPreflopRaiseCount = 0;
private int huPreflopLastRaiser = -1;
private boolean huSmartFacing3BetPending = false;
private boolean huFlopCbetResponsePending = false;
private boolean huGodCbetFlopSeen = false;
private int huTurnFirstActor = -1;
private boolean huTurnFirstActorChecked = false;
private boolean huTurnCheckBackObserved = false;
private boolean huTurnBarrelResponsePending = false;
private boolean huTurnBarrelSeen = false;
private boolean huRiverLargeBetResponsePending = false;
private boolean huRiverLargeBetSeen = false;
// PHASE 8 COGNITIVE MATRIX: Street Tracking State
// 0 = Preflop, 1 = Flop, 2 = Turn, 3 = River
public int currentStreet = 0;
public boolean isShowdown = false;
public PokerGame(PokerPlayer[] players) { // initialiaze a poker game
ACTIVE_GAME.set(this);
this.players = players;
mainPlayer = players[0];
mp = new PlayerStat(0);
for (int i = 0; i < players.length; i++) { // randomizes bot chip amount
if (players[i] instanceof PokerBot) {
if (Math.random() >= 0.5) {
players[i].addChips((int) (Math.random() * 150));
} else {
players[i].removeChips((int) (Math.random() * 150));
}
}
}
// shuffles player positions
List<PokerPlayer> temp = Arrays.asList(players);
Collections.shuffle(temp);
this.players = temp.toArray(new PokerPlayer[players.length]);
pot = new PokerPot(this.players);
}
public static PokerGame getActiveGame() {
return ACTIVE_GAME.get();
}
public int getCurrentHandNumber() {
return hands + 1;
}
public PlayerStat getStats() {
return mp;
}
private String cardsToString(Card[] cards) {
if (cards == null || cards.length == 0)
return "(none)";
StringBuilder sb = new StringBuilder();
for (int i = 0; i < cards.length; i++) {
if (cards[i] != null)
sb.append(cards[i].getValue()).append(" ");
}
return sb.toString().trim();
}
private String seatRoleTag(int idx) {
if (idx == 0)
return "SB";
if (idx == 1)
return "BB";
if (idx == players.length - 1)
return "D";
return "-";
}
private String getTransparencySnapshot(String phase, Card[] board, int actingIndex) {
if (!BotDiagnostics.enabled()) {
return "";
}
StringBuilder sb = new StringBuilder();
String boardText = (board == null) ? "(preflop)" : cardsToString(board);
sb.append("[TABLE TRACE][").append(phase).append("] board=").append(boardText).append(", pot=")
.append(pot.getTotalPot())
.append(", tableBet=").append(currBet).append(", lastRaise=").append(lastRaise).append("\n");
for (int seat = 0; seat < players.length; seat++) {
PokerPlayer pl = players[seat];
if (pl == null)
continue;
int contributed = (currConts != null && seat < currConts.length) ? currConts[seat] : 0;
String acting = (seat == actingIndex) ? " <- ACTING" : "";
sb.append(" seat=").append(seat).append(" [").append(seatRoleTag(seat)).append("] name=").append(pl.getName())
.append(", inHand=")
.append(pl.inHand()).append(", chips=").append(pl.getChips()).append(", contributed=").append(contributed)
.append(", hole=")
.append(cardsToString(pl.getHand())).append(acting).append("\n");
}
sb.append("\n");
return sb.toString();
}
public void init() { // start the main loop
System.out
.println("\n\nWelcome to the Texas Hold'em table!\nHere are the players in today and their current buy-ins!");
for (PokerPlayer p : players) {
System.out.println(p.toString());
}
System.out.println("Press enter to continue...");
sc.nextLine();
Utils.clearScreen();
players[0].setStatus(1);
players[1].setStatus(2);
preflop();
}
private void endGame(boolean t) {
Utils.clearScreen();
System.out.println("\nGame Over! " + ((t) ? " You ran out of primogems ✨ :(" : "You ended the game early."));
System.out.println(mp);
}
private void preflop() { // code to execute preflop
resetHuSmartLeakTracking();
currentStreet = 0;
isShowdown = false; // PHASE 8: Reset showdown state
currBet = blinds;
lastRaise = blinds; // Pre-flop min raise is 1BB
currConts = new int[players.length];
preflopVPIPFlags = new boolean[players.length];
preflopPFRFlags = new boolean[players.length];
postflopAggressionActions = new int[players.length][4];
postflopAggressionOpportunities = new int[players.length][4];
sawFlopThisHand = new boolean[players.length];
foldToCbetOpportunity = new boolean[players.length];
foldedToCbet = new boolean[players.length];
cbetFiredOnFlop = false;
cbetAggressorIndex = -1;
lastPlayer = 2;
Card[][] holeCards = p.deal(players.length);
for (int i = 0; i < players.length; i++) {
players[i].setHand(holeCards[i]);
// PHASE 8/9 COGNITIVE MATRIX: Update profile each hand
if (shouldTrackCognitivePlayer(players[i])) {
String pName = players[i].getName();
PokerBot.CognitiveProfile profile = PokerBot.getOrCreateCognitiveProfile(pName);
profile.handsPlayed++;
profile.currentStackBB = (blinds > 0) ? (double) players[i].getChips() / blinds : 100.0;
profile.ltmAlpha = PokerSimulator.ltmAlpha;
}
}
currAction = new int[2];
if (blinds / 2 < players[0].getChips()) {
if (players[0] == mainPlayer)
mp.addBet(blinds / 2);
pot.addPlayerContribution(0, blinds / 2);
currConts[0] += blinds / 2;
} else {
if (players[0] == mainPlayer) {
mp.addBet(players[0].getChips());
mp.allIn();
}
pot.addPlayerContribution(0, players[0].getChips());
currConts[0] += players[0].getChips();
}
if (blinds < players[1].getChips()) {
if (players[1] == mainPlayer)
mp.addBet(blinds);
pot.addPlayerContribution(1, blinds);
currConts[1] += blinds;
} else {
if (players[1] == mainPlayer) {
mp.addBet(players[1].getChips());
mp.allIn();
}
pot.addPlayerContribution(1, players[1].getChips());
currConts[1] += players[1].getChips();
}
int i = 2;
String roundName = "*** PREFLOP ***\n";
String realPlayerOrder = "Real player order: ";
int count = 0;
for (int k = 0; k < players.length; k++) {
int idx = (i + k) % players.length;
if (!(players[idx] instanceof PokerBot) && players[idx].inHand()) {
realPlayerOrder += players[idx].getName() + " -> ";
count++;
}
}
if (count > 0) {
realPlayerOrder = realPlayerOrder.substring(0, realPlayerOrder.length() - 4) + "\n";
roundName += realPlayerOrder;
}
String positionMap = "Action order: ";
for (int k = 0; k < players.length; k++) {
int idx = (i + k) % players.length;
if (players[idx].inHand()) {
positionMap += players[idx].getName();
if (idx == 0)
positionMap += " (SB)";
else if (idx == 1)
positionMap += " (BB)";
else if (idx == players.length - 1)
positionMap += " (D)";
positionMap += " -> ";
}
}
if (positionMap.length() > 14) {
positionMap = positionMap.substring(0, positionMap.length() - 4) + "\n\n";
roundName += positionMap;
} else {
roundName += "\n";
}
Utils.clearScreen();
System.out.print(getTransparencySnapshot("PREFLOP-DEAL", null, -1));
System.out.print(roundName + pot.toString() + "\n\n");
String roundHistory = "";
do {
if (players[i].inHand() && players[i].getChips() > 0) {
Utils.clearScreen();
System.out.print(getTransparencySnapshot("PREFLOP-TURN", null, i));
System.out.print(roundName + pot.toString() + "\n\n" + roundHistory);
String turnHeader = players[i].getName().toUpperCase() + "'s turn!\n";
if (players[i] instanceof PokerBot) {
turnHeader += "Their stack: ✨" + players[i].getChips() + "\n";
System.out.print(turnHeader);
if (!skipMode)
Utils.sleep(1000);
} else {
System.out.print(turnHeader);
}
if (players[i] instanceof PokerBot) {
PokerBot temp = (PokerBot) players[i];
int livePot = pot.getTotalPot();
for (int c : currConts)
livePot += c;
currAction = temp.action("preflop", currConts[i], currBet, blinds, lastRaise, null, livePot, players, i,
preflopAggressorIndex, 0, 1);
} else {
System.out.println("Are you " + players[i].getName() + "? Press Enter to confirm and show your hand...");
Utils.flushInput();
sc.nextLine();
Utils.clearScreen();
System.out.print(getTransparencySnapshot("PREFLOP-TURN", null, i));
System.out.print(roundName + pot.toString() + "\n\n" + roundHistory);
System.out.print(turnHeader);
currAction = players[i].action("preflop", currConts[i], currBet, blinds, lastRaise);
}
String actionLog = handleAction(i);
if (currAction[0] == 3 || currAction[0] == 4) {
if (currAction[1] > 0)
preflopAggressorIndex = i;
}
if (!(players[i] instanceof PokerBot))
actionLog += " (real)";
// Reprint with updated state after action.
turnHeader = players[i].getName().toUpperCase() + "'s turn!\n";
turnHeader += "Their stack: ✨" + players[i].getChips() + "\n";
Utils.clearScreen();
System.out.print(getTransparencySnapshot("PREFLOP-AFTER-ACTION", null, i));
System.out.print(roundName + pot.toString() + "\n\n" + roundHistory);
System.out.print(turnHeader);
System.out.println(actionLog + "\n");
roundHistory += turnHeader + actionLog + "\n\n";
if (!skipMode)
Utils.sleep(1000);
}
if (i == players.length - 1)
i = 0;
else
i++;
} while (i != lastPlayer && stillIn() > 1);
finalizePreflopTelemetry();
Utils.clearScreen();
System.out.print(getTransparencySnapshot("PREFLOP-END", null, -1));
System.out.print(roundName + pot.toString() + "\n\n" + roundHistory);
if (!skipMode && realPlayersIn() == 0) {
System.out.println("Type \"skip\" to fast forward the hand, or hit enter to continue:");
Utils.flushInput();
Player.sc = new Scanner(System.in);
sc = Player.sc;
String input = sc.nextLine().strip().toLowerCase();
if (input.equals("skip"))
skipMode = true;
} else if (!skipMode) {
System.out.println("Press Enter to continue:");
Utils.flushInput();
Player.sc = new Scanner(System.in);
sc = Player.sc;
sc.nextLine();
}
Utils.clearScreen();
if (stillIn() < 2)
showdown(0);
else
postflop();
}
private void postflop() { // all code to execute postflop, including flop, turn and river
currConts = new int[players.length];
for (int idx = 0; idx < players.length; idx++) {
sawFlopThisHand[idx] = players[idx].inHand();
}
cbetFiredOnFlop = false;
cbetAggressorIndex = -1;
Card[] b = p.deal();
ArrayList<Card> boardForBot = new ArrayList<>();
boardForBot.add(b[0]);
boardForBot.add(b[1]);
boardForBot.add(b[2]);
lastPlayer = 0;
currBet = 0;
lastRaise = blinds; // Post-flop min-lead is 1BB
int i = 0;
for (int j = 0; j < 3; j++) {
currentStreet = j + 1; // PHASE 8: 1=Flop, 2=Turn, 3=River
if (currentStreet == 2) {
huTurnFirstActor = -1;
huTurnFirstActorChecked = false;
huTurnCheckBackObserved = false;
huTurnBarrelResponsePending = false;
huTurnBarrelSeen = false;
}
if (currentStreet == 3) {
huRiverLargeBetResponsePending = false;
huRiverLargeBetSeen = false;
}
String roundName = ((j == 0) ? "*** THE FLOP ***\n"
: ((j == 1) ? "*** THE TURN (4th Street) ***\n" : "*** THE RIVER (5th Street) ***\n"));
String realPlayerOrder = "Real player order: ";
int count = 0;
for (int k = 0; k < players.length; k++) {
int idx = (i + k) % players.length;
if (!(players[idx] instanceof PokerBot) && players[idx].inHand()) {
realPlayerOrder += players[idx].getName() + " -> ";
count++;
}
}
if (count > 0) {
realPlayerOrder = realPlayerOrder.substring(0, realPlayerOrder.length() - 4) + "\n";
roundName += realPlayerOrder;
}
String positionMap = "Action order: ";
for (int k = 0; k < players.length; k++) {
int idx = (i + k) % players.length;
if (players[idx].inHand()) {
positionMap += players[idx].getName();
if (idx == 0)
positionMap += " (SB)";
else if (idx == 1)
positionMap += " (BB)";
else if (idx == players.length - 1)
positionMap += " (D)";
positionMap += " -> ";
}
}
if (positionMap.length() > 14) {
positionMap = positionMap.substring(0, positionMap.length() - 4) + "\n\n";
roundName += positionMap;
} else {
roundName += "\n";
}
String roundHistory = "";
do {
if (players[i].inHand() && players[i].getChips() > 0) {
String boardStr = "Board: " + b[0].getValue() + " - " + b[1].getValue() + " - " + b[2].getValue()
+ ((j > 0) ? (" - " + b[3].getValue()) : "") + ((j == 2) ? " - " + b[4].getValue() : "");
Utils.clearScreen();
Card[] visibleBoard = boardForBot.toArray(new Card[0]);
System.out.print(getTransparencySnapshot("POSTFLOP-TURN", visibleBoard, i));
System.out.print(roundName + boardStr + "\n\n" + pot.toString() + "\n\n" + roundHistory);
String turnHeader = players[i].getName().toUpperCase() + "'s turn!\n";
if (players[i] instanceof PokerBot) {
turnHeader += "Their stack: ✨" + players[i].getChips() + "\n";
System.out.print(turnHeader);
if (!skipMode)
Utils.sleep(1000);
} else {
System.out.print(turnHeader);
}
if (players[i] instanceof PokerBot) {
PokerBot temp = (PokerBot) players[i];
int livePot = pot.getTotalPot();
for (int c : currConts)
livePot += c;
currAction = temp.action("postflop", currConts[i], currBet, blinds, lastRaise,
boardForBot.toArray(new Card[j + 3]), livePot, players, i, preflopAggressorIndex, 0, 1);
} else {
System.out.println("Are you " + players[i].getName() + "? Press Enter to confirm and show your hand...");
Utils.flushInput();
sc.nextLine();
Utils.clearScreen();
visibleBoard = boardForBot.toArray(new Card[0]);
System.out.print(getTransparencySnapshot("POSTFLOP-TURN", visibleBoard, i));
System.out.print(roundName + boardStr + "\n\n" + pot.toString() + "\n\n" + roundHistory);
System.out.print(turnHeader);
currAction = players[i].action("postflop", currConts[i], currBet, blinds, lastRaise);
}
String actionLog = handleAction(i);
if (!(players[i] instanceof PokerBot))
actionLog += " (real)";
// Reprint with updated stack after action
turnHeader = players[i].getName().toUpperCase() + "'s turn!\n";
turnHeader += "Their stack: ✨" + players[i].getChips() + "\n";
Utils.clearScreen();
Card[] visibleBoard2 = boardForBot.toArray(new Card[0]);
System.out.print(getTransparencySnapshot("POSTFLOP-AFTER-ACTION", visibleBoard2, i));
System.out.print(roundName + boardStr + "\n\n" + pot.toString() + "\n\n" + roundHistory);
System.out.print(turnHeader);
System.out.println(actionLog + "\n");
roundHistory += turnHeader + actionLog + "\n\n";
if (!skipMode)
Utils.sleep(1000);
}
if (i == players.length - 1)
i = 0;
else
i++;
} while (i != lastPlayer && stillIn() > 1);
Utils.clearScreen();
String boardStr = "Board: " + b[0].getValue() + " - " + b[1].getValue() + " - " + b[2].getValue()
+ ((j > 0) ? (" - " + b[3].getValue()) : "") + ((j == 2) ? " - " + b[4].getValue() : "");
System.out.print(getTransparencySnapshot("POSTFLOP-STREET-END", boardForBot.toArray(new Card[0]), -1));
System.out.print(roundName + boardStr + "\n\n" + pot.toString() + "\n\n" + roundHistory);
if (!skipMode && realPlayersIn() == 0 && j < 2) {
System.out.println("Type \"skip\" to fast forward the hand, or hit enter to continue:");
Utils.flushInput();
Player.sc = new Scanner(System.in);
sc = Player.sc;
String input = sc.nextLine().strip().toLowerCase();
if (input.equals("skip"))
skipMode = true;
} else if (!skipMode) {
System.out.println("Press Enter to continue:");
Utils.flushInput();
Player.sc = new Scanner(System.in);
sc = Player.sc;
sc.nextLine();
}
Utils.clearScreen();
i = 0;
lastPlayer = 0;
currConts = new int[players.length];
currBet = 0;
lastRaise = blinds; // Reset min-lead to 1BB for the next street
if (j < 2)
boardForBot.add(b[j + 3]);
if (stillIn() < 2)
break;
}
if (stillIn() < 2)
showdown(0);
else
showdown(1);
}
private void showdown(int c) { // assign winnner at end of hand
isShowdown = (c == 1); // PHASE 8: Flag if went to showdown
finalizePostflopTelemetry(c);
int[] stats = pot.assignWinner(p, c, mainPlayer);
if (stats[0] == 1) {
mp.addWin(stats[1]);
mp.setGain(stats[1]);
} else {
for (int i = 0; i < players.length; i++) {
if (players[i] == mainPlayer) {
mp.addLoss(pot.getContributions()[i]);
}
}
}
System.out.println();
newHand();
}
private void newHand() { // setup for new hand
skipMode = false;
hands++;
mp.hands();
boolean gameOver = false;
p.reset();
preflopAggressorIndex = -1;
players[0].setStatus(0);
players[1].setStatus(0);
PokerPlayer first = players[players.length - 1];
for (int i = players.length - 1; i > 0; i--)
players[i] = players[i - 1];
players[0] = first;
int handsPerRound = (players.length <= 8) ? (players.length * 3) : (players.length * 2);
if (hands >= handsPerRound) {
if (blinds < 320) {
blinds *= 2;
System.out.println("Blinds increasing to ✨" + blinds);
} else {
System.out.println("Round finished");
}
hands = 0;
}
for (int i = 0; i < players.length; i++) {
if (players[i] == mainPlayer)
mp.setChips(players[i].getChips());
if (players[i].getChips() > 0)
players[i].setInHand(true);
else if (players[i] != mainPlayer) {
String ogName = players[i].getName();
players[i] = new PokerBot(players);
System.out.println(
ogName + " has run out of primogems ✨, they have been replaced by newcomer " + players[i].getName());
} else
gameOver = true;
}
// Random player join/leave (7% chance per hand)
if (!gameOver && Math.random() < 0.07) {
boolean tryRemove = Math.random() < 0.5;
if (tryRemove && players.length > 6) {
// Find all bot indices eligible to leave
int godBotCount = 0;
for (PokerPlayer p : players) {
if (p instanceof PokerBot && ((PokerBot) p).getBotLevel() == 2)
godBotCount++;
}
ArrayList<Integer> botIndices = new ArrayList<>();
for (int k = 0; k < players.length; k++) {
if (players[k] instanceof PokerBot) {
if (godBotCount > 1 || ((PokerBot) players[k]).getBotLevel() != 2)
botIndices.add(k);
}
}
if (!botIndices.isEmpty()) {
int removeIdx = botIndices.get((int) (Math.random() * botIndices.size()));
String leavingName = players[removeIdx].getName();
PokerPlayer[] newPlayers = new PokerPlayer[players.length - 1];
int idx = 0;
for (int k = 0; k < players.length; k++) {
if (k != removeIdx)
newPlayers[idx++] = players[k];
}
players = newPlayers;
System.out.println(leavingName + " has left the table.");
}
} else if (players.length < 12) {
// PHASE 10: Spawn a new bot — God Bot tree or 7-archetype random
PokerPlayer[] newPlayers = new PokerPlayer[players.length + 1];
for (int k = 0; k < players.length; k++) newPlayers[k] = players[k];
PokerBot newBot = PokerBot.createLiveGameBot(players);
newPlayers[players.length] = newBot;
players = newPlayers;
System.out.println("A new player has joined the table: " + newBot.getName() + "!");
}
}
if (!gameOver) {
players[0].setStatus(1);
players[1].setStatus(2);
pot.resetPot(players);
System.out.println("Continue to next hand? [y/n]");
String s = sc.nextLine().strip().toLowerCase();
if (s.equals("n") || s.equals("no") || s.equals("nah")) {
endGame(false);
} else {
Utils.clearScreen();
preflop();
}
} else
endGame(true);
}
private boolean shouldTrackCognitivePlayer(PokerPlayer player) {
if (player == null)
return false;
return true; // PHASE 10: Track everyone so matrix can profile all opponents
}
private void resetHuSmartLeakTracking() {
huSmartGod = false;
huSmartName = null;
huSmartIdx = -1;
huGodIdx = -1;
huPreflopRaiseCount = 0;
huPreflopLastRaiser = -1;
huSmartFacing3BetPending = false;
huFlopCbetResponsePending = false;
huGodCbetFlopSeen = false;
huTurnFirstActor = -1;
huTurnFirstActorChecked = false;
huTurnCheckBackObserved = false;
huTurnBarrelResponsePending = false;
huTurnBarrelSeen = false;
huRiverLargeBetResponsePending = false;
huRiverLargeBetSeen = false;
}
private void refreshHuSmartContext() {
int alivePlayers = 0;
int dumbCount = 0;
int smartCount = 0;
int godCount = 0;
int smartIdx = -1;
int godIdx = -1;
for (int idx = 0; idx < players.length; idx++) {
PokerPlayer p = players[idx];
if (p == null || p.getChips() <= 0)
continue;
alivePlayers++;
if (p instanceof PokerBot) {
int level = ((PokerBot) p).getBotLevel();
if (level == 0) {
dumbCount++;
} else if (level == 1) {
smartCount++;
smartIdx = idx;
} else if (level == 2) {
godCount++;
godIdx = idx;
}
}
}
huSmartGod = (alivePlayers == 2 && dumbCount == 0 && smartCount == 1 && godCount == 1);
if (huSmartGod) {
huSmartIdx = smartIdx;
huGodIdx = godIdx;
huSmartName = players[smartIdx].getName();
return;
}
huSmartName = null;
huSmartIdx = -1;
huGodIdx = -1;
huSmartFacing3BetPending = false;
huFlopCbetResponsePending = false;
huTurnBarrelResponsePending = false;
huRiverLargeBetResponsePending = false;
}
private void markPreflopActionForTelemetry(int playerIndex, int preActionTableBet, int preActionContribution,
int paid) {
if (currentStreet != 0 || preflopVPIPFlags == null || preflopPFRFlags == null)
return;
if (playerIndex < 0 || playerIndex >= players.length)
return;
if (!shouldTrackCognitivePlayer(players[playerIndex]))
return;
boolean contributed = paid > 0 && currConts[playerIndex] > preActionContribution;
boolean isAggressiveAction = (currAction[0] == 3 || currAction[0] == 4);
boolean isRaise = isAggressiveAction && currConts[playerIndex] > preActionTableBet;
if (contributed && (currAction[0] == 1 || isAggressiveAction)) {
preflopVPIPFlags[playerIndex] = true;
}
if (isRaise) {
preflopPFRFlags[playerIndex] = true;
preflopVPIPFlags[playerIndex] = true;
}
}
private void finalizePreflopTelemetry() {
if (preflopVPIPFlags == null || preflopPFRFlags == null)
return;
for (int i = 0; i < players.length; i++) {
if (!shouldTrackCognitivePlayer(players[i]))
continue;
String pName = players[i].getName();
PokerBot.updatePreflopTelemetryTracked(pName, preflopVPIPFlags[i], preflopPFRFlags[i], PREFLOP_EMA_ALPHA);
}
preflopVPIPFlags = null;
preflopPFRFlags = null;
}
private String getAFqStreetStatKey(int street) {
if (street == 1)
return "AFq_Flop";
if (street == 2)
return "AFq_Turn";
if (street == 3)
return "AFq_River";
return "AFq_Preflop";
}
private void markPostflopActionForTelemetry(int playerIndex, int preActionTableBet, int preActionContribution,
int paid) {
if (currentStreet < 1 || currentStreet > 3)
return;
if (postflopAggressionActions == null || postflopAggressionOpportunities == null)
return;
if (playerIndex < 0 || playerIndex >= players.length)
return;
if (!shouldTrackCognitivePlayer(players[playerIndex]))
return;
boolean isAggressiveAction = (currAction[0] == 3 || currAction[0] == 4)
&& currConts[playerIndex] > preActionTableBet;
boolean isCall = (currAction[0] == 1 && paid > 0);
boolean isFoldFacingBet = (currAction[0] == 2 && preActionTableBet > preActionContribution);
if (isAggressiveAction || isCall || isFoldFacingBet) {
postflopAggressionOpportunities[playerIndex][currentStreet]++;
if (isAggressiveAction)
postflopAggressionActions[playerIndex][currentStreet]++;
}
if (currentStreet == 1) {
if (!cbetFiredOnFlop && preActionTableBet == 0 && playerIndex == preflopAggressorIndex && isAggressiveAction) {
cbetFiredOnFlop = true;
cbetAggressorIndex = playerIndex;
}
if (cbetFiredOnFlop && playerIndex != cbetAggressorIndex && preActionTableBet > preActionContribution) {
foldToCbetOpportunity[playerIndex] = true;
if (currAction[0] == 2)
foldedToCbet[playerIndex] = true;
}
}
}
private void finalizePostflopTelemetry(int showdownMode) {
if (postflopAggressionActions == null || postflopAggressionOpportunities == null)
return;
for (int i = 0; i < players.length; i++) {
if (!shouldTrackCognitivePlayer(players[i]))
continue;
String pName = players[i].getName();
for (int street = 1; street <= 3; street++) {
int opps = postflopAggressionOpportunities[i][street];
if (opps > 0) {
double afqValue = (double) postflopAggressionActions[i][street] / opps;
PokerBot.updateCognitiveStatTracked(pName, getAFqStreetStatKey(street), afqValue, POSTFLOP_EMA_ALPHA);
}
}
if (foldToCbetOpportunity[i]) {
PokerBot.updateCognitiveStatTracked(pName, "FoldToCBet", foldedToCbet[i] ? 1.0 : 0.0, POSTFLOP_EMA_ALPHA);
}
if (sawFlopThisHand != null && sawFlopThisHand[i]) {
boolean reachedShowdown = (showdownMode == 1 && players[i].inHand());
PokerBot.updateCognitiveStatTracked(pName, "WTSD", reachedShowdown ? 1.0 : 0.0, POSTFLOP_EMA_ALPHA);
}
}
postflopAggressionActions = null;
postflopAggressionOpportunities = null;
sawFlopThisHand = null;
foldToCbetOpportunity = null;
foldedToCbet = null;
cbetFiredOnFlop = false;
cbetAggressorIndex = -1;
}
private String handleAction(int i) { // do certain things based on a player's action
String log = "";
int paid = 0;
int preActionTableBet = currBet;
int preActionContribution = currConts[i];
int preActionPot = pot.getTotalPot();
refreshHuSmartContext();
boolean actorIsHuSmart = huSmartGod && i == huSmartIdx;
boolean actorIsHuGod = huSmartGod && i == huGodIdx;
if (!(players[i] instanceof PokerBot)) {
String actionLabel = "UNKNOWN";
int amount = currAction[1];
if (currAction[0] == 1) {
actionLabel = (amount == 0 && currConts[i] == currBet) ? "CHECK" : "CALL";
} else if (currAction[0] == 2) {
actionLabel = "FOLD";
amount = 0;
} else if (currAction[0] >= 3) {
actionLabel = (currAction[0] == 4) ? "ALL-IN" : "BET/RAISE";
}
BotDiagnostics.recordPlayerDecision(
(currentStreet == 0 ? "PREFLOP" : (currentStreet == 1 ? "FLOP" : (currentStreet == 2 ? "TURN" : "RIVER"))),
players[i].getName(), actionLabel, currConts[i] + amount,
"tableBet=" + currBet + ", prevBet=" + currConts[i]);
}
switch (currAction[0]) {
case 1: // CALL/CHECK
paid = pot.addPlayerContribution(i, currAction[1]);
currConts[i] += paid;
if (players[i] == mainPlayer)
mp.addBet(paid);
if (players[i].status() == 2) {
if (paid > 0)
log = players[i].getName() + " in big blind CALLS FOR ✨" + currConts[i] + ".";
else
log = players[i].getName() + " in big blind CHECKS.";
} else {
if (paid > 0)
log = players[i].getName() + ((players[i].status() == 1) ? " in small blind " : " ") + "CALLS FOR ✨"
+ currConts[i] + ".";
else
log = players[i].getName() + ((players[i].status() == 1) ? " in small blind " : " ") + "CHECKS.";
}
break;
case 2: // FOLD
players[i].setInHand(false);
if (players[i].status() == 2)
log = players[i].getName() + " in big blind FOLDS.";
else
log = players[i].getName() + ((players[i].status() == 1) ? " in small blind " : " ") + "FOLDS.";
break;
default: // BET/RAISE/ALL-IN
if (players[i] == mainPlayer && currAction[0] == 4)
mp.allIn();
// Actually move the chips
paid = pot.addPlayerContribution(i, currAction[1]);
currConts[i] += paid;
if (players[i] == mainPlayer)
mp.addBet(paid);
// Determine action name for log
String actionVerb = (currAction[0] == 4) ? "GOES ALL IN FOR" : (currBet == 0 ? "BETS" : "RAISES TO");
// Log based on ACTUAL contribution
log = players[i].getName()
+ ((players[i].status() == 1) ? " in small blind " : (players[i].status() == 2 ? " in big blind " : " "))
+ actionVerb + " ✨" + currConts[i] + ".";
// Update table bet state
if (currConts[i] > currBet) {
int increment = currConts[i] - currBet;
lastPlayer = i; // Ensure everyone responds to the new bet amount
// Only change the minimum raise size if it's a FULL raise
if (increment >= lastRaise) {
lastRaise = increment;
}
// PHASE 8 COGNITIVE MATRIX TRACKING (Only run for Humans)
if (!(players[i] instanceof PokerBot)) {
String pName = players[i].getName();
PokerBot.CognitiveProfile profile = PokerBot.getOrCreateCognitiveProfile(pName);
// Massive overbets or jams
if (currAction[0] == 4 || increment > currBet * 1.5) {
profile.aggressiveActions += 2;
} else {
profile.aggressiveActions += 1;
}
}
currBet = currConts[i];
}
break;
}
boolean actionIsFold = (currAction[0] == 2);
boolean actionIsCheck = (currAction[0] == 1 && preActionTableBet == 0 && currConts[i] == preActionContribution);
boolean actionIsAggressive = (currConts[i] > preActionTableBet);
if (huSmartGod) {
if (currentStreet == 0) {
if (actionIsAggressive) {
huPreflopRaiseCount++;
if (huPreflopRaiseCount >= 2 && actorIsHuGod && huPreflopLastRaiser == huSmartIdx) {
huSmartFacing3BetPending = true;
}
huPreflopLastRaiser = i;
}
if (huSmartFacing3BetPending && actorIsHuSmart) {
PokerBot.observeSmartFoldTo3BetHU(huSmartName, actionIsFold);
huSmartFacing3BetPending = false;
}
} else if (currentStreet == 1) {
if (actorIsHuGod && preflopAggressorIndex == huGodIdx && preActionTableBet == 0 && actionIsAggressive) {
huFlopCbetResponsePending = true;
huGodCbetFlopSeen = true;
}
if (huFlopCbetResponsePending && actorIsHuSmart) {
PokerBot.observeSmartFlopCbetResponseHU(huSmartName, actionIsFold, actionIsAggressive);
huFlopCbetResponsePending = false;
}
} else if (currentStreet == 2) {
if (huTurnFirstActor == -1) {
huTurnFirstActor = i;
huTurnFirstActorChecked = actionIsCheck;
} else if (!huTurnCheckBackObserved && actorIsHuSmart && huTurnFirstActor == huGodIdx
&& huTurnFirstActorChecked) {
PokerBot.observeSmartTurnCheckBackHU(huSmartName, actionIsCheck);
huTurnCheckBackObserved = true;
}
if (!huTurnBarrelSeen && huGodCbetFlopSeen && actorIsHuGod && actionIsAggressive) {
huTurnBarrelResponsePending = true;
huTurnBarrelSeen = true;
}
if (huTurnBarrelResponsePending && actorIsHuSmart) {
PokerBot.observeSmartTurnBarrelResponseHU(huSmartName, actionIsFold);
huTurnBarrelResponsePending = false;
}
} else if (currentStreet == 3) {
if (!huRiverLargeBetSeen && actorIsHuGod && actionIsAggressive) {
int wagerSize = currConts[i] - preActionTableBet;
int largeBetThreshold = Math.max(blinds * 2, (int) (preActionPot * 0.75));
if (wagerSize >= largeBetThreshold) {
huRiverLargeBetResponsePending = true;
huRiverLargeBetSeen = true;
}
}
if (huRiverLargeBetResponsePending && actorIsHuSmart) {
PokerBot.observeSmartRiverLargeBetResponseHU(huSmartName, actionIsFold);
huRiverLargeBetResponsePending = false;
}
}
}
markPreflopActionForTelemetry(i, preActionTableBet, preActionContribution, paid);
markPostflopActionForTelemetry(i, preActionTableBet, preActionContribution, paid);
return log;
}
private int stillIn() { // gets # of players still in
int in = 0;
for (int i = 0; i < players.length; i++)
if (players[i].inHand())
in++;
return in;
}
private int realPlayersIn() {
int count = 0;
for (PokerPlayer p : players) {
if (!(p instanceof PokerBot) && p.inHand())
count++;
}
return count;
}
}