annotate lisp/play/mpuz.el @ 21819:c98ba82f4b52

(Flsh, Fash): Handle out-of-range shift counts reasonably.
author Richard M. Stallman <rms@gnu.org>
date Wed, 29 Apr 1998 00:59:22 +0000
parents f1fc193b6958
children 0291983fe62f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
14169
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 14040
diff changeset
3 ;; Copyright (C) 1990 Free Software Foundation, Inc.
838
c8798ebd7d95 entered into RCS
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 791
diff changeset
4
19963
2f60572de4d1 Comment change.
Richard M. Stallman <rms@gnu.org>
parents: 18383
diff changeset
5 ;; Author: Philippe Schnoebelen <phs@lsv.ens-cachan.fr>
791
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
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
8 ;; This file is part of GNU Emacs.
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
9
6736
3e1323443b1a Fix copying conditions for current GPL version.
Richard M. Stallman <rms@gnu.org>
parents: 4400
diff changeset
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
3e1323443b1a Fix copying conditions for current GPL version.
Richard M. Stallman <rms@gnu.org>
parents: 4400
diff changeset
11 ;; it under the terms of the GNU General Public License as published by
3e1323443b1a Fix copying conditions for current GPL version.
Richard M. Stallman <rms@gnu.org>
parents: 4400
diff changeset
12 ;; the Free Software Foundation; either version 2, or (at your option)
3e1323443b1a Fix copying conditions for current GPL version.
Richard M. Stallman <rms@gnu.org>
parents: 4400
diff changeset
13 ;; any later version.
142
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
14
6736
3e1323443b1a Fix copying conditions for current GPL version.
Richard M. Stallman <rms@gnu.org>
parents: 4400
diff changeset
15 ;; GNU Emacs is distributed in the hope that it will be useful,
3e1323443b1a Fix copying conditions for current GPL version.
Richard M. Stallman <rms@gnu.org>
parents: 4400
diff changeset
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
3e1323443b1a Fix copying conditions for current GPL version.
Richard M. Stallman <rms@gnu.org>
parents: 4400
diff changeset
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3e1323443b1a Fix copying conditions for current GPL version.
Richard M. Stallman <rms@gnu.org>
parents: 4400
diff changeset
18 ;; GNU General Public License for more details.
3e1323443b1a Fix copying conditions for current GPL version.
Richard M. Stallman <rms@gnu.org>
parents: 4400
diff changeset
19
3e1323443b1a Fix copying conditions for current GPL version.
Richard M. Stallman <rms@gnu.org>
parents: 4400
diff changeset
20 ;; You should have received a copy of the GNU General Public License
14169
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 14040
diff changeset
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 14040
diff changeset
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 14040
diff changeset
23 ;; Boston, MA 02111-1307, USA.
142
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
24
2308
f287613dfc28 Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 1217
diff changeset
25 ;;; Commentary:
f287613dfc28 Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 1217
diff changeset
26
f287613dfc28 Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 1217
diff changeset
27 ;; When this package is loaded, `M-x mpuz' generates a random multiplication
f287613dfc28 Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 1217
diff changeset
28 ;; puzzle. This is a multiplication example in which each digit has been
f287613dfc28 Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 1217
diff changeset
29 ;; consistently replaced with some letter. Your job is to reconstruct
f287613dfc28 Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 1217
diff changeset
30 ;; the original digits. Type `?' while the mode is active for detailed help.
f287613dfc28 Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 1217
diff changeset
31
791
203c23c9f22c *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 659
diff changeset
32 ;;; Code:
203c23c9f22c *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 659
diff changeset
33
21363
f1fc193b6958 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 19963
diff changeset
34 (defgroup mpuz nil
f1fc193b6958 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 19963
diff changeset
35 "Multiplication puzzle."
f1fc193b6958 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 19963
diff changeset
36 :prefix "mpuz-"
f1fc193b6958 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 19963
diff changeset
37 :group 'games)
f1fc193b6958 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 19963
diff changeset
38
142
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
39 (random t) ; randomize
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
40
21363
f1fc193b6958 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 19963
diff changeset
41 (defcustom mpuz-silent nil
f1fc193b6958 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 19963
diff changeset
42 "*Set this to T if you don't want dings on inputs."
f1fc193b6958 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 19963
diff changeset
43 :type 'boolean
f1fc193b6958 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 19963
diff changeset
44 :group 'mpuz)
142
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
45
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
46 (defun mpuz-ding ()
232
c0bd9c7f9c42 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 142
diff changeset
47 "Dings, unless global variable `mpuz-silent' forbids it."
142
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
48 (or mpuz-silent (ding t)))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
49
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
50
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
51 ;; Mpuz mode and keymaps
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
52 ;;----------------------
21363
f1fc193b6958 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 19963
diff changeset
53 (defcustom mpuz-mode-hook nil
f1fc193b6958 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 19963
diff changeset
54 "Hook to run upon entry to mpuz."
f1fc193b6958 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 19963
diff changeset
55 :type 'hook
f1fc193b6958 Customized.
Stephen Eglen <stephen@gnu.org>
parents: 19963
diff changeset
56 :group 'mpuz)
142
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
57
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
58 (defvar mpuz-mode-map nil
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
59 "Local keymap to use in Mult Puzzle.")
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
60
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
61 (if mpuz-mode-map nil
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
62 (setq mpuz-mode-map (make-sparse-keymap))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
63 (define-key mpuz-mode-map "a" 'mpuz-try-letter)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
64 (define-key mpuz-mode-map "b" 'mpuz-try-letter)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
65 (define-key mpuz-mode-map "c" 'mpuz-try-letter)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
66 (define-key mpuz-mode-map "d" 'mpuz-try-letter)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
67 (define-key mpuz-mode-map "e" 'mpuz-try-letter)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
68 (define-key mpuz-mode-map "f" 'mpuz-try-letter)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
69 (define-key mpuz-mode-map "g" 'mpuz-try-letter)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
70 (define-key mpuz-mode-map "h" 'mpuz-try-letter)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
71 (define-key mpuz-mode-map "i" 'mpuz-try-letter)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
72 (define-key mpuz-mode-map "j" 'mpuz-try-letter)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
73 (define-key mpuz-mode-map "A" 'mpuz-try-letter)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
74 (define-key mpuz-mode-map "B" 'mpuz-try-letter)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
75 (define-key mpuz-mode-map "C" 'mpuz-try-letter)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
76 (define-key mpuz-mode-map "D" 'mpuz-try-letter)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
77 (define-key mpuz-mode-map "E" 'mpuz-try-letter)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
78 (define-key mpuz-mode-map "F" 'mpuz-try-letter)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
79 (define-key mpuz-mode-map "G" 'mpuz-try-letter)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
80 (define-key mpuz-mode-map "H" 'mpuz-try-letter)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
81 (define-key mpuz-mode-map "I" 'mpuz-try-letter)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
82 (define-key mpuz-mode-map "J" 'mpuz-try-letter)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
83 (define-key mpuz-mode-map "\C-g" 'mpuz-offer-abort)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
84 (define-key mpuz-mode-map "?" 'describe-mode))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
85
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
86 (defun mpuz-mode ()
1217
d0b19afef0ae Fix setup of mpuz-read-map not to depend on keymap format.
Richard M. Stallman <rms@gnu.org>
parents: 838
diff changeset
87 "Multiplication puzzle mode.
142
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
88
232
c0bd9c7f9c42 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 142
diff changeset
89 You have to guess which letters stand for which digits in the
1217
d0b19afef0ae Fix setup of mpuz-read-map not to depend on keymap format.
Richard M. Stallman <rms@gnu.org>
parents: 838
diff changeset
90 multiplication displayed inside the `*Mult Puzzle*' buffer.
142
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
91
1217
d0b19afef0ae Fix setup of mpuz-read-map not to depend on keymap format.
Richard M. Stallman <rms@gnu.org>
parents: 838
diff changeset
92 You may enter a guess for a letter's value by typing first the letter,
d0b19afef0ae Fix setup of mpuz-read-map not to depend on keymap format.
Richard M. Stallman <rms@gnu.org>
parents: 838
diff changeset
93 then the digit. Thus, to guess that A=3, type A 3.
142
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
94
1217
d0b19afef0ae Fix setup of mpuz-read-map not to depend on keymap format.
Richard M. Stallman <rms@gnu.org>
parents: 838
diff changeset
95 To leave the game to do other editing work, just switch buffers.
d0b19afef0ae Fix setup of mpuz-read-map not to depend on keymap format.
Richard M. Stallman <rms@gnu.org>
parents: 838
diff changeset
96 Then you may resume the game with M-x mpuz.
d0b19afef0ae Fix setup of mpuz-read-map not to depend on keymap format.
Richard M. Stallman <rms@gnu.org>
parents: 838
diff changeset
97 You may abort a game by typing \\<mpuz-mode-map>\\[mpuz-offer-abort]."
142
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
98 (interactive)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
99 (setq major-mode 'mpuz-mode
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
100 mode-name "Mult Puzzle")
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
101 (use-local-map mpuz-mode-map)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
102 (run-hooks 'mpuz-mode-hook))
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
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
105 ;; Some variables for statistics
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 (defvar mpuz-nb-errors 0
232
c0bd9c7f9c42 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 142
diff changeset
108 "Number of errors made in current game.")
142
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-nb-completed-games 0
232
c0bd9c7f9c42 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 142
diff changeset
111 "Number of games completed.")
142
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-nb-cumulated-errors 0
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
114 "Number of errors made in previous games.")
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
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
117 ;; Some variables for game tracking
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 (defvar mpuz-in-progress nil
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
120 "True if a game is currently in progress.")
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
121
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
122 (defvar mpuz-found-digits (make-vector 10 nil)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
123 "A vector recording which digits have been decrypted.")
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
124
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
125 (defmacro mpuz-digit-solved-p (digit)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
126 (list 'aref 'mpuz-found-digits digit))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
127
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 ;; A puzzle uses a permutation of [0..9] into itself.
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
130 ;; We use both the permutation and its inverse.
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 (defvar mpuz-digit-to-letter (make-vector 10 0)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
133 "A permutation from [0..9] to [0..9].")
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 (defvar mpuz-letter-to-digit (make-vector 10 0)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
136 "The inverse of mpuz-digit-to-letter.")
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
137
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
138 (defmacro mpuz-to-digit (letter)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
139 (list 'aref 'mpuz-letter-to-digit letter))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
140
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
141 (defmacro mpuz-to-letter (digit)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
142 (list 'aref 'mpuz-digit-to-letter digit))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
143
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
144 (defun mpuz-build-random-perm ()
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
145 "Initialize puzzle coding with a random permutation."
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
146 (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
147 (index 10)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
148 elem)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
149 (while letters
4400
a549089c0d15 (mpuz-random): Remove.
Paul Eggert <eggert@twinsun.com>
parents: 4345
diff changeset
150 (setq elem (nth (random index) letters)
142
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
151 letters (delq elem letters)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
152 index (1- index))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
153 (aset mpuz-digit-to-letter index elem)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
154 (aset mpuz-letter-to-digit elem index))))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
155
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
156
3591
507f64624555 Apply typo patches from Paul Eggert.
Jim Blandy <jimb@redhat.com>
parents: 2451
diff changeset
157 ;; A puzzle also uses a board displaying a multiplication.
142
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
158 ;; Every digit appears in the board, crypted or not.
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 (defvar mpuz-board (make-vector 10 nil)
4345
49e68bc65e26 * mpuz.el (mpuz-board): Doc fix.
Jim Blandy <jimb@redhat.com>
parents: 3591
diff changeset
161 "The board associates to any digit the list of squares where it appears.")
142
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
162
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
163 (defun mpuz-put-digit-on-board (number square)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
164 "Put (last digit of) NUMBER on SQUARE of the puzzle board."
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
165 ;; i.e. push SQUARE on NUMBER square-list
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
166 (setq number (% number 10))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
167 (aset mpuz-board number (cons square (aref mpuz-board number))))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
168
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
169 (defun mpuz-check-all-solved ()
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
170 "Check whether all digits have been solved. Return t if yes."
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
171 (catch 'found
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
172 (let ((digit -1))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
173 (while (> 10 (setq digit (1+ digit)))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
174 (if (and (not (mpuz-digit-solved-p digit)) ; unsolved
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
175 (aref mpuz-board digit)) ; and appearing in the puzzle !
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
176 (throw 'found nil))))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
177 t))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
178
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
179
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
180 ;; To build a puzzle, we take two random numbers and multiply them.
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
181 ;; We also take a random permutation for encryption.
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
182 ;; 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
183 ;; of the board. Everything is stored in individual squares.
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
184 ;;---------------------------------------------------------------------------
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
185 (defun mpuz-random-puzzle ()
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
186 "Draw random values to be multiplied in a puzzle."
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
187 (mpuz-build-random-perm)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
188 (fillarray mpuz-board nil) ; erase the board
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
189 (let (A B C D E)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
190 ;; A,B,C,D & E, are the five rows of our multiplication.
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
191 ;; Choose random values, discarding uninteresting cases.
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
192 (while (progn
4400
a549089c0d15 (mpuz-random): Remove.
Paul Eggert <eggert@twinsun.com>
parents: 4345
diff changeset
193 (setq A (random 1000)
a549089c0d15 (mpuz-random): Remove.
Paul Eggert <eggert@twinsun.com>
parents: 4345
diff changeset
194 B (random 100)
142
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
195 C (* A (% B 10))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
196 D (* A (/ B 10))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
197 E (* A B))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
198 (or (< C 1000) (< D 1000)))) ; forbid leading zeros in C or D
14040
187735b53d52 Comment fixes.
Karl Heuer <kwzh@gnu.org>
parents: 6736
diff changeset
199 ;; Individual digits are now put on their respective squares.
142
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
200 ;; [NB: A square is a pair <row,column> of the screen.]
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
201 (mpuz-put-digit-on-board A '(2 . 9))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
202 (mpuz-put-digit-on-board (/ A 10) '(2 . 7))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
203 (mpuz-put-digit-on-board (/ A 100) '(2 . 5))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
204 (mpuz-put-digit-on-board B '(4 . 9))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
205 (mpuz-put-digit-on-board (/ B 10) '(4 . 7))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
206 (mpuz-put-digit-on-board C '(6 . 9))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
207 (mpuz-put-digit-on-board (/ C 10) '(6 . 7))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
208 (mpuz-put-digit-on-board (/ C 100) '(6 . 5))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
209 (mpuz-put-digit-on-board (/ C 1000) '(6 . 3))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
210 (mpuz-put-digit-on-board D '(8 . 7))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
211 (mpuz-put-digit-on-board (/ D 10) '(8 . 5))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
212 (mpuz-put-digit-on-board (/ D 100) '(8 . 3))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
213 (mpuz-put-digit-on-board (/ D 1000) '(8 . 1))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
214 (mpuz-put-digit-on-board E '(10 . 9))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
215 (mpuz-put-digit-on-board (/ E 10) '(10 . 7))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
216 (mpuz-put-digit-on-board (/ E 100) '(10 . 5))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
217 (mpuz-put-digit-on-board (/ E 1000) '(10 . 3))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
218 (mpuz-put-digit-on-board (/ E 10000) '(10 . 1))))
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 ;; Display
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 (defconst mpuz-framework
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 . . .
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
225 Number of errors (this game): 0
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
226 x . .
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
227 -------
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
228 . . . .
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
229 Number of completed games: 0
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
230 . . . .
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
231 --------- Average number of errors: 0.00
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
232 . . . . ."
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
233 "The general picture of the puzzle screen, as a string.")
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
234
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
235 (defun mpuz-create-buffer ()
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
236 "Create (or recreate) the puzzle buffer. Return it."
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
237 (let ((buff (get-buffer-create "*Mult Puzzle*")))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
238 (save-excursion
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
239 (set-buffer buff)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
240 (let ((buffer-read-only nil))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
241 (erase-buffer)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
242 (insert mpuz-framework)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
243 (mpuz-paint-board)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
244 (mpuz-paint-errors)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
245 (mpuz-paint-statistics)))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
246 buff))
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-errors ()
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
249 "Paint error count on the puzzle screen."
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
250 (mpuz-switch-to-window)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
251 (let ((buffer-read-only nil))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
252 (goto-line 3)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
253 (move-to-column 49)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
254 (mpuz-delete-line)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
255 (insert (prin1-to-string mpuz-nb-errors))))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
256
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
257 (defun mpuz-paint-statistics ()
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
258 "Paint statistics about previous games on the puzzle screen."
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
259 (let* ((mean (if (zerop mpuz-nb-completed-games) 0
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
260 (/ (+ mpuz-nb-completed-games (* 200 mpuz-nb-cumulated-errors))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
261 (* 2 mpuz-nb-completed-games))))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
262 (frac-part (% mean 100)))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
263 (let ((buffer-read-only nil))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
264 (goto-line 7)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
265 (move-to-column 51)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
266 (mpuz-delete-line)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
267 (insert (prin1-to-string mpuz-nb-completed-games))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
268 (goto-line 9)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
269 (move-to-column 50)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
270 (mpuz-delete-line)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
271 (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
272
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
273 (defun mpuz-paint-board ()
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
274 "Paint board situation on the puzzle screen."
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
275 (mpuz-switch-to-window)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
276 (let ((letter -1))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
277 (while (> 10 (setq letter (1+ letter)))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
278 (mpuz-paint-digit (mpuz-to-digit letter))))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
279 (goto-char (point-min)))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
280
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
281 (defun mpuz-paint-digit (digit)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
282 "Paint all occurrences of DIGIT on the puzzle board."
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
283 ;; (mpuz-switch-to-window)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
284 (let ((char (if (mpuz-digit-solved-p digit)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
285 (+ digit ?0)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
286 (+ (mpuz-to-letter digit) ?A)))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
287 (square-l (aref mpuz-board digit)))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
288 (let ((buffer-read-only nil))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
289 (while square-l
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
290 (goto-line (car (car square-l))) ; line before column !
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
291 (move-to-column (cdr (car square-l)))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
292 (insert char)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
293 (delete-char 1)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
294 (backward-char 1)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
295 (setq square-l (cdr square-l))))))
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-delete-line ()
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
298 "Clear from point to next newline." ; & put nothing in the kill ring
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
299 (while (not (= ?\n (char-after (point))))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
300 (delete-char 1)))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
301
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
302 (defun mpuz-get-buffer ()
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
303 "Get the puzzle buffer if it exists."
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
304 (get-buffer "*Mult Puzzle*"))
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 (defun mpuz-switch-to-window ()
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
307 "Find or create the Mult-Puzzle buffer, and display it."
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
308 (let ((buff (mpuz-get-buffer)))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
309 (or buff (setq buff (mpuz-create-buffer)))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
310 (switch-to-buffer buff)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
311 (or buffer-read-only (toggle-read-only))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
312 (mpuz-mode)))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
313
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
314
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
315 ;; Game control
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-abort-game ()
3591
507f64624555 Apply typo patches from Paul Eggert.
Jim Blandy <jimb@redhat.com>
parents: 2451
diff changeset
318 "Abort any puzzle in progress."
142
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
319 (message "Mult Puzzle aborted.")
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
320 (setq mpuz-in-progress nil
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
321 mpuz-nb-errors 0)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
322 (fillarray mpuz-board nil)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
323 (let ((buff (mpuz-get-buffer)))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
324 (if buff (kill-buffer buff))))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
325
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
326 (defun mpuz-start-new-game ()
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
327 "Start a new puzzle."
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
328 (message "Here we go...")
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
329 (setq mpuz-nb-errors 0
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
330 mpuz-in-progress t)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
331 (fillarray mpuz-found-digits nil) ; initialize mpuz-found-digits
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
332 (mpuz-random-puzzle)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
333 (mpuz-switch-to-window)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
334 (mpuz-paint-board)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
335 (mpuz-paint-errors)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
336 (mpuz-ask-for-try))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
337
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
338 (defun mpuz-offer-new-game ()
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
339 "Ask if user wants to start a new puzzle."
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
340 (if (y-or-n-p "Start a new game ")
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
341 (mpuz-start-new-game)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
342 (message "OK. I won't.")))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
343
1217
d0b19afef0ae Fix setup of mpuz-read-map not to depend on keymap format.
Richard M. Stallman <rms@gnu.org>
parents: 838
diff changeset
344 ;;;###autoload
d0b19afef0ae Fix setup of mpuz-read-map not to depend on keymap format.
Richard M. Stallman <rms@gnu.org>
parents: 838
diff changeset
345 (defun mpuz ()
142
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
346 "Multiplication puzzle with GNU Emacs."
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
347 ;; Main entry point
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
348 (interactive)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
349 (mpuz-switch-to-window)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
350 (if mpuz-in-progress
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
351 (mpuz-offer-abort)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
352 (mpuz-start-new-game)))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
353
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
354 (defun mpuz-offer-abort ()
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
355 "Ask if user wants to abort current puzzle."
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
356 (interactive)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
357 (if (y-or-n-p "Abort game ")
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
358 (mpuz-abort-game)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
359 (mpuz-ask-for-try)))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
360
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
361 (defun mpuz-ask-for-try ()
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
362 "Ask for user proposal in puzzle."
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
363 (message "Your try ?"))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
364
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
365 (defun mpuz-try-letter ()
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
366 "Propose a digit for a letter in puzzle."
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
367 (interactive)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
368 (if mpuz-in-progress
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
369 (let (letter-char digit digit-char message)
2451
a149f1464f40 (mpuz-try-letter): Use read-char to read digit.
Richard M. Stallman <rms@gnu.org>
parents: 2308
diff changeset
370 (setq letter-char (upcase last-command-char)
142
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
371 digit (mpuz-to-digit (- letter-char ?A)))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
372 (cond ((mpuz-digit-solved-p digit)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
373 (message "%c already solved." letter-char))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
374 ((null (aref mpuz-board digit))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
375 (message "%c does not appear." letter-char))
2451
a149f1464f40 (mpuz-try-letter): Use read-char to read digit.
Richard M. Stallman <rms@gnu.org>
parents: 2308
diff changeset
376 ((progn (message "%c = " letter-char)
142
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
377 ;; <char> has been entered.
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
378 ;; Print "<char> =" and
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
379 ;; read <num> or = <num>
2451
a149f1464f40 (mpuz-try-letter): Use read-char to read digit.
Richard M. Stallman <rms@gnu.org>
parents: 2308
diff changeset
380 (setq digit-char (read-char))
a149f1464f40 (mpuz-try-letter): Use read-char to read digit.
Richard M. Stallman <rms@gnu.org>
parents: 2308
diff changeset
381 (if (eq digit-char ?=)
a149f1464f40 (mpuz-try-letter): Use read-char to read digit.
Richard M. Stallman <rms@gnu.org>
parents: 2308
diff changeset
382 (setq digit-char (read-char)))
142
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
383 (message "%c = %c" letter-char digit-char)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
384 (or (> digit-char ?9) (< digit-char ?0))) ; bad input
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
385 (ding t))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
386 (t
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
387 (mpuz-try-proposal letter-char digit-char))))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
388 (mpuz-offer-new-game)))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
389
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
390 (defun mpuz-try-proposal (letter-char digit-char)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
391 "Propose LETTER-CHAR as code for DIGIT-CHAR."
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
392 (let* ((letter (- letter-char ?A))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
393 (digit (- digit-char ?0))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
394 (correct-digit (mpuz-to-digit letter)))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
395 (cond ((mpuz-digit-solved-p correct-digit)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
396 (message "%c has already been found."))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
397 ((= digit correct-digit)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
398 (message "%c = %c correct !" letter-char digit-char)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
399 (mpuz-ding)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
400 (mpuz-correct-guess digit))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
401 (t ;;; incorrect guess
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
402 (message "%c = %c incorrect !" letter-char digit-char)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
403 (mpuz-ding)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
404 (setq mpuz-nb-errors (1+ mpuz-nb-errors))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
405 (mpuz-paint-errors)))))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
406
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
407 (defun mpuz-correct-guess (digit)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
408 "Handle correct guessing of DIGIT."
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
409 (aset mpuz-found-digits digit t) ; Mark digit as solved
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
410 (mpuz-paint-digit digit) ; Repaint it (now as a digit)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
411 (if (mpuz-check-all-solved)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
412 (mpuz-close-game)))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
413
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
414 (defun mpuz-close-game ()
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
415 "Housecleaning when puzzle has been solved."
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
416 (setq mpuz-in-progress nil
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
417 mpuz-nb-cumulated-errors (+ mpuz-nb-cumulated-errors mpuz-nb-errors)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
418 mpuz-nb-completed-games (1+ mpuz-nb-completed-games))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
419 (mpuz-paint-statistics)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
420 (let ((message (mpuz-congratulate)))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
421 (message message)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
422 (sit-for 4)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
423 (if (y-or-n-p (concat message " Start a new game "))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
424 (mpuz-start-new-game)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
425 (message "Good Bye !"))))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
426
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
427 (defun mpuz-congratulate ()
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
428 "Build a congratulation message when puzzle is solved."
18314
6e13dd3d1e11 (mpuz-congratulate): Don't say "1 errors".
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
429 (format "Puzzle solved with %d error%s. %s"
142
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
430 mpuz-nb-errors
18314
6e13dd3d1e11 (mpuz-congratulate): Don't say "1 errors".
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
431 (if (= mpuz-nb-errors 1) "" "s")
142
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
432 (cond ((= mpuz-nb-errors 0) "That's perfect !")
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
433 ((= mpuz-nb-errors 1) "That's very good !")
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
434 ((= mpuz-nb-errors 2) "That's good.")
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
435 ((= mpuz-nb-errors 3) "That's not bad.")
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
436 ((= mpuz-nb-errors 4) "That's not too bad...")
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
437 ((and (>= mpuz-nb-errors 5)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
438 (< mpuz-nb-errors 10)) "That's bad !")
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
439 ((and (>= mpuz-nb-errors 10)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
440 (< mpuz-nb-errors 15)) "That's awful.")
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
441 ((>= mpuz-nb-errors 15) "That's not serious."))))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
442
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
443 (defun mpuz-show-solution ()
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
444 "Display solution for debugging purposes."
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
445 (interactive)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
446 (mpuz-switch-to-window)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
447 (let (digit list)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
448 (setq digit -1)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
449 (while (> 10 (setq digit (1+ digit)))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
450 (or (mpuz-digit-solved-p digit)
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
451 (setq list (cons digit list))))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
452 (mapcar 'mpuz-correct-guess list)))
df6ec648915f Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
453
18383
11218164bc54 Add provide call.
Richard M. Stallman <rms@gnu.org>
parents: 18314
diff changeset
454 (provide 'mpuz)
11218164bc54 Add provide call.
Richard M. Stallman <rms@gnu.org>
parents: 18314
diff changeset
455
659
505130d1ddf8 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 232
diff changeset
456 ;;; mpuz.el ends here