]> code.delx.au - gnu-emacs/blobdiff - lisp/play/5x5.el
Update copyright year to 2015
[gnu-emacs] / lisp / play / 5x5.el
index 75f7c2df009ea66de8ba9723d4165ba896d938fb..0258f1e4e4a609e3d7046c5a73a29918277a9d11 100644 (file)
@@ -1,6 +1,6 @@
 ;;; 5x5.el --- simple little puzzle game -*- coding: utf-8 -*-
 
-;; Copyright (C) 1999-201 Free Software Foundation, Inc.
+;; Copyright (C) 1999-2015 Free Software Foundation, Inc.
 
 ;; Author: Dave Pearson <davep@davep.org>
 ;; Maintainer: Dave Pearson <davep@davep.org>
@@ -50,8 +50,7 @@
 
 ;; Things we need.
 
-(eval-when-compile
-  (require 'cl))
+(eval-when-compile (require 'cl-lib))
 
 ;; Customize options.
 
     (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.")
 
 (5x5-defvar-local 5x5-solver-output nil
-  "List that is is the output of artihmetic solver.
+  "List that is the output of an arithmetic solver.
 
 This list L is such that
 
@@ -160,8 +161,8 @@ M is the move count when the solve output was stored.
 S_1 ... S_N are all the solutions ordered from least to greatest
 number of strokes.  S_1 is the solution to be displayed.
 
-Each solution S_1, ..., S_N is a list (STROKE-COUNT GRID) where
-STROKE-COUNT is to number of strokes to achieve the solution and
+Each solution S_1, ..., S_N is a list (STROKE-COUNT GRID) where
+STROKE-COUNT is the number of strokes to achieve the solution and
 GRID is the grid of positions to click.")
 
 
@@ -174,6 +175,9 @@ GRID is the grid of positions to click.")
     ["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]
@@ -181,19 +185,8 @@ GRID is the grid of positions to click.")
 
 ;; Gameplay functions.
 
-(put '5x5-mode 'mode-class 'special)
-
-(defun 5x5-mode ()
-  "A mode for playing `5x5'.
-
-The key bindings for `5x5-mode' are:
-
-\\{5x5-mode-map}"
-  (kill-all-local-variables)
-  (use-local-map 5x5-mode-map)
-  (setq major-mode '5x5-mode
-        mode-name  "5x5")
-  (run-mode-hooks '5x5-mode-hook)
+(define-derived-mode 5x5-mode special-mode "5x5"
+  "A mode for playing `5x5'."
   (setq buffer-read-only t
         truncate-lines   t)
   (buffer-disable-undo))
@@ -207,18 +200,21 @@ squares you must fill the grid.
 
 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 +235,8 @@ Quit current game         \\[5x5-quit-game]"
     (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)))
 
@@ -251,8 +248,8 @@ Quit current game         \\[5x5-quit-game]"
 (defun 5x5-make-new-grid ()
   "Create and return a new `5x5' grid structure."
   (let ((grid (make-vector 5x5-grid-size nil)))
-    (loop for y from 0 to (1- 5x5-grid-size) do
-          (aset grid y (make-vector 5x5-grid-size nil)))
+    (dotimes (y 5x5-grid-size)
+      (aset grid y (make-vector 5x5-grid-size nil)))
     grid))
 
 (defun 5x5-cell (grid y x)
@@ -270,9 +267,9 @@ Quit current game         \\[5x5-quit-game]"
 (defun 5x5-copy-grid (grid)
   "Make a new copy of GRID."
   (let ((copy (5x5-make-new-grid)))
-    (loop for y from 0 to (1- 5x5-grid-size) do
-          (loop for x from 0 to (1- 5x5-grid-size) do
-                (5x5-set-cell copy y x (5x5-cell grid y x))))
+    (dotimes (y 5x5-grid-size)
+      (dotimes (x 5x5-grid-size)
+        (5x5-set-cell copy y x (5x5-cell grid y x))))
     copy))
 
 (defun 5x5-make-move (grid row col)
@@ -290,53 +287,59 @@ Quit current game         \\[5x5-quit-game]"
 
 (defun 5x5-row-value (row)
   "Get the \"on-value\" for grid row ROW."
-  (loop for y from 0 to (1- 5x5-grid-size) sum (if (aref row y) 1 0)))
+  (cl-loop for y from 0 to (1- 5x5-grid-size) sum (if (aref row y) 1 0)))
 
 (defun 5x5-grid-value (grid)
   "Get the \"on-value\" for grid GRID."
-  (loop for y from 0 to (1- 5x5-grid-size) sum (5x5-row-value (aref grid y))))
+  (cl-loop for y from 0 to (1- 5x5-grid-size)
+           sum (5x5-row-value (aref grid y))))
 
 (defun 5x5-draw-grid-end ()
   "Draw the top/bottom of the grid."
   (insert "+")
-  (loop for x from 0 to (1- 5x5-grid-size) do
-        (insert "-" (make-string 5x5-x-scale ?-)))
+  (dotimes (x 5x5-grid-size)
+    (insert "-" (make-string 5x5-x-scale ?-)))
   (insert "-+ "))
 
 (defun 5x5-draw-grid (grids)
   "Draw the grids GRIDS into the current buffer."
   (let ((inhibit-read-only t) grid-org)
     (erase-buffer)
-    (loop for grid in grids do (5x5-draw-grid-end))
+    (dolist (grid grids) (5x5-draw-grid-end))
     (insert "\n")
     (setq grid-org (point))
-    (loop for y from 0 to (1- 5x5-grid-size) do
-          (loop for lines from 0 to (1- 5x5-y-scale) do
-                (loop for grid in grids do
-                      (loop for x from 0 to (1- 5x5-grid-size) do
-                            (insert (if (zerop x) "| " " ")
-                                    (make-string 5x5-x-scale
-                                                 (if (5x5-cell grid y x) ?# ?.))))
-                      (insert " | "))
-                (insert "\n")))
+    (dotimes (y 5x5-grid-size)
+      (dotimes (lines 5x5-y-scale)
+        (dolist (grid grids)
+          (dotimes (x 5x5-grid-size)
+            (insert (if (zerop x) "| " " ")
+                    (make-string 5x5-x-scale
+                                 (if (5x5-cell grid y x) ?# ?.))))
+          (insert " | "))
+        (insert "\n")))
     (when 5x5-solver-output
       (if (= (car 5x5-solver-output) 5x5-moves)
          (save-excursion
            (goto-char grid-org)
            (beginning-of-line (+ 1 (/ 5x5-y-scale 2)))
-           (let ((solution-grid (cdadr 5x5-solver-output)))
-             (dotimes (y  5x5-grid-size)
+           (let ((solution-grid (cl-cdadr 5x5-solver-output)))
+             (dotimes (y 5x5-grid-size)
                (save-excursion
                  (forward-char  (+ 1 (/ (1+ 5x5-x-scale) 2)))
-                 (dotimes (x   5x5-grid-size)
+                 (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)))
-    (loop for grid in grids do (5x5-draw-grid-end))
+    (dolist (grid grids) (5x5-draw-grid-end))
     (insert "\n")
     (insert (format "On: %d  Moves: %d" (5x5-grid-value (car grids)) 5x5-moves))))
 
@@ -348,16 +351,16 @@ Quit current game         \\[5x5-quit-game]"
 
 (defun 5x5-made-move ()
   "Keep track of how many moves have been made."
-  (incf 5x5-moves))
+  (cl-incf 5x5-moves))
 
 (defun 5x5-make-random-grid (&optional move)
   "Make a random grid."
   (setq move (or move (symbol-function '5x5-flip-cell)))
   (let ((grid (5x5-make-new-grid)))
-    (loop for y from 0 to (1- 5x5-grid-size) do
-          (loop for x from 0 to (1- 5x5-grid-size) do
-                (if (zerop (random 2))
-                    (funcall move grid y x))))
+    (dotimes (y 5x5-grid-size)
+      (dotimes (x 5x5-grid-size)
+        (if (zerop (random 2))
+            (funcall move grid y x))))
     grid))
 
 ;; Cracker functions.
@@ -430,20 +433,20 @@ should return a grid vector array that is the new solution."
 (defun 5x5-make-xor-with-mutation (current best)
   "Xor current and best solution then mutate the result."
   (let ((xored (5x5-make-new-grid)))
-    (loop for y from 0 to (1- 5x5-grid-size) do
-          (loop for x from 0 to (1- 5x5-grid-size) do
-                (5x5-set-cell xored y x
-                              (5x5-xor (5x5-cell current y x)
-                                       (5x5-cell best    y x)))))
+    (dotimes (y 5x5-grid-size)
+      (dotimes (x 5x5-grid-size)
+        (5x5-set-cell xored y x
+                      (5x5-xor (5x5-cell current y x)
+                               (5x5-cell best    y x)))))
     (5x5-mutate-solution xored)))
 
 (defun 5x5-mutate-solution (solution)
   "Randomly flip bits in the solution."
-  (loop for y from 0 to (1- 5x5-grid-size) do
-        (loop for x from 0 to (1- 5x5-grid-size) do
-              (if (= (random (/ (* 5x5-grid-size 5x5-grid-size) 2))
-                     (/ (/ (* 5x5-grid-size 5x5-grid-size) 2) 2))
-                  (5x5-flip-cell solution y x))))
+  (dotimes (y 5x5-grid-size)
+    (dotimes (x 5x5-grid-size)
+      (if (= (random (/ (* 5x5-grid-size 5x5-grid-size) 2))
+             (/ (/ (* 5x5-grid-size 5x5-grid-size) 2) 2))
+          (5x5-flip-cell solution y x))))
   solution)
 
 (defun 5x5-play-solution (solution best)
@@ -451,15 +454,15 @@ should return a grid vector array that is the new solution."
 in progress because it is an animated attempt."
   (5x5-new-game)
   (let ((inhibit-quit t))
-    (loop for y from 0 to (1- 5x5-grid-size) do
-          (loop for x from 0 to (1- 5x5-grid-size) do
-                (setq 5x5-y-pos y
-                      5x5-x-pos x)
-                (if (5x5-cell solution y x)
-                    (5x5-flip-current))
-                (5x5-draw-grid (list 5x5-grid solution best))
-                (5x5-position-cursor)
-                (sit-for 5x5-animate-delay))))
+    (dotimes (y 5x5-grid-size)
+      (dotimes (x 5x5-grid-size)
+        (setq 5x5-y-pos y
+              5x5-x-pos x)
+        (if (5x5-cell solution y x)
+            (5x5-flip-current))
+        (5x5-draw-grid (list 5x5-grid solution best))
+        (5x5-position-cursor)
+        (sit-for 5x5-animate-delay))))
   5x5-grid)
 
 ;; Arithmetic solver
@@ -492,6 +495,9 @@ position."
 
 (eval-and-compile
 (if nil; set to t to enable solver logging
+    ;; Note these logging facilities were not cleaned out as the arithmetic
+    ;; solver is not yet complete --- it works only for grid size = 5.
+    ;; So they may be useful again to design a more generic solution.
     (progn
       (defvar 5x5-log-buffer nil)
       (defun 5x5-log-init ()
@@ -516,8 +522,8 @@ easy to log a value with minimal rewrite of code."
            (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)))
 
 (declare-function math-map-vec "calc-vec" (f a))
 (declare-function math-sub "calc" (a b))
@@ -551,18 +557,18 @@ to complete the 5x5.
 
 Solutions are sorted from least to greatest Hamming weight."
   (require 'calc-ext)
-  (flet ((5x5-mat-mode-2
-         (a)
-         (math-map-vec
-          (lambda (y)
-            (math-map-vec
-             (lambda (x) `(mod ,x 2))
-             y))
-          a)))
+  (cl-flet ((5x5-mat-mode-2
+             (a)
+             (math-map-vec
+              (lambda (y)
+                (math-map-vec
+                 (lambda (x) `(mod ,x 2))
+                 y))
+              a)))
     (let* (calc-command-flags
           (grid-size-squared (* 5x5-grid-size 5x5-grid-size))
 
-          ;; targetv is the vector the origine of which is org="current
+          ;; targetv is the vector the origin of which is org="current
           ;; grid" and the end of which is dest="all ones".
           (targetv
            (5x5-log
@@ -632,7 +638,7 @@ Solutions are sorted from least to greatest Hamming weight."
           ;; 23x25 is a diagonal of 1, and the two last columns are a
           ;; base of kernel of transferm.
           ;;
-          ;; base-change must be by construction inversible.
+          ;; base-change must be by construction invertible.
           (base-change
            (5x5-log
             "p"
@@ -641,8 +647,8 @@ Solutions are sorted from least to greatest Hamming weight."
                       (cdr (5x5-mat-mode-2
                             '(vec (vec 0 1 1 1 0 1 0 1 0 1 1 1 0 1
                                        1 1 0 1 0 1 0 1 1 1 0)
-                                  (vec 1 1 0 1 1 0 0 0 0 0 1 1 0 1
-                                       1 0 0 0 0 0 1 1 0 1 1)))))
+                               (vec 1 1 0 1 1 0 0 0 0 0 1 1 0 1
+                                    1 0 0 0 0 0 1 1 0 1 1)))))
               (calcFunc-trn id))))
 
           (inv-base-change
@@ -741,9 +747,9 @@ Solutions are sorted from least to greatest Hamming weight."
                    ;; The Hamming Weight is computed by matrix reduction
                    ;; with an ad-hoc operator.
                    (math-reduce-vec
-                    ;; (cadadr '(vec (mod x 2))) => x
-                    (lambda (r x) (+ (if (integerp r) r (cadadr r))
-                                     (cadadr x)))
+                    ;; (cl-cadadr '(vec (mod x 2))) => x
+                    (lambda (r x) (+ (if (integerp r) r (cl-cadadr r))
+                                     (cl-cadadr x)))
                     solution); car
                    (5x5-vec-to-grid
                     (calcFunc-arrange solution 5x5-grid-size));cdr
@@ -786,6 +792,63 @@ Argument N is ignored."
   (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 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 ()
@@ -804,28 +867,28 @@ Argument N is ignored."
   "Move up."
   (interactive)
   (unless (zerop 5x5-y-pos)
-    (decf 5x5-y-pos)
+    (cl-decf 5x5-y-pos)
     (5x5-position-cursor)))
 
 (defun 5x5-down ()
   "Move down."
   (interactive)
   (unless (= 5x5-y-pos (1- 5x5-grid-size))
-    (incf 5x5-y-pos)
+    (cl-incf 5x5-y-pos)
     (5x5-position-cursor)))
 
 (defun 5x5-left ()
   "Move left."
   (interactive)
   (unless (zerop 5x5-x-pos)
-    (decf 5x5-x-pos)
+    (cl-decf 5x5-x-pos)
     (5x5-position-cursor)))
 
 (defun 5x5-right ()
   "Move right."
   (interactive)
   (unless (= 5x5-x-pos (1- 5x5-grid-size))
-    (incf 5x5-x-pos)
+    (cl-incf 5x5-x-pos)
     (5x5-position-cursor)))
 
 (defun 5x5-bol ()
@@ -861,7 +924,8 @@ Argument N is ignored."
     (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)))
@@ -878,8 +942,6 @@ Argument N is ignored."
       (y-or-n-p prompt)
     t))
 
-(random t)
-
 (provide '5x5)
 
 ;;; 5x5.el ends here