[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] 21/255: display sgf file as a go board in a buffer
From: |
Eric Schulte |
Subject: |
[elpa] 21/255: display sgf file as a go board in a buffer |
Date: |
Sun, 16 Mar 2014 01:02:11 +0000 |
eschulte pushed a commit to branch go
in repository elpa.
commit 47e5b733f8ac3f2d5d5999df67fdc46c9a4422cb
Author: Eric Schulte <address@hidden>
Date: Tue May 15 19:28:30 2012 -0400
display sgf file as a go board in a buffer
---
sgf.el | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 71 insertions(+), 0 deletions(-)
diff --git a/sgf.el b/sgf.el
index 5a8c207..ed6d0fe 100644
--- a/sgf.el
+++ b/sgf.el
@@ -194,6 +194,16 @@
;; - keep an index into the sgf file
;; - write functions for building boards from sgf files (forwards and
backwards)
;; - sgf movement keys
+
+(defvar *board* nil "Holds the board local to a GO buffer.")
+(make-variable-buffer-local '*board*)
+
+(defvar *sgf* nil "Holds the sgf data structure local to a GO buffer.")
+(make-variable-buffer-local '*sgf*)
+
+(defvar *index* nil "Index into the sgf data structure local to a GO buffer.")
+(make-variable-buffer-local '*index*)
+
(defun make-board (size) (make-vector (* size size) nil))
(defun board-size (board) (round (sqrt (length board))))
@@ -245,6 +255,54 @@
(body (board-body-to-string board)))
(mapconcat #'identity (list header body header) "\n")))
+(defun update-display ()
+ (delete-region (point-min) (point-max))
+ (goto-char (point-min))
+ (insert
+ "\n"
+ (board-to-string *board*)
+ "\n\n")
+ (let ((comment (cdr (assoc "C" (sgf-ref *sgf* *index*)))))
+ (when comment
+ (insert (make-string (+ 6 (* 2 (board-size *board*))) ?=)
+ "\n\n")
+ (let ((beg (point)))
+ (insert *comment*)
+ (fill-region beg (point)))))
+ (goto-char (point-min)))
+
+(defun display-sgf (game)
+ (let* ((root (car game))
+ (name (format "*%s*"
+ (or (second (assoc "GN" root))
+ (second (assoc "EV" root))
+ "GO")))
+ (buffer (get-buffer-create name)))
+ (with-current-buffer buffer
+ (setf *sgf* game)
+ (setf *board* (make-board (cdr (assoc "S" root))))
+ (setf *index* '(1))
+ (update-display))
+ (pop-to-buffer buffer)))
+
+(defun sgf-ref (sgf index)
+ (let ((part sgf))
+ (while (car index)
+ (setq part (nth (car index) part))
+ (setq index (cdr index)))
+ part))
+
+(defun up ())
+(defun down ())
+(defun left ())
+(defun right (&optional num)
+ (interactive "p")
+ (incf (car (last *index*)) num)
+ (unless (sgf-ref *sgf* *index*)
+ (error "sgf: no more forward moves."))
+ (apply-move *board* (sgf-ref *sgf* *index*))
+ (update-display))
+
;;; Board manipulation functions
(defun apply-move (board move)
@@ -254,6 +312,10 @@
(t (error "sgf: invalid move %s" (car move)))))
board)
+(defun revert-move (board move)
+ (setf (aref board (pos-to-index (cdr move) (board-size board))) nil)
+ board)
+
;;; Tests
(require 'ert)
@@ -367,3 +429,12 @@
(apply-move board (car moves)))
(board-to-string board)
(should t)))
+
+(ert-deftest sgf-display-fresh-sgf-buffer ()
+ (let* ((joseki (car (read-from-file "sgf-files/3-4-joseki.sgf")))
+ (buffer (display-sgf joseki)))
+ (with-current-buffer buffer
+ (should *board*)
+ (should *sgf*)
+ (should *index*))
+ (should (kill-buffer buffer))))
- [elpa] 15/255: board display, (continued)
- [elpa] 15/255: board display, Eric Schulte, 2014/03/15
- [elpa] 22/255: example sgf file generated using gcoban, Eric Schulte, 2014/03/15
- [elpa] 17/255: not using "I" as an index, Eric Schulte, 2014/03/15
- [elpa] 20/255: alright "i" *is* a valid position identifier, Eric Schulte, 2014/03/15
- [elpa] 16/255: cleaned up board printing, Eric Schulte, 2014/03/15
- [elpa] 09/255: parsing more complex trees, Eric Schulte, 2014/03/15
- [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 <=
- [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, 2014/03/15
- [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