]> code.delx.au - gnu-emacs/blob - lisp/play/mpuz.el
Update FSF's address.
[gnu-emacs] / lisp / play / mpuz.el
1 ;;; mpuz.el --- multiplication puzzle for GNU Emacs
2
3 ;; Copyright (C) 1990, 2002, 2005 Free Software Foundation, Inc.
4
5 ;; Author: Philippe Schnoebelen <phs@lsv.ens-cachan.fr>
6 ;; Overhauled: Daniel Pfeiffer <occitan@esperanto.org>
7 ;; Keywords: games
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;; `M-x mpuz' generates a random multiplication puzzle. This is a
29 ;; multiplication example in which each digit has been consistently replaced
30 ;; with some letter. Your job is to reconstruct the original digits. Type
31 ;; `?' while the mode is active for detailed help.
32
33 ;;; Code:
34
35 (defgroup mpuz nil
36 "Multiplication puzzle."
37 :prefix "mpuz-"
38 :group 'games)
39
40 (random t) ; randomize
41
42 (defcustom mpuz-silent 'error
43 "*Set this to nil if you want dings on inputs.
44 t means never ding, and `error' means only ding on wrong input."
45 :type '(choice (const :tag "No" nil)
46 (const :tag "Yes" t)
47 (const :tag "If correct" error))
48 :group 'mpuz)
49
50 (defcustom mpuz-solve-when-trivial t
51 "*Solve any row that can be trivially calculated from what you've found."
52 :type 'boolean
53 :group 'mpuz)
54
55 (defcustom mpuz-allow-double-multiplicator nil
56 "*Allow 2nd factors like 33 or 77."
57 :type 'boolean
58 :group 'mpuz)
59
60 (defface mpuz-unsolved
61 '((((class color)) (:foreground "red1" :bold t))
62 (t (:bold t)))
63 "*Face to use for letters to be solved."
64 :group 'mpuz)
65 ;; backward-compatibility alias
66 (put 'mpuz-unsolved-face 'face-alias 'mpuz-unsolved)
67
68 (defface mpuz-solved
69 '((((class color)) (:foreground "green1" :bold t))
70 (t (:bold t)))
71 "*Face to use for solved digits."
72 :group 'mpuz)
73 ;; backward-compatibility alias
74 (put 'mpuz-solved-face 'face-alias 'mpuz-solved)
75
76 (defface mpuz-trivial
77 '((((class color)) (:foreground "blue" :bold t))
78 (t (:bold t)))
79 "*Face to use for trivial digits solved for you."
80 :group 'mpuz)
81 ;; backward-compatibility alias
82 (put 'mpuz-trivial-face 'face-alias 'mpuz-trivial)
83
84 (defface mpuz-text
85 '((t (:inherit variable-pitch)))
86 "*Face to use for text on right."
87 :group 'mpuz)
88 ;; backward-compatibility alias
89 (put 'mpuz-text-face 'face-alias 'mpuz-text)
90
91 \f
92 ;; Mpuz mode and keymaps
93 ;;----------------------
94 (defcustom mpuz-mode-hook nil
95 "Hook to run upon entry to mpuz."
96 :type 'hook
97 :group 'mpuz)
98
99 (defvar mpuz-mode-map nil
100 "Local keymap to use in Mult Puzzle.")
101
102 (if mpuz-mode-map nil
103 (setq mpuz-mode-map (make-sparse-keymap))
104 (define-key mpuz-mode-map "a" 'mpuz-try-letter)
105 (define-key mpuz-mode-map "b" 'mpuz-try-letter)
106 (define-key mpuz-mode-map "c" 'mpuz-try-letter)
107 (define-key mpuz-mode-map "d" 'mpuz-try-letter)
108 (define-key mpuz-mode-map "e" 'mpuz-try-letter)
109 (define-key mpuz-mode-map "f" 'mpuz-try-letter)
110 (define-key mpuz-mode-map "g" 'mpuz-try-letter)
111 (define-key mpuz-mode-map "h" 'mpuz-try-letter)
112 (define-key mpuz-mode-map "i" 'mpuz-try-letter)
113 (define-key mpuz-mode-map "j" 'mpuz-try-letter)
114 (define-key mpuz-mode-map "A" 'mpuz-try-letter)
115 (define-key mpuz-mode-map "B" 'mpuz-try-letter)
116 (define-key mpuz-mode-map "C" 'mpuz-try-letter)
117 (define-key mpuz-mode-map "D" 'mpuz-try-letter)
118 (define-key mpuz-mode-map "E" 'mpuz-try-letter)
119 (define-key mpuz-mode-map "F" 'mpuz-try-letter)
120 (define-key mpuz-mode-map "G" 'mpuz-try-letter)
121 (define-key mpuz-mode-map "H" 'mpuz-try-letter)
122 (define-key mpuz-mode-map "I" 'mpuz-try-letter)
123 (define-key mpuz-mode-map "J" 'mpuz-try-letter)
124 (define-key mpuz-mode-map "\C-g" 'mpuz-offer-abort)
125 (define-key mpuz-mode-map "?" 'describe-mode))
126
127 (defun mpuz-mode ()
128 "Multiplication puzzle mode.
129
130 You have to guess which letters stand for which digits in the
131 multiplication displayed inside the `*Mult Puzzle*' buffer.
132
133 You may enter a guess for a letter's value by typing first the letter,
134 then the digit. Thus, to guess that A=3, type `A 3'.
135
136 To leave the game to do other editing work, just switch buffers.
137 Then you may resume the game with M-x mpuz.
138 You may abort a game by typing \\<mpuz-mode-map>\\[mpuz-offer-abort]."
139 (interactive)
140 (kill-all-local-variables)
141 (setq major-mode 'mpuz-mode
142 mode-name "Mult Puzzle"
143 tab-width 30)
144 (use-local-map mpuz-mode-map)
145 (run-mode-hooks 'mpuz-mode-hook))
146
147 \f
148 ;; Some variables for statistics
149 ;;------------------------------
150 (defvar mpuz-nb-errors 0
151 "Number of errors made in current game.")
152
153 (defvar mpuz-nb-completed-games 0
154 "Number of games completed.")
155
156 (defvar mpuz-nb-cumulated-errors 0
157 "Number of errors made in previous games.")
158
159
160 ;; Some variables for game tracking
161 ;;---------------------------------
162 (defvar mpuz-in-progress nil
163 "True if a game is currently in progress.")
164
165 (defvar mpuz-found-digits (make-bool-vector 10 nil)
166 "A vector recording which digits have been decrypted.")
167
168 (defvar mpuz-trivial-digits (make-bool-vector 10 nil)
169 "A vector recording which digits have been solved for you.")
170
171 (defmacro mpuz-digit-solved-p (digit)
172 `(or (aref mpuz-found-digits ,digit)
173 (aref mpuz-trivial-digits ,digit)))
174
175
176 ;; A puzzle uses a permutation of [0..9] into itself.
177 ;; We use both the permutation and its inverse.
178 ;;---------------------------------------------------
179 (defvar mpuz-digit-to-letter (make-vector 10 0)
180 "A permutation from [0..9] to [0..9].")
181
182 (defvar mpuz-letter-to-digit (make-vector 10 0)
183 "The inverse of mpuz-digit-to-letter.")
184
185 (defmacro mpuz-to-digit (letter)
186 (list 'aref 'mpuz-letter-to-digit letter))
187
188 (defmacro mpuz-to-letter (digit)
189 (list 'aref 'mpuz-digit-to-letter digit))
190
191 (defun mpuz-build-random-perm ()
192 "Initialize puzzle coding with a random permutation."
193 (let ((letters (list 0 1 2 3 4 5 6 7 8 9)) ; new cons cells, because of delq
194 (index 10)
195 elem)
196 (while letters
197 (setq elem (nth (random index) letters)
198 letters (delq elem letters)
199 index (1- index))
200 (aset mpuz-digit-to-letter index elem)
201 (aset mpuz-letter-to-digit elem index))))
202
203
204 ;; A puzzle also uses a board displaying a multiplication.
205 ;; Every digit appears in the board, crypted or not.
206 ;;------------------------------------------------------
207 (defvar mpuz-board (make-vector 10 nil)
208 "The board associates to any digit the list of squares where it appears.")
209
210 (defun mpuz-put-number-on-board (number row &rest l)
211 "Put (last digit of) NUMBER on ROW and COLUMNS of the puzzle board."
212 (let (digit)
213 (while l
214 (setq digit (% number 10)
215 number (/ number 10))
216 (aset mpuz-board digit `((,row . ,(car l)) ,@(aref mpuz-board digit)))
217 (setq l (cdr l)))))
218
219 (defun mpuz-check-all-solved (&optional row col)
220 "Check whether all digits have been solved. Return t if yes."
221 (catch 'solved
222 (let (A B1 B2 C D E squares)
223 (and mpuz-solve-when-trivial
224 (not row)
225 (while
226 (cond ((or (and (setq B1 (or B1 (mpuz-check-all-solved 4 7))
227 B2 (or B2 (mpuz-check-all-solved 4 9))
228 E (or E (mpuz-check-all-solved 10))
229 A (or A (mpuz-check-all-solved 2)))
230 B1 B2)
231 (and E (or A (and B1 B2))))
232 (mpuz-solve)
233 (mpuz-paint-board)
234 (throw 'solved t))
235 ((and (setq D (or D (mpuz-check-all-solved 8))
236 C (or C (mpuz-check-all-solved 6)))
237 D (not E))
238 (mpuz-solve 10))
239 ((and E (not (eq C D)))
240 (mpuz-solve (if D 6 8)))
241 ((and A (not (eq B2 C)))
242 (mpuz-solve (if C 4 6) (if C 9)))
243 ((and A (not (eq B1 D)))
244 (mpuz-solve (if D 4 8) (if D 7)))
245 ((and (not A) (or (and B2 C) (and B1 D)))
246 (mpuz-solve 2)))))
247 (mpuz-paint-board)
248 (mapc (lambda (digit)
249 (and (not (mpuz-digit-solved-p digit)) ; unsolved
250 (setq squares (aref mpuz-board digit))
251 (if row
252 (if col
253 (member (cons row col) squares)
254 (assq row squares))
255 squares) ; and appearing in the puzzle!
256 (throw 'solved nil)))
257 [0 1 2 3 4 5 6 7 8 9]))
258 t))
259
260
261 ;; To build a puzzle, we take two random numbers and multiply them.
262 ;; We also take a random permutation for encryption.
263 ;; The random numbers are only use to see which digit appears in which square
264 ;; of the board. Everything is stored in individual squares.
265 ;;---------------------------------------------------------------------------
266 (defun mpuz-random-puzzle ()
267 "Draw random values to be multiplied in a puzzle."
268 (mpuz-build-random-perm)
269 (fillarray mpuz-board nil) ; erase the board
270 ;; A,B,C,D & E, are the five rows of our multiplication.
271 ;; Choose random values, discarding cases with leading zeros in C or D.
272 (let* ((A (+ 112 (random 888)))
273 (min (1+ (/ 1000 A)))
274 (B1 (+ min (random (- 10 min))))
275 B2 C D E)
276 (while (if (= B1 (setq B2 (+ min (random (- 10 min)))))
277 (not mpuz-allow-double-multiplicator)))
278 (setq C (* A B2)
279 D (* A B1)
280 E (+ C (* D 10)))
281 ;; Individual digits are now put on their respective squares.
282 ;; [NB: A square is a pair (row . column) of the screen.]
283 (mpuz-put-number-on-board A 2 9 7 5)
284 (mpuz-put-number-on-board (+ (* B1 10) B2) 4 9 7)
285 (mpuz-put-number-on-board C 6 9 7 5 3)
286 (mpuz-put-number-on-board D 8 7 5 3 1)
287 (mpuz-put-number-on-board E 10 9 7 5 3 1)))
288 \f
289 ;; Display
290 ;;--------
291 (defconst mpuz-framework
292 "
293 . . .
294 Number of errors (this game): 0
295 x . .
296 -------
297 . . . .
298 Number of completed games: 0
299 . . . .
300 --------- Average number of errors: 0.00
301 . . . . ."
302 "The general picture of the puzzle screen, as a string.")
303
304 (defun mpuz-create-buffer ()
305 "Create (or recreate) the puzzle buffer. Return it."
306 (let ((buf (get-buffer-create "*Mult Puzzle*"))
307 (face '(face mpuz-text))
308 buffer-read-only)
309 (save-excursion
310 (set-buffer buf)
311 (erase-buffer)
312 (insert mpuz-framework)
313 (set-text-properties 13 42 face)
314 (set-text-properties 79 105 face)
315 (set-text-properties 128 153 face)
316 (mpuz-paint-board)
317 (mpuz-paint-errors)
318 (mpuz-paint-statistics))
319 buf))
320
321 (defun mpuz-paint-number (n &optional eol words)
322 (end-of-line eol)
323 (let (buffer-read-only)
324 (delete-region (point)
325 (progn (backward-word (or words 1)) (point)))
326 (insert n)))
327
328 (defun mpuz-paint-errors ()
329 "Paint error count on the puzzle screen."
330 (mpuz-switch-to-window)
331 (goto-line 3)
332 (mpuz-paint-number (prin1-to-string mpuz-nb-errors)))
333
334 (defun mpuz-paint-statistics ()
335 "Paint statistics about previous games on the puzzle screen."
336 (goto-line 7)
337 (mpuz-paint-number (prin1-to-string mpuz-nb-completed-games))
338 (mpuz-paint-number
339 (format "%.2f"
340 (if (zerop mpuz-nb-completed-games)
341 0
342 (/ (+ 0.0 mpuz-nb-cumulated-errors)
343 mpuz-nb-completed-games)))
344 3 2))
345
346 (defun mpuz-paint-board ()
347 "Paint board situation on the puzzle screen."
348 (mpuz-switch-to-window)
349 (mapc 'mpuz-paint-digit [0 1 2 3 4 5 6 7 8 9])
350 (goto-char (point-min)))
351
352 (defun mpuz-paint-digit (digit)
353 "Paint all occurrences of DIGIT on the puzzle board."
354 (let ((char (if (mpuz-digit-solved-p digit)
355 (+ digit ?0)
356 (+ (mpuz-to-letter digit) ?A)))
357 (face `(face
358 ,(cond ((aref mpuz-trivial-digits digit) 'mpuz-trivial)
359 ((aref mpuz-found-digits digit) 'mpuz-solved)
360 ('mpuz-unsolved))))
361 buffer-read-only)
362 (mapc (lambda (square)
363 (goto-line (car square)) ; line before column!
364 (move-to-column (cdr square))
365 (insert char)
366 (set-text-properties (1- (point)) (point) face)
367 (delete-char 1))
368 (aref mpuz-board digit))))
369
370 (defun mpuz-get-buffer ()
371 "Get the puzzle buffer if it exists."
372 (get-buffer "*Mult Puzzle*"))
373
374 (defun mpuz-switch-to-window ()
375 "Find or create the Mult-Puzzle buffer, and display it."
376 (let ((buf (mpuz-get-buffer)))
377 (or buf (setq buf (mpuz-create-buffer)))
378 (switch-to-buffer buf)
379 (setq buffer-read-only t)
380 (mpuz-mode)))
381
382 \f
383 ;; Game control
384 ;;-------------
385 (defun mpuz-start-new-game ()
386 "Start a new puzzle."
387 (message "Here we go...")
388 (setq mpuz-nb-errors 0
389 mpuz-in-progress t)
390 (fillarray mpuz-found-digits nil) ; initialize mpuz-found-digits
391 (fillarray mpuz-trivial-digits nil)
392 (mpuz-random-puzzle)
393 (mpuz-switch-to-window)
394 (mpuz-paint-board)
395 (mpuz-paint-errors)
396 (mpuz-ask-for-try))
397
398 ;;;###autoload
399 (defun mpuz ()
400 "Multiplication puzzle with GNU Emacs."
401 ;; Main entry point
402 (interactive)
403 (mpuz-switch-to-window)
404 (if mpuz-in-progress
405 (mpuz-offer-abort)
406 (mpuz-start-new-game)))
407
408 (defun mpuz-offer-abort ()
409 "Ask if user wants to abort current puzzle."
410 (interactive)
411 (if (y-or-n-p "Abort game ")
412 (let ((buf (mpuz-get-buffer)))
413 (message "Mult Puzzle aborted.")
414 (setq mpuz-in-progress nil
415 mpuz-nb-errors 0)
416 (fillarray mpuz-board nil)
417 (if buf (kill-buffer buf)))
418 (mpuz-ask-for-try)))
419
420 (defun mpuz-ask-for-try ()
421 "Ask for user proposal in puzzle."
422 (message "Your try?"))
423
424 (defun mpuz-ding (error)
425 "Dings, unless global variable `mpuz-silent' forbids it."
426 (cond ((eq mpuz-silent t))
427 ((not mpuz-silent) (ding t))
428 (error (ding t))))
429
430 (defun mpuz-try-letter ()
431 "Propose a digit for a letter in puzzle."
432 (interactive)
433 (if mpuz-in-progress
434 (let (letter-char digit digit-char message)
435 (setq letter-char (upcase last-command-char)
436 digit (mpuz-to-digit (- letter-char ?A)))
437 (cond ((mpuz-digit-solved-p digit)
438 (message "%c already solved." letter-char)
439 (mpuz-ding t))
440 ((null (aref mpuz-board digit))
441 (message "%c does not appear." letter-char)
442 (mpuz-ding t))
443 ((progn (message "%c = " letter-char)
444 ;; <char> has been entered.
445 ;; Print "<char> =" and
446 ;; read <num> or = <num>
447 (setq digit-char (read-char))
448 (if (eq digit-char ?=)
449 (setq digit-char (read-char)))
450 (or (> digit-char ?9) (< digit-char ?0))) ; bad input
451 (message "%c = %c" letter-char digit-char)
452 (mpuz-ding t))
453 (t
454 (mpuz-try-proposal letter-char digit-char))))
455 (if (y-or-n-p "Start a new game ")
456 (mpuz-start-new-game)
457 (message "OK. I won't."))))
458
459 (defun mpuz-try-proposal (letter-char digit-char)
460 "Propose LETTER-CHAR as code for DIGIT-CHAR."
461 (let* ((letter (- letter-char ?A))
462 (digit (- digit-char ?0))
463 (correct-digit (mpuz-to-digit letter))
464 (game mpuz-nb-completed-games))
465 (cond ((mpuz-digit-solved-p correct-digit)
466 (message "%c has already been found." (+ correct-digit ?0)))
467 ((mpuz-digit-solved-p digit)
468 (message "%c has already been placed." digit-char))
469 ((= digit correct-digit)
470 (message "%c = %c correct!" letter-char digit-char)
471 (mpuz-ding nil)
472 (aset mpuz-found-digits digit t) ; Mark digit as solved
473 (and (mpuz-check-all-solved)
474 (mpuz-close-game)))
475 (t ;;; incorrect guess
476 (message "%c = %c incorrect!" letter-char digit-char)
477 (mpuz-ding t)
478 (setq mpuz-nb-errors (1+ mpuz-nb-errors))
479 (mpuz-paint-errors)))))
480
481 (defun mpuz-close-game ()
482 "Housecleaning when puzzle has been solved."
483 (setq mpuz-in-progress nil
484 mpuz-nb-cumulated-errors (+ mpuz-nb-cumulated-errors mpuz-nb-errors)
485 mpuz-nb-completed-games (1+ mpuz-nb-completed-games))
486 (mpuz-paint-statistics)
487 (let ((message (format "Puzzle solved with %d error%s. That's %s"
488 mpuz-nb-errors
489 (if (= mpuz-nb-errors 1) "" "s")
490 (cond ((= mpuz-nb-errors 0) "perfect!")
491 ((= mpuz-nb-errors 1) "very good!")
492 ((= mpuz-nb-errors 2) "good.")
493 ((= mpuz-nb-errors 3) "not bad.")
494 ((= mpuz-nb-errors 4) "not too bad...")
495 ((< mpuz-nb-errors 10) "bad!")
496 ((< mpuz-nb-errors 15) "awful.")
497 (t "not serious.")))))
498 (message message)
499 (sit-for 4)
500 (if (y-or-n-p (concat message " Start a new game "))
501 (mpuz-start-new-game)
502 (message "Good Bye!"))))
503
504 (defun mpuz-solve (&optional row col)
505 "Find solution for autosolving."
506 (mapc (lambda (digit)
507 (or (mpuz-digit-solved-p digit)
508 (if row
509 (not (if col
510 (member (cons row col) (aref mpuz-board digit))
511 (assq row (aref mpuz-board digit)))))
512 (aset mpuz-trivial-digits digit t)))
513 [0 1 2 3 4 5 6 7 8 9])
514 t)
515
516 (defun mpuz-show-solution (row)
517 "Display solution for debugging purposes."
518 (interactive "P")
519 (mpuz-switch-to-window)
520 (mpuz-solve (if row (* 2 (prefix-numeric-value row))))
521 (mpuz-paint-board)
522 (if (mpuz-check-all-solved)
523 (mpuz-close-game)))
524
525 (provide 'mpuz)
526
527 ;;; arch-tag: 2781d6ba-89e7-43b5-85c7-5d3a2e73feb1
528 ;;; mpuz.el ends here