-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGo.html
More file actions
1339 lines (1165 loc) · 43.9 KB
/
Go.html
File metadata and controls
1339 lines (1165 loc) · 43.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">
<script>
(function() {
var ua = navigator.userAgent;
var isPhone = /iPhone|iPad|iPod|Android/i.test(ua) || window.innerWidth < 768;
if (isPhone) {
var meta = document.querySelector('meta[name="viewport"]');
if (meta) {
var targetWidth = 780;
var scale = window.innerWidth / targetWidth;
meta.setAttribute('content', 'width=' + targetWidth + ', initial-scale=' + scale + ', maximum-scale=3.0, user-scalable=yes');
}
}
})();
</script>
<meta name="theme-color" content="#0c1016">
<title>Go — Free Online Go (Baduk) Game, 19×19 | Board Gaming Hub</title>
<meta name="description" content="Play Go (Baduk) online for free. 19×19 board, territory scoring, single-player vs AI. The ancient game of strategy and influence. No signup.">
<link rel="canonical" href="https://boardgaminghub.com/Go.html">
<meta property="og:type" content="website">
<meta property="og:url" content="https://boardgaminghub.com/Go.html">
<meta property="og:title" content="Go — Free Online 19×19">
<meta property="og:description" content="Free in-browser Go on a 19×19 board. Single-player vs AI.">
<meta property="og:image" content="https://boardgaminghub.com/og-image.png">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://boardgaminghub.com/og-image.png">
<script type="application/ld+json">
{ "@context": "https://schema.org", "@type": "VideoGame", "name": "Go (Baduk)",
"description": "The ancient game of territory and influence on a 19×19 grid.",
"url": "https://boardgaminghub.com/Go.html", "genre": "Board Game",
"applicationCategory": "Game", "operatingSystem": "Any",
"offers": { "@type": "Offer", "price": "0", "priceCurrency": "USD" } }
</script>
<link rel="icon" href="data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'><text y='.9em' font-size='90'>⚫</text></svg>">
<style>
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
font-family: 'Georgia', serif;
background: #1A1810;
color: #C8D0B8;
display: flex;
flex-direction: column;
align-items: center;
padding: 20px;
min-height: 100vh;
}
h1 {
font-size: 1.7em;
color: #DEB070;
margin-bottom: 2px;
text-shadow: 1px 1px 3px rgba(0,0,0,0.5);
letter-spacing: 0.2em;
}
.subtitle {
font-size: 0.8em;
color: #8B7040;
margin-bottom: 2px;
font-style: italic;
}
.subtitle-cn {
font-size: 0.7em;
color: #6A5A38;
margin-bottom: 12px;
}
/* Mode selector */
.mode-selector {
display: flex;
margin-bottom: 10px;
border-radius: 6px;
overflow: hidden;
border: 2px solid #8B7040;
}
.mode-btn {
padding: 6px 18px;
font-size: 0.95em;
font-family: 'Georgia', serif;
cursor: pointer;
border: none;
background: #2A2418;
color: #6A5A38;
transition: background 0.2s, color 0.2s;
}
.mode-btn:not(:last-child) { border-right: 1px solid #8B7040; }
.mode-btn.active { background: #8B7040; color: #1A1810; cursor: default; }
.mode-btn:hover:not(.active) { background: #3A3020; color: #DEB070; }
/* Size selector */
.size-selector {
display: flex;
margin-bottom: 10px;
border-radius: 6px;
overflow: hidden;
border: 2px solid #5A4A30;
}
.size-btn {
padding: 5px 14px;
font-size: 0.85em;
font-family: 'Georgia', serif;
cursor: pointer;
border: none;
background: #2A2418;
color: #6A5A38;
transition: background 0.2s, color 0.2s;
}
.size-btn:not(:last-child) { border-right: 1px solid #5A4A30; }
.size-btn.active { background: #5A4A30; color: #DEB070; cursor: default; }
.size-btn:hover:not(.active) { background: #3A3020; color: #DEB070; }
/* Game info */
.game-info { text-align: center; margin-bottom: 8px; }
#turn-indicator { font-size: 1.15em; font-weight: bold; margin-bottom: 4px; }
#message {
font-size: 0.95em;
color: #90B868;
min-height: 1.4em;
margin-bottom: 6px;
}
.buttons { margin-bottom: 12px; }
button {
padding: 8px 22px;
font-size: 1em;
font-family: 'Georgia', serif;
cursor: pointer;
margin: 0 5px;
border: 2px solid #8B7040;
border-radius: 6px;
background: #2A2418;
color: #C8D0B8;
transition: background 0.2s;
}
button:hover:not(:disabled) { background: #3A3020; }
button:disabled { opacity: 0.4; cursor: not-allowed; }
/* Main layout */
.container { display: flex; align-items: flex-start; gap: 18px; }
.side-panel {
display: flex;
flex-direction: column;
align-items: center;
min-width: 80px;
padding-top: 10px;
}
.side-panel h3 {
font-size: 0.85em;
margin-bottom: 6px;
color: #8B7040;
}
.prisoner-count {
font-size: 1.6em;
font-weight: bold;
margin-bottom: 8px;
}
.prisoner-label {
font-size: 0.7em;
color: #6A5A38;
margin-bottom: 10px;
}
.pass-indicator {
font-size: 0.75em;
color: #90B868;
margin-top: 6px;
min-height: 1.2em;
}
.stone-icon {
width: 22px;
height: 22px;
border-radius: 50%;
margin-bottom: 6px;
}
.stone-icon.black {
background: radial-gradient(circle at 35% 30%, #555, #111);
box-shadow: 1px 1px 3px rgba(0,0,0,0.6);
}
.stone-icon.white {
background: radial-gradient(circle at 35% 30%, #fff, #d0d0d0);
box-shadow: 1px 1px 3px rgba(0,0,0,0.3);
}
/* Board */
.board-frame {
border: 4px solid #8B7040;
border-radius: 4px;
box-shadow: 0 4px 20px rgba(0,0,0,0.7), 0 0 40px rgba(0,0,0,0.3);
padding: 0;
display: inline-block;
}
.board-wrapper {
position: relative;
overflow: hidden;
}
.board-surface {
position: absolute;
top: 0; left: 0; right: 0; bottom: 0;
background: #DEB070;
/* Subtle wood grain */
background-image:
repeating-linear-gradient(
90deg,
transparent,
transparent 8px,
rgba(180,140,70,0.15) 8px,
rgba(180,140,70,0.15) 9px
),
repeating-linear-gradient(
0deg,
transparent,
transparent 40px,
rgba(160,120,50,0.08) 40px,
rgba(160,120,50,0.08) 42px
);
z-index: 0;
}
.board-grid {
position: relative;
z-index: 1;
display: grid;
}
.cell {
position: relative;
cursor: pointer;
}
.cell:hover .hover-stone {
opacity: 0.3;
}
/* Grid lines via pseudo-elements on cells */
.cell::before {
content: '';
position: absolute;
background: #5A4020;
z-index: 1;
pointer-events: none;
}
/* Horizontal line */
.cell::after {
content: '';
position: absolute;
background: #5A4020;
z-index: 1;
pointer-events: none;
}
/* Horizontal line through center */
.cell::before {
top: 50%;
left: 0;
right: 0;
height: 1px;
transform: translateY(-0.5px);
}
/* Vertical line through center */
.cell::after {
left: 50%;
top: 0;
bottom: 0;
width: 1px;
transform: translateX(-0.5px);
}
/* Edge cells: clip lines */
.cell.edge-top::after { top: 50%; }
.cell.edge-bottom::after { bottom: 50%; }
.cell.edge-left::before { left: 50%; }
.cell.edge-right::before { right: 50%; }
/* Star point (hoshi) */
.hoshi {
position: absolute;
width: 8px;
height: 8px;
background: #5A4020;
border-radius: 50%;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
z-index: 2;
pointer-events: none;
}
/* Stones */
.stone {
position: absolute;
border-radius: 50%;
z-index: 5;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
pointer-events: none;
}
.stone.black {
background: radial-gradient(circle at 35% 30%, #555, #222 40%, #111 100%);
box-shadow: 2px 2px 4px rgba(0,0,0,0.6), inset -1px -1px 2px rgba(0,0,0,0.3);
}
.stone.white {
background: radial-gradient(circle at 35% 30%, #fff, #e8e8e8 40%, #d0d0d0 100%);
box-shadow: 2px 2px 4px rgba(0,0,0,0.3), inset -1px -1px 2px rgba(0,0,0,0.05);
}
.stone.last-move {
box-shadow: 0 0 0 2px #90B868, 2px 2px 4px rgba(0,0,0,0.5);
}
.hover-stone {
position: absolute;
border-radius: 50%;
z-index: 4;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
pointer-events: none;
opacity: 0;
transition: opacity 0.1s;
}
.hover-stone.black {
background: radial-gradient(circle at 35% 30%, #555, #111);
}
.hover-stone.white {
background: radial-gradient(circle at 35% 30%, #fff, #d0d0d0);
}
/* Score display */
.score-display {
margin-top: 15px;
padding: 12px 20px;
background: #2A2418;
border: 2px solid #8B7040;
border-radius: 8px;
text-align: center;
display: none;
}
.score-display h3 {
color: #DEB070;
margin-bottom: 8px;
}
.score-line {
font-size: 0.95em;
margin: 4px 0;
}
.score-winner {
font-size: 1.1em;
font-weight: bold;
color: #90B868;
margin-top: 8px;
}
/* Rules */
.rules {
margin-top: 18px;
max-width: 540px;
font-size: 0.78em;
color: #6A5A38;
line-height: 1.5;
}
.rules h3 { color: #8B7040; margin-bottom: 6px; }
.rules ul { padding-left: 18px; }
.rules li { margin-bottom: 3px; }
/* Mobile game nav */
</style>
<script type="application/ld+json">{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Board Gaming Hub","item":"https://boardgaminghub.com/"},{"@type":"ListItem","position":2,"name":"Board Games","item":"https://boardgaminghub.com/#board"},{"@type":"ListItem","position":3,"name":"Go","item":"https://boardgaminghub.com/Go.html"}]}</script>
<script type="application/ld+json">{"@context":"https://schema.org","@type":"FAQPage","mainEntity":[{"@type":"Question","name":"How is Go different from chess?","acceptedAnswer":{"@type":"Answer","text":"Both are abstract strategy with perfect information, but Go favors positional accumulation over tactical exchange. The game tree is far larger; computer mastery only arrived in 2016 with AlphaGo."}},{"@type":"Question","name":"What is the ko rule?","acceptedAnswer":{"@type":"Answer","text":"You may not immediately recapture a single stone in a way that recreates the previous board position. Prevents infinite capture loops. Larger ko fights are central to high-level strategy."}},{"@type":"Question","name":"What's a good board size for beginners?","acceptedAnswer":{"@type":"Answer","text":"9×9 — completes in 10–15 minutes and reveals territory and capture concepts cleanly. Move to 13×13 once captures feel natural, 19×19 for full-board strategy."}},{"@type":"Question","name":"What is komi?","acceptedAnswer":{"@type":"Answer","text":"A points compensation given to White (the second player) at the end. Modern komi is 6.5 or 7.5, fractional to prevent draws."}}]}</script>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-2835365593874464"
crossorigin="anonymous"></script>
</head>
<body>
<style id="siteNav-css">
#siteNav { position: sticky; top: 0; z-index: 99999; background: rgba(16,20,28,0.96); border-bottom: 1px solid #2a3540; font-family: Georgia, "Times New Roman", serif; box-shadow: 0 2px 10px rgba(0,0,0,0.6); color: #d8d0c0; }
#siteNav * { box-sizing: border-box; }
#siteNav .nav-bar { display: flex; justify-content: space-between; align-items: center; padding: 8px 16px; max-width: 1400px; margin: 0 auto; gap: 12px; }
#siteNav .nav-home { color: #d8d0c0; text-decoration: none; font-size: 0.82em; letter-spacing: 2px; padding: 5px 12px; border: 1px solid #3a5060; border-radius: 3px; white-space: nowrap; }
#siteNav .nav-home:hover { background: #1e2838; color: #f0e0b0; }
#siteNav .nav-title { color: #f0d89c; font-size: 0.95em; letter-spacing: 3px; font-weight: bold; text-align: center; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#siteNav .nav-toggle { background: none; border: 1px solid #3a5060; color: #d8d0c0; font-size: 0.85em; letter-spacing: 2px; padding: 5px 12px; border-radius: 3px; cursor: pointer; font-family: inherit; white-space: nowrap; }
#siteNav .nav-toggle:hover { background: #1e2838; color: #f0e0b0; }
#siteNav .nav-menu { display: none; background: #0f141c; border-top: 1px solid #2a4050; padding: 14px 16px; max-width: 1400px; margin: 0 auto; }
#siteNav .nav-menu.open { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
#siteNav .nav-cat { min-width: 0; }
#siteNav .nav-cat-label { color: #8098a8; font-size: 0.72em; letter-spacing: 4px; padding: 4px 0 8px 0; border-bottom: 1px solid #2a3540; margin-bottom: 6px; }
#siteNav .nav-cat a { display: block; padding: 6px 10px; color: #a8b0c0; text-decoration: none; font-size: 0.88em; border-radius: 3px; }
#siteNav .nav-cat a:hover { background: #1e2838; color: #f0e0b0; }
#siteNav .nav-cat a.current { color: #f0d89c; font-weight: bold; background: #1e2838; }
@media (max-width: 640px) {
#siteNav .nav-menu.open { grid-template-columns: 1fr; gap: 14px; }
#siteNav .nav-title { font-size: 0.82em; letter-spacing: 2px; }
#siteNav .nav-home,
#siteNav .nav-toggle { font-size: 0.74em; padding: 4px 8px; }
}
</style>
<div id="siteNav">
<div class="nav-bar">
<a href="index.html" class="nav-home">HOME</a>
<span class="nav-title">Go</span>
<button class="nav-toggle" onclick="document.querySelector('#siteNav .nav-menu').classList.toggle('open')">GAMES ☰</button>
</div>
<div class="nav-menu">
<div class="nav-cat">
<div class="nav-cat-label">BOARD GAMES</div>
<a href="Agora.html">Agora</a>
<a href="Aresia.html">Aresia</a>
<a href="Backgammon.html">Backgammon</a>
<a href="Bisque.html">Bisque</a>
<a href="Chess.html">Chess</a>
<a href="Convergence.html">Convergence</a>
<a href="Go.html" class="current">Go</a>
<a href="Mancala.html">Mancala</a>
<a href="Odyssey.html">Odyssey</a>
<a href="Othello.html">Othello</a>
<a href="PenteGrammai.html">Pente Grammai</a>
<a href="Senet.html">Senet</a>
<a href="Tidelands.html">Tidelands</a>
<a href="Ur.html">Ur</a>
</div>
<div class="nav-cat">
<div class="nav-cat-label">SIMULATIONS</div>
<a href="Apoapsis.html">Apoapsis</a>
<a href="BiosphereBlue.html">Biosphere Blue</a>
<a href="BonnevilleSpillwayOperator.html">Bonneville Spillway</a>
<a href="Cliffwalkers.html">Cliffwalkers</a>
<a href="Doctrine.html">Doctrine</a>
<a href="EclipsePredictor.html">Eclipse Predictor</a>
<a href="Floodline.html">Floodline</a>
<a href="Metropolis2K.html">Metropolis 2K</a>
<a href="Tower.html">Tower</a>
</div>
</div>
</div>
<h1>GO</h1>
<div class="subtitle">The Surrounding Game</div>
<div class="subtitle-cn">— 圍棋 —</div>
<div class="mode-selector">
<button class="mode-btn active" id="mode-1p" onclick="setMode(true)">1 Player</button>
<button class="mode-btn" id="mode-2p" onclick="setMode(false)">2 Players</button>
</div>
<div class="size-selector">
<button class="size-btn active" id="size-9" onclick="setBoardSize(9)">9×9</button>
<button class="size-btn" id="size-13" onclick="setBoardSize(13)">13×13</button>
<button class="size-btn" id="size-19" onclick="setBoardSize(19)">19×19</button>
</div>
<div class="game-info">
<div id="turn-indicator"></div>
<div id="message"></div>
</div>
<div class="buttons">
<button id="pass-btn" onclick="passMove()">Pass</button>
<button onclick="resetGame()">Reset Game</button>
</div>
<div class="container">
<div class="side-panel" id="panel-black">
<h3>Black (P1)</h3>
<div class="stone-icon black"></div>
<div class="prisoner-count" id="prisoners-black">0</div>
<div class="prisoner-label">captured</div>
<div class="pass-indicator" id="pass-black"></div>
</div>
<div class="board-frame">
<div class="board-wrapper" id="board-wrapper">
<div class="board-surface"></div>
<div class="board-grid" id="board"></div>
</div>
</div>
<div class="side-panel" id="panel-white">
<h3 id="white-label">White (AI)</h3>
<div class="stone-icon white"></div>
<div class="prisoner-count" id="prisoners-white">0</div>
<div class="prisoner-label">captured</div>
<div class="pass-indicator" id="pass-white"></div>
</div>
</div>
<div class="score-display" id="score-display">
<h3>Game Over — Final Score</h3>
<div class="score-line" id="score-black"></div>
<div class="score-line" id="score-white"></div>
<div class="score-winner" id="score-winner"></div>
</div>
<div class="rules">
<h3>Rules</h3>
<ul>
<li>Black plays first. Place stones on <b>intersections</b> of the grid lines.</li>
<li>Stones of the same color connected horizontally/vertically form a <b>group</b>.</li>
<li>A group's <b>liberties</b> are the number of empty adjacent intersections.</li>
<li>When a group has zero liberties, it is <b>captured</b> and removed from the board.</li>
<li>You may not play a stone that would have no liberties (suicide), unless it captures opponent stones.</li>
<li><b>Ko rule</b>: you may not recreate the immediately previous board position.</li>
<li>Either player may <b>pass</b>. Two consecutive passes end the game.</li>
<li><b>Scoring</b> (Chinese/area rules): stones on the board + surrounded empty territory.</li>
</ul>
</div>
<script>
// =============================================
// Constants & Config
// =============================================
var BLACK = 1;
var WHITE = 2;
var EMPTY = 0;
var CELL_SIZES = { 9: 44, 13: 32, 19: 24 };
var STONE_SCALE = 0.85; // stone diameter relative to cell size
var HOSHI_POINTS = {
9: [[2,2],[2,6],[6,2],[6,6],[4,4]],
13: [[3,3],[3,9],[9,3],[9,9],[6,6],[3,6],[9,6],[6,3],[6,9]],
19: [[3,3],[3,9],[3,15],[9,3],[9,9],[9,15],[15,3],[15,9],[15,15]]
};
var KOMI = 6.5; // compensation for white
// =============================================
// Game State
// =============================================
var boardSize = 9;
var cellSize = 44;
var board = []; // 2D array [row][col] = EMPTY/BLACK/WHITE
var currentPlayer = BLACK;
var prisoners = { 1: 0, 2: 0 }; // stones captured BY each player
var prevBoardState = null; // for ko detection
var consecutivePasses = 0;
var gameOver = false;
var lastMove = null; // {r, c} or null
var aiMode = true;
var aiTimeout = null;
var moveHistory = [];
var boardEl = document.getElementById('board');
var boardWrapper = document.getElementById('board-wrapper');
// =============================================
// Mode & Size
// =============================================
function setMode(isAi) {
if (aiMode === isAi) return;
aiMode = isAi;
document.getElementById('mode-1p').classList.toggle('active', isAi);
document.getElementById('mode-2p').classList.toggle('active', !isAi);
document.getElementById('white-label').textContent = isAi ? 'White (AI)' : 'White (P2)';
resetGame();
}
function setBoardSize(size) {
if (boardSize === size) return;
boardSize = size;
cellSize = CELL_SIZES[size];
document.getElementById('size-9').classList.toggle('active', size === 9);
document.getElementById('size-13').classList.toggle('active', size === 13);
document.getElementById('size-19').classList.toggle('active', size === 19);
resetGame();
}
// =============================================
// Board State Helpers
// =============================================
function createEmptyBoard() {
var b = [];
for (var r = 0; r < boardSize; r++) {
b[r] = [];
for (var c = 0; c < boardSize; c++) {
b[r][c] = EMPTY;
}
}
return b;
}
function cloneBoard(b) {
var nb = [];
for (var r = 0; r < boardSize; r++) {
nb[r] = b[r].slice();
}
return nb;
}
function boardsEqual(a, b) {
if (!a || !b) return false;
for (var r = 0; r < boardSize; r++) {
for (var c = 0; c < boardSize; c++) {
if (a[r][c] !== b[r][c]) return false;
}
}
return true;
}
function boardToString(b) {
var s = '';
for (var r = 0; r < boardSize; r++) {
for (var c = 0; c < boardSize; c++) {
s += b[r][c];
}
}
return s;
}
function neighbors(r, c) {
var adj = [];
if (r > 0) adj.push([r - 1, c]);
if (r < boardSize - 1) adj.push([r + 1, c]);
if (c > 0) adj.push([r, c - 1]);
if (c < boardSize - 1) adj.push([r, c + 1]);
return adj;
}
// =============================================
// Group & Liberty Logic
// =============================================
function getGroup(b, r, c) {
var color = b[r][c];
if (color === EMPTY) return { stones: [], liberties: 0 };
var visited = {};
var stones = [];
var liberties = 0;
var libertySet = {};
var stack = [[r, c]];
visited[r + ',' + c] = true;
while (stack.length > 0) {
var pos = stack.pop();
var pr = pos[0], pc = pos[1];
stones.push([pr, pc]);
var adj = neighbors(pr, pc);
for (var i = 0; i < adj.length; i++) {
var nr = adj[i][0], nc = adj[i][1];
var key = nr + ',' + nc;
if (visited[key]) continue;
if (b[nr][nc] === color) {
visited[key] = true;
stack.push([nr, nc]);
} else if (b[nr][nc] === EMPTY) {
if (!libertySet[key]) {
libertySet[key] = true;
liberties++;
}
}
}
}
return { stones: stones, liberties: liberties };
}
function removeGroup(b, stones) {
for (var i = 0; i < stones.length; i++) {
b[stones[i][0]][stones[i][1]] = EMPTY;
}
return stones.length;
}
// =============================================
// Move Validation & Execution
// =============================================
function tryMove(r, c, player) {
// Must be on board and empty
if (r < 0 || r >= boardSize || c < 0 || c >= boardSize) return null;
if (board[r][c] !== EMPTY) return null;
var opponent = player === BLACK ? WHITE : BLACK;
var testBoard = cloneBoard(board);
testBoard[r][c] = player;
// Check for captures of opponent groups
var captured = 0;
var adj = neighbors(r, c);
for (var i = 0; i < adj.length; i++) {
var nr = adj[i][0], nc = adj[i][1];
if (testBoard[nr][nc] === opponent) {
var group = getGroup(testBoard, nr, nc);
if (group.liberties === 0) {
captured += removeGroup(testBoard, group.stones);
}
}
}
// Check if placing stone results in self-capture (suicide)
var selfGroup = getGroup(testBoard, r, c);
if (selfGroup.liberties === 0) {
return null; // Suicide - illegal
}
// Ko check: can't recreate the previous board state
if (prevBoardState && boardsEqual(testBoard, prevBoardState)) {
return null; // Ko violation
}
return { board: testBoard, captured: captured };
}
function isLegalMove(r, c, player) {
return tryMove(r, c, player) !== null;
}
function playMove(r, c, player) {
var result = tryMove(r, c, player);
if (!result) return false;
prevBoardState = cloneBoard(board);
board = result.board;
prisoners[player] += result.captured;
lastMove = { r: r, c: c };
consecutivePasses = 0;
moveHistory.push({ r: r, c: c, player: player });
currentPlayer = player === BLACK ? WHITE : BLACK;
return true;
}
// =============================================
// Pass & Game End
// =============================================
function passMove() {
if (gameOver) return;
if (aiMode && currentPlayer === WHITE) return;
doPass(currentPlayer);
render();
}
function doPass(player) {
consecutivePasses++;
var name = player === BLACK ? 'Black' : 'White';
setMessage(name + ' passes.');
if (player === BLACK) {
document.getElementById('pass-black').textContent = 'Passed';
} else {
document.getElementById('pass-white').textContent = 'Passed';
}
if (consecutivePasses >= 2) {
endGame();
return;
}
prevBoardState = null; // ko is reset after a pass
lastMove = null;
currentPlayer = player === BLACK ? WHITE : BLACK;
}
// =============================================
// Scoring (Chinese / Area Scoring)
// =============================================
function calculateScore() {
var territory = createEmptyBoard(); // 0=neutral, 1=black, 2=white
var visited = {};
// Count stones on board
var blackStones = 0;
var whiteStones = 0;
for (var r = 0; r < boardSize; r++) {
for (var c = 0; c < boardSize; c++) {
if (board[r][c] === BLACK) blackStones++;
else if (board[r][c] === WHITE) whiteStones++;
}
}
// Flood-fill empty regions to determine territory
var blackTerritory = 0;
var whiteTerritory = 0;
for (var r = 0; r < boardSize; r++) {
for (var c = 0; c < boardSize; c++) {
var key = r + ',' + c;
if (board[r][c] !== EMPTY || visited[key]) continue;
// BFS to find connected empty region
var region = [];
var touchesBlack = false;
var touchesWhite = false;
var stack = [[r, c]];
visited[key] = true;
while (stack.length > 0) {
var pos = stack.pop();
var pr = pos[0], pc = pos[1];
region.push([pr, pc]);
var adj = neighbors(pr, pc);
for (var i = 0; i < adj.length; i++) {
var nr = adj[i][0], nc = adj[i][1];
var nkey = nr + ',' + nc;
if (board[nr][nc] === BLACK) { touchesBlack = true; continue; }
if (board[nr][nc] === WHITE) { touchesWhite = true; continue; }
if (visited[nkey]) continue;
visited[nkey] = true;
stack.push([nr, nc]);
}
}
// If region only touches one color, it's that color's territory
if (touchesBlack && !touchesWhite) {
blackTerritory += region.length;
} else if (touchesWhite && !touchesBlack) {
whiteTerritory += region.length;
}
}
}
return {
blackStones: blackStones,
whiteStones: whiteStones,
blackTerritory: blackTerritory,
whiteTerritory: whiteTerritory,
blackTotal: blackStones + blackTerritory,
whiteTotal: whiteStones + whiteTerritory + KOMI
};
}
function endGame() {
gameOver = true;
var score = calculateScore();
document.getElementById('score-black').textContent =
'Black: ' + score.blackStones + ' stones + ' + score.blackTerritory + ' territory = ' + score.blackTotal;
document.getElementById('score-white').textContent =
'White: ' + score.whiteStones + ' stones + ' + score.whiteTerritory + ' territory + ' + KOMI + ' komi = ' + score.whiteTotal;
var winner;
if (score.blackTotal > score.whiteTotal) {
winner = 'Black wins by ' + (score.blackTotal - score.whiteTotal) + ' points!';
} else if (score.whiteTotal > score.blackTotal) {
winner = 'White wins by ' + (score.whiteTotal - score.blackTotal) + ' points!';
} else {
winner = 'The game is a tie!';
}
document.getElementById('score-winner').textContent = winner;
document.getElementById('score-display').style.display = 'block';
setMessage('Game over. ' + winner);
document.getElementById('pass-btn').disabled = true;
}
// =============================================
// Board Creation & Rendering
// =============================================
function createBoard() {
boardEl.innerHTML = '';
var totalSize = cellSize * boardSize;
var padding = Math.floor(cellSize / 2);
var wrapperSize = totalSize;
boardWrapper.style.width = wrapperSize + 'px';
boardWrapper.style.height = wrapperSize + 'px';
boardEl.style.gridTemplateColumns = 'repeat(' + boardSize + ', ' + cellSize + 'px)';
boardEl.style.gridTemplateRows = 'repeat(' + boardSize + ', ' + cellSize + 'px)';
var stoneSize = Math.floor(cellSize * STONE_SCALE);
var hoshiSet = {};
var hoshiList = HOSHI_POINTS[boardSize] || [];
for (var h = 0; h < hoshiList.length; h++) {
hoshiSet[hoshiList[h][0] + ',' + hoshiList[h][1]] = true;
}
for (var r = 0; r < boardSize; r++) {
for (var c = 0; c < boardSize; c++) {
var cell = document.createElement('div');
cell.classList.add('cell');
cell.style.width = cellSize + 'px';
cell.style.height = cellSize + 'px';
// Edge classes to clip grid lines
if (r === 0) cell.classList.add('edge-top');
if (r === boardSize - 1) cell.classList.add('edge-bottom');
if (c === 0) cell.classList.add('edge-left');
if (c === boardSize - 1) cell.classList.add('edge-right');
// Hoshi point
if (hoshiSet[r + ',' + c]) {
var dot = document.createElement('div');
dot.classList.add('hoshi');
cell.appendChild(dot);
}
// Hover stone indicator
var hover = document.createElement('div');
hover.classList.add('hover-stone');
hover.style.width = stoneSize + 'px';
hover.style.height = stoneSize + 'px';
cell.appendChild(hover);
cell.dataset.row = r;
cell.dataset.col = c;
(function(row, col) {
cell.addEventListener('click', function() { handleCellClick(row, col); });
})(r, c);
boardEl.appendChild(cell);
}
}
}
function render() {
var stoneSize = Math.floor(cellSize * STONE_SCALE);
var cells = boardEl.querySelectorAll('.cell');
for (var i = 0; i < cells.length; i++) {
var cell = cells[i];
var r = parseInt(cell.dataset.row);
var c = parseInt(cell.dataset.col);
// Remove old stones
var oldStone = cell.querySelector('.stone');
if (oldStone) oldStone.remove();
// Update hover stone color
var hover = cell.querySelector('.hover-stone');
if (hover) {
hover.classList.remove('black', 'white');
if (board[r][c] === EMPTY && !gameOver) {
hover.classList.add(currentPlayer === BLACK ? 'black' : 'white');
}
}
// Place stone if occupied
if (board[r][c] !== EMPTY) {
var stone = document.createElement('div');
stone.classList.add('stone');
stone.classList.add(board[r][c] === BLACK ? 'black' : 'white');
stone.style.width = stoneSize + 'px';
stone.style.height = stoneSize + 'px';
// Last move indicator
if (lastMove && lastMove.r === r && lastMove.c === c) {
stone.classList.add('last-move');
}
cell.appendChild(stone);
// Hide hover on occupied cells
if (hover) hover.style.display = 'none';
} else {
if (hover) hover.style.display = '';
}
}
// Update UI
document.getElementById('prisoners-black').textContent = prisoners[BLACK];
document.getElementById('prisoners-white').textContent = prisoners[WHITE];
updateTurnIndicator();
scheduleAi();
}
function updateTurnIndicator() {
if (gameOver) {
document.getElementById('turn-indicator').textContent = 'Game Over';
document.getElementById('pass-btn').disabled = true;
return;
}
var name = currentPlayer === BLACK ? 'Black' : 'White';
var suffix = isAiTurn() ? ' (AI thinking...)' : "'s Turn";
document.getElementById('turn-indicator').textContent = name + suffix;
document.getElementById('pass-btn').disabled = isAiTurn();
}
// =============================================
// User Interaction
// =============================================
function handleCellClick(r, c) {