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