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