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