-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblackjack-calculator.html
More file actions
1119 lines (1029 loc) · 40.9 KB
/
blackjack-calculator.html
File metadata and controls
1119 lines (1029 loc) · 40.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
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Blackjack Solver</title>
<link href="https://fonts.googleapis.com/css2?family=DM+Serif+Display&family=JetBrains+Mono:wght@400;600&family=Outfit:wght@300;400;500;600;700&display=swap" rel="stylesheet">
<style>
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
:root {
--felt: #0e2a1a;
--felt-light: #143824;
--felt-surface: #193f2b;
--gold: #c9a84c;
--gold-dim: #8a7435;
--gold-glow: #e8c54a;
--cream: #f0ebe0;
--cream-dim: #b0a890;
--red: #e74c3c;
--green: #27ae60;
--amber: #f39c12;
--cyan: #1abc9c;
--bg: #060d08;
--card-bg: rgba(14, 42, 26, 0.7);
--card-border: rgba(201, 168, 76, 0.15);
--input-bg: rgba(10, 20, 14, 0.8);
--input-border: rgba(201, 168, 76, 0.25);
}
body {
font-family: 'Outfit', sans-serif;
background: var(--bg);
color: var(--cream);
min-height: 100vh;
overflow-x: hidden;
}
body::before {
content: '';
position: fixed;
inset: 0;
background:
radial-gradient(ellipse 80% 60% at 50% 0%, rgba(20, 56, 36, 0.5) 0%, transparent 70%),
radial-gradient(ellipse 60% 40% at 80% 100%, rgba(201, 168, 76, 0.04) 0%, transparent 60%);
pointer-events: none;
z-index: 0;
}
/* Felt noise texture via SVG filter */
body::after {
content: '';
position: fixed;
inset: 0;
opacity: 0.03;
background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 256 256' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
background-size: 256px 256px;
pointer-events: none;
z-index: 0;
}
.shell {
position: relative;
z-index: 1;
max-width: 1120px;
margin: 0 auto;
padding: 32px 24px 64px;
}
/* ── Header ─────────────────────────── */
header {
text-align: center;
margin-bottom: 40px;
padding-bottom: 28px;
border-bottom: 1px solid var(--card-border);
}
header h1 {
font-family: 'DM Serif Display', serif;
font-size: 2.4rem;
font-weight: 400;
color: var(--gold);
letter-spacing: 0.02em;
text-shadow: 0 0 40px rgba(201, 168, 76, 0.15);
}
header p {
font-size: 0.82rem;
color: var(--cream-dim);
margin-top: 6px;
letter-spacing: 0.08em;
text-transform: uppercase;
font-weight: 300;
}
/* ── Card panels ────────────────────── */
.panel {
background: var(--card-bg);
border: 1px solid var(--card-border);
border-radius: 12px;
padding: 24px;
backdrop-filter: blur(8px);
position: relative;
overflow: hidden;
}
.panel::before {
content: '';
position: absolute;
top: 0; left: 0; right: 0;
height: 1px;
background: linear-gradient(90deg, transparent, rgba(201,168,76,0.2), transparent);
}
.panel-label {
font-size: 0.7rem;
text-transform: uppercase;
letter-spacing: 0.14em;
color: var(--gold-dim);
margin-bottom: 16px;
font-weight: 600;
}
/* ── Input area ─────────────────────── */
.input-grid {
display: grid;
grid-template-columns: 1.4fr 1fr 0.8fr;
gap: 20px;
margin-bottom: 28px;
}
@media (max-width: 768px) {
.input-grid { grid-template-columns: 1fr; }
}
.field-label {
display: block;
font-size: 0.72rem;
text-transform: uppercase;
letter-spacing: 0.1em;
color: var(--cream-dim);
margin-bottom: 6px;
font-weight: 500;
}
textarea, input[type="text"], input[type="number"] {
width: 100%;
background: var(--input-bg);
border: 1px solid var(--input-border);
border-radius: 8px;
color: var(--cream);
font-family: 'JetBrains Mono', monospace;
font-size: 0.88rem;
padding: 10px 14px;
outline: none;
transition: border-color 0.2s, box-shadow 0.2s;
}
textarea:focus, input:focus {
border-color: var(--gold-dim);
box-shadow: 0 0 0 3px rgba(201,168,76,0.08);
}
textarea {
resize: vertical;
min-height: 110px;
line-height: 1.5;
}
textarea::placeholder, input::placeholder {
color: rgba(176, 168, 144, 0.35);
}
.field-row {
display: flex;
flex-direction: column;
gap: 14px;
}
.toggle-row {
display: flex;
align-items: center;
gap: 10px;
margin-top: 4px;
}
.toggle-row input[type="checkbox"] {
width: 18px;
height: 18px;
accent-color: var(--gold);
cursor: pointer;
}
.toggle-row span {
font-size: 0.78rem;
color: var(--cream-dim);
}
/* ── Results grid ───────────────────── */
.results-grid {
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: auto auto;
gap: 20px;
}
@media (max-width: 768px) {
.results-grid { grid-template-columns: 1fr; }
}
/* ── Action badge ───────────────────── */
.action-display {
text-align: center;
padding: 10px 0 4px;
}
.action-badge {
display: inline-block;
font-family: 'DM Serif Display', serif;
font-size: 2.6rem;
letter-spacing: 0.04em;
line-height: 1;
padding: 4px 0;
transition: color 0.3s;
}
.action-badge.hit { color: var(--red); }
.action-badge.stand { color: var(--green); }
.action-badge.double { color: var(--amber); }
.action-badge.split { color: var(--cyan); }
.action-badge.blackjack { color: var(--gold-glow); }
.action-badge.none { color: var(--cream-dim); }
.hand-value-tag {
display: inline-block;
margin-top: 8px;
font-family: 'JetBrains Mono', monospace;
font-size: 0.82rem;
color: var(--cream-dim);
background: rgba(255,255,255,0.04);
padding: 3px 14px;
border-radius: 20px;
border: 1px solid rgba(255,255,255,0.06);
}
.ev-label {
text-align: center;
margin-top: 12px;
font-size: 0.78rem;
color: var(--cream-dim);
}
.ev-label strong {
font-family: 'JetBrains Mono', monospace;
font-weight: 600;
font-size: 0.92rem;
}
.ev-label strong.positive { color: var(--green); }
.ev-label strong.negative { color: var(--red); }
/* ── EV Bars ────────────────────────── */
.ev-bars {
margin-top: 20px;
display: flex;
flex-direction: column;
gap: 8px;
}
.ev-bar-row {
display: grid;
grid-template-columns: 54px 1fr 64px;
align-items: center;
gap: 10px;
}
.ev-bar-row .label {
font-size: 0.72rem;
text-transform: uppercase;
letter-spacing: 0.08em;
color: var(--cream-dim);
text-align: right;
font-weight: 500;
}
.ev-bar-row .label.best {
color: var(--gold);
}
.ev-bar-track {
height: 22px;
background: rgba(255,255,255,0.03);
border-radius: 4px;
position: relative;
overflow: hidden;
}
.ev-bar-fill {
position: absolute;
top: 2px;
bottom: 2px;
border-radius: 3px;
transition: width 0.4s cubic-bezier(0.22, 1, 0.36, 1), left 0.4s cubic-bezier(0.22, 1, 0.36, 1);
}
.ev-bar-center {
position: absolute;
top: 0; bottom: 0;
left: 50%;
width: 1px;
background: rgba(255,255,255,0.1);
}
.ev-bar-fill.hit { background: var(--red); }
.ev-bar-fill.stand { background: var(--green); }
.ev-bar-fill.double { background: var(--amber); }
.ev-bar-fill.split { background: var(--cyan); }
.ev-bar-row .val {
font-family: 'JetBrains Mono', monospace;
font-size: 0.74rem;
color: var(--cream-dim);
text-align: right;
}
/* ── Dealer chart ───────────────────── */
.dealer-chart {
display: flex;
align-items: flex-end;
gap: 6px;
height: 120px;
margin-top: 12px;
padding-bottom: 22px;
position: relative;
}
.dealer-chart::after {
content: '';
position: absolute;
bottom: 20px;
left: 0; right: 0;
height: 1px;
background: rgba(255,255,255,0.06);
}
.dealer-bar-wrap {
flex: 1;
display: flex;
flex-direction: column;
align-items: center;
height: 100%;
justify-content: flex-end;
}
.dealer-bar {
width: 100%;
max-width: 48px;
border-radius: 4px 4px 0 0;
transition: height 0.4s cubic-bezier(0.22, 1, 0.36, 1);
min-height: 2px;
position: relative;
}
.dealer-bar.bust { background: var(--red); opacity: 0.8; }
.dealer-bar.bj { background: var(--gold-glow); }
.dealer-bar.total { background: var(--green); opacity: 0.65; }
.dealer-bar .pct {
position: absolute;
top: -18px;
left: 50%;
transform: translateX(-50%);
font-family: 'JetBrains Mono', monospace;
font-size: 0.62rem;
color: var(--cream-dim);
white-space: nowrap;
}
.dealer-bar-label {
margin-top: 4px;
font-size: 0.66rem;
color: var(--cream-dim);
letter-spacing: 0.04em;
font-weight: 500;
}
/* ── Shoe info ──────────────────────── */
.shoe-stats {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 10px;
margin-bottom: 16px;
}
.shoe-stat {
text-align: center;
padding: 10px;
background: rgba(255,255,255,0.02);
border-radius: 8px;
border: 1px solid rgba(255,255,255,0.04);
}
.shoe-stat .num {
font-family: 'JetBrains Mono', monospace;
font-size: 1.3rem;
font-weight: 600;
color: var(--cream);
}
.shoe-stat .desc {
font-size: 0.65rem;
text-transform: uppercase;
letter-spacing: 0.1em;
color: var(--cream-dim);
margin-top: 2px;
}
.card-counts {
display: grid;
grid-template-columns: repeat(10, 1fr);
gap: 4px;
}
.card-count {
text-align: center;
padding: 6px 0;
background: rgba(255,255,255,0.02);
border-radius: 6px;
border: 1px solid rgba(255,255,255,0.04);
}
.card-count .sym {
font-family: 'JetBrains Mono', monospace;
font-size: 0.72rem;
color: var(--cream);
font-weight: 600;
}
.card-count .cnt {
font-family: 'JetBrains Mono', monospace;
font-size: 0.62rem;
color: var(--cream-dim);
margin-top: 1px;
}
.card-count.dead {
border-color: rgba(201,168,76,0.2);
background: rgba(201,168,76,0.04);
}
/* ── Round EV ───────────────────────── */
.round-ev-display {
text-align: center;
padding: 8px 0;
}
.round-verdict {
font-family: 'DM Serif Display', serif;
font-size: 1.8rem;
letter-spacing: 0.03em;
line-height: 1;
margin-bottom: 6px;
}
.round-verdict.favorable { color: var(--green); }
.round-verdict.unfavorable { color: var(--red); }
.round-verdict.neutral { color: var(--cream-dim); }
.round-ev-num {
font-family: 'JetBrains Mono', monospace;
font-size: 1.6rem;
font-weight: 600;
margin: 8px 0 4px;
}
.round-ev-num.positive { color: var(--green); }
.round-ev-num.negative { color: var(--red); }
.round-meta {
font-size: 0.72rem;
color: var(--cream-dim);
margin-top: 10px;
line-height: 1.7;
}
.round-meta .row {
display: flex;
justify-content: space-between;
padding: 0 12px;
}
.round-meta .val {
font-family: 'JetBrains Mono', monospace;
color: var(--cream);
font-weight: 500;
}
.round-source {
display: inline-block;
margin-top: 12px;
font-size: 0.62rem;
text-transform: uppercase;
letter-spacing: 0.12em;
padding: 3px 10px;
border-radius: 10px;
border: 1px solid rgba(255,255,255,0.08);
}
.round-source.estimate {
color: var(--amber);
border-color: rgba(243,156,18,0.2);
}
.round-source.exact {
color: var(--green);
border-color: rgba(39,174,96,0.2);
}
.round-source.computing {
color: var(--cream-dim);
border-color: rgba(255,255,255,0.08);
}
@keyframes pulse {
0%, 100% { opacity: 0.4; }
50% { opacity: 1; }
}
.computing-dot {
display: inline-block;
animation: pulse 1.2s ease-in-out infinite;
}
/* ── Error ──────────────────────────── */
.error-banner {
background: rgba(231, 76, 60, 0.1);
border: 1px solid rgba(231, 76, 60, 0.25);
color: #e8836f;
padding: 10px 16px;
border-radius: 8px;
font-size: 0.82rem;
margin-bottom: 20px;
}
/* ── Empty state ────────────────────── */
.empty-state {
text-align: center;
padding: 30px 20px;
color: var(--cream-dim);
font-size: 0.85rem;
}
.empty-state .suit {
font-size: 1.6rem;
margin-bottom: 8px;
opacity: 0.25;
}
/* ── Animations ─────────────────────── */
@keyframes fadeUp {
from { opacity: 0; transform: translateY(12px); }
to { opacity: 1; transform: translateY(0); }
}
.panel {
animation: fadeUp 0.5s ease-out backwards;
}
.results-grid .panel:nth-child(1) { animation-delay: 0.05s; }
.results-grid .panel:nth-child(2) { animation-delay: 0.10s; }
.results-grid .panel:nth-child(3) { animation-delay: 0.15s; }
.results-grid .panel:nth-child(4) { animation-delay: 0.20s; }
/* ── Strategy Chart Button ─────────── */
.chart-section {
margin-top: 28px;
}
.chart-btn {
display: flex;
align-items: center;
justify-content: center;
gap: 8px;
width: 100%;
padding: 14px 28px;
font-family: 'Outfit', sans-serif;
font-size: 0.82rem;
font-weight: 600;
letter-spacing: 0.08em;
text-transform: uppercase;
color: var(--gold);
background: rgba(201, 168, 76, 0.06);
border: 1px solid rgba(201, 168, 76, 0.2);
border-radius: 10px;
cursor: pointer;
transition: all 0.25s;
}
.chart-btn:hover {
background: rgba(201, 168, 76, 0.12);
border-color: rgba(201, 168, 76, 0.4);
box-shadow: 0 0 24px rgba(201, 168, 76, 0.06);
}
.chart-btn:disabled {
opacity: 0.5;
cursor: wait;
}
.chart-btn-icon {
font-size: 1rem;
opacity: 0.7;
}
.chart-container {
margin-top: 16px;
border-radius: 12px;
overflow: hidden;
border: 1px solid var(--card-border);
background: var(--card-bg);
backdrop-filter: blur(8px);
}
.chart-container iframe {
width: 100%;
border: none;
min-height: 720px;
background: transparent;
display: block;
}
</style>
</head>
<body>
<div class="shell">
<header>
<h1>Blackjack Solver</h1>
<p>Exact backward-induction engine</p>
</header>
<div class="input-grid">
<div class="panel" style="animation-delay:0s">
<div class="panel-label">Hand Logs</div>
<label class="field-label" for="deadCards">Dealt cards, one hand per line</label>
<textarea id="deadCards" placeholder="2j3 ak5 q0a 756" spellcheck="false"></textarea>
</div>
<div class="panel" style="animation-delay:0.06s">
<div class="panel-label">Current Hand</div>
<div class="field-row">
<div>
<label class="field-label" for="playerCards">Your cards</label>
<input type="text" id="playerCards" placeholder="a5" maxlength="10" spellcheck="false">
</div>
<div>
<label class="field-label" for="dealerCard">Dealer upcard</label>
<input type="text" id="dealerCard" placeholder="6" maxlength="2" spellcheck="false">
</div>
</div>
</div>
<div class="panel" style="animation-delay:0.12s">
<div class="panel-label">Rules</div>
<div class="field-row">
<div>
<label class="field-label" for="deckCount">Decks</label>
<input type="number" id="deckCount" value="8" min="1" max="8">
</div>
<div class="toggle-row">
<input type="checkbox" id="hitSoft17" checked>
<span>Dealer hits soft 17</span>
</div>
<div class="toggle-row">
<input type="checkbox" id="dealerPeeks" checked>
<span>Dealer peeks for BJ</span>
</div>
</div>
</div>
</div>
<div id="errorDiv"></div>
<div class="results-grid">
<!-- Hand Analysis -->
<div class="panel" id="analysisPanel">
<div class="panel-label">Hand Analysis</div>
<div id="analysisContent">
<div class="empty-state">
<div class="suit">♠ ♥</div>
Enter your cards and dealer upcard
</div>
</div>
</div>
<!-- Dealer Outcomes -->
<div class="panel" id="dealerPanel">
<div class="panel-label">Dealer Outcomes</div>
<div id="dealerContent">
<div class="empty-state">
<div class="suit">♦ ♣</div>
Waiting for hand data
</div>
</div>
</div>
<!-- Round EV -->
<div class="panel" id="roundPanel">
<div class="panel-label">Next Round</div>
<div id="roundContent"></div>
</div>
<!-- Shoe Info -->
<div class="panel" id="shoePanel">
<div class="panel-label">Shoe Composition</div>
<div id="shoeContent"></div>
</div>
</div>
<!-- Strategy Chart -->
<div class="chart-section">
<button class="chart-btn" id="chartBtn">
<span class="chart-btn-icon">♦</span>
Generate Strategy Chart
</button>
<div class="chart-container" id="chartContainer" style="display:none">
<iframe id="chartIframe" src="strategy-chart.html"></iframe>
</div>
</div>
</div>
<script type="module">
function normalizeCard(card) {
card = String(card).toLowerCase();
if (['10', 'j', 'q', 'k'].includes(card)) return '0';
if (card === 'a') return '1';
return card;
}
function normalizeCards(cards) {
return cards.map(normalizeCard);
}
const VALID_CHARS = new Set(['2','3','4','5','6','7','8','9','1','0','j','q','k','a']);
const CARD_TYPES = ['2','3','4','5','6','7','8','9','0','1'];
const CARD_LABELS = { '2':'2','3':'3','4':'4','5':'5','6':'6','7':'7','8':'8','9':'9','0':'10','1':'A' };
function parseCards(str) {
if (!str) return [];
return str.toLowerCase().split('').filter(c => VALID_CHARS.has(c)).map(normalizeCard);
}
function parseHandLogs(text) {
const deadCards = {};
let autoPlayer = '', autoDealer = '';
if (!text) return { deadCards, autoPlayer, autoDealer };
const lines = text.split('\n');
const nonEmpty = lines.filter(l => l.trim());
const lastComplete = nonEmpty.length > 0 && parseCards(nonEmpty[nonEmpty.length - 1]).length >= 3;
const lastNonEmptyIdx = nonEmpty.length > 0
? lines.lastIndexOf(nonEmpty[nonEmpty.length - 1])
: -1;
lines.forEach((line, i) => {
const cards = parseCards(line.trim());
// If this is the last complete hand and it will auto-fill,
// exclude its cards from deadCards — the solver subtracts them internally
if (lastComplete && i === lastNonEmptyIdx) {
autoPlayer = cards[0] + cards[2];
autoDealer = cards[1];
// Only count extra cards beyond the auto-filled 3 (player1, dealer, player2)
cards.slice(3).forEach(c => { deadCards[c] = (deadCards[c] || 0) + 1; });
} else {
cards.forEach(c => { deadCards[c] = (deadCards[c] || 0) + 1; });
}
});
return { deadCards, autoPlayer, autoDealer };
}
function fmtEV(v) {
if (v === null || v === undefined) return '—';
const pct = (v * 100).toFixed(2);
return (v >= 0 ? '+' : '') + pct + '%';
}
function showError(container, message) {
const banner = document.createElement('div');
banner.className = 'error-banner';
banner.textContent = message;
container.textContent = '';
container.appendChild(banner);
}
let solveAbort = null;
let solveDebounce = null;
function updateResults() {
const errorDiv = document.getElementById('errorDiv');
errorDiv.textContent = '';
try {
const handData = parseHandLogs(document.getElementById('deadCards').value);
const deadCards = handData.deadCards;
const pf = document.getElementById('playerCards');
const df = document.getElementById('dealerCard');
if (!pf.dataset.lastAuto) pf.dataset.lastAuto = '';
if (!df.dataset.lastAuto) df.dataset.lastAuto = '';
const pManual = pf.value !== pf.dataset.lastAuto;
const dManual = df.value !== df.dataset.lastAuto;
let pInput = pManual && pf.value.trim() ? pf.value : handData.autoPlayer;
if (!pManual && handData.autoPlayer) {
pf.value = handData.autoPlayer;
pf.dataset.lastAuto = handData.autoPlayer;
}
let dInput = dManual && df.value.trim() ? df.value : handData.autoDealer;
if (!dManual && handData.autoDealer) {
df.value = handData.autoDealer;
df.dataset.lastAuto = handData.autoDealer;
}
const playerCards = parseCards(pInput);
const dealerCard = dInput ? normalizeCard(dInput.toLowerCase().trim()) : '';
const deckCount = parseInt(document.getElementById('deckCount').value) || 8;
const hitOnSoft17 = document.getElementById('hitSoft17').checked;
const dealerPeeks = document.getElementById('dealerPeeks').checked;
// Shoe info
renderShoe(deckCount, deadCards);
// Hand analysis — debounced fetch to server
const validDealer = dealerCard && CARD_TYPES.includes(dealerCard);
if (playerCards.length >= 2 && validDealer) {
clearTimeout(solveDebounce);
solveDebounce = setTimeout(() => {
if (solveAbort) solveAbort.abort();
solveAbort = new AbortController();
fetch('/api/solve', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ dealerCard, playerCards, options: { deckCount, deadCards, hitOnSoft17, dealerPeeks } }),
signal: solveAbort.signal,
})
.then(r => r.json())
.then(result => {
if (result.error) throw new Error(result.error);
renderAnalysis(result);
renderDealer(result.dealerOutcomes);
})
.catch(err => {
if (err.name !== 'AbortError') {
showError(errorDiv, err.message);
}
});
}, 150);
} else {
clearTimeout(solveDebounce);
document.getElementById('analysisContent').innerHTML =
'<div class="empty-state"><div class="suit">♠ ♥</div>Enter your cards and dealer upcard</div>';
document.getElementById('dealerContent').innerHTML =
'<div class="empty-state"><div class="suit">♦ ♣</div>Waiting for hand data</div>';
}
} catch (err) {
showError(errorDiv, err.message);
}
}
function renderAnalysis(r) {
const action = r.action.toUpperCase();
const evClass = r.ev >= 0 ? 'positive' : 'negative';
const hv = r.handValue === 'BJ' ? 'Blackjack' : r.handValue;
// Build EV bars
const actions = [];
const d = r.details;
if (d.standEV !== null && d.standEV !== undefined) actions.push({ key: 'stand', label: 'Stand', ev: d.standEV });
if (d.hitEV !== null && d.hitEV !== undefined) actions.push({ key: 'hit', label: 'Hit', ev: d.hitEV });
if (d.doubleEV !== null && d.doubleEV !== undefined) actions.push({ key: 'double', label: 'Double', ev: d.doubleEV });
if (d.splitEV !== null && d.splitEV !== undefined) actions.push({ key: 'split', label: 'Split', ev: d.splitEV });
const maxAbs = Math.max(0.01, ...actions.map(a => Math.abs(a.ev)));
const bestKey = r.action;
let barsHTML = '';
for (const a of actions) {
const pct = (a.ev / maxAbs) * 50;
const width = Math.abs(pct);
const left = a.ev >= 0 ? 50 : 50 - width;
const isBest = a.key === bestKey;
barsHTML += `
<div class="ev-bar-row">
<div class="label ${isBest ? 'best' : ''}">${a.label}</div>
<div class="ev-bar-track">
<div class="ev-bar-center"></div>
<div class="ev-bar-fill ${a.key}" style="left:${left}%;width:${width}%"></div>
</div>
<div class="val">${fmtEV(a.ev)}</div>
</div>`;
}
document.getElementById('analysisContent').innerHTML = `
<div class="action-display">
<div class="action-badge ${r.action}">${action}</div>
<div><span class="hand-value-tag">${hv}</span></div>
<div class="ev-label">Best EV <strong class="${evClass}">${fmtEV(r.ev)}</strong></div>
</div>
<div class="ev-bars">${barsHTML}</div>`;
}
function renderDealer(outcomes) {
const order = ['17','18','19','20','21','BJ','bust'];
const labels = { '17':'17','18':'18','19':'19','20':'20','21':'21','BJ':'BJ','bust':'Bust' };
let maxP = 0;
const data = order.map(k => {
const p = outcomes[k] || 0;
if (p > maxP) maxP = p;
return { key: k, p };
});
let html = '<div class="dealer-chart">';
for (const d of data) {
const h = maxP > 0 ? (d.p / maxP) * 90 : 0;
const cls = d.key === 'bust' ? 'bust' : d.key === 'BJ' ? 'bj' : 'total';
const pctText = (d.p * 100).toFixed(1) + '%';
html += `
<div class="dealer-bar-wrap">
<div class="dealer-bar ${cls}" style="height:${h}%">
${d.p > 0.001 ? `<span class="pct">${pctText}</span>` : ''}
</div>
<div class="dealer-bar-label">${labels[d.key]}</div>
</div>`;
}
html += '</div>';
document.getElementById('dealerContent').innerHTML = html;
}
function renderShoe(deckCount, deadCards) {
const totalPerType = CARD_TYPES.map(c => c === '0' ? deckCount * 16 : deckCount * 4);
let totalCards = 0, totalDead = 0;
const info = CARD_TYPES.map((c, i) => {
const dead = deadCards[c] || 0;
const remaining = Math.max(0, totalPerType[i] - dead);
totalCards += totalPerType[i];
totalDead += dead;
return { card: c, total: totalPerType[i], dead, remaining };
});
let html = `
<div class="shoe-stats">
<div class="shoe-stat">
<div class="num">${totalCards - totalDead}</div>
<div class="desc">Cards remaining</div>
</div>
<div class="shoe-stat">
<div class="num">${totalDead}</div>
<div class="desc">Cards seen</div>
</div>
</div>
<div class="card-counts">`;
for (const c of info) {
const hasDead = c.dead > 0;
html += `
<div class="card-count ${hasDead ? 'dead' : ''}">
<div class="sym">${CARD_LABELS[c.card]}</div>
<div class="cnt">${c.remaining}/${c.total}</div>
</div>`;
}
html += '</div>';
document.getElementById('shoeContent').innerHTML = html;
}
// ── Round EV: instant Hi-Lo estimate ──────────────────────────
function hiLoEstimate(deckCount, deadCards) {
const high = (deadCards['0'] || 0) + (deadCards['1'] || 0);
const low = (deadCards['2'] || 0) + (deadCards['3'] || 0) +
(deadCards['4'] || 0) + (deadCards['5'] || 0) + (deadCards['6'] || 0);
const running = low - high;
const totalDead = Object.values(deadCards).reduce((s, c) => s + c, 0);
const decksLeft = Math.max(0.25, (deckCount * 52 - totalDead) / 52);
const trueCount = running / decksLeft;
const ev = -0.005 + trueCount * 0.005;
return { ev, trueCount, running, decksLeft };
}
function renderRoundEV(estimate, exact, computing) {
const data = exact || estimate;
const isExact = !!exact;
const evPct = (data.ev * 100).toFixed(3);
const evSign = data.ev >= 0 ? '+' : '';
const evClass = data.ev > 0.001 ? 'positive' : data.ev < -0.001 ? 'negative' : '';
const verdict = data.ev > 0.001 ? 'BET' : data.ev < -0.001 ? 'SIT OUT' : 'NEUTRAL';
const verdictClass = data.ev > 0.001 ? 'favorable' : data.ev < -0.001 ? 'unfavorable' : 'neutral';
let sourceTag;
if (computing && !isExact) {
sourceTag = `<span class="round-source computing"><span class="computing-dot">●</span> Solving exact…</span>`;
} else if (isExact) {
sourceTag = `<span class="round-source exact">Exact solve${exact.elapsed ? ' · ' + exact.elapsed + 's' : ''}</span>`;
} else {
sourceTag = `<span class="round-source estimate">Hi-Lo estimate</span>`;
}
let metaHTML = '';
if (estimate) {
metaHTML = `
<div class="round-meta">
<div class="row"><span>True count</span><span class="val">${estimate.trueCount >= 0 ? '+' : ''}${estimate.trueCount.toFixed(2)}</span></div>
<div class="row"><span>Running count</span><span class="val">${estimate.running >= 0 ? '+' : ''}${estimate.running}</span></div>
<div class="row"><span>Decks remaining</span><span class="val">${estimate.decksLeft.toFixed(1)}</span></div>
</div>`;
}
document.getElementById('roundContent').innerHTML = `
<div class="round-ev-display">
<div class="round-verdict ${verdictClass}">${verdict}</div>
<div class="round-ev-num ${evClass}">${evSign}${evPct}%</div>
${metaHTML}
${sourceTag}
</div>`;
}
// ── Server-side exact round EV ─────────────────────────────
let evAbort = null;
let lastExactResult = null;
let lastExactKey = '';
let currentEstimate = null;
function currentShoeKey() {
const dc = document.getElementById('deckCount').value;
const h17 = document.getElementById('hitSoft17').checked;
const peek = document.getElementById('dealerPeeks').checked;
const logs = document.getElementById('deadCards').value;
return `${dc}_${h17}_${peek}_${logs}`;
}
function updateRoundEV(deckCount, deadCards, hitOnSoft17, dealerPeeks) {
// Instant Hi-Lo estimate
currentEstimate = hiLoEstimate(deckCount, deadCards);
// Check if exact result is still valid
const key = currentShoeKey();
if (key === lastExactKey && lastExactResult) {
renderRoundEV(currentEstimate, lastExactResult, false);
return;
}
// Show estimate, kick off exact solve on server
lastExactResult = null;
if (evAbort) evAbort.abort();
evAbort = new AbortController();