[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] 33/255: removing dead pieces after moves
From: |
Eric Schulte |
Subject: |
[elpa] 33/255: removing dead pieces after moves |
Date: |
Sun, 16 Mar 2014 01:02:13 +0000 |
eschulte pushed a commit to branch go
in repository elpa.
commit 603f60464f982f0fa97fdbe7dd50a21aafbbb04b
Author: Eric Schulte <address@hidden>
Date: Thu May 17 10:41:30 2012 -0400
removing dead pieces after moves
---
sgf.el | 65 +++++++++++++++++++++++++++++++++++++++++++--------------------
1 files changed, 44 insertions(+), 21 deletions(-)
diff --git a/sgf.el b/sgf.el
index f93ab7d..3ab0575 100644
--- a/sgf.el
+++ b/sgf.el
@@ -387,13 +387,10 @@
(length *board*)))
(clear-labels *board*)
(apply-moves *board* (sgf-ref *sgf* *index*))
- (remove-dead *board*)
(push (cons :pieces (board-to-pieces *board*))
(sgf-ref *sgf* *index*))))
(update-display)))
-(sgf-ref *game* '(0))
-
;;; Board manipulation functions
(defun move-type (move)
@@ -412,8 +409,13 @@
(t nil)))))
(dolist (move moves board)
(case (move-type move)
- (:move (set (car move) (cdr move)))
- (:label (mapcar (lambda (data) (set (car move) data)) (cdr move)))))))
+ (:move
+ (set (car move) (cdr move))
+ (let ((color (if (string= "B" (car move)) :b :w)))
+ (remove-dead *board* (other-color color))
+ (remove-dead *board* color)))
+ (:label
+ (mapcar (lambda (data) (set (car move) data)) (cdr move)))))))
(defun clear-labels (board)
(dotimes (point (length board))
@@ -421,23 +423,37 @@
(unless (member (aref board point) '(:b :w))
(setf (aref board point) nil)))))
-(defun any (func seq)
- (reduce ))
-
(defun neighbors (board piece)
- )
-
-(defun alive-p (board piece)
- (let ((val (aref board piece))
- (neighbors (neighbors board piece)))
- (or (any #'null neighbors)
- (any #'alive-p (remove-if-not (lambda (n) (equal (aref board piece)
val))
- neighbors)))))
-
-(defun remove-dead (board)
- (dotimes (n (length board) board)
- (let ((val (aref board n)))
- (when (and val )))))
+ (let ((size (board-size board))
+ neighbors)
+ (when (not (= (mod piece size) (1- size))) (push (1+ piece) neighbors))
+ (when (not (= (mod piece size) 0)) (push (1- piece) neighbors))
+ (when (< (+ piece size) (length board)) (push (+ piece size) neighbors))
+ (when (> (- piece size) 0) (push (- piece size) neighbors))
+ neighbors))
+
+(defun alive-p (board piece &optional already)
+ (let* ((val (aref board piece))
+ (enemy (other-color val))
+ (neighbors (remove-if (lambda (n) (member n already))
+ (neighbors board piece)))
+ (neighbor-vals (mapcar (lambda (n) (aref board n)) neighbors))
+ (friendly-neighbors (delete nil (map 'list (lambda (n v)
+ (when (equal v val) n))
+ neighbors neighbor-vals)))
+ (already (cons piece (append friendly-neighbors already))))
+ (or (any neighbor-vals ; touching open space
+ (lambda (v) (not (equal v enemy))))
+ (any friendly-neighbors ; touching alive dragon
+ (lambda (n) (alive-p board n already))))))
+
+(defun remove-dead (board color)
+ ;; must remove one color at a time for ko situations
+ (let (cull)
+ (dotimes (n (length board) board)
+ (when (and (equal (aref board n) color) (not (alive-p board n)))
+ (push n cull)))
+ (dolist (n cull cull) (setf (aref board n) nil))))
;;; Display mode
@@ -584,3 +600,10 @@
(right 4)
(should (= points-length (length (assoc :points (sgf-ref joseki
'(0)))))))
(should (kill-buffer buffer))))
+
+(ert-deftest sgf-neighbors ()
+ (let ((board (make-board 19)))
+ (should (= 2 (length (neighbors board 0))))
+ (should (= 2 (length (neighbors board (length board)))))
+ (should (= 4 (length (neighbors board (/ (length board) 2)))))
+ (should (= 3 (length (neighbors board 1))))))
- [elpa] 19/255: moving games/ -> sgf-files/, (continued)
- [elpa] 19/255: moving games/ -> sgf-files/, Eric Schulte, 2014/03/15
- [elpa] 10/255: helper functions, Eric Schulte, 2014/03/15
- [elpa] 24/255: right and left implemented but buggy, Eric Schulte, 2014/03/15
- [elpa] 23/255: more complex but more intuitive and useful `range', Eric Schulte, 2014/03/15
- [elpa] 21/255: display sgf file as a go board in a buffer, Eric Schulte, 2014/03/15
- [elpa] 25/255: right and left are mainly working, Eric Schulte, 2014/03/15
- [elpa] 29/255: q now quits a sgf display buffer, Eric Schulte, 2014/03/15
- [elpa] 28/255: adding a major mode for board navigation keys, Eric Schulte, 2014/03/15
- [elpa] 27/255: implementation notes, Eric Schulte, 2014/03/15
- [elpa] 31/255: a test to protect against unwanted state changes, Eric Schulte, 2014/03/15
- [elpa] 33/255: removing dead pieces after moves,
Eric Schulte <=
- [elpa] 35/255: compiled and caught some minor issues, Eric Schulte, 2014/03/15
- [elpa] 32/255: consolidating utility functions, Eric Schulte, 2014/03/15
- [elpa] 30/255: saving the board configuration with each sgf step, Eric Schulte, 2014/03/15
- [elpa] 18/255: applying moves to a board, Eric Schulte, 2014/03/15
- [elpa] 37/255: remove debug printf, Eric Schulte, 2014/03/15
- [elpa] 34/255: beginning to stub out tests for dead stone removal, Eric Schulte, 2014/03/15
- [elpa] 38/255: more capture tests, failing multistone captures, Eric Schulte, 2014/03/15
- [elpa] 36/255: passing all tests (at least it did this once), Eric Schulte, 2014/03/15
- [elpa] 26/255: improvements, considering labels and moves, Eric Schulte, 2014/03/15
- [elpa] 40/255: parsing empty properties, Eric Schulte, 2014/03/15