Switch license to GPLv3 or later.
[bpt/emacs.git] / lisp / play / 5x5.el
index 394c6a0..bea52cb 100644 (file)
@@ -1,18 +1,18 @@
-;;; 5x5.el -- Simple little puzzle game.
+;;; 5x5.el --- simple little puzzle game
 
-;; Copyright (C) 1999 Free Software Foundationm, Inc.
+;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
+;;   2005, 2006, 2007 Free Software Foundation, Inc.
 
-;; Author: Dave Pearson <davep@hagbard.demon.co.uk>
-;; Maintainer: Dave Pearson <davep@hagbard.demon.co.uk>
+;; Author: Dave Pearson <davep@davep.org>
+;; Maintainer: Dave Pearson <davep@davep.org>
 ;; Created: 1998-10-03
-;; Version: $Revision: 1.15 $
 ;; Keywords: games puzzles
 
 ;; This file is part of GNU Emacs.
 
 ;; GNU Emacs is free software; you can redistribute it and/or modify
 ;; it under the terms of the GNU General Public License as published by
-;; the Free Software Foundation; either version 2, or (at your option)
+;; the Free Software Foundation; either version 3, or (at your option)
 ;; any later version.
 
 ;; GNU Emacs is distributed in the hope that it will be useful,
@@ -22,8 +22,8 @@
 
 ;; You should have received a copy of the GNU General Public License
 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
-;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-;; Boston, MA 02111-1307, USA.
+;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+;; Boston, MA 02110-1301, USA.
 
 ;;; Commentary:
 
@@ -35,7 +35,7 @@
 ;; o The code for updating the grid needs to be re-done. At the moment it
 ;;   simply re-draws the grid every time a move is made.
 ;;
-;; o Look into starting up the display with colour. gamegrid.el looks
+;; o Look into tarting up the display with colour. gamegrid.el looks
 ;;   interesting, perhaps that is the way to go?
 
 ;;; Thanks:
@@ -46,6 +46,8 @@
 ;; Pascal Q. Porcupine <joshagam@cs.nmsu.edu> for inspiring the animated
 ;; solver.
 
+;;; Code:
+
 ;; Things we need.
 
 (eval-when-compile
     (define-key map [right]                   #'5x5-right)
     (define-key map [(control a)]             #'5x5-bol)
     (define-key map [(control e)]             #'5x5-eol)
+    (define-key map [(control p)]             #'5x5-up)
+    (define-key map [(control n)]             #'5x5-down)
+    (define-key map [(control b)]             #'5x5-left)
+    (define-key map [(control f)]             #'5x5-right)
     (define-key map [home]                    #'5x5-bol)
     (define-key map [end]                     #'5x5-eol)
     (define-key map [prior]                   #'5x5-first)
     (define-key map [(control c) (control r)] #'5x5-crack-randomly)
     (define-key map [(control c) (control c)] #'5x5-crack-mutating-current)
     (define-key map [(control c) (control b)] #'5x5-crack-mutating-best)
-    (define-key map [(control c) (control x)] #'5x5-crack-xor-mutate)  
+    (define-key map [(control c) (control x)] #'5x5-crack-xor-mutate)
     (define-key map "n"                       #'5x5-new-game)
     (define-key map "q"                       #'5x5-quit-game)
     (setq 5x5-mode-map map)))
 (put '5x5-mode 'mode-class 'special)
 
 (defun 5x5-mode ()
-  "A mode for playing `5x5'
+  "A mode for playing `5x5'.
 
 The key bindings for 5x5-mode are:
 
@@ -173,10 +179,10 @@ The key bindings for 5x5-mode are:
   (use-local-map 5x5-mode-map)
   (setq major-mode '5x5-mode
         mode-name  "5x5")
-  (run-hooks '5x5-mode-hook)  
+  (run-mode-hooks '5x5-mode-hook)
   (setq buffer-read-only t
         truncate-lines   t)
-  (buffer-disable-undo (current-buffer)))
+  (buffer-disable-undo))
 
 ;;;###autoload
 (defun 5x5 (&optional size)
@@ -187,16 +193,16 @@ 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]        
+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]
-Mutaue best cracker       \\[5x5-crack-mutating-best]
+Mutate best cracker       \\[5x5-crack-mutating-best]
 Mutate xor cracker        \\[5x5-crack-xor-mutate]
 Quit current game         \\[5x5-quit-game]"
 
@@ -219,9 +225,8 @@ Quit current game         \\[5x5-quit-game]"
           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))
-    (when (interactive-p)
-      (5x5-draw-grid (list 5x5-grid))
-      (5x5-position-cursor))))
+    (5x5-draw-grid (list 5x5-grid))
+    (5x5-position-cursor)))
 
 (defun 5x5-quit-game ()
   "Quit the current game of `5x5'."
@@ -277,7 +282,7 @@ Quit current game         \\[5x5-quit-game]"
   (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"
+  "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 ?-)))
@@ -342,8 +347,8 @@ Quit current game         \\[5x5-quit-game]"
 
 ;;;###autoload
 (defun 5x5-crack-xor-mutate ()
-  "Attempt to crack 5x5 by xor the current and best solution and then
-mutating the result."
+  "Attempt to crack 5x5 by xoring the current and best solution.
+Mutate the result."
   (interactive)
   (5x5-crack #'5x5-make-xor-with-mutation))
 
@@ -353,7 +358,7 @@ mutating the result."
 
 5x5-crack takes the argument BREEDER which should be a function that takes
 two parameters, the first will be a grid vector array that is the current
-solution and the second will be the best solution so far. The function
+solution and the second will be the best solution so far.  The function
 should return a grid vector array that is the new solution."
 
   (interactive "aBreeder function: ")
@@ -388,7 +393,7 @@ should return a grid vector array that is the new solution."
   (5x5-mutate-solution best))
 
 (defun 5x5-make-xor-with-mutation (current best)
-  "xor current and best solution then mutate the result."
+  "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
@@ -407,20 +412,21 @@ should return a grid vector array that is the new solution."
   solution)
 
 (defun 5x5-play-solution (solution best)
-  "Play a solution on an empty grid. This destroys the current game in
-progress because it is an animated attempt."
+  "Play a solution on an empty grid.  This destroys the current game
+in progress because it is an animated attempt."
   (5x5-new-game)
-  (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)))
+  (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))))
   5x5-grid)
-  
+
 ;; Keyboard response functions.
 
 (defun 5x5-flip-current ()
@@ -506,13 +512,16 @@ progress because it is an animated attempt."
 (defun 5x5-xor (x y)
   "Boolean exclusive-or of X and Y."
   (and (or x y) (not (and x y))))
-                   
+
 (defun 5x5-y-or-n-p (prompt)
-  "5x5 wrapper for y-or-n-p which respects the 5x5-hassle-me setting."
+  "5x5 wrapper for `y-or-n-p' which respects the `5x5-hassle-me' setting."
   (if 5x5-hassle-me
       (y-or-n-p prompt)
     t))
 
+(random t)
+
 (provide '5x5)
 
+;;; arch-tag: ec4dabd5-572d-41ea-b48c-ec5ce0d68fa9
 ;;; 5x5.el ends here