-
Notifications
You must be signed in to change notification settings - Fork 3
/
igo-model.el
1885 lines (1608 loc) · 68.2 KB
/
igo-model.el
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
;;; igo-model.el --- Igo Game Model -*- lexical-binding: t; -*-
;; Copyright (C) 2020 AKIYAMA Kouhei
;; Author: AKIYAMA Kouhei
;; Keywords: games
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; This file contains data structures for representing and processing Go games.
;;
;; - Position & Move
;; - Color & Empty (Intersection State)
;; - Board (W, H, Intersections, Turn, Prisoners, Ko-Position)
;; - Board Changes (Used in undo move and setup node)
;; - Node (Game Tree)
;; - Game (Board, Game Tree, Undo Stack)
;;
;; https://en.wikipedia.org/wiki/Rules_of_Go
;;; Code:
(require 'cl-lib)
(require 'igo-sgf-parser)
(defgroup el-igo nil
"Go Game Editor"
:prefix "igo-"
:group 'games)
;;
;; Position & Move
;;
;; Position
(defconst igo-npos -1) ;;same as nmove
(defun igo-npos-p (pos) (= pos igo-npos))
(defun igo-pos-on-board-p (move-or-pos &optional w h)
(and
(>= move-or-pos 0)
(or (null w) (null h) (< move-or-pos (* w h)))))
(defun igo-xy-to-pos (x y w) (+ x (* y w)))
(defun igo-pos-to-x (pos w) (% pos w))
(defun igo-pos-to-y (pos w) (/ pos w))
;; Move (Placement(Position), Pass, Resign)
(defconst igo-nmove -1) ;;same as npos
(defconst igo-pass -2)
(defconst igo-resign -3)
(defun igo-nmove-p (move) (= move igo-nmove))
(defun igo-pass-p (move) (= move igo-pass))
(defun igo-resign-p (move) (= move igo-resign))
(defun igo-placement-p (move) (igo-pos-on-board-p move))
(defun igo-move-string (move &optional board)
(cond
((igo-pass-p move) "Pass")
((igo-resign-p move) "Resign")
((igo-placement-p move)
(if board
(format "%s %s"
(1+ (igo-board-pos-to-x board move))
(1+ (igo-board-pos-to-y board move)))
(format "%s" move)))
(t "Not Move")))
;;
;; Color & Empty (Intersection State)
;;
(defun igo-empty-p (obj) (eq obj 'empty))
(defun igo-black-p (obj) (eq obj 'black))
(defun igo-white-p (obj) (eq obj 'white))
(defun igo-intersection-state-p (obj)
(or (eq obj 'empty) (eq obj 'black) (eq obj 'white)))
(defun igo-not-intersection-state-p (obj)
(not (igo-intersection-state-p obj)))
(defun igo-same-intersection-state-p (obj1 obj2)
(eq obj1 obj2))
(defun igo-diff-intersection-state-p (obj1 obj2)
(not (eq obj1 obj2)))
(defun igo-color-p (obj) (or (eq obj 'black) (eq obj 'white)))
(defun igo-same-color-p (obj1 obj2) (eq obj1 obj2))
(defun igo-diff-color-p (obj1 obj2) (not (eq obj1 obj2)))
(defun igo-opposite-color (color)
(cond ((eq color 'black) 'white)
((eq color 'white) 'black)
(t color)))
;;
;; Board
;;
(defvar igo-board-default-w 9)
(defvar igo-board-default-h 9)
(defun igo-board (w h &optional intersections turn prisoners ko-pos)
(if (null w) (setq w igo-board-default-w)
(igo-board-validate-w w))
(if (null h) (setq h igo-board-default-h)
(igo-board-validate-h h))
(if (null intersections) (setq intersections (make-vector (* w h) 'empty))
(igo-board-validate-intersections intersections w h))
(if (null turn) (setq turn 'black)
(igo-board-validate-turn turn))
(if (null prisoners) (setq prisoners (cons 0 0))
(igo-board-validate-prisoners prisoners))
(if (null ko-pos) (setq ko-pos igo-npos)
(igo-board-validate-ko-pos ko-pos))
(vector w h intersections turn prisoners ko-pos))
(defconst igo-board--idx-w 0)
(defconst igo-board--idx-h 1)
(defconst igo-board--idx-intersections 2)
(defconst igo-board--idx-turn 3)
(defconst igo-board--idx-prisoners 4)
(defconst igo-board--idx-ko-pos 5)
(defun igo-board-clone (board)
(let ((w (igo-board-w board))
(h (igo-board-h board)))
(igo-board
w h
(copy-sequence (igo-board-intersections board))
(igo-board-turn board)
(cons (igo-board-get-black-prisoners board)
(igo-board-get-white-prisoners board))
(igo-board-ko-pos board))))
(defun igo-board-validate-w (w)
(if (not (and (integerp w) (> w 0) (<= w 52)))
(error "Invalid argument igo-board w=%s" w)))
(defun igo-board-validate-h (h)
(if (not (and (integerp h) (> h 0) (<= h 52)))
(error "Invalid argument igo-board h=%s" h)))
(defun igo-board-validate-intersections (intersections w h)
(if (not (and (vectorp intersections)
(= (length intersections) (* w h))
;; all elements are valid
(null (seq-some #'igo-not-intersection-state-p intersections))))
(error "Invalid argument igo-board intersections=%s" intersections)))
(defun igo-board-validate-turn (turn)
(if (not (igo-color-p turn))
(error "Invalid argument igo-board turn=%s" turn)))
(defun igo-board-validate-prisoners (prisoners)
(if (not (and (consp prisoners)
(integerp (car prisoners))
(>= (car prisoners) 0)
(integerp (cdr prisoners))
(>= (cdr prisoners) 0)))
(error "Invalid argument igo-board prisoners=%s" prisoners)))
(defun igo-board-validate-ko-pos (ko-pos)
(if (not (and (integerp ko-pos)
(or (igo-pos-on-board-p ko-pos) (igo-npos-p ko-pos))))
(error "Invalid argument igo-board ko-pos=%s" ko-pos)))
;; Board - Basic Accessors
(defun igo-board-w (board) (aref board igo-board--idx-w))
(defun igo-board-h (board) (aref board igo-board--idx-h))
(defun igo-board-intersections (board) (aref board igo-board--idx-intersections))
(defun igo-board-turn (board) (aref board igo-board--idx-turn))
(defun igo-board-prisoners (board) (aref board igo-board--idx-prisoners))
(defun igo-board-ko-pos (board) (aref board igo-board--idx-ko-pos))
;; Board - Intersections
(defun igo-board-intersection-count (board) (* (igo-board-w board)
(igo-board-h board)))
(defun igo-board-get-at (board pos)
(aref (igo-board-intersections board) pos))
(defun igo-board-set-at (board pos istate)
(aset (igo-board-intersections board) pos istate))
(defun igo-board-empty-p-at (board pos)
(and (not (igo-npos-p pos)) (igo-empty-p (igo-board-get-at board pos))))
(defun igo-board-remove-stone (board pos)
(igo-board-set-at board pos 'empty))
;; Board - Position(Index of Intersection)
(defun igo-board-xy-to-pos (board x y)
(igo-xy-to-pos x y (igo-board-w board)))
(defun igo-board-pos-to-x (board pos)
(igo-pos-to-x pos (igo-board-w board)))
(defun igo-board-pos-to-y (board pos)
(igo-pos-to-y pos (igo-board-w board)))
(defun igo-board-pos-on-board-p (board pos)
(and (integerp pos)
(igo-pos-on-board-p pos (igo-board-w board) (igo-board-h board))))
(defun igo-board-left-of (board pos)
(if (or (igo-npos-p pos) (= (% pos (igo-board-w board)) 0))
igo-npos
(1- pos)))
(defun igo-board-right-of (board pos)
(if (or (igo-npos-p pos) (= (% (1+ pos) (igo-board-w board)) 0))
igo-npos
(1+ pos)))
(defun igo-board-above (board pos)
(if (or (igo-npos-p pos) (< pos (igo-board-w board)))
igo-npos
(- pos (igo-board-w board))))
(defun igo-board-below (board pos)
(let ((w (igo-board-w board))
(h (igo-board-h board)))
(if (or (igo-npos-p pos) (>= pos (* w (1- h))))
igo-npos
(+ pos w))))
;; Board - Turn
(defun igo-board-set-turn (board color)
(aset board igo-board--idx-turn color))
;; Board - Prisoners
(defun igo-board-add-prisoners (board color num-prisoners)
(let ((cell (igo-board-prisoners board)))
(cond
((igo-black-p color) (setcar cell (+ (car cell) num-prisoners)))
((igo-white-p color) (setcdr cell (+ (cdr cell) num-prisoners))))))
(defun igo-board-remove-prisoners (board color num-prisoners)
(igo-board-add-prisoners board color (- num-prisoners)))
(defun igo-board-get-prisoners (board color)
(cond
((igo-black-p color) (car (igo-board-prisoners board)))
((igo-white-p color) (cdr (igo-board-prisoners board)))))
(defun igo-board-get-black-prisoners (board)
(car (igo-board-prisoners board)))
(defun igo-board-get-white-prisoners (board)
(cdr (igo-board-prisoners board)))
;; Board - Move
(defun igo-board-pass (board &optional color allow-illegal-move)
(if (null color) (setq color (igo-board-turn board)))
;; Check COLOR's pass is legal. If the pass is illegal, return nil
(when (or allow-illegal-move
(igo-same-color-p color (igo-board-turn board)))
;; legal
(let ((ko-pos-old (igo-board-ko-pos board))
(turn-old (igo-board-turn board)))
(igo-board-set-ko-pos board igo-npos)
(igo-board-set-turn board (igo-opposite-color color)) ;;not opposite turn, if allow illegal moves
;; return undo data
(igo-board-changes nil nil nil ko-pos-old turn-old nil nil))))
(defun igo-board-put-stone (board pos &optional color allow-illegal-move)
(if (null color) (setq color (igo-board-turn board)))
;; Check the move(pos, color) is legal. If the move is illegal, return nil
(when (or allow-illegal-move
(igo-board-legal-move-p board pos color))
;; Illegal move behavior is based on the SGF specification:
;; SGF FF[5] - Move vs. Adding Stones
;; https://www.red-bean.com/sgf/ff5/m_vs_ax.htm
(let* (;; Put COLOR's stone at POS
(istate-old (igo-board-get-at board pos))
(_istate-new (igo-board-set-at board pos color))
;; Remove captured stones
(removed-stones
(append
(igo-board-remove-string-if-surrounded board (igo-board-left-of board pos) color)
(igo-board-remove-string-if-surrounded board (igo-board-right-of board pos) color)
(igo-board-remove-string-if-surrounded board (igo-board-above board pos) color)
(igo-board-remove-string-if-surrounded board (igo-board-below board pos) color)))
;; Remove suicide stones
(suicide-stones
(if allow-illegal-move
(igo-board-remove-string-if-surrounded board pos (igo-opposite-color color))))
;; Change ko-pos
(ko-pos-old (igo-board-ko-pos board))
(ko-pos-new (igo-board-get-new-ko-pos board pos color removed-stones))
;; Rotate turn
(turn-old (igo-board-turn board))
(turn-new (igo-opposite-color color)) ;;not opposite turn, if allow illegal moves
)
(igo-board-add-prisoners board color (length suicide-stones))
(igo-board-add-prisoners board (igo-opposite-color color) (length removed-stones))
(igo-board-set-ko-pos board ko-pos-new)
(igo-board-set-turn board turn-new)
;; return undo data
(igo-board-changes-make-move-undo color pos istate-old removed-stones suicide-stones ko-pos-old turn-old))))
(defun igo-board-legal-move-p (board pos color)
(and
board
(igo-color-p color) ;; valid color
(igo-board-pos-on-board-p board pos) ;; valid position
(igo-same-color-p color (igo-board-turn board)) ;; color's turn
(igo-board-empty-p-at board pos) ;; there is not already stone
(not (igo-board-suicide-move-p board pos color)) ;; not suicide move
(not (igo-board-ko-move-p board pos)) ;; pos is not ko
))
(defun igo-board-suicide-move-p (board pos color)
;; pre-conditions
;; - valid pos
;; - valid color
;; - pos is empty
(if (or (igo-board-empty-p-at board (igo-board-left-of board pos))
(igo-board-empty-p-at board (igo-board-right-of board pos))
(igo-board-empty-p-at board (igo-board-above board pos))
(igo-board-empty-p-at board (igo-board-below board pos)))
nil
;; there are no empty point
(let ((old-istate (igo-board-get-at board pos)))
;; put stone temporary
(igo-board-set-at board pos color)
(let ((suicide (and (igo-board-string-surrounded-p board pos)
(not (or (igo-board-string-surrounded-and-diff-color-p board (igo-board-left-of board pos) color)
(igo-board-string-surrounded-and-diff-color-p board (igo-board-right-of board pos) color)
(igo-board-string-surrounded-and-diff-color-p board (igo-board-above board pos) color)
(igo-board-string-surrounded-and-diff-color-p board (igo-board-below board pos) color))))))
;; remove stone temporary puted
(igo-board-set-at board pos old-istate)
suicide))))
(defun igo-board-ko-move-p (board pos)
(= pos (igo-board-ko-pos board)))
(defun igo-board-string-surrounded-p (board pos)
"Return t if stones specified by pos are surrounded by any colors."
(not (igo-board-find-liberty board pos)))
(defun igo-board-string-surrounded-and-diff-color-p (board pos color)
(if (igo-board-pos-on-board-p board pos)
(let ((pos-istate (igo-board-get-at board pos)))
(and
(not (igo-empty-p pos-istate)) ;; not empty
(igo-diff-color-p pos-istate color) ;; different color
(igo-board-string-surrounded-p board pos)))))
(defun igo-board-find-liberty (board pos)
(if (igo-board-pos-on-board-p board pos)
(let ((pos-istate (igo-board-get-at board pos)))
(or (igo-empty-p pos-istate)
(igo-board-find-liberty-recursive board pos pos-istate (make-vector (igo-board-intersection-count board) nil))))))
(defun igo-board-find-liberty-recursive (board pos color visited-arr)
(if (or (igo-npos-p pos) ;; out of board
(aref visited-arr pos)) ;; already visited
nil
(aset visited-arr pos t)
(let ((istate (igo-board-get-at board pos)))
(if (igo-empty-p istate)
t
(if (not (igo-same-color-p istate color))
nil
(or (igo-board-find-liberty-recursive board (igo-board-left-of board pos) color visited-arr)
(igo-board-find-liberty-recursive board (igo-board-right-of board pos) color visited-arr)
(igo-board-find-liberty-recursive board (igo-board-above board pos) color visited-arr)
(igo-board-find-liberty-recursive board (igo-board-below board pos) color visited-arr)))))))
(defun igo-board-remove-string-if-surrounded (board pos turn)
(if (igo-npos-p pos)
nil
(let ((istate (igo-board-get-at board pos)))
(if (or (igo-empty-p istate) ;; empty
(igo-same-color-p istate turn) ;; this turn's color
(not (igo-board-string-surrounded-p board pos))) ;;not surrounded
nil
(igo-board-remove-string board pos istate)))))
(defun igo-board-remove-string (board pos color)
(if (or (igo-npos-p pos) ;; out of board
(not (igo-same-color-p (igo-board-get-at board pos) color))) ;; color changed
nil
(igo-board-remove-stone board pos)
(append
(list pos)
(igo-board-remove-string board (igo-board-left-of board pos) color)
(igo-board-remove-string board (igo-board-right-of board pos) color)
(igo-board-remove-string board (igo-board-above board pos) color)
(igo-board-remove-string board (igo-board-below board pos) color))))
;; Board - ko-pos
(defun igo-board-set-ko-pos (board ko-pos)
(aset board igo-board--idx-ko-pos ko-pos))
(defun igo-board-get-new-ko-pos (board pos color removed-stones)
(if (or (igo-npos-p pos)
(igo-empty-p color)
(not (= (length removed-stones) 1))
(not (igo-same-color-p (igo-board-get-at board pos) color)))
igo-npos
(let ((num-empty-or-same-color 0)
(pos-empty-or-same-color igo-npos))
(dolist (neighbor-pos (list (igo-board-left-of board pos)
(igo-board-right-of board pos)
(igo-board-above board pos)
(igo-board-below board pos)))
(if (not (igo-npos-p neighbor-pos))
(let ((neighbor-istate (igo-board-get-at board neighbor-pos)))
(when (or (igo-empty-p neighbor-istate) (igo-same-color-p neighbor-istate color))
(setq num-empty-or-same-color (1+ num-empty-or-same-color))
(setq pos-empty-or-same-color neighbor-pos)))))
(if (= num-empty-or-same-color 1)
pos-empty-or-same-color
igo-npos))))
;;
;; Board Changes
;;
(defun igo-board-changes (black white empty ko-pos turn black-prisoners white-prisoners)
(vector (list black white empty) ko-pos turn black-prisoners white-prisoners))
;; Board Changes - Basic Accessors
(defun igo-board-changes-pos-list-for-each-istate (changes) (aref changes 0))
(defun igo-board-changes-ko-pos (changes) (if changes (aref changes 1)))
(defun igo-board-changes-turn (changes) (if changes (aref changes 2)))
(defun igo-board-changes-black-prisoners (changes) (if changes (aref changes 3)))
(defun igo-board-changes-white-prisoners (changes) (if changes (aref changes 4)))
(defun igo-board-changes-turn-set (changes turn)
(if changes
(aset changes 2 turn)))
;; Board Changes - Predicate
(defun igo-board-changes-empty-p (changes)
(and
(null (igo-board-changes-black changes))
(null (igo-board-changes-white changes))
(null (igo-board-changes-empty changes))
(null (igo-board-changes-ko-pos changes))
(null (igo-board-changes-turn changes))
(null (igo-board-changes-black-prisoners changes))
(null (igo-board-changes-white-prisoners changes))))
;; Board Changes - Apply Changes
(defun igo-board-changes-apply (changes board)
(when (and changes board)
;; Intersection States(Black Stones, White Stones, Empty Intersections)
(igo-board-changes-apply-intersections
(igo-board-changes-black changes) 'black board)
(igo-board-changes-apply-intersections
(igo-board-changes-white changes) 'white board)
(igo-board-changes-apply-intersections
(igo-board-changes-empty changes) 'empty board)
(let ((ko-pos (igo-board-changes-ko-pos changes))
(turn (igo-board-changes-turn changes))
(black-prisoners (igo-board-changes-black-prisoners changes))
(white-prisoners (igo-board-changes-white-prisoners changes)))
(if (integerp ko-pos) (igo-board-set-ko-pos board ko-pos))
(if (igo-color-p turn) (igo-board-set-turn board turn))
(if (integerp black-prisoners) (igo-board-add-prisoners board 'black black-prisoners))
(if (integerp white-prisoners) (igo-board-add-prisoners board 'white white-prisoners))
)))
(defun igo-board-changes-apply-intersections (positions new-istate board)
;; ensure list
(if (integerp positions)
(setq positions (list positions)))
(if (and board
(igo-intersection-state-p new-istate)
(listp positions))
(dolist (pos positions)
(if (and (integerp pos) (igo-board-pos-on-board-p board pos))
(igo-board-set-at board pos new-istate)))))
;; Board Changes - List of Intersection Positions
(defun igo-board-changes-black (changes)
(nth 0 (igo-board-changes-pos-list-for-each-istate changes)))
(defun igo-board-changes-white (changes)
(nth 1 (igo-board-changes-pos-list-for-each-istate changes)))
(defun igo-board-changes-empty (changes)
(nth 2 (igo-board-changes-pos-list-for-each-istate changes)))
(defun igo-board-changes-istate-index (istate)
(cond
((igo-black-p istate) 0)
((igo-white-p istate) 1)
((igo-empty-p istate) 2)))
(defun igo-board-changes-istate-list ()
'(black white empty))
(defun igo-board-changes-pos-list-cell (changes istate)
(nthcdr (igo-board-changes-istate-index istate)
(igo-board-changes-pos-list-for-each-istate changes)))
(defun igo-board-changes-pos-list (changes istate)
(car (igo-board-changes-pos-list-cell changes istate)))
;; Board Changes - Intersection State
(defun igo--find-previous-of (pred elements)
"Return the cell before the first element in ELEMENTS where (PRED
element) is non-nil."
(let (prev-cell)
(while (and elements (not (funcall pred (car elements))))
(setq prev-cell elements)
(setq elements (cdr elements)))
prev-cell))
(defun igo-board-changes-get-at (changes pos)
"Return the changed state(\\='black, \\='white, \\='empty) of the
intersection specified by POS."
(when changes
(cond
((member pos (igo-board-changes-black changes)) 'black)
((member pos (igo-board-changes-white changes)) 'white)
((member pos (igo-board-changes-empty changes)) 'empty)
(t nil))))
(defun igo-board-changes-delete-at (changes pos)
(if changes
;; Remove POS in position list for each istate
(let ((pos-list (igo-board-changes-pos-list-for-each-istate changes)))
(while pos-list
(setcar pos-list (delete pos (car pos-list)))
(setq pos-list (cdr pos-list))))))
(defun igo-board-changes-set-at (changes pos istate)
(when changes
(igo-board-changes-delete-at changes pos)
;; Insert in ascending order by POS
(let* ((cell (igo-board-changes-pos-list-cell changes istate))
(insertion-point (igo--find-previous-of
(lambda (elem) (> elem pos))
(car cell))))
(if insertion-point
(setcdr insertion-point (cons pos (cdr insertion-point)))
(setcar cell (cons pos (car cell)))))))
;; TEST: igo-board-changes-set-at
;; (let ((c (igo-board-changes '(1 3 5) '(2 4 6) '(8 10 12) nil nil nil nil)))
;; (igo-board-changes-set-at c 0 'white)
;; c)
;; => [((1 3 5) (0 2 4 6) (8 10 12)) nil nil nil nil]
;; BoardChange - Creation
(defun igo-board-changes-diff-board (to-board from-board)
"Return a igo-board-changes that change from FROM-BOARD to TO-BOARD."
(let ((pos-lists (igo-board-changes--diff-intersections to-board from-board)))
(igo-board-changes
(cdr (assq 'black pos-lists))
(cdr (assq 'white pos-lists))
(cdr (assq 'empty pos-lists))
(igo-board-changes--diff-value
to-board from-board 'igo-board-ko-pos '=)
(igo-board-changes--diff-value
to-board from-board 'igo-board-turn 'igo-same-color-p)
(igo-board-changes--diff-integer-delta
to-board from-board 'igo-board-get-black-prisoners)
(igo-board-changes--diff-integer-delta
to-board from-board 'igo-board-get-white-prisoners))))
(defun igo-board-changes--diff-intersections (to-board from-board)
(if (or (/= (igo-board-w to-board) (igo-board-w from-board))
(/= (igo-board-h to-board) (igo-board-h from-board)))
(error "Board sizes not match"))
(let ((pos-lists (list (cons 'black nil)
(cons 'white nil)
(cons 'empty nil)))
(num-intersections (igo-board-intersection-count to-board)))
(dotimes (pos num-intersections)
(let ((to-istate (igo-board-get-at to-board pos))
(from-istate (igo-board-get-at from-board pos)))
(if (not (igo-same-intersection-state-p to-istate from-istate))
(let ((pos-list (assq to-istate pos-lists)))
(push pos (cdr pos-list))))))
(dolist (cell pos-lists)
(setcdr cell (nreverse (cdr cell))))
pos-lists))
(defun igo-board-changes--diff-value (to-board from-board getter fun-equal)
(let ((to-value (funcall getter to-board))
(from-value (funcall getter from-board)))
(if (not (funcall fun-equal to-value from-value)) to-value)))
(defun igo-board-changes--diff-integer-delta (to-board from-board getter)
(let ((to-value (funcall getter to-board))
(from-value (funcall getter from-board)))
(if (/= to-value from-value)
(- to-value from-value))))
(defun igo-board-changes-make-initial-board (board)
"Return a igo-board-changes that create BOARD from empty board."
(igo-board-changes-diff-board
board
(igo-board (igo-board-w board) (igo-board-h board))))
(defun igo-board-changes-make-move-undo
(color pos istate-old removed-stones suicide-stones ko-pos-old turn-old)
"Return a igo-board-changes that revert move."
(let* (;; Calculate number of prisoners increased
(num-removed-stones (length removed-stones))
(num-suicide-stones (length suicide-stones))
(num-black-prisoners
(if (igo-black-p color) num-suicide-stones num-removed-stones))
(num-white-prisoners
(if (igo-white-p color) num-suicide-stones num-removed-stones))
;; Guess current istate at POS
(istate-new (if (member pos suicide-stones) 'empty color))
;; Remove POS from SUICIDE-STONES
(suicide-stones-without-pos (remove pos suicide-stones))
;; Make pos-list
(black-pos-list
(if (igo-black-p color) suicide-stones-without-pos removed-stones))
(white-pos-list
(if (igo-white-p color) suicide-stones-without-pos removed-stones))
(empty-pos-list nil))
;; Restore istate of POS
(if (not (igo-same-intersection-state-p istate-new istate-old))
(cond
((igo-black-p istate-old) (push pos black-pos-list))
((igo-white-p istate-old) (push pos white-pos-list))
((igo-empty-p istate-old) (push pos empty-pos-list))))
;;@todo sort pos-list?
(igo-board-changes
black-pos-list
white-pos-list
empty-pos-list
ko-pos-old
turn-old ;; Not necessarily the same as COLOR if allow illegal moves
(if (> num-black-prisoners 0) (- num-black-prisoners))
(if (> num-white-prisoners 0) (- num-white-prisoners)))))
;; TEST
;; (igo-board-changes-make-changes-undo
;; (igo-board-changes '(1 2 3) nil nil nil 'white 2 0)
;; (let ((board (igo-board 9 9))) (igo-board-put-stone board 2 'black) (igo-board-put-stone board 3 'white) board))
;; => [(nil (3) (1)) nil black -2 nil]
(defun igo-board-changes-make-changes-undo (changes board)
"Return a igo-board-change that revert CHANGES."
(let ((to-board (igo-board-clone board)))
(igo-board-changes-apply changes to-board)
(igo-board-changes-diff-board board to-board)))
;;
;; Node
;;
(defun igo-node (prev &optional move color)
(vector
prev ;;0:prev
(or move igo-nmove) ;;1:move
color ;;2:color
nil ;;3:properties
nil ;;4:next nodes
nil ;;5:last-visited
))
(defconst igo-node--idx-prev 0)
(defconst igo-node--idx-move 1)
(defconst igo-node--idx-color 2)
(defconst igo-node--idx-properties 3)
(defconst igo-node--idx-next-nodes 4)
(defconst igo-node--idx-last-visited 5)
(defun igo-node-prev (node) (aref node igo-node--idx-prev))
(defun igo-node-move (node) (aref node igo-node--idx-move))
(defun igo-node-color (node) (aref node igo-node--idx-color))
(defun igo-node-properties (node) (aref node igo-node--idx-properties))
(defun igo-node-next-nodes (node) (aref node igo-node--idx-next-nodes))
(defun igo-node-last-visited (node) (aref node igo-node--idx-last-visited))
(defun igo-node-clone-new-frame (old-node new-prev)
(list
nil ;;0:new-next-nodes
old-node ;;1:old-node
(igo-node new-prev
(igo-node-move old-node)
(igo-node-color old-node));;2:new-node
(igo-node-next-nodes old-node) ;;3:old-next-nodes
))
(defun igo-node-clone (top-node &optional top-new-prev)
(let ((stack (list (igo-node-clone-new-frame top-node top-new-prev)))
result)
(while stack
(let* ((frame (car stack))
(new-next-nodes (nth 0 frame))
(old-node (nth 1 frame))
(new-node (nth 2 frame)))
(cl-symbol-macrolet ((old-next-nodes (nth 3 frame)))
(if old-next-nodes
;; Clone sub-nodes
(push (igo-node-clone-new-frame (pop old-next-nodes) new-node) stack)
;; Set subnodes
(aset new-node igo-node--idx-next-nodes
(nreverse new-next-nodes))
;; Clone last-visited
(when-let ((index (seq-position (igo-node-next-nodes old-node)
(igo-node-last-visited old-node) #'eq)))
(aset new-node igo-node--idx-last-visited
(nth index (igo-node-next-nodes new-node))))
;; Clone properties
(aset new-node igo-node--idx-properties
(igo-node-clone-properties (igo-node-properties old-node)))
;; Return a new node
(pop stack)
(if stack
(push new-node (nth 0 (car stack))) ;;callee's new-next-nodes
(setq result new-node))))))
result))
;; 再帰版 (max-lisp-eval-depth制限に引っかかる)
;; (defun igo-node-clone (node &optional new-prev)
;; (let ((new-node (igo-node new-prev
;; (igo-node-move node)
;; (igo-node-color node))))
;; ;; clone properties
;; (aset new-node igo-node--idx-properties
;; (igo-node-clone-properties (igo-node-properties node)))
;; ;; clone next-nodes
;; (aset new-node igo-node--idx-next-nodes
;; (igo-node-clone-next-nodes (igo-node-next-nodes node) new-node))
;; ;; clone last-visited
;; (if-let ((index (seq-position (igo-node-next-nodes node)
;; (igo-node-last-visited node) #'eq)))
;; (aset new-node igo-node--idx-last-visited
;; (nth index (igo-node-next-nodes new-node))))
;; new-node))
;;
;; (defun igo-node-clone-next-nodes (next-nodes new-prev)
;; (mapcar (lambda (next)
;; (igo-node-clone next new-prev))
;; next-nodes))
(defun igo-node-clone-properties (props)
;; Copy properties
;; - ATOM (symbol, string, number)
;; - list
;; - vector
;; - igo-board-changes (vector)
(copy-tree props))
;; Node - Node Type(Setup or Move(Resign, Pass, Placement))
(defun igo-node-set-move-and-color (node move color)
(aset node igo-node--idx-move move)
(aset node igo-node--idx-color color))
(defun igo-node-setup-p (node) (igo-nmove-p (igo-node-move node)))
(defun igo-node-move-p (node) (not (igo-nmove-p (igo-node-move node))))
(defun igo-node-pass-p (node) (igo-pass-p (igo-node-move node)))
(defun igo-node-resign-p (node) (igo-resign-p (igo-node-move node)))
(defun igo-node-placement-p (node) (igo-placement-p (igo-node-move node)))
(defun igo-node-second-consecutive-pass-p (node)
(and
(igo-node-pass-p node)
(let ((prev-node (igo-node-previous-move node)))
(and prev-node
(not (igo-same-color-p (igo-node-color node)
(igo-node-color prev-node)))
(igo-node-pass-p prev-node)))))
;; Node - Make Root
(defun igo-node-make-root (node board)
(when (not (igo-node-root-p node))
(let ((old-root-node (igo-node-get-root node)))
;; Copy game info properties from old-root
;; (if igo-sgf-parser loaded)
(if (boundp 'igo-sgf-game-info-properties)
(dolist (prop igo-sgf-game-info-properties)
(let* ((prop-id (igo-sgf-game-info-prop-id prop))
(value (igo-node-get-sgf-property old-root-node prop-id)))
(if value
(igo-node-set-sgf-property node prop-id value)))))
;; Set move number property
(if (null (igo-node-get-move-number-property node))
(igo-node-set-move-number-property
node
(igo-node-move-number node)))
;; Unlink previous node
(aset node igo-node--idx-prev nil)
;; Clear move (indicate this node is a setup node)
(igo-node-set-move-and-color node igo-nmove nil)
;; Set setup property to reproduce BOARD (discard old setup if it exists)
(igo-node-set-setup-property
node
(igo-board-changes-make-initial-board board))
))
node)
;; Node - Previous Node(Parent)
(defun igo-node-root-p (node)
(null (igo-node-prev node)))
(defun igo-node-get-root (node)
(let (prev)
(while (setq prev (igo-node-prev node))
(setq node prev))
node))
(defun igo-node-previous-move (node)
"Return previous move of NODE. Skip setup nodes."
(setq node (igo-node-prev node))
(while (and node (not (igo-node-move-p node)))
(setq node (igo-node-prev node)))
node)
(defun igo-node-number-of-siblings (node)
"Return number of sibling nodes (including NODE itself)."
(let ((prev (igo-node-prev node)))
(if prev (length (igo-node-next-nodes prev))
1)))
(defun igo-node-first-sibling-p (node)
(let ((prev (igo-node-prev node)))
(if prev (eq (car (igo-node-next-nodes prev)) node))))
(defun igo-node-last-sibling-p (node)
(let ((prev (igo-node-prev node)))
(if prev (eq (car (last (igo-node-next-nodes prev))) node))))
(defun igo-node-move-number (node)
"Return the depth of NODE from the root node or previous MN
property, not include setup node."
(let ((num 0) mn-prop)
(while (and node (null (setq mn-prop (igo-node-get-move-number-property node))))
(if (igo-node-move-p node)
(setq num (1+ num)))
(setq node (igo-node-prev node)))
(+ (or mn-prop 0) num)))
(defun igo-node-depth (node)
"Return the depth of NODE from the root node, including setup node."
(let ((num 0))
(while node
(setq num (1+ num))
(setq node (igo-node-prev node)))
num))
(defun igo-node-next-color (node)
(let (result)
(while (and
node
(null
(setq result
;; opposite color or PL property
(if (igo-node-move-p node)
(igo-opposite-color (igo-node-color node))
(if-let ((changes (igo-node-get-setup-property node)))
(igo-board-changes-turn changes))))))
(setq node (igo-node-prev node)))
(or result 'black)))
(defun igo-node-path-from-root (node &optional fork-only)
(let (dirs prev)
(while (and node (setq prev (igo-node-prev node)))
(if (or (not fork-only) (>= (length (igo-node-next-nodes prev)) 2))
(push (seq-position (igo-node-next-nodes prev) node) dirs))
(setq node prev))
dirs))
(defun igo-node-find-move-back (node move)
(while (and node (not (= (igo-node-move node) move)))
(setq node (igo-node-prev node)))
node)
(defun igo-node-move-number-at (node pos)
(igo-node-move-number (igo-node-find-move-back node pos)))
(defun igo-node-prev-nth (node n &optional clamp-p)
(let (next)
(while (and (> n 0) node)
(setq next node)
(setq node (igo-node-prev node))
(setq n (1- n)))
(if (and clamp-p (null node)) next node)))
(defun igo-node-ancestor-p (node ancestor)
(igo-node-descendant-p ancestor node))
;; Node - Next Nodes(Children)
(defun igo-node-set-last-visited (node next-node)
;; check next-node is in next-nodes
(if (or (null next-node)
(seq-find (lambda (n) (eq n next-node)) (igo-node-next-nodes node)))
(aset node igo-node--idx-last-visited next-node)))
(defun igo-node-next-node-default (node)
(if node
(or
;; last visited node
(igo-node-last-visited node)
;; no branch
(let ((next-nodes (igo-node-next-nodes node)))
(if (= (length next-nodes) 1)
(car next-nodes))))))
(defun igo-node-find-next-by-move (node move color)
"Return the node that matches MOVE and COLOR from the next nodes."
(seq-find (lambda (next) (and (= (igo-node-move next) move)
(or (null color)
(igo-same-color-p (igo-node-color next)
color))))
(igo-node-next-nodes node)))
(defun igo-node-get-next-setup-node (node index)
(let ((next-nodes (igo-node-next-nodes node))
result)
(while (and next-nodes (null result))
(if (igo-node-setup-p (car next-nodes))
(if (= index 0)
(setq result (car next-nodes))
(setq index (1- index))))
(setq next-nodes (cdr next-nodes)))
result))
(defun igo-node-add-next-node (node next-node)
(aset node igo-node--idx-next-nodes
(nconc (igo-node-next-nodes node)
(list next-node)))
next-node)
(defun igo-node-create-next-node (node &optional move color)
(if (null move) (setq move igo-nmove))
;; Create a new node, add to NODE, return new node.
(igo-node-add-next-node node (igo-node node move color)))
(defun igo-node-delete-next (node target-node)
;; remove TARGET-NODE from next-nodes
(aset node igo-node--idx-next-nodes
(delq target-node (igo-node-next-nodes node)))
;; ensure last-visited does not point to NODE
(if (eq target-node (igo-node-last-visited node))
(igo-node-set-last-visited node (car (igo-node-next-nodes node)))))
(defun igo-node-find-by-path (node dirs &optional fork-only)
(while (and node dirs)
(let* ((next-nodes (igo-node-next-nodes node))
(len-next-nodes (length next-nodes))
(dir (if (or (not fork-only) (>= len-next-nodes 2))
(pop dirs)
0)))
(if (and (>= dir 0) (< dir len-next-nodes))
(setq node (nth dir next-nodes))
(setq dirs nil)
(setq node nil))))
node)
(defun igo-node-find-by-queries (node queries board-w board-h)
(while queries
(let ((q (car queries)))
(cond
;; integer : nth node(choose first variation)
((integerp q)
(setq node (igo-node-nth node q t)))
((stringp q)
(cond
;; "xx" : find point breadth-first