annotate lisp/play/mpuz.el @ 621:eca8812e61cd

*** empty log message ***
author Jim Blandy <jimb@redhat.com>
date Fri, 24 Apr 1992 08:11:54 +0000
parents c0bd9c7f9c42
children 505130d1ddf8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
142
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1 ;;; Multiplication puzzle for GNU Emacs
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
2 ;;; by Philippe Schnoebelen <phs@lifia.imag.fr>
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
3 ;;; Last modified on 11 Nov 1990
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
4 ;;; Copyright (C) 1990 Free Software Foundation, Inc.
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
5
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
6 ;; This file is part of GNU Emacs.
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
7
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
8 ;; GNU Emacs is distributed in the hope that it will be useful,
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
9 ;; but WITHOUT ANY WARRANTY. No author or distributor
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
10 ;; accepts responsibility to anyone for the consequences of using it
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
11 ;; or for whether it serves any particular purpose or works at all,
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
12 ;; unless he says so in writing. Refer to the GNU Emacs General Public
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
13 ;; License for full details.
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
14
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
15 ;; Everyone is granted permission to copy, modify and redistribute
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
16 ;; GNU Emacs, but only under the conditions described in the
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
17 ;; GNU Emacs General Public License. A copy of this license is
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
18 ;; supposed to have been given to you along with GNU Emacs so you
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
19 ;; can know your rights and responsibilities. It should be in a
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
20 ;; file named COPYING. Among other things, the copyright notice
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
21 ;; and this notice must be preserved on all copies.
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
22
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
23 (random t) ; randomize
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
24
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
25 (defun mpuz-random (n)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
26 "Return a random integer between 0 and N - 1 inclusive."
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
27 (setq n (% (random) n))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
28 (if (< n 0) (- n) n))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
29
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
30 (defvar mpuz-silent nil
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
31 "*Set this to T if you don't want dings on inputs.")
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
32
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
33 (defun mpuz-ding ()
232
c0bd9c7f9c42 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 142
diff changeset
34 "Dings, unless global variable `mpuz-silent' forbids it."
142
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
35 (or mpuz-silent (ding t)))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
36
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
37
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
38 ;; Mpuz mode and keymaps
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
39 ;;----------------------
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
40 (defvar mpuz-mode-hook nil)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
41
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
42 (defvar mpuz-mode-map nil
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
43 "Local keymap to use in Mult Puzzle.")
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
44
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
45 (defvar mpuz-read-map nil
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
46 "Local keymap to use (sometimes) in Mult Puzzle.")
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
47
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
48 (if mpuz-mode-map nil
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
49 (setq mpuz-mode-map (make-sparse-keymap))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
50 (define-key mpuz-mode-map "a" 'mpuz-try-letter)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
51 (define-key mpuz-mode-map "b" 'mpuz-try-letter)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
52 (define-key mpuz-mode-map "c" 'mpuz-try-letter)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
53 (define-key mpuz-mode-map "d" 'mpuz-try-letter)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
54 (define-key mpuz-mode-map "e" 'mpuz-try-letter)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
55 (define-key mpuz-mode-map "f" 'mpuz-try-letter)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
56 (define-key mpuz-mode-map "g" 'mpuz-try-letter)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
57 (define-key mpuz-mode-map "h" 'mpuz-try-letter)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
58 (define-key mpuz-mode-map "i" 'mpuz-try-letter)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
59 (define-key mpuz-mode-map "j" 'mpuz-try-letter)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
60 (define-key mpuz-mode-map "A" 'mpuz-try-letter)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
61 (define-key mpuz-mode-map "B" 'mpuz-try-letter)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
62 (define-key mpuz-mode-map "C" 'mpuz-try-letter)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
63 (define-key mpuz-mode-map "D" 'mpuz-try-letter)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
64 (define-key mpuz-mode-map "E" 'mpuz-try-letter)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
65 (define-key mpuz-mode-map "F" 'mpuz-try-letter)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
66 (define-key mpuz-mode-map "G" 'mpuz-try-letter)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
67 (define-key mpuz-mode-map "H" 'mpuz-try-letter)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
68 (define-key mpuz-mode-map "I" 'mpuz-try-letter)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
69 (define-key mpuz-mode-map "J" 'mpuz-try-letter)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
70 (define-key mpuz-mode-map "\C-g" 'mpuz-offer-abort)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
71 (define-key mpuz-mode-map "?" 'describe-mode))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
72
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
73 (if mpuz-read-map nil
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
74 (setq mpuz-read-map (make-keymap))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
75 (fillarray mpuz-read-map 'exit-minibuffer))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
76
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
77 (defun mpuz-mode ()
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
78 "Multiplication puzzle with GNU Emacs.
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
79
232
c0bd9c7f9c42 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 142
diff changeset
80 You have to guess which letters stand for which digits in the
c0bd9c7f9c42 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 142
diff changeset
81 multiplication displayed inside the *Mult Puzzle* buffer.
142
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
82
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
83 You may enter a proposal (e.g. A=3) by hitting first the letter A,
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
84 then the digit 3, on your keyboard.
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
85
232
c0bd9c7f9c42 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 142
diff changeset
86 At any time you may leave the game to do other editing work. :-)
142
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
87 Then you may resume the game with M-x mult-puzzle.
232
c0bd9c7f9c42 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 142
diff changeset
88 You may abort a game by hitting \\[keyboard-quit]."
142
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
89 (interactive)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
90 (setq major-mode 'mpuz-mode
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
91 mode-name "Mult Puzzle")
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
92 (use-local-map mpuz-mode-map)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
93 (run-hooks 'mpuz-mode-hook))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
94
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
95
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
96 ;; Some variables for statistics
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
97 ;;------------------------------
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
98 (defvar mpuz-nb-errors 0
232
c0bd9c7f9c42 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 142
diff changeset
99 "Number of errors made in current game.")
142
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
100
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
101 (defvar mpuz-nb-completed-games 0
232
c0bd9c7f9c42 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 142
diff changeset
102 "Number of games completed.")
142
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
103
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
104 (defvar mpuz-nb-cumulated-errors 0
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
105 "Number of errors made in previous games.")
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
106
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
107
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
108 ;; Some variables for game tracking
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
109 ;;---------------------------------
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
110 (defvar mpuz-in-progress nil
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
111 "True if a game is currently in progress.")
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
112
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
113 (defvar mpuz-found-digits (make-vector 10 nil)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
114 "A vector recording which digits have been decrypted.")
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
115
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
116 (defmacro mpuz-digit-solved-p (digit)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
117 (list 'aref 'mpuz-found-digits digit))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
118
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
119
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
120 ;; A puzzle uses a permutation of [0..9] into itself.
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
121 ;; We use both the permutation and its inverse.
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
122 ;;---------------------------------------------------
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
123 (defvar mpuz-digit-to-letter (make-vector 10 0)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
124 "A permutation from [0..9] to [0..9].")
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
125
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
126 (defvar mpuz-letter-to-digit (make-vector 10 0)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
127 "The inverse of mpuz-digit-to-letter.")
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
128
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
129 (defmacro mpuz-to-digit (letter)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
130 (list 'aref 'mpuz-letter-to-digit letter))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
131
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
132 (defmacro mpuz-to-letter (digit)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
133 (list 'aref 'mpuz-digit-to-letter digit))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
134
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
135 (defun mpuz-build-random-perm ()
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
136 "Initialize puzzle coding with a random permutation."
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
137 (let ((letters (list 0 1 2 3 4 5 6 7 8 9)) ; new cons cells, because of delq
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
138 (index 10)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
139 elem)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
140 (while letters
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
141 (setq elem (nth (mpuz-random index) letters)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
142 letters (delq elem letters)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
143 index (1- index))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
144 (aset mpuz-digit-to-letter index elem)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
145 (aset mpuz-letter-to-digit elem index))))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
146
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
147
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
148 ;; A puzzle also uses a board displaying a mulplication.
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
149 ;; Every digit appears in the board, crypted or not.
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
150 ;;------------------------------------------------------
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
151 (defvar mpuz-board (make-vector 10 nil)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
152 "The board associates ot any digit the list of squares where it appears.")
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
153
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
154 (defun mpuz-put-digit-on-board (number square)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
155 "Put (last digit of) NUMBER on SQUARE of the puzzle board."
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
156 ;; i.e. push SQUARE on NUMBER square-list
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
157 (setq number (% number 10))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
158 (aset mpuz-board number (cons square (aref mpuz-board number))))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
159
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
160 (defun mpuz-check-all-solved ()
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
161 "Check whether all digits have been solved. Return t if yes."
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
162 (catch 'found
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
163 (let ((digit -1))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
164 (while (> 10 (setq digit (1+ digit)))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
165 (if (and (not (mpuz-digit-solved-p digit)) ; unsolved
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
166 (aref mpuz-board digit)) ; and appearing in the puzzle !
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
167 (throw 'found nil))))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
168 t))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
169
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
170
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
171 ;; To build a puzzle, we take two random numbers and multiply them.
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
172 ;; We also take a random permutation for encryption.
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
173 ;; The random numbers are only use to see which digit appears in which square
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
174 ;; of the board. Everything is stored in individual squares.
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
175 ;;---------------------------------------------------------------------------
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
176 (defun mpuz-random-puzzle ()
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
177 "Draw random values to be multiplied in a puzzle."
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
178 (mpuz-build-random-perm)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
179 (fillarray mpuz-board nil) ; erase the board
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
180 (let (A B C D E)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
181 ;; A,B,C,D & E, are the five rows of our multiplication.
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
182 ;; Choose random values, discarding uninteresting cases.
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
183 (while (progn
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
184 (setq A (mpuz-random 1000)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
185 B (mpuz-random 100)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
186 C (* A (% B 10))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
187 D (* A (/ B 10))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
188 E (* A B))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
189 (or (< C 1000) (< D 1000)))) ; forbid leading zeros in C or D
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
190 ;; Individual digits are now put on their respectives squares.
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
191 ;; [NB: A square is a pair <row,column> of the screen.]
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
192 (mpuz-put-digit-on-board A '(2 . 9))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
193 (mpuz-put-digit-on-board (/ A 10) '(2 . 7))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
194 (mpuz-put-digit-on-board (/ A 100) '(2 . 5))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
195 (mpuz-put-digit-on-board B '(4 . 9))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
196 (mpuz-put-digit-on-board (/ B 10) '(4 . 7))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
197 (mpuz-put-digit-on-board C '(6 . 9))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
198 (mpuz-put-digit-on-board (/ C 10) '(6 . 7))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
199 (mpuz-put-digit-on-board (/ C 100) '(6 . 5))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
200 (mpuz-put-digit-on-board (/ C 1000) '(6 . 3))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
201 (mpuz-put-digit-on-board D '(8 . 7))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
202 (mpuz-put-digit-on-board (/ D 10) '(8 . 5))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
203 (mpuz-put-digit-on-board (/ D 100) '(8 . 3))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
204 (mpuz-put-digit-on-board (/ D 1000) '(8 . 1))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
205 (mpuz-put-digit-on-board E '(10 . 9))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
206 (mpuz-put-digit-on-board (/ E 10) '(10 . 7))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
207 (mpuz-put-digit-on-board (/ E 100) '(10 . 5))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
208 (mpuz-put-digit-on-board (/ E 1000) '(10 . 3))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
209 (mpuz-put-digit-on-board (/ E 10000) '(10 . 1))))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
210
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
211 ;; Display
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
212 ;;--------
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
213 (defconst mpuz-framework
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
214 "
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
215 . . .
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
216 Number of errors (this game): 0
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
217 x . .
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
218 -------
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
219 . . . .
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
220 Number of completed games: 0
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
221 . . . .
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
222 --------- Average number of errors: 0.00
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
223 . . . . ."
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
224 "The general picture of the puzzle screen, as a string.")
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
225
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
226 (defun mpuz-create-buffer ()
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
227 "Create (or recreate) the puzzle buffer. Return it."
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
228 (let ((buff (get-buffer-create "*Mult Puzzle*")))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
229 (save-excursion
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
230 (set-buffer buff)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
231 (let ((buffer-read-only nil))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
232 (erase-buffer)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
233 (insert mpuz-framework)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
234 (mpuz-paint-board)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
235 (mpuz-paint-errors)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
236 (mpuz-paint-statistics)))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
237 buff))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
238
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
239 (defun mpuz-paint-errors ()
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
240 "Paint error count on the puzzle screen."
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
241 (mpuz-switch-to-window)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
242 (let ((buffer-read-only nil))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
243 (goto-line 3)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
244 (move-to-column 49)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
245 (mpuz-delete-line)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
246 (insert (prin1-to-string mpuz-nb-errors))))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
247
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
248 (defun mpuz-paint-statistics ()
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
249 "Paint statistics about previous games on the puzzle screen."
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
250 (let* ((mean (if (zerop mpuz-nb-completed-games) 0
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
251 (/ (+ mpuz-nb-completed-games (* 200 mpuz-nb-cumulated-errors))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
252 (* 2 mpuz-nb-completed-games))))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
253 (frac-part (% mean 100)))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
254 (let ((buffer-read-only nil))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
255 (goto-line 7)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
256 (move-to-column 51)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
257 (mpuz-delete-line)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
258 (insert (prin1-to-string mpuz-nb-completed-games))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
259 (goto-line 9)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
260 (move-to-column 50)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
261 (mpuz-delete-line)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
262 (insert (format "%d.%d%d" (/ mean 100) (/ frac-part 10) (% frac-part 10))))))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
263
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
264 (defun mpuz-paint-board ()
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
265 "Paint board situation on the puzzle screen."
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
266 (mpuz-switch-to-window)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
267 (let ((letter -1))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
268 (while (> 10 (setq letter (1+ letter)))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
269 (mpuz-paint-digit (mpuz-to-digit letter))))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
270 (goto-char (point-min)))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
271
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
272 (defun mpuz-paint-digit (digit)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
273 "Paint all occurrences of DIGIT on the puzzle board."
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
274 ;; (mpuz-switch-to-window)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
275 (let ((char (if (mpuz-digit-solved-p digit)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
276 (+ digit ?0)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
277 (+ (mpuz-to-letter digit) ?A)))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
278 (square-l (aref mpuz-board digit)))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
279 (let ((buffer-read-only nil))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
280 (while square-l
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
281 (goto-line (car (car square-l))) ; line before column !
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
282 (move-to-column (cdr (car square-l)))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
283 (insert char)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
284 (delete-char 1)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
285 (backward-char 1)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
286 (setq square-l (cdr square-l))))))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
287
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
288 (defun mpuz-delete-line ()
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
289 "Clear from point to next newline." ; & put nothing in the kill ring
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
290 (while (not (= ?\n (char-after (point))))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
291 (delete-char 1)))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
292
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
293 (defun mpuz-get-buffer ()
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
294 "Get the puzzle buffer if it exists."
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
295 (get-buffer "*Mult Puzzle*"))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
296
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
297 (defun mpuz-switch-to-window ()
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
298 "Find or create the Mult-Puzzle buffer, and display it."
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
299 (let ((buff (mpuz-get-buffer)))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
300 (or buff (setq buff (mpuz-create-buffer)))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
301 (switch-to-buffer buff)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
302 (or buffer-read-only (toggle-read-only))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
303 (mpuz-mode)))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
304
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
305
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
306 ;; Game control
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
307 ;;-------------
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
308 (defun mpuz-abort-game ()
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
309 "Abort any puzzle in progess."
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
310 (message "Mult Puzzle aborted.")
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
311 (setq mpuz-in-progress nil
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
312 mpuz-nb-errors 0)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
313 (fillarray mpuz-board nil)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
314 (let ((buff (mpuz-get-buffer)))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
315 (if buff (kill-buffer buff))))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
316
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
317 (defun mpuz-start-new-game ()
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
318 "Start a new puzzle."
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
319 (message "Here we go...")
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
320 (setq mpuz-nb-errors 0
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
321 mpuz-in-progress t)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
322 (fillarray mpuz-found-digits nil) ; initialize mpuz-found-digits
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
323 (mpuz-random-puzzle)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
324 (mpuz-switch-to-window)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
325 (mpuz-paint-board)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
326 (mpuz-paint-errors)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
327 (mpuz-ask-for-try))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
328
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
329 (defun mpuz-offer-new-game ()
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
330 "Ask if user wants to start a new puzzle."
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
331 (if (y-or-n-p "Start a new game ")
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
332 (mpuz-start-new-game)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
333 (message "OK. I won't.")))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
334
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
335 (defun mult-puzzle ()
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
336 "Multiplication puzzle with GNU Emacs."
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
337 ;; Main entry point
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
338 (interactive)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
339 (mpuz-switch-to-window)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
340 (if mpuz-in-progress
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
341 (mpuz-offer-abort)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
342 (mpuz-start-new-game)))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
343
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
344 (defun mpuz-offer-abort ()
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
345 "Ask if user wants to abort current puzzle."
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
346 (interactive)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
347 (if (y-or-n-p "Abort game ")
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
348 (mpuz-abort-game)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
349 (mpuz-ask-for-try)))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
350
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
351 (defun mpuz-ask-for-try ()
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
352 "Ask for user proposal in puzzle."
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
353 (message "Your try ?"))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
354
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
355 (defun mpuz-try-letter ()
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
356 "Propose a digit for a letter in puzzle."
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
357 (interactive)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
358 (if mpuz-in-progress
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
359 (let (letter-char digit digit-char message)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
360 (setq letter-char (if (or (< last-command-char ?a)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
361 (> last-command-char ?z))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
362 last-command-char
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
363 (- last-command-char 32))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
364 digit (mpuz-to-digit (- letter-char ?A)))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
365 (cond ((mpuz-digit-solved-p digit)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
366 (message "%c already solved." letter-char))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
367 ((null (aref mpuz-board digit))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
368 (message "%c does not appear." letter-char))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
369 ((progn (setq message (format "%c = " letter-char))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
370 ;; <char> has been entered.
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
371 ;; Print "<char> =" and
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
372 ;; read <num> or = <num>
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
373 (read-from-minibuffer message nil mpuz-read-map)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
374 (if (= last-input-char ?\=)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
375 (read-from-minibuffer message nil mpuz-read-map))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
376 (setq digit-char last-input-char)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
377 (message "%c = %c" letter-char digit-char)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
378 (or (> digit-char ?9) (< digit-char ?0))) ; bad input
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
379 (ding t))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
380 (t
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
381 (mpuz-try-proposal letter-char digit-char))))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
382 (mpuz-offer-new-game)))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
383
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
384 (defun mpuz-try-proposal (letter-char digit-char)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
385 "Propose LETTER-CHAR as code for DIGIT-CHAR."
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
386 (let* ((letter (- letter-char ?A))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
387 (digit (- digit-char ?0))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
388 (correct-digit (mpuz-to-digit letter)))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
389 (cond ((mpuz-digit-solved-p correct-digit)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
390 (message "%c has already been found."))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
391 ((= digit correct-digit)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
392 (message "%c = %c correct !" letter-char digit-char)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
393 (mpuz-ding)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
394 (mpuz-correct-guess digit))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
395 (t ;;; incorrect guess
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
396 (message "%c = %c incorrect !" letter-char digit-char)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
397 (mpuz-ding)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
398 (setq mpuz-nb-errors (1+ mpuz-nb-errors))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
399 (mpuz-paint-errors)))))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
400
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
401 (defun mpuz-correct-guess (digit)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
402 "Handle correct guessing of DIGIT."
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
403 (aset mpuz-found-digits digit t) ; Mark digit as solved
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
404 (mpuz-paint-digit digit) ; Repaint it (now as a digit)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
405 (if (mpuz-check-all-solved)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
406 (mpuz-close-game)))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
407
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
408 (defun mpuz-close-game ()
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
409 "Housecleaning when puzzle has been solved."
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
410 (setq mpuz-in-progress nil
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
411 mpuz-nb-cumulated-errors (+ mpuz-nb-cumulated-errors mpuz-nb-errors)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
412 mpuz-nb-completed-games (1+ mpuz-nb-completed-games))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
413 (mpuz-paint-statistics)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
414 (let ((message (mpuz-congratulate)))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
415 (message message)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
416 (sit-for 4)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
417 (if (y-or-n-p (concat message " Start a new game "))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
418 (mpuz-start-new-game)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
419 (message "Good Bye !"))))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
420
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
421 (defun mpuz-congratulate ()
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
422 "Build a congratulation message when puzzle is solved."
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
423 (format "Puzzle solved with %d errors. %s"
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
424 mpuz-nb-errors
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
425 (cond ((= mpuz-nb-errors 0) "That's perfect !")
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
426 ((= mpuz-nb-errors 1) "That's very good !")
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
427 ((= mpuz-nb-errors 2) "That's good.")
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
428 ((= mpuz-nb-errors 3) "That's not bad.")
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
429 ((= mpuz-nb-errors 4) "That's not too bad...")
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
430 ((and (>= mpuz-nb-errors 5)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
431 (< mpuz-nb-errors 10)) "That's bad !")
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
432 ((and (>= mpuz-nb-errors 10)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
433 (< mpuz-nb-errors 15)) "That's awful.")
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
434 ((>= mpuz-nb-errors 15) "That's not serious."))))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
435
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
436 (defun mpuz-show-solution ()
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
437 "Display solution for debugging purposes."
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
438 (interactive)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
439 (mpuz-switch-to-window)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
440 (let (digit list)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
441 (setq digit -1)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
442 (while (> 10 (setq digit (1+ digit)))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
443 (or (mpuz-digit-solved-p digit)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
444 (setq list (cons digit list))))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
445 (mapcar 'mpuz-correct-guess list)))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
446
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
447 ;;; End of mult-puzzle
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
448