* 5x5 again
@ 2011-05-24 5:08 Vincent Belaïche
2011-05-24 18:23 ` Stefan Monnier
0 siblings, 1 reply; 8+ messages in thread
From: Vincent Belaïche @ 2011-05-24 5:08 UTC (permalink / raw)
To: emacs-devel, Stefan Monnier, Jay P Belanger; +Cc: Vincent Belaïche
[-- Attachment #1: Type: text/plain, Size: 884 bytes --]
Hello,
Sorry to dwell on it. But I just realized that my contribution does not
work correctly when the code is compiled. I also corrected by the way a
bug --- the solution display was not cleaned out along with the other
game variables in case that a new game is stated.
Here follows the Changelog and the patch. Sorry for the extra
disturbance.
Vincent.
2011-05-24 Vincent Belaïche <vincentb1@users.sourceforge.net>
* play/5x5.el (5x5-new-game, 5x5-randomize): reset
5x5-solver-output to nil when a new grid is cast.
(5x5-log-init, 5x5-log): use defsubst instead of defmacro to shunt
these debugging traces, as defmacro breaks the compiled code. Note
these logging facilities were not cleaned out as the aritmetic
solver is not yet complete --- it works only for grid size = 5. So
they may be useful again to design a more generic solution.
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 5x5.el.diff --]
[-- Type: text/x-patch, Size: 1239 bytes --]
=== modified file 'lisp/play/5x5.el'
--- lisp/play/5x5.el 2011-05-23 14:46:41 +0000
+++ lisp/play/5x5.el 2011-05-24 04:53:57 +0000
@@ -239,7 +239,8 @@
(setq 5x5-x-pos (/ 5x5-grid-size 2)
5x5-y-pos (/ 5x5-grid-size 2)
5x5-moves 0
- 5x5-grid (5x5-make-move (5x5-make-new-grid) 5x5-y-pos 5x5-x-pos))
+ 5x5-grid (5x5-make-move (5x5-make-new-grid) 5x5-y-pos 5x5-x-pos)
+ 5x5-solver-output nil)
(5x5-draw-grid (list 5x5-grid))
(5x5-position-cursor)))
@@ -515,8 +516,8 @@
(with-current-buffer 5x5-log-buffer
(insert name ?= value-to-log ?\n))))
value))
- (defmacro 5x5-log-init ())
- (defmacro 5x5-log (name value) value))
+ (defsubst 5x5-log-init ())
+ (defsubst 5x5-log (name value) value))
(defun 5x5-solver (grid)
"Return a list of solutions for GRID.
@@ -843,7 +844,8 @@
(setq 5x5-x-pos (/ 5x5-grid-size 2)
5x5-y-pos (/ 5x5-grid-size 2)
5x5-moves 0
- 5x5-grid (5x5-make-random-grid (symbol-function '5x5-make-move)))
+ 5x5-grid (5x5-make-random-grid (symbol-function '5x5-make-move))
+ 5x5-solver-output nil)
(unless 5x5-cracking
(5x5-draw-grid (list 5x5-grid)))
(5x5-position-cursor)))
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: 5x5 again
2011-05-24 5:08 5x5 again Vincent Belaïche
@ 2011-05-24 18:23 ` Stefan Monnier
2011-05-25 6:13 ` Vincent Belaïche
0 siblings, 1 reply; 8+ messages in thread
From: Stefan Monnier @ 2011-05-24 18:23 UTC (permalink / raw)
To: Vincent Belaïche; +Cc: Jay P Belanger, emacs-devel
> Here follows the Changelog and the patch. Sorry for the extra
> disturbance.
Installed, with the following nitpicks:
> * play/5x5.el (5x5-new-game, 5x5-randomize): reset
> 5x5-solver-output to nil when a new grid is cast.
"reset" needs to be capitalized.
> (5x5-log-init, 5x5-log): use defsubst instead of defmacro to shunt
"use" needs to be capitalized.
> these debugging traces, as defmacro breaks the compiled code. Note
Two spaces after ".".
> these logging facilities were not cleaned out as the aritmetic
> solver is not yet complete --- it works only for grid size = 5. So
> they may be useful again to design a more generic solution.
I moved this into the code, where it belongs.
Stefan
^ permalink raw reply [flat|nested] 8+ messages in thread
* RE: 5x5 again
2011-05-24 18:23 ` Stefan Monnier
@ 2011-05-25 6:13 ` Vincent Belaïche
2011-05-28 1:17 ` Stefan Monnier
0 siblings, 1 reply; 8+ messages in thread
From: Vincent Belaïche @ 2011-05-25 6:13 UTC (permalink / raw)
To: monnier; +Cc: Jay Belanger, emacs-devel
[-- Attachment #1: Type: text/plain, Size: 1447 bytes --]
Ooops...
I noticed another thing: in the docstring of 5x5, there the keymap is listed, but I omitted to add that line:
Solve with Calc \\[5x5-solve-suggest]
I can prepare one more patch & Changelog --- with well capitalized sentences --- if you like. If I do this, I would also add a function to browse through the other solutions found by the solver --- only the one with least Hamming weight being shown.
Vincent.
> From: monnier@iro.umontreal.ca
> To: vincent.b.1@hotmail.fr
> CC: emacs-devel@gnu.org; jay.p.belanger@gmail.com
> Subject: Re: 5x5 again
> Date: Tue, 24 May 2011 15:23:59 -0300
>
> > Here follows the Changelog and the patch. Sorry for the extra
> > disturbance.
>
> Installed, with the following nitpicks:
>
> > * play/5x5.el (5x5-new-game, 5x5-randomize): reset
> > 5x5-solver-output to nil when a new grid is cast.
>
> "reset" needs to be capitalized.
>
> > (5x5-log-init, 5x5-log): use defsubst instead of defmacro to shunt
>
> "use" needs to be capitalized.
>
> > these debugging traces, as defmacro breaks the compiled code. Note
>
> Two spaces after ".".
>
> > these logging facilities were not cleaned out as the aritmetic
> > solver is not yet complete --- it works only for grid size = 5. So
> > they may be useful again to design a more generic solution.
>
> I moved this into the code, where it belongs.
>
>
> Stefan
[-- Attachment #2: Type: text/html, Size: 1915 bytes --]
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: 5x5 again
2011-05-25 6:13 ` Vincent Belaïche
@ 2011-05-28 1:17 ` Stefan Monnier
0 siblings, 0 replies; 8+ messages in thread
From: Stefan Monnier @ 2011-05-28 1:17 UTC (permalink / raw)
To: Vincent Belaïche; +Cc: Jay Belanger, emacs-devel
> Ooops...
> I noticed another thing: in the docstring of 5x5, there the keymap is
> listed, but I omitted to add that line:
> Solve with Calc \\[5x5-solve-suggest]
> I can prepare one more patch & Changelog --- with well capitalized
> sentences --- if you like.
That wouldn't be necessary, thanks.
> If I do this, I would also add a function to browse through the other
> solutions found by the solver --- only the one with least Hamming
> weight being shown.
Then please do and include the above fix in this new patch.
Stefan
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: 5x5 again
@ 2011-05-28 6:05 Vincent Belaïche
2011-05-30 13:40 ` Stefan Monnier
0 siblings, 1 reply; 8+ messages in thread
From: Vincent Belaïche @ 2011-05-28 6:05 UTC (permalink / raw)
To: emacs-devel, Stefan Monnier; +Cc: Jay P Belanger, Vincent Belaïche
[-- Attachment #1: Type: text/plain, Size: 1192 bytes --]
[...]
>> If I do this, I would also add a function to browse through the other
>> solutions found by the solver --- only the one with least Hamming
>> weight being shown.
>
>Then please do and include the above fix in this new patch.
>
>
> Stefan
>
Salut Stéfan,
Here is the ChangeLog:
-----------------------------------------------------------------------
2011-05-28 Vincent Belaïche <vincentb1@users.sourceforge.net>
* play/5x5.el (5x5-solve-rotate-left, 5x5-solve-rotate-right): New
functions.
(5x5-mode-map): Add keys for 5x5-solve-rotate-left &
5x5-solve-rotate-right.
(5x5-mode-menu): Add menu entries for 5x5-solve-rotate-left &
5x5-solve-rotate-right and make solver a separate section.
(5x5): Update docstring for showing keymap for 5x5-solve-suggest,
5x5-solve-rotate-left & 5x5-solve-rotate-right.
(5x5-draw-grid): Use "()" instead of "O" to display solution when
the x scale is even, as this is better looking.
-----------------------------------------------------------------------
And here is the patch (it seems that my change of 2011-05-24 has not
been applied, so this patch is relative to the previous one):
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 5x5.el.diff --]
[-- Type: text/x-patch, Size: 5591 bytes --]
=== modified file 'lisp/play/5x5.el'
--- lisp/play/5x5.el 2011-05-23 14:46:41 +0000
+++ lisp/play/5x5.el 2011-05-28 05:59:59 +0000
@@ -144,6 +144,8 @@
(define-key map [(control c) (control x)] #'5x5-crack-xor-mutate)
(define-key map "n" #'5x5-new-game)
(define-key map "s" #'5x5-solve-suggest)
+ (define-key map "<" #'5x5-solve-rotate-left)
+ (define-key map ">" #'5x5-solve-rotate-right)
(define-key map "q" #'5x5-quit-game)
map)
"Local keymap for the 5x5 game.")
@@ -174,6 +176,9 @@
["Quit game" 5x5-quit-game t]
"---"
["Use Calc solver" 5x5-solve-suggest t]
+ ["Rotate left list of Calc solutions" 5x5-solve-rotate-left t]
+ ["Rotate right list of Calc solutions" 5x5-solve-rotate-right t]
+ "---"
["Crack randomly" 5x5-crack-randomly t]
["Crack mutating current" 5x5-crack-mutating-current t]
["Crack mutating best" 5x5-crack-mutating-best t]
@@ -207,18 +212,21 @@
5x5 keyboard bindings are:
\\<5x5-mode-map>
-Flip \\[5x5-flip-current]
-Move up \\[5x5-up]
-Move down \\[5x5-down]
-Move left \\[5x5-left]
-Move right \\[5x5-right]
-Start new game \\[5x5-new-game]
-New game with random grid \\[5x5-randomize]
-Random cracker \\[5x5-crack-randomly]
-Mutate current cracker \\[5x5-crack-mutating-current]
-Mutate best cracker \\[5x5-crack-mutating-best]
-Mutate xor cracker \\[5x5-crack-xor-mutate]
-Quit current game \\[5x5-quit-game]"
+Flip \\[5x5-flip-current]
+Move up \\[5x5-up]
+Move down \\[5x5-down]
+Move left \\[5x5-left]
+Move right \\[5x5-right]
+Start new game \\[5x5-new-game]
+New game with random grid \\[5x5-randomize]
+Random cracker \\[5x5-crack-randomly]
+Mutate current cracker \\[5x5-crack-mutating-current]
+Mutate best cracker \\[5x5-crack-mutating-best]
+Mutate xor cracker \\[5x5-crack-xor-mutate]
+Solve with Calc \\[5x5-solve-suggest]
+Rotate left Calc Solutions \\[5x5-solve-rotate-left]
+Rotate right Calc Solutions \\[5x5-solve-rotate-right]
+Quit current game \\[5x5-quit-game]"
(interactive "P")
(setq 5x5-cracking nil)
@@ -239,7 +247,8 @@
(setq 5x5-x-pos (/ 5x5-grid-size 2)
5x5-y-pos (/ 5x5-grid-size 2)
5x5-moves 0
- 5x5-grid (5x5-make-move (5x5-make-new-grid) 5x5-y-pos 5x5-x-pos))
+ 5x5-grid (5x5-make-move (5x5-make-new-grid) 5x5-y-pos 5x5-x-pos)
+ 5x5-solver-output nil)
(5x5-draw-grid (list 5x5-grid))
(5x5-position-cursor)))
@@ -330,9 +339,14 @@
(forward-char (+ 1 (/ (1+ 5x5-x-scale) 2)))
(dotimes (x 5x5-grid-size)
(when (5x5-cell solution-grid y x)
+ (if (= 0 (mod 5x5-x-scale 2))
+ (progn
+ (insert "()")
+ (delete-region (point) (+ (point) 2))
+ (backward-char 2))
(insert-char ?O 1)
(delete-char 1)
- (backward-char))
+ (backward-char)))
(forward-char (1+ 5x5-x-scale))))
(forward-line 5x5-y-scale))))
(setq 5x5-solver-output nil)))
@@ -515,8 +529,8 @@
(with-current-buffer 5x5-log-buffer
(insert name ?= value-to-log ?\n))))
value))
- (defmacro 5x5-log-init ())
- (defmacro 5x5-log (name value) value))
+ (defsubst 5x5-log-init ())
+ (defsubst 5x5-log (name value) value))
(defun 5x5-solver (grid)
"Return a list of solutions for GRID.
@@ -768,6 +782,49 @@
(5x5-draw-grid (list 5x5-grid))
(5x5-position-cursor))
+(defun 5x5-solve-rotate-left (&optional n)
+ "Rotate left by N the list of solutions in 5x5-solver-output.
+If N is not supplied, rotate by 1."
+ (interactive "P")
+ (let ((len (length 5x5-solver-output)))
+ (when (>= len 3)
+ (setq n (if (integerp n) n 1)
+ n (mod n (1- len)))
+ (unless (eq n 0)
+ (setq n (- len n 1))
+ (let* ((p-tail (last 5x5-solver-output (1+ n)))
+ (tail (cdr p-tail))
+ (l-tail (last tail)))
+ ;;
+ ;; For n = 2:
+ ;;
+ ;; +--+--+ +--+--+ +--+--+ +--+--+ +--+--+
+ ;; |M | ---->|S1| ---->|S2| ---->|S3| ---->|S4| ----> nil
+ ;; +--+--+ +--+--+ +--+--+ +--+--+ +--+--+
+ ;; ^ ^ ^ ^
+ ;; | | | |
+ ;; + 5x5-solver-output | | + l-tail
+ ;; + p-tail |
+ ;; + tail
+ ;;
+ (setcdr l-tail (cdr 5x5-solver-output))
+ (setcdr 5x5-solver-output tail)
+ (unless (eq p-tail 5x5-solver-output)
+ (setcdr p-tail nil)))
+ (5x5-draw-grid (list 5x5-grid))
+ (5x5-position-cursor)))))
+
+(defun 5x5-solve-rotate-right (&optional n)
+ "Rotate right by N the list of solutions in 5x5-solver-output.
+If N is not supplied, rotate by 1."
+ (interactive "P")
+ (setq n
+ (if (integerp n) (- n)
+ -1))
+ (5x5-solve-rotate-left n))
+
+
+
;; Keyboard response functions.
(defun 5x5-flip-current ()
@@ -843,7 +900,8 @@
(setq 5x5-x-pos (/ 5x5-grid-size 2)
5x5-y-pos (/ 5x5-grid-size 2)
5x5-moves 0
- 5x5-grid (5x5-make-random-grid (symbol-function '5x5-make-move)))
+ 5x5-grid (5x5-make-random-grid (symbol-function '5x5-make-move))
+ 5x5-solver-output nil)
(unless 5x5-cracking
(5x5-draw-grid (list 5x5-grid)))
(5x5-position-cursor)))
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: 5x5 again
2011-05-28 6:05 Vincent Belaïche
@ 2011-05-30 13:40 ` Stefan Monnier
0 siblings, 0 replies; 8+ messages in thread
From: Stefan Monnier @ 2011-05-30 13:40 UTC (permalink / raw)
To: Vincent Belaïche; +Cc: Jay P Belanger, emacs-devel
> And here is the patch (it seems that my change of 2011-05-24 has not
> been applied, so this patch is relative to the previous one):
Hmm.. it looks to me like it was applied.
Can you try to re-make the patch again?
And while we're at it, could you add some explanation to
5x5-solve-rotate-left/right's docstring about what they do? I (speaking
as a naive 5x5 user) have no idea what it means to rotate a solution in
this context.
Stefan
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: 5x5 again
@ 2011-05-31 19:52 Vincent Belaïche
2011-06-21 3:16 ` Stefan Monnier
0 siblings, 1 reply; 8+ messages in thread
From: Vincent Belaïche @ 2011-05-31 19:52 UTC (permalink / raw)
To: Stefan Monnier, emacs-devel, Jay P Belanger; +Cc: Vincent Belaïche
[-- Attachment #1: Type: text/plain, Size: 201 bytes --]
Hello,
Sorry for my bzr-naivety. So, here is an updated patch relative to the
last revision.
I have inflated the scanty docstring with additional and hopefully
sufficient explanations.
Vincent.
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 5x5.el.diff --]
[-- Type: text/x-patch, Size: 5213 bytes --]
=== modified file 'lisp/play/5x5.el'
--- lisp/play/5x5.el 2011-05-24 18:22:09 +0000
+++ lisp/play/5x5.el 2011-05-31 19:40:18 +0000
@@ -144,6 +144,8 @@
(define-key map [(control c) (control x)] #'5x5-crack-xor-mutate)
(define-key map "n" #'5x5-new-game)
(define-key map "s" #'5x5-solve-suggest)
+ (define-key map "<" #'5x5-solve-rotate-left)
+ (define-key map ">" #'5x5-solve-rotate-right)
(define-key map "q" #'5x5-quit-game)
map)
"Local keymap for the 5x5 game.")
@@ -174,6 +176,9 @@
["Quit game" 5x5-quit-game t]
"---"
["Use Calc solver" 5x5-solve-suggest t]
+ ["Rotate left list of Calc solutions" 5x5-solve-rotate-left t]
+ ["Rotate right list of Calc solutions" 5x5-solve-rotate-right t]
+ "---"
["Crack randomly" 5x5-crack-randomly t]
["Crack mutating current" 5x5-crack-mutating-current t]
["Crack mutating best" 5x5-crack-mutating-best t]
@@ -207,18 +212,21 @@
5x5 keyboard bindings are:
\\<5x5-mode-map>
-Flip \\[5x5-flip-current]
-Move up \\[5x5-up]
-Move down \\[5x5-down]
-Move left \\[5x5-left]
-Move right \\[5x5-right]
-Start new game \\[5x5-new-game]
-New game with random grid \\[5x5-randomize]
-Random cracker \\[5x5-crack-randomly]
-Mutate current cracker \\[5x5-crack-mutating-current]
-Mutate best cracker \\[5x5-crack-mutating-best]
-Mutate xor cracker \\[5x5-crack-xor-mutate]
-Quit current game \\[5x5-quit-game]"
+Flip \\[5x5-flip-current]
+Move up \\[5x5-up]
+Move down \\[5x5-down]
+Move left \\[5x5-left]
+Move right \\[5x5-right]
+Start new game \\[5x5-new-game]
+New game with random grid \\[5x5-randomize]
+Random cracker \\[5x5-crack-randomly]
+Mutate current cracker \\[5x5-crack-mutating-current]
+Mutate best cracker \\[5x5-crack-mutating-best]
+Mutate xor cracker \\[5x5-crack-xor-mutate]
+Solve with Calc \\[5x5-solve-suggest]
+Rotate left Calc Solutions \\[5x5-solve-rotate-left]
+Rotate right Calc Solutions \\[5x5-solve-rotate-right]
+Quit current game \\[5x5-quit-game]"
(interactive "P")
(setq 5x5-cracking nil)
@@ -331,9 +339,14 @@
(forward-char (+ 1 (/ (1+ 5x5-x-scale) 2)))
(dotimes (x 5x5-grid-size)
(when (5x5-cell solution-grid y x)
+ (if (= 0 (mod 5x5-x-scale 2))
+ (progn
+ (insert "()")
+ (delete-region (point) (+ (point) 2))
+ (backward-char 2))
(insert-char ?O 1)
(delete-char 1)
- (backward-char))
+ (backward-char)))
(forward-char (1+ 5x5-x-scale))))
(forward-line 5x5-y-scale))))
(setq 5x5-solver-output nil)))
@@ -790,6 +803,64 @@
(5x5-draw-grid (list 5x5-grid))
(5x5-position-cursor))
+(defun 5x5-solve-rotate-left (&optional n)
+ "Rotate left by N the list of solutions in 5x5-solver-output.
+
+If N is not supplied rotate by 1, that is to say put the last
+element first in the list.
+
+The 5x5 game has in general several solutions. For grid size=5,
+there are 4 possible solutions. When function
+`5x5-solve-suggest' (press `\\[5x5-solve-suggest]') is called the
+solution that is presented is the one that needs least number of
+strokes --- other solutions can be viewed by rotating through the
+list. The list of solution is ordered by number of strokes, so
+rotating left just after calling `5x5-solve-suggest' will show
+the the solution with second least number of strokes, while
+rotating right will show the solution with greatest number of
+strokes."
+ (interactive "P")
+ (let ((len (length 5x5-solver-output)))
+ (when (>= len 3)
+ (setq n (if (integerp n) n 1)
+ n (mod n (1- len)))
+ (unless (eq n 0)
+ (setq n (- len n 1))
+ (let* ((p-tail (last 5x5-solver-output (1+ n)))
+ (tail (cdr p-tail))
+ (l-tail (last tail)))
+ ;;
+ ;; For n = 2:
+ ;;
+ ;; +--+--+ +--+--+ +--+--+ +--+--+ +--+--+
+ ;; |M | ---->|S1| ---->|S2| ---->|S3| ---->|S4| ----> nil
+ ;; +--+--+ +--+--+ +--+--+ +--+--+ +--+--+
+ ;; ^ ^ ^ ^
+ ;; | | | |
+ ;; + 5x5-solver-output | | + l-tail
+ ;; + p-tail |
+ ;; + tail
+ ;;
+ (setcdr l-tail (cdr 5x5-solver-output))
+ (setcdr 5x5-solver-output tail)
+ (unless (eq p-tail 5x5-solver-output)
+ (setcdr p-tail nil)))
+ (5x5-draw-grid (list 5x5-grid))
+ (5x5-position-cursor)))))
+
+(defun 5x5-solve-rotate-right (&optional n)
+ "Rotate right by N the list of solutions in 5x5-solver-output.
+If N is not supplied, rotate by 1. Similar to function
+`5x5-solve-rotate-left' except that rotation is right instead of
+lest."
+ (interactive "P")
+ (setq n
+ (if (integerp n) (- n)
+ -1))
+ (5x5-solve-rotate-left n))
+
+
+
;; Keyboard response functions.
(defun 5x5-flip-current ()
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: 5x5 again
2011-05-31 19:52 Vincent Belaïche
@ 2011-06-21 3:16 ` Stefan Monnier
0 siblings, 0 replies; 8+ messages in thread
From: Stefan Monnier @ 2011-06-21 3:16 UTC (permalink / raw)
To: Vincent Belaïche; +Cc: Jay P Belanger, emacs-devel
> I have inflated the scanty docstring with additional and hopefully
> sufficient explanations.
Thanks. Installed. In the future, please don't forget to include
a ChangeLog entry.
Stefan
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2011-06-21 3:16 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-05-24 5:08 5x5 again Vincent Belaïche
2011-05-24 18:23 ` Stefan Monnier
2011-05-25 6:13 ` Vincent Belaïche
2011-05-28 1:17 ` Stefan Monnier
-- strict thread matches above, loose matches on Subject: below --
2011-05-28 6:05 Vincent Belaïche
2011-05-30 13:40 ` Stefan Monnier
2011-05-31 19:52 Vincent Belaïche
2011-06-21 3:16 ` Stefan Monnier
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).