From: Dieter Deyke <dieter.deyke@gmail.com>
To: emacs-devel@gnu.org
Subject: [PATCH] elpa/packages/sokoban/sokoban.el
Date: Tue, 12 Feb 2019 13:30:18 +0100 [thread overview]
Message-ID: <87wom59okl.fsf@gmail.com> (raw)
OK to commit this?
===== Commit message: =====
* sokoban/sokoban.el: Add save/load capability
===== Patch: =====
diff --git a/sokoban.el b/sokoban.el
index 57aab47..128d59a 100644
--- a/sokoban.el
+++ b/sokoban.el
@@ -4,7 +4,7 @@
;; Author: Glynn Clements <glynn.clements@xemacs.org>
;; Maintainer: Dieter Deyke <dieter.deyke@gmail.com>
-;; Version: 1.4.7
+;; Version: 1.4.8
;; Comment: While we set lexical-binding, it currently doesn't make use
;; of closures, which is why it can still work in Emacs-23.1.
;; Package-Requires: ((emacs "23.1") (cl-lib "0.5"))
@@ -509,6 +509,8 @@ static char * player_on_target_xpm[] = {
(define-key map "r" 'sokoban-restart-level)
(define-key map "g" 'sokoban-goto-level)
(define-key map "F" 'fit-frame-to-buffer)
+ (define-key map "s" 'sokoban-save)
+ (define-key map "l" 'sokoban-load)
(define-key map [left] 'sokoban-move-left)
(define-key map [right] 'sokoban-move-right)
@@ -868,12 +870,58 @@ static char * player_on_target_xpm[] = {
(setq sokoban-level 0)
(sokoban-next-level))
+(defvar sokoban-grid-state)
+
+(defconst sokoban-state-variables '(
+ sokoban-level
+ sokoban-level-map
+ sokoban-targets
+ sokoban-x
+ sokoban-y
+ sokoban-moves
+ sokoban-pushes
+ sokoban-done
+ sokoban-undo-list
+ sokoban-grid-state
+ ))
+(defun sokoban-save (filename)
+ "Save current Sokoban state."
+ (interactive "FSave file: ")
+ (let ((buf (current-buffer)))
+ (setq sokoban-grid-state nil)
+ (dotimes (y sokoban-height)
+ (dotimes (x sokoban-width)
+ (push (gamegrid-get-cell x y) sokoban-grid-state)))
+ (setq sokoban-grid-state (reverse sokoban-grid-state))
+ (with-temp-file filename
+ (dolist (var sokoban-state-variables)
+ (print
+ (with-current-buffer buf (eval var))
+ (current-buffer))))))
+
+(defun sokoban-load (filename)
+ "Restore saved Sokoban state."
+ (interactive "fLoad file: ")
+ (let ((buf (current-buffer)))
+ (with-temp-buffer
+ (insert-file-contents filename)
+ (goto-char (point-min))
+ (dolist (var sokoban-state-variables)
+ (let ((value (read (current-buffer))))
+ (with-current-buffer buf (set var value))))))
+ (dotimes (y sokoban-height)
+ (dotimes (x sokoban-width)
+ (gamegrid-set-cell x y (pop sokoban-grid-state))))
+ (sokoban-draw-score))
+
(easy-menu-define sokoban-popup-menu nil "Popup menu for Sokoban mode."
'("Sokoban Commands"
["Restart this level" sokoban-restart-level]
["Start new game" sokoban-start-game]
["Go to specific level" sokoban-goto-level]
- ["Fit frame to buffer" fit-frame-to-buffer]))
+ ["Fit frame to buffer" fit-frame-to-buffer]
+ ["Save current state" sokoban-save]
+ ["Restore saved state" sokoban-load]))
(define-key sokoban-mode-map [down-mouse-3] sokoban-popup-menu)
(define-derived-mode sokoban-mode special-mode "Sokoban"
@@ -903,6 +951,8 @@ sokoban-mode keybindings:
\\[sokoban-restart-level] Restarts the current level
\\[sokoban-goto-level] Jumps to a specified level
\\[fit-frame-to-buffer] Fit frame to buffer
+\\[sokoban-save] Save current state
+\\[sokoban-load] Restore saved state
\\[sokoban-move-left] Move one square to the left
\\[sokoban-move-right] Move one square to the right
\\[sokoban-move-up] Move one square up
--
Dieter Deyke
mailto:dieter.deyke@gmail.com
Get my Gnupg key:
gpg --keyserver keys.gnupg.net --recv-keys B116EA20
next reply other threads:[~2019-02-12 12:30 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-02-12 12:30 Dieter Deyke [this message]
-- strict thread matches above, loose matches on Subject: below --
2019-02-07 10:51 [PATCH] elpa/packages/sokoban/sokoban.el Dieter Deyke
2019-02-07 12:53 ` Clément Pit-Claudel
2019-02-07 14:42 ` Eli Zaretskii
2019-02-07 15:57 ` Dieter Deyke
2019-02-07 16:08 ` Clément Pit-Claudel
2019-02-07 16:11 ` Dieter Deyke
2019-02-07 16:55 ` Andreas Schwab
2019-02-08 3:34 ` Stefan Monnier
2019-02-07 14:56 ` Stefan Monnier
2017-07-25 11:36 Dieter Deyke
2017-07-25 14:24 ` Stefan Monnier
2017-07-16 14:14 Dieter Deyke
2017-07-16 16:01 ` Stefan Monnier
2017-07-16 16:57 ` Dieter Deyke
2017-07-09 13:36 Dieter Deyke
2017-07-09 14:15 ` Eli Zaretskii
2017-07-09 14:34 ` Dieter Deyke
2017-07-09 14:52 ` Eli Zaretskii
2017-07-11 15:17 ` Stefan Monnier
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87wom59okl.fsf@gmail.com \
--to=dieter.deyke@gmail.com \
--cc=emacs-devel@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.