22
|
1 ;;; mine.el --- Mine game for GNU Emacs
|
|
2
|
|
3 ;; Author: Jacques Duthen <duthen@cegelec-red.fr>
|
|
4 ;; Keywords: games
|
|
5 ;; Time-stamp: <97/01/20 14:37:36 duthen>
|
|
6 ;; Version: 1.17
|
|
7
|
|
8 (defconst mine-version-number "1.17" "Emacs Mine version number.")
|
110
|
9 (defconst mine-version (format "XEmacs Mine v%sx by Jacques Duthen © 1997"
|
22
|
10 mine-version-number)
|
|
11 "Full Emacs Mine version number.")
|
|
12
|
|
13 ;; This file is not yet part of GNU Emacs.
|
|
14
|
|
15 ;; GNU Emacs is free software; you can redistribute it and/or modify
|
|
16 ;; it under the terms of the GNU General Public License as published by
|
|
17 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
18 ;; any later version.
|
|
19
|
|
20 ;; GNU Emacs is distributed in the hope that it will be useful,
|
|
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
23 ;; GNU General Public License for more details.
|
|
24
|
|
25 ;; You should have received a copy of the GNU General Public License
|
|
26 ;; along with GNU Emacs; see the file COPYING. If not, write to the
|
|
27 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
28 ;; Boston, MA 02111-1307, USA.
|
|
29
|
|
30 ;;; Commentary:
|
|
31
|
|
32 ;; The object of this classical game is to locate the hidden mines.
|
|
33 ;; To do this, you hit the squares on the game board that do not
|
|
34 ;; contain mines, and you mark the squares that do contain mines.
|
|
35
|
|
36 ;; The number of hidden mines remaining in the mine field is indicated
|
|
37 ;; inside the buffer. Every time you mark a square as a mine, this
|
|
38 ;; number decreases by one, even if you incorrectly mark a square.
|
|
39
|
|
40 ;; To hit a square: Point to the square, and click the left button.
|
28
|
41 ;; If the square is a mine, you lose.
|
22
|
42 ;; If the square isn't a mine, a number appears, which represents
|
|
43 ;; the number of mines in the surrounding eight squares.
|
|
44
|
|
45 ;; To mark a square as a mine: Point to the square, and click
|
|
46 ;; the right button.
|
|
47
|
|
48 ;; To play Mine, compile it if you want, load it, and type `M-x mine'.
|
|
49
|
|
50 ;; To get help and doc, see the functions `mine' and `mine-help'
|
|
51 ;; (ie. type `?' in the *Mine* buffer or type `C-h f mine')
|
|
52
|
|
53 ;; This module has been developed and tested with GNU Emacs 19.31.1,
|
|
54 ;; but it should run with any GNU Emacs 19.* (at least with versions
|
|
55 ;; superior to 19.31).
|
|
56
|
|
57 ;; This module has not been tested (yet) with XEmacs. It may or may
|
|
58 ;; not run (can anybody tell me?).
|
|
59
|
|
60 ;; Send any comment or bug report (do you expect to find any? ;-) to me:
|
|
61 ;; duthen@cegelec-red.fr (Jacques Duthen)
|
|
62
|
|
63 ;; Good luck.
|
|
64
|
|
65 ;; 1.17 Thanks to Vladimir Alexiev <vladimir@cs.ualberta.ca>.
|
|
66 ;; Fix bug: (void-function unless), add minimal support for xemacs.
|
|
67 ;; (mine-xemacs-p): Added.
|
|
68 ;; (event-point): New function.
|
|
69 ;; (mine-mouse-hit, mine-mouse-mark): Use (interactive "@e") and `event-point'
|
|
70 ;; (mine-init-mode-map): Support xemacs mouse binding.
|
|
71 ;; (mine-make-face): Support xemacs get-face.
|
|
72 ;; (mine-goto): Support `auto-show-make-point-visible' as well as
|
|
73 ;; `hscroll-point-visible'.
|
|
74
|
|
75 ;; 1.16 Initial released version.
|
|
76
|
|
77 ;;; Code:
|
|
78
|
|
79 (defvar mine-xemacs-p (string-match "XEmacs\\|Lucid" emacs-version))
|
|
80
|
|
81 ;;; ================================================================
|
|
82 ;;; User Variables:
|
|
83
|
|
84 ;;; -1- size
|
|
85
|
|
86 ;;; The mine field is a rectangle (mine-xmax x mine-ymax), which is
|
|
87 ;;; duplicated to fill a bigger rectangle periodically tiled with the
|
|
88 ;;; smaller one, the period being (mine-xmax x mine-ymax).
|
|
89
|
|
90 (defvar mine-xmax 16 "*The logical width of the mine field.")
|
|
91 (defvar mine-ymax 24 "*The logical height of the mine field.")
|
|
92
|
|
93 (defvar mine-mines-% 16
|
|
94 "*Percentage (between 0 and 100) of mines in the mine field.")
|
|
95
|
28
|
96 (defvar mine-torus (not mine-xemacs-p)
|
|
97 "*Non-nil to play the game on a periodic board (a torus).
|
|
98 This is the default unless using graphics (XEmacs)")
|
22
|
99
|
|
100 (defvar mine-nb-tiles-x 2
|
|
101 "*Number of duplications in the x direction, when `mine-torus' is non-nil.
|
|
102 Indicate the number of times the original mine field is duplicated
|
|
103 in the x direction.
|
|
104 It's better looking when it's an integer.
|
|
105 nil means fill exactly the whole window.
|
|
106 0 means fill the whole window with the biggest integer that fits.
|
|
107 a negative number means use exactly the opposite number. If it's
|
|
108 too big, the rows are truncated by emacs. Automatic horizontal
|
|
109 scrolling will occur if you move to an invisible point.
|
|
110 a positive float means limit to the window width if needed.
|
|
111 a positive integer means limit to the window width if needed,
|
|
112 with the biggest possible integer value anyway.
|
|
113 ")
|
|
114
|
|
115 (defvar mine-nb-tiles-y 2
|
|
116 "*Number of duplications in the y direction, when `mine-torus' is non-nil.
|
|
117 Indicate the number of times the original mine field is duplicated
|
|
118 in the y direction.
|
|
119 It's better looking when it's an integer.
|
|
120 nil means fill exactly the whole window.
|
|
121 0 means fill the whole window with the biggest integer that fits.
|
|
122 a negative number means use exactly the opposite number. If it's
|
|
123 too big, the rows will be simply scrolled up or down by emacs.
|
|
124 a positive float means limit to the window height if needed.
|
|
125 a positive integer means limit to the window height if needed,
|
|
126 with the biggest possible integer value anyway.
|
|
127 ")
|
|
128
|
|
129 ;;; -2- square characters
|
|
130
|
|
131 ;;; All these characters may be changed but the first three ones
|
|
132 ;;; `unmarked' `marked' `zero' must differ from each other.
|
|
133
|
|
134 (defvar mine-char-unmarked ?-
|
|
135 "*Character for a square not yet marked nor hit.")
|
|
136 (defvar mine-char-marked ?@
|
|
137 "*Character for a square marked as containing a mine.")
|
|
138 (defvar mine-char-zero ?\
|
|
139 "*Character for a square hit with no adjacent mine.")
|
|
140
|
|
141 (defvar mine-char-pad ?\
|
|
142 "*Character to pad in the x direction or nil (not yet implemented).")
|
|
143 (defvar mine-char-not-found ?o
|
|
144 "*Character for a square marked but with no mine.")
|
|
145 (defvar mine-char-bogus ?x
|
|
146 "*Character for a square not marked but with a mine.")
|
|
147
|
|
148 ;;; -3- colors
|
|
149
|
|
150 (defvar mine-colorp (if window-system 't 'nil)
|
|
151 "*Non-nil means with colors. Nil means in black and white.")
|
|
152
|
|
153 (defvar mine-colors nil
|
|
154 "*Set this variable to override the colors defined by
|
|
155 `mine-default-colors' (use the same format).")
|
|
156
|
|
157 (defconst mine-default-colors
|
|
158 '((mine-face-unmarked . "LightBlue")
|
|
159 (mine-face-marked . "Red")
|
|
160 (0 . nil)
|
|
161 (1 . "Cyan")
|
|
162 (2 . "Green")
|
|
163 (3 . "Yellow")
|
|
164 (4 . "Orange")
|
|
165 (5 . "OrangeRed")
|
|
166 (6 . "Red")
|
|
167 (7 . "Red")
|
|
168 (8 . "Red")
|
|
169 (mine-face-pad . nil)
|
|
170 (mine-face-not-found . "Red")
|
|
171 (mine-face-bogus . "Red")
|
|
172 )
|
|
173 "A-list of default colors for Mine faces. Don't change its value.
|
|
174 You can override these settings with `mine-colors' using the same format.")
|
|
175
|
|
176 ;;; -4- redisplay
|
|
177
|
|
178 (defvar mine-level 2
|
|
179 "*Redisplay speed. 0 is the slowest redisplay, 5 is the fastest one.
|
|
180 0 means redisplay when every single square changes.
|
|
181 1 means redisplay when one square and its periodic images change.
|
|
182 2 means redisplay every `mine-count1-max' change.
|
|
183 3 means redisplay every `mine-count1-max'*`mine-count2-max' change.
|
|
184 -1 or nil means redisplay only when all the changes are done.
|
|
185 ")
|
|
186
|
|
187 (defvar mine-count1-max 16
|
|
188 "*See `mine-level'.
|
|
189 Redisplay when the number of empty squares which have changed
|
|
190 is greater than `mine-count1-max'.
|
|
191 8 means redisplay each time 8 squares have been changed.
|
|
192 -1 means redisplay only when all the changes are done.")
|
|
193
|
|
194 (defvar mine-count2-max 4
|
|
195 "*See `mine-level'.
|
|
196 Redisplay when the number of empty squares which have changed
|
|
197 is greater than `mine-count1-max'.
|
|
198 8 means redisplay each time 8 squares have been changed.
|
|
199 -1 means redisplay only when all the changes are done.")
|
|
200
|
|
201 (defvar mine-hscroll-step 4
|
|
202 "*Local value for `hscroll-step'")
|
|
203
|
|
204 (defvar mine-mode-hook nil
|
|
205 "*Hook called by `mine-mode-hook'.")
|
|
206
|
|
207 ;;; ================================================================
|
|
208 ;;; Internal variables:
|
|
209
|
28
|
210 ;; XEmacs stuffs
|
|
211 (defvar mine-glyph-directory (concat data-directory "mine")
|
|
212 "Directory where mine glyphs are kept.")
|
|
213 (defun mine-make-glyph (file)
|
|
214 (when mine-xemacs-p
|
|
215 (make-glyph (list (cons 'x
|
|
216 (expand-file-name file mine-glyph-directory))))))
|
|
217 (defvar mine-default-glyphs
|
|
218 `((mine-face-unmarked . ,(mine-make-glyph "empty_16_up.gif"))
|
|
219 (mine-face-marked . ,(mine-make-glyph "flagged_16_up.gif"))
|
|
220 (0 . ,(mine-make-glyph "empty_16_flat.gif"))
|
|
221 (1 . ,(mine-make-glyph "1_16_flat.gif"))
|
|
222 (2 . ,(mine-make-glyph "2_16_flat.gif"))
|
|
223 (3 . ,(mine-make-glyph "3_16_flat.gif"))
|
|
224 (4 . ,(mine-make-glyph "4_16_flat.gif"))
|
|
225 (5 . ,(mine-make-glyph "5_16_flat.gif"))
|
|
226 (6 . ,(mine-make-glyph "6_16_flat.gif"))
|
|
227 (7 . ,(mine-make-glyph "7_16_flat.gif"))
|
|
228 (8 . ,(mine-make-glyph "8_16_flat.gif"))
|
|
229 (mine-face-pad . ,(mine-make-glyph "empty_16_down.gif"))
|
|
230 (mine-face-not-found . ,(mine-make-glyph "bomb_16_flat.gif"))
|
|
231 (mine-face-bogus . ,(mine-make-glyph "question_16_up.gif"))
|
|
232 )
|
|
233 "A-list of default graphics for various mine characters. Unless you
|
|
234 have an entire replacement set of graphics I wouldn't suggest changing it.")
|
|
235
|
22
|
236 (defvar mine-user-variables
|
|
237 '("Size"
|
|
238 mine-xmax mine-ymax mine-mines-%
|
|
239 mine-torus mine-nb-tiles-x mine-nb-tiles-y
|
|
240 "Square characters"
|
|
241 mine-char-unmarked mine-char-marked mine-char-zero
|
|
242 mine-char-pad mine-char-not-found mine-char-bogus
|
|
243 "Colors"
|
|
244 mine-colorp mine-colors
|
|
245 "Redisplay"
|
|
246 mine-level mine-count1-max mine-count2-max
|
|
247 "Scrolling"
|
|
248 mine-hscroll-step
|
|
249 "Hook"
|
|
250 mine-mode-hook))
|
|
251
|
|
252 (defvar mine-user-commands
|
|
253 '("Help"
|
|
254 mine mine-help mine-help-bindings mine-help-variables
|
|
255 "Mouse control"
|
|
256 mine-mouse-hit mine-mouse-mark
|
|
257 "Move"
|
|
258 mine-left mine-right mine-up mine-down
|
|
259 mine-bol mine-eol mine-top mine-bottom
|
|
260 "Hit and mark"
|
|
261 mine-hit-curpoint mine-mark-curpoint
|
|
262 "Quit"
|
|
263 mine-quit))
|
|
264
|
|
265 ;; pad x factor == (if mine-char-pad 2 1)
|
|
266 (defvar mine-padx*)
|
|
267
|
|
268 (defvar mine-width)
|
|
269 (defvar mine-height)
|
|
270
|
|
271 ;; (x y) of current point
|
|
272 (defvar mine-x) ;; 1 <= mine-x <= mine-width
|
|
273 (defvar mine-y) ;; 1 <= mine-y <= mine-height
|
|
274
|
|
275 ;; limits of the playable part of the board
|
|
276 (defvar mine-point-min)
|
|
277 (defvar mine-point-max)
|
|
278
|
|
279 (defvar mine-point-remaining-mines)
|
|
280 (defvar mine-point-mines-hit)
|
|
281
|
|
282 (defvar mine-mode-map nil)
|
|
283
|
|
284 (defvar mine-real-mines)
|
|
285
|
|
286 (defvar mine-nb-remaining-mines)
|
|
287 (defvar mine-nb-remaining-marks)
|
|
288 (defvar mine-nb-mines-hit)
|
|
289
|
|
290 (defvar mine-faces)
|
|
291
|
|
292 ;;; This variable is more special rather than global.
|
|
293 (defvar mine-adjacent-points)
|
|
294
|
|
295 (defvar mine-count1)
|
|
296 (defvar mine-count2)
|
|
297
|
|
298 ;;; ================================================================
|
|
299 ;;; Macros (stolen from "cl.el" (soon in "subr.el" (thanks to rms)))
|
|
300
|
|
301 (eval-when-compile
|
|
302 (or (fboundp 'when)
|
|
303 (defmacro when (cond &rest body)
|
|
304 "(when COND BODY...): if COND yields non-nil, do BODY, else return nil."
|
|
305 (list 'if cond (cons 'progn body)))))
|
|
306
|
|
307 ;;; ================================================================
|
|
308 ;;; User commands
|
|
309
|
|
310 ;;;###autoload
|
|
311 (defun mine (num)
|
|
312 "Play Mine. Optional prefix argument is the number of mines.
|
|
313
|
|
314 To play Mine, type `\\[mine]' or `\\[universal-argument] NUM \\[mine]'.
|
|
315
|
|
316 An optional prefix argument specifies the number of mines to be hidden
|
|
317 in the field. If no prefix argument is given, a percentage
|
|
318 `mine-mines-%' of the field will contain mines.
|
|
319
|
|
320 What is Mine?\\<mine-mode-map>
|
|
321
|
|
322 Mine is a classical game of hide and seek played on a rectangular grid
|
|
323 containing `mine-xmax' by `mine-ymax' squares (the mine field).
|
|
324
|
|
325 Your opponent (Emacs, in this case) has hidden several mines within
|
|
326 this field. The object of the game is to find every hidden mine.
|
|
327
|
|
328 When you're sure a square does NOT contain a mine, you can hit it:
|
|
329 move the mouse over the square and press `\\[mine-mouse-hit]' or
|
|
330 move the cursor with the usual keys and press `\\[mine-hit-curpoint]'.
|
|
331
|
28
|
332 If the square is a mine, you lose.
|
22
|
333 If the square isn't a mine, a number appears which represents
|
|
334 the number of mines in the surrounding eight squares.
|
|
335
|
|
336 When you think a square DOES contain a mine, you can mark it:
|
|
337 move the mouse over the square and press `\\[mine-mouse-mark]' or
|
|
338 move the cursor with the usual keys and press `\\[mine-mark-curpoint]'.
|
|
339
|
|
340 The number of hidden mines remaining in the mine field is indicated
|
|
341 inside the buffer. Every time you mark a square as a mine, this
|
|
342 number decreases by one, even if you incorrectly mark a square.
|
|
343
|
|
344 If `mine-torus' is non-nil (the default), the Mine game is played over
|
|
345 a periodic field (like a torus). Each mine is hidden periodically
|
|
346 over the mine board `mine-nb-tiles-x' times in the x direction and
|
|
347 `mine-nb-tiles-y' times in the y direction.
|
|
348
|
|
349 If `mine-colorp' is non-nil (the default, if the system allows it),
|
|
350 the game is displayed with colors. The colors can be chosen with the
|
|
351 variable `mine-colors'.
|
|
352
|
|
353 If the redisplay is not fast enough, increase `mine-level'. If you
|
|
354 want to see a smoother (slower) redisplay, decrease `mine-level',
|
|
355 `mine-count1-max' and `mine-count2-max'.
|
|
356
|
|
357 You can get help on `mine-mode' and its key bindings by pressing `\\[mine-help]'
|
|
358 while in the *Mine* buffer.
|
|
359 "
|
|
360 (interactive "P")
|
|
361 (switch-to-buffer "*Mine*")
|
|
362 (mine-mode)
|
|
363 (setq buffer-read-only 't)
|
|
364 (buffer-disable-undo (current-buffer))
|
|
365 (setq mine-nb-remaining-mines
|
|
366 (or num (round (/ (* mine-xmax mine-ymax mine-mines-%) 100)))
|
|
367 mine-nb-remaining-marks mine-nb-remaining-mines)
|
|
368 (if (> mine-nb-remaining-mines (* mine-xmax mine-ymax))
|
|
369 (error "Too many mines: %d" mine-nb-remaining-mines))
|
|
370 (mine-init-faces)
|
|
371 (setq mine-real-mines (mine-init-mines mine-nb-remaining-mines))
|
|
372 (setq mine-nb-mines-hit 0)
|
|
373 (mine-init-board)
|
|
374 (mine-reset-counters)
|
|
375 (mine-update-remaining-mines)
|
|
376 (setq hscroll-step mine-hscroll-step)
|
|
377 ;; initial position
|
|
378 (setq mine-x 1)
|
|
379 (setq mine-y 1)
|
|
380 (mine-goto mine-x mine-y)
|
|
381 )
|
|
382
|
|
383 ;; Mine mode is suitable only for specially formatted data.
|
|
384 (put 'mine-mode 'mode-class 'special)
|
|
385
|
|
386 (defun mine-mode ()
|
|
387 "Major mode for playing Mine. To learn how to play Mine, see `mine'.
|
|
388
|
|
389 If you have a mouse, you can do:\\<mine-mode-map>
|
|
390
|
|
391 `\\[mine-mouse-hit]' -- hit point
|
|
392 `\\[mine-mouse-mark]' -- mark or unmark a mine at point
|
|
393
|
|
394 If you don't have a mouse, you can move the cursor over the mine
|
|
395 field with the usual mnemonic keys and:
|
|
396
|
|
397 `\\[mine-hit-curpoint]' -- hit point
|
|
398 `\\[mine-mark-curpoint]' -- mark or unmark a mine at point
|
|
399
|
|
400 `\\[mine-quit]' -- give up and see the hidden mines
|
|
401
|
|
402 You can get help with:
|
|
403
|
|
404 `\\[mine-help-variables]' -- get help on Mine variables
|
|
405 `\\[mine-help-bindings]' -- get help on Mine bindings
|
|
406
|
|
407 \\{mine-mode-map}
|
|
408 "
|
|
409 (interactive)
|
|
410 (kill-all-local-variables)
|
|
411 (make-local-variable 'hscroll-step)
|
|
412 (use-local-map mine-mode-map)
|
|
413 (setq truncate-lines 't)
|
|
414 (setq major-mode 'mine-mode)
|
|
415 (setq mode-name "Mine")
|
|
416 (run-hooks 'mine-mode-hook)
|
|
417 )
|
|
418
|
|
419 ;;;###autoload
|
|
420 (defun mine-version ()
|
|
421 "Return string describing the current version of Mine.
|
|
422 When called interactively, displays the version."
|
|
423 (interactive)
|
|
424 (if (interactive-p)
|
|
425 (message (mine-version))
|
|
426 mine-version))
|
|
427
|
|
428 ;;;###autoload
|
|
429 (defun mine-help ()
|
|
430 "*Get help on `mine-mode'."
|
|
431 (interactive)
|
|
432 (save-excursion
|
|
433 (switch-to-buffer "*Mine*")
|
|
434 (mine-mode)
|
|
435 (describe-mode)))
|
|
436
|
|
437 (defun mine-help-variables ()
|
|
438 "*Get help on Mine variables."
|
|
439 (interactive)
|
|
440 (save-excursion
|
|
441 (switch-to-buffer "*Mine*")
|
|
442 (mine-mode)
|
|
443 (apropos-symbols mine-user-variables 't)))
|
|
444
|
|
445 (defun mine-help-bindings ()
|
|
446 "*Get help on Mine bindings."
|
|
447 (interactive)
|
|
448 (save-excursion
|
|
449 (switch-to-buffer "*Mine*")
|
|
450 (mine-mode)
|
|
451 (apropos-symbols mine-user-commands 't)))
|
|
452
|
|
453 (defun mine-print-settings ()
|
|
454 "*Print the current Mine settings (value of all the user variables)."
|
|
455 (interactive)
|
|
456 (with-output-to-temp-buffer "*scratch*"
|
|
457 (mine-print-variables mine-user-variables)))
|
|
458
|
|
459 ;;; ================================================================
|
|
460 ;;; Click events - nop hit mark
|
|
461
|
|
462 ;;; [jack] The elisp manual says:
|
|
463 ;;; If you want to take action as soon as a button is pressed,
|
|
464 ;;; you need to handle "button-down" events.
|
|
465 ;;; The global map (cf. `mouse.el') has, by default, the binding:
|
|
466 ;;; (define-key global-map [down-mouse-1] 'mouse-drag-region)
|
|
467 ;;; It seems that this function "eats" the final event [mouse-1].
|
|
468 ;;; So, we need a local binding for [down-mouse-1] which shadows
|
|
469 ;;; the global one and prevents `mouse-drag-region' from being called.
|
|
470 ;;; Hence, in `mine-init-mode-map' I use the following binding:
|
|
471 ;;; (define-key mine-mode-map [down-mouse-1] 'mine-mouse-nop)
|
|
472 ;;; I found a better binding in "apropos.el"
|
|
473 ;;; (define-key mine-mode-map [down-mouse-1] nil)
|
|
474 ;;; but, as it does not work, let's go back to nop...
|
|
475
|
|
476 (or (fboundp 'event-point)
|
|
477 (defun event-point (event)
|
|
478 (posn-point (event-end event))))
|
|
479
|
|
480 (defun mine-mouse-nop (event)
|
|
481 "Nop"
|
|
482 (interactive "e"))
|
|
483
|
|
484 (defun mine-mouse-hit (event)
|
|
485 "Move point to the position clicked on with the mouse and hit this point."
|
|
486 (interactive "@e")
|
110
|
487 (if (mine-goto-point (event-closest-point event))
|
22
|
488 (mine-hit-curpoint)
|
|
489 (mine-message 'mine-msg-click-precisely)))
|
|
490
|
|
491 (defun mine-mouse-mark (event)
|
|
492 "Move point to the position clicked on with the mouse and mark or unmark
|
|
493 this point."
|
|
494 (interactive "@e")
|
|
495 (if (mine-goto-point (event-point event))
|
|
496 (mine-mark-curpoint)
|
|
497 (mine-message 'mine-msg-click-precisely)))
|
|
498
|
|
499 ;;; ================================================================
|
|
500 ;;; Key events - hit mark quit
|
|
501
|
|
502 (defun mine-hit-curpoint ()
|
|
503 "Hit point"
|
|
504 (interactive)
|
|
505 (mine-reset-counters)
|
|
506 (let ((c (following-char)))
|
|
507 (save-excursion
|
|
508 (cond
|
|
509 ((eq c mine-char-marked)
|
|
510 (mine-message 'mine-msg-unmark-before-hit))
|
|
511 ((not (eq c mine-char-unmarked))
|
|
512 (mine-message 'mine-msg-point-already-hit))
|
|
513 ((mine-mine-at-point-p (point) 'slowp)
|
|
514 (setq mine-nb-mines-hit (1+ mine-nb-mines-hit))
|
|
515 (mine-update-mines-hit)
|
28
|
516 (mine-message 'mine-msg-lose)
|
22
|
517 (mine-quit))
|
|
518 (t ;; the real job...
|
|
519 (let* ((x.y (mine-top-left (mine-point-to-x.y (point))))
|
|
520 (pxy (cons (point) x.y))
|
|
521 (mine-adjacent-points (list pxy))) ; special variable
|
|
522 (while mine-adjacent-points
|
|
523 (setq pxy (car mine-adjacent-points)
|
|
524 mine-adjacent-points (cdr mine-adjacent-points))
|
|
525 (mine-deep-hit pxy))))))))
|
|
526
|
|
527 (defun mine-mark-curpoint ()
|
|
528 "Mark or unmark current position"
|
|
529 (interactive)
|
|
530 (mine-reset-counters)
|
|
531 (let ((c (following-char)))
|
|
532 (save-excursion
|
|
533 (cond
|
|
534 ((eq c mine-char-unmarked)
|
|
535 (mine-mark-board (point))
|
|
536 (setq mine-nb-remaining-marks
|
|
537 (1- mine-nb-remaining-marks))
|
|
538 (if (mine-mine-at-point-p (point) 'slowp)
|
|
539 (setq mine-nb-remaining-mines
|
|
540 (1- mine-nb-remaining-mines))))
|
|
541 ((eq c mine-char-marked)
|
|
542 (mine-unmark-board (point))
|
|
543 (setq mine-nb-remaining-marks
|
|
544 (1+ mine-nb-remaining-marks))
|
|
545 (if (mine-mine-at-point-p (point) 'slowp)
|
|
546 (setq mine-nb-remaining-mines
|
|
547 (1+ mine-nb-remaining-mines))))
|
|
548 (t
|
|
549 (mine-message 'mine-msg-cannot-mark)))
|
|
550 (mine-update-remaining-mines))))
|
|
551
|
|
552 (defun mine-quit ()
|
|
553 "*Display hidden and bogus mines."
|
|
554 (interactive)
|
|
555 (when (y-or-n-p "Do you want to see the remaining and bogus mines? ")
|
|
556 (mine-show-bogus-mines)))
|
|
557
|
|
558 (defun mine-show-bogus-mines ()
|
|
559 (mine-reset-counters)
|
|
560 (let ((nrb 0) (nbb 0)
|
|
561 (x.y (cons nil nil))
|
|
562 (y 1) x
|
|
563 point c)
|
|
564 (while (<= y mine-ymax)
|
|
565 (setq x 1)
|
|
566 (setcdr x.y y)
|
|
567 (while (<= x mine-xmax)
|
|
568 (setq point (mine-xy-to-point x y)
|
|
569 c (char-after point))
|
|
570 (cond
|
|
571 ((eq c mine-char-unmarked)
|
|
572 (setcar x.y x)
|
|
573 (when (mine-mine-at-xy-p x.y)
|
|
574 (setq nrb (1+ nrb))
|
|
575 (mine-update-board point mine-char-not-found 'mine-face-not-found)))
|
|
576 ((eq c mine-char-marked)
|
|
577 (setcar x.y x)
|
|
578 (when (not (mine-mine-at-xy-p x.y))
|
|
579 (setq nbb (1+ nbb))
|
|
580 (mine-update-board point mine-char-bogus 'mine-face-bogus))))
|
|
581 (setq x (1+ x)))
|
|
582 (setq y (1+ y)))
|
|
583 (mine-update-bogus-mines nrb nbb)))
|
|
584
|
|
585 ;;; ================================================================
|
|
586 ;;; Key events - moves
|
|
587
|
|
588 (defun mine-left ()
|
|
589 "Move left"
|
|
590 (interactive)
|
|
591 (setq mine-x (1- mine-x))
|
|
592 (when (<= mine-x 0)
|
|
593 (while (<= mine-x mine-width)
|
|
594 (setq mine-x (+ mine-x mine-xmax)))
|
|
595 (setq mine-x (- mine-x mine-xmax)))
|
|
596 (mine-goto mine-x mine-y))
|
|
597
|
|
598 (defun mine-right ()
|
|
599 "Move right"
|
|
600 (interactive)
|
|
601 (setq mine-x (1+ mine-x))
|
|
602 (when (> mine-x mine-width)
|
|
603 (while (>= mine-x 0)
|
|
604 (setq mine-x (- mine-x mine-xmax)))
|
|
605 (setq mine-x (+ mine-x mine-xmax)))
|
|
606 (mine-goto mine-x mine-y))
|
|
607
|
|
608 (defun mine-up ()
|
|
609 "Move up"
|
|
610 (interactive)
|
|
611 (setq mine-y (1- mine-y))
|
|
612 (when (<= mine-y 0)
|
|
613 (while (<= mine-y mine-height)
|
|
614 (setq mine-y (+ mine-y mine-ymax)))
|
|
615 (setq mine-y (- mine-y mine-ymax)))
|
|
616 (mine-goto mine-x mine-y))
|
|
617
|
|
618 (defun mine-down ()
|
|
619 "Move down"
|
|
620 (interactive)
|
|
621 (setq mine-y (1+ mine-y))
|
|
622 (when (> mine-y mine-height)
|
|
623 (while (>= mine-y 0)
|
|
624 (setq mine-y (- mine-y mine-ymax)))
|
|
625 (setq mine-y (+ mine-y mine-ymax)))
|
|
626 (mine-goto mine-x mine-y))
|
|
627
|
|
628
|
|
629 (defun mine-bol ()
|
|
630 "Move to the beginning of the row"
|
|
631 (interactive)
|
|
632 (setq mine-x 1)
|
|
633 (mine-goto mine-x mine-y))
|
|
634
|
|
635 (defun mine-eol ()
|
|
636 "Move to the end of the row"
|
|
637 (interactive)
|
|
638 (setq mine-x mine-width)
|
|
639 (mine-goto mine-x mine-y))
|
|
640
|
|
641 (defun mine-top ()
|
|
642 "Move to the top of the column"
|
|
643 (interactive)
|
|
644 (setq mine-y 1)
|
|
645 (mine-goto mine-x mine-y))
|
|
646
|
|
647 (defun mine-bottom ()
|
|
648 "Move to the bottom of the column"
|
|
649 (interactive)
|
|
650 (setq mine-y mine-height)
|
|
651 (mine-goto mine-x mine-y))
|
|
652
|
|
653 ;;; ================================================================
|
|
654 ;;; Internal model functions
|
|
655
|
|
656 (defun mine-init-mines (num-mines)
|
|
657 (random t)
|
|
658 (let ((mines (list)) (n num-mines) x y x.y)
|
|
659 (while (> n 0)
|
|
660 (setq n (1- n)
|
|
661 x (1+ (random mine-xmax))
|
|
662 y (1+ (random mine-ymax))
|
|
663 x.y (cons x y))
|
|
664 (while (mine-member x.y mines 'nil)
|
|
665 ;; replace by the point to the right (or next row if eol)
|
|
666 (if (< x mine-xmax)
|
|
667 (setcar x.y (setq x (1+ x)))
|
|
668 (setcar x.y (setq x 1))
|
|
669 (setcdr x.y (setq y (if (< y mine-ymax) (1+ y) 1)))))
|
|
670 (setq mines (cons x.y mines)))
|
|
671 mines))
|
|
672
|
|
673 (defun mine-mine-at-point-p (point slowp)
|
|
674 (mine-member (mine-top-left (mine-point-to-x.y point))
|
|
675 mine-real-mines slowp))
|
|
676
|
|
677 (defun mine-mine-at-xy-p (x.y)
|
|
678 (mine-member x.y mine-real-mines 'nil))
|
|
679
|
|
680 ;;; Returns non-nil if ELT is an element of LIST.
|
|
681 ;;; Constant time execution if slowp is non-nil.
|
|
682 (defun mine-member (x.y list slowp)
|
|
683 (let ((found 'nil))
|
|
684 (while (and list (or slowp (not found)))
|
|
685 (if (equal x.y (car list))
|
|
686 (setq found 't))
|
|
687 (setq list (cdr list)))
|
|
688 found))
|
|
689
|
|
690 ;;; ================================================================
|
|
691 ;;; Internal model & interface functions
|
|
692
|
|
693 (defun mine-pxy (x y)
|
|
694 (cons (mine-xy-to-point x y) (cons x y)))
|
|
695
|
|
696 ;; pxy == (point . (x . y))
|
|
697 ;; with 1 <= {xy} <= mine-{xy}max
|
|
698 (defun mine-deep-hit (pxy)
|
|
699 (interactive)
|
|
700 (let (point x.y c)
|
|
701 (setq point (car pxy)
|
|
702 x.y (cdr pxy)
|
|
703 c (char-after point))
|
|
704 (cond
|
|
705 ((eq c mine-char-marked)) ;; free but marked (user bug)
|
|
706 ((not (eq c mine-char-unmarked))) ;; already done
|
|
707 ((mine-mine-at-xy-p x.y)
|
|
708 (error "Internal error: mine-deep-hit mine at %s" point))
|
|
709 (t ;; the real job...
|
|
710 (let* ((adjacent-points (mine-adjacent-points point x.y))
|
|
711 (nb-adjacent-mines (mine-nb-adjacent-mines adjacent-points)))
|
|
712 (mine-display-nb-adjacent-mines point nb-adjacent-mines)
|
|
713 (when (zerop nb-adjacent-mines)
|
|
714 ;; Stack overflow: "Lisp nesting exceeds max-lisp-eval-depth"
|
|
715 ;;(mapc 'mine-deep-hit adjacent-points)
|
|
716 (setq mine-adjacent-points
|
|
717 (nconc adjacent-points mine-adjacent-points))))))))
|
|
718
|
|
719 ;; return == ((point . (x . y))*)
|
|
720 ;; with 1 <= {xy} <= mine-{xy}max
|
|
721 (defun mine-adjacent-points (point x.y)
|
|
722 (mine-random-permut
|
|
723 (if mine-torus
|
|
724 (mine-adjacent-points-on-torus point x.y)
|
|
725 (mine-adjacent-points-no-torus point x.y))))
|
|
726
|
|
727 (defun mine-random-permut (l)
|
|
728 (let ((ll (nthcdr (random (length l)) l)))
|
|
729 (nconc ll l)
|
|
730 (prog1 (cdr ll) (setcdr ll ()))))
|
|
731
|
|
732 (defun mine-adjacent-points-no-torus (point x.y)
|
|
733 (let ((x (car x.y)) (y (cdr x.y)) (points (list)) xx yy)
|
|
734 ;; left column
|
|
735 (when (not (= x 1))
|
|
736 (setq xx (1- x))
|
|
737 (when (not (= y 1))
|
|
738 (setq yy (1- y))
|
|
739 (setq points (cons (mine-pxy xx yy) points)))
|
|
740 (setq points (cons (mine-pxy xx y) points))
|
|
741 (when (not (= y mine-ymax))
|
|
742 (setq yy (1+ y))
|
|
743 (setq points (cons (mine-pxy xx yy) points))))
|
|
744 ;; middle column
|
|
745 (setq xx x)
|
|
746 (when (not (= y 1))
|
|
747 (setq yy (1- y))
|
|
748 (setq points (cons (mine-pxy xx yy) points)))
|
|
749 (when (not (= y mine-ymax))
|
|
750 (setq yy (1+ y))
|
|
751 (setq points (cons (mine-pxy xx yy) points)))
|
|
752 ;; right column
|
|
753 (when (not (= x mine-xmax))
|
|
754 (setq xx (1+ x))
|
|
755 (when (not (= y 1))
|
|
756 (setq yy (1- y))
|
|
757 (setq points (cons (mine-pxy xx yy) points)))
|
|
758 (setq points (cons (mine-pxy xx y) points))
|
|
759 (when (not (= y mine-ymax))
|
|
760 (setq yy (1+ y))
|
|
761 (setq points (cons (mine-pxy xx yy) points))))
|
|
762 (nreverse points)))
|
|
763
|
|
764 (defun mine-adjacent-points-on-torus (point x.y)
|
|
765 (let ((x (car x.y)) (y (cdr x.y)) (points (list)) xx yy)
|
|
766 ;; left column
|
|
767 (setq xx (if (= x 1) mine-xmax (1- x)))
|
|
768 (setq yy (if (= y 1) mine-ymax (1- y)))
|
|
769 (setq points (cons (mine-pxy xx yy) points))
|
|
770 (setq points (cons (mine-pxy xx y) points))
|
|
771 (setq yy (if (= y mine-ymax) 1 (1+ y)))
|
|
772 (setq points (cons (mine-pxy xx yy) points))
|
|
773 ;; middle column
|
|
774 (setq xx x)
|
|
775 (setq yy (if (= y 1) mine-ymax (1- y)))
|
|
776 (setq points (cons (mine-pxy xx yy) points))
|
|
777 (setq yy (if (= y mine-ymax) 1 (1+ y)))
|
|
778 (setq points (cons (mine-pxy xx yy) points))
|
|
779 ;; right column
|
|
780 (setq xx (if (= x mine-xmax) 1 (1+ x)))
|
|
781 (setq yy (if (= y 1) mine-ymax (1- y)))
|
|
782 (setq points (cons (mine-pxy xx yy) points))
|
|
783 (setq points (cons (mine-pxy xx y) points))
|
|
784 (setq yy (if (= y mine-ymax) 1 (1+ y)))
|
|
785 (setq points (cons (mine-pxy xx yy) points))
|
|
786 (nreverse points)))
|
|
787
|
|
788 ;; l == ((p . (x . y))*)
|
|
789 (defun mine-nb-adjacent-mines (l)
|
|
790 (let ((nb 0) pxy x.y)
|
|
791 (while l
|
|
792 (setq pxy (car l) l (cdr l) x.y (cdr pxy))
|
|
793 (if (mine-mine-at-xy-p x.y)
|
|
794 (setq nb (1+ nb))))
|
|
795 nb))
|
|
796
|
|
797 ;;; ================================================================
|
|
798 ;;; Mode map
|
|
799
|
|
800 (defun mine-init-mode-map ()
|
|
801 (let ((map (make-keymap)) (gm global-map))
|
|
802 ;; All normally self-inserting keys (except digits) are undefined
|
|
803 (suppress-keymap map 'nil)
|
|
804 ;; Help
|
|
805 (define-key map "?" 'mine-help)
|
|
806 (define-key map "h" 'mine-help)
|
|
807 (define-key map "b" 'mine-help-bindings)
|
|
808 (define-key map "v" 'mine-help-variables)
|
|
809 (cond
|
|
810 (mine-xemacs-p
|
|
811 ;; Mouse control
|
|
812 (define-key map [mouse-1] 'mine-mouse-hit)
|
|
813 (define-key map [mouse-3] 'mine-mouse-mark)
|
|
814 ;; Mouse control to prevent problems
|
|
815 (define-key map [mouse-2] 'mine-mouse-nop))
|
|
816 (t
|
|
817 ;; Mouse control
|
|
818 (define-key map [mouse-1] 'mine-mouse-hit)
|
|
819 (define-key map [mouse-3] 'mine-mouse-mark)
|
|
820 ;; Mouse control to prevent problems
|
|
821 (define-key map [mouse-2] 'mine-mouse-nop)
|
|
822 (define-key map [down-mouse-1] 'mine-mouse-nop)
|
|
823 (define-key map [down-mouse-2] 'mine-mouse-nop)
|
|
824 (define-key map [down-mouse-3] 'mine-mouse-nop)
|
|
825 (define-key map [drag-mouse-1] 'mine-mouse-nop)
|
|
826 (define-key map [drag-mouse-2] 'mine-mouse-nop)
|
|
827 (define-key map [drag-mouse-3] 'mine-mouse-nop)
|
|
828 (define-key map [mouse-2] 'mine-mouse-nop)))
|
|
829 ;; Move
|
|
830 (substitute-key-definition 'backward-char 'mine-left map gm)
|
|
831 (substitute-key-definition 'forward-char 'mine-right map gm)
|
|
832 (substitute-key-definition 'previous-line 'mine-up map gm)
|
|
833 (substitute-key-definition 'next-line 'mine-down map gm)
|
|
834
|
|
835 (substitute-key-definition 'beginning-of-line 'mine-bol map gm)
|
|
836 (substitute-key-definition 'backward-word 'mine-bol map gm)
|
|
837 (substitute-key-definition 'backward-sexp 'mine-bol map gm)
|
|
838 (substitute-key-definition 'end-of-line 'mine-eol map gm)
|
|
839 (substitute-key-definition 'forward-word 'mine-eol map gm)
|
|
840 (substitute-key-definition 'forward-sexp 'mine-eol map gm)
|
|
841 (define-key map "\M-p" 'mine-top)
|
|
842 (define-key map "\M-n" 'mine-bottom)
|
|
843 ;; Hit and mark
|
|
844 (define-key map " " 'mine-hit-curpoint)
|
|
845 (define-key map "\C-m" 'mine-mark-curpoint)
|
|
846 (define-key map [kp-enter] 'mine-mark-curpoint)
|
|
847 (define-key map "m" 'mine-mark-curpoint)
|
|
848 (define-key map "q" 'mine-quit)
|
|
849
|
|
850 (setq mine-mode-map map)))
|
|
851
|
|
852 ;;; ================================================================
|
|
853 ;;; Faces
|
|
854
|
|
855 (defun mine-init-faces ()
|
|
856 (setq mine-faces (list))
|
|
857 (when mine-colorp
|
|
858 (let ((l (append mine-colors mine-default-colors))
|
|
859 key.col key col name)
|
|
860 (while l
|
|
861 (setq key.col (car l)
|
|
862 l (cdr l)
|
|
863 key (car key.col)
|
|
864 col (cdr key.col))
|
|
865 (when (null (assoc key mine-faces))
|
|
866 (setq name
|
|
867 (cond
|
|
868 ((null key) nil)
|
|
869 ((symbolp key) (mine-make-face key col))
|
|
870 ((not (integerp key))
|
|
871 (error "Key should be a symbol or a number: '%s'" key))
|
|
872 ((or (< key 0) (> key 8))
|
|
873 (error "Key should be a number between 0 and 8: '%s'" key))
|
|
874 (t
|
|
875 (setq name (intern (concat "mine-face-" key)))
|
|
876 (mine-make-face name col))))
|
|
877 (setq mine-faces (cons (cons key name) mine-faces))))
|
|
878 (setq mine-faces (nreverse mine-faces)))))
|
|
879
|
|
880 (defun mine-make-face (name col)
|
|
881 (or (if (fboundp 'internal-find-face)
|
|
882 (internal-find-face name)
|
|
883 (find-face name))
|
|
884 (let ((face (make-face name)))
|
|
885 (unless (or (not mine-xemacs-p) col)
|
|
886 (setq col (cdr (face-background 'default 'global))))
|
|
887 (set-face-background face col)
|
|
888 face))
|
|
889 name)
|
|
890
|
|
891 (defun mine-get-face (key)
|
|
892 (cdr (assoc key mine-faces)))
|
|
893
|
28
|
894 (defun mine-get-glyph (key)
|
|
895 (if mine-xemacs-p
|
|
896 (cdr (assoc key mine-default-glyphs))
|
|
897 nil))
|
22
|
898 ;;; ================================================================
|
|
899 ;;; Init board
|
|
900
|
|
901 (defun mine-init-board ()
|
|
902 (setq mine-padx* (if mine-char-pad 2 1))
|
|
903 (if (not mine-torus)
|
|
904 (setq mine-width mine-xmax
|
|
905 mine-height mine-ymax)
|
|
906 (let (window-xmax window-nb-tiles-x window-xmax-int
|
|
907 window-ymax window-nb-tiles-y window-ymax-int)
|
|
908 (setq window-xmax (/ (window-width) mine-padx*)
|
|
909 window-nb-tiles-x (/ window-xmax mine-xmax)
|
|
910 window-xmax-int (* window-nb-tiles-x window-xmax))
|
|
911 (setq mine-width
|
|
912 (max mine-xmax ; at least mine-xmax
|
|
913 (cond
|
|
914 ((null mine-nb-tiles-x) window-xmax)
|
|
915 ((not (numberp mine-nb-tiles-x))
|
|
916 (error "mine-nb-tiles-x should be nil or a number: %s"
|
|
917 mine-nb-tiles-x))
|
|
918 ((zerop mine-nb-tiles-x) window-xmax-int)
|
|
919 ((< mine-nb-tiles-x 0)
|
|
920 (floor (* mine-xmax (- mine-nb-tiles-x))))
|
|
921 ((floatp mine-nb-tiles-x)
|
|
922 (min window-xmax (floor (* mine-xmax mine-nb-tiles-x))))
|
|
923 (t (min window-xmax-int (* mine-xmax mine-nb-tiles-x))))))
|
|
924 (setq window-ymax (- (window-height) 5)
|
|
925 window-nb-tiles-y (/ window-ymax mine-ymax)
|
|
926 window-ymax-int (* window-nb-tiles-y window-ymax))
|
|
927 (setq mine-height
|
|
928 (max mine-ymax
|
|
929 (cond
|
|
930 ((null mine-nb-tiles-y) window-ymax)
|
|
931 ((not (numberp mine-nb-tiles-y))
|
|
932 (error "mine-nb-tiles-y should be nil or a number: %s"
|
|
933 mine-nb-tiles-y))
|
|
934 ((zerop mine-nb-tiles-y) window-ymax-int)
|
|
935 ((< mine-nb-tiles-y 0)
|
|
936 (floor (* mine-ymax (- mine-nb-tiles-y))))
|
|
937 ((floatp mine-nb-tiles-y)
|
|
938 (min window-ymax (floor (* mine-ymax mine-nb-tiles-y))))
|
|
939 (t (min window-ymax-int (* mine-ymax mine-nb-tiles-y))))))))
|
|
940 (let ((buffer-read-only 'nil)
|
|
941 (face-unmarked (mine-get-face 'mine-face-unmarked))
|
28
|
942 (glyph-unmarked (mine-get-glyph 'mine-face-unmarked))
|
22
|
943 (face-pad (mine-get-face 'mine-face-pad))
|
28
|
944 (glyph-pad (mine-get-glyph 'mine-face-pad))
|
22
|
945 row col)
|
|
946 (erase-buffer)
|
|
947 (mine-insert-copyright)
|
|
948 (mine-insert-remaining-mines)
|
|
949 (mine-insert-mines-hit)
|
|
950 (setq mine-point-min (point))
|
|
951 (setq row mine-height)
|
|
952 (while (>= (setq row (1- row)) 0)
|
|
953 (setq col (1- mine-width))
|
|
954 (insert mine-char-unmarked)
|
28
|
955 (when (and (not glyph-unmarked) face-unmarked)
|
22
|
956 (put-text-property (1- (point)) (point) 'face face-unmarked))
|
28
|
957 (when glyph-unmarked
|
|
958 (let ((e))
|
|
959 (setq e (make-extent (1- (point)) (point)))
|
|
960 (set-extent-property e 'invisible t)
|
|
961 (set-extent-property e 'end-open t)
|
|
962 (set-extent-property e 'start-open nil)
|
|
963 (set-extent-end-glyph e glyph-unmarked)))
|
22
|
964 (while (>= (setq col (1- col)) 0)
|
|
965 (when mine-char-pad
|
|
966 (insert mine-char-pad)
|
|
967 (when face-pad
|
|
968 (put-text-property (1- (point)) (point) 'face face-pad)))
|
|
969 (insert mine-char-unmarked)
|
28
|
970 (when (and (not glyph-unmarked) face-unmarked)
|
|
971 (put-text-property (1- (point)) (point) 'face face-unmarked))
|
|
972 (when glyph-unmarked
|
|
973 (let ((e))
|
|
974 (setq e (make-extent (1- (point)) (point)))
|
|
975 (set-extent-property e 'invisible t)
|
|
976 (set-extent-property e 'end-open t)
|
|
977 (set-extent-property e 'start-open nil)
|
|
978 (set-extent-end-glyph e glyph-unmarked))))
|
|
979
|
22
|
980 (insert ?\n))
|
|
981 (setq mine-point-max (1- (point)))
|
|
982 (mine-update-remaining-mines)
|
|
983 (mine-update-mines-hit)
|
|
984 (set-buffer-modified-p 'nil)))
|
|
985
|
|
986 ;;; ================================================================
|
|
987 ;;; Internal moves
|
|
988
|
|
989 (defun mine-goto-point (point)
|
|
990 (let ((x.y (mine-point-to-x.y point)))
|
|
991 (setq mine-x (car x.y) mine-y (cdr x.y))
|
|
992 (mine-goto mine-x mine-y)
|
|
993 (= point (point))))
|
|
994
|
|
995 (defun mine-goto (x y)
|
|
996 (goto-char (mine-xy-to-point x y))
|
|
997 (cond ((fboundp 'hscroll-point-visible)
|
|
998 (hscroll-point-visible))
|
|
999 ((fboundp 'auto-show-make-point-visible)
|
|
1000 (auto-show-make-point-visible))))
|
|
1001
|
|
1002 ;;; ================================================================
|
|
1003 ;;; Conversions
|
|
1004
|
|
1005 (defun mine-xy-to-point (x y)
|
|
1006 ;; p = pmin + 2*w*(y-1) + 2*(x-1)
|
|
1007 (+ mine-point-min
|
|
1008 (* mine-padx* mine-width (1- y))
|
|
1009 (* mine-padx* (1- x))))
|
|
1010
|
|
1011 ;;; Returns the topleft equivalent of point,
|
|
1012 ;;; on the periodic board, ie. converts point to model coordinates.
|
|
1013 (defun mine-top-left (x.y)
|
|
1014 (setcar x.y (1+ (mod (1- (car x.y)) mine-xmax)))
|
|
1015 (setcdr x.y (1+ (mod (1- (cdr x.y)) mine-ymax)))
|
|
1016 x.y)
|
|
1017
|
|
1018 (defun mine-point-to-x.y (point)
|
|
1019 (let (x y (p0 (- point mine-point-min)))
|
|
1020 (cond
|
|
1021 ((<= p0 0)
|
|
1022 (setq x 1 y 1))
|
|
1023 ((>= point mine-point-max)
|
|
1024 (setq x mine-width y mine-height))
|
|
1025 (t
|
|
1026 ;; p = pmin + 2*w*(y-1) + 2*(x-1)
|
|
1027 ;; y = (p - pmin)/2w + 1
|
|
1028 ;; x = (p - pmin - 2*w*(y-1)) / 2 + 1
|
|
1029 (setq y (1+ (/ p0 mine-width mine-padx*))
|
|
1030 x (1+ (/ (- p0 (* mine-padx* mine-width (1- y))) mine-padx*)))))
|
|
1031 (cons x y)))
|
|
1032
|
|
1033 ;;; ================================================================
|
|
1034 ;;; Screen display
|
|
1035
|
|
1036 (defun mine-mark-board (point)
|
|
1037 (mine-update-board point mine-char-marked 'mine-face-marked))
|
|
1038
|
|
1039 (defun mine-unmark-board (point)
|
|
1040 (mine-update-board point mine-char-unmarked 'mine-face-unmarked))
|
|
1041
|
|
1042 (defun mine-display-nb-adjacent-mines (point nb)
|
|
1043 (mine-update-board point
|
|
1044 (if (zerop nb) mine-char-zero (+ ?0 nb))
|
|
1045 nb))
|
|
1046
|
|
1047 ;; todo: enumerer tous les points periodiques
|
|
1048 (defun mine-update-board (point c key)
|
|
1049 (let ((buffer-read-only 'nil)
|
|
1050 (face (mine-get-face key))
|
28
|
1051 (glyph (mine-get-glyph key))
|
22
|
1052 (x.y (mine-top-left (mine-point-to-x.y point)))
|
|
1053 x y)
|
|
1054 (setq x (car x.y))
|
|
1055 (while (<= x mine-width)
|
|
1056 (setq y (cdr x.y))
|
|
1057 (while (<= y mine-height)
|
28
|
1058 (mine-update-point (mine-xy-to-point x y) c face glyph)
|
22
|
1059 (setq y (+ y mine-ymax)))
|
|
1060 (setq x (+ x mine-xmax)))
|
|
1061 (mine-reach-level 1) ; redisplay point and its periodic images
|
|
1062 (set-buffer-modified-p 'nil)))
|
|
1063
|
28
|
1064 (defun mine-update-point (point c face &optional glyph)
|
22
|
1065 (goto-char point)
|
28
|
1066 (if glyph
|
|
1067 (progn
|
|
1068 (insert c)
|
|
1069 (delete-char 1))
|
|
1070 (delete-char 1)
|
|
1071 (insert c))
|
|
1072 (when (and (not glyph) face)
|
22
|
1073 (put-text-property point (point) 'face face))
|
28
|
1074 (when glyph
|
|
1075 ;; (set-extent-end-glyph (extent-at (point)) nil)
|
|
1076 (set-extent-end-glyph (extent-at (point) nil 'end-glyph nil 'at) glyph))
|
22
|
1077 (mine-reach-level 0)) ; redisplay point
|
|
1078
|
|
1079 (defun mine-reach-level (level)
|
|
1080 (cond
|
|
1081 ((null mine-level)) ; no update at all
|
|
1082 ((< mine-level 0)) ; no update at all
|
|
1083 ((zerop mine-level) ; unconditional update
|
|
1084 (sit-for 0))
|
|
1085 ((zerop level)) ; wait for level 1
|
|
1086 ((= level 1)
|
|
1087 (cond
|
|
1088 ((= mine-level level)
|
|
1089 (sit-for 0))
|
|
1090 ((= mine-count1 mine-count1-max)
|
|
1091 (setq mine-count1 0)
|
|
1092 (mine-reach-level (1+ level)))
|
|
1093 (t (setq mine-count1 (1+ mine-count1)))))
|
|
1094 ((= level 2)
|
|
1095 (setq mine-count1 0)
|
|
1096 (cond
|
|
1097 ((= mine-level level)
|
|
1098 (sit-for 0))
|
|
1099 ((= mine-count2 mine-count2-max)
|
|
1100 (setq mine-count2 0)
|
|
1101 (mine-reach-level (1+ level)))
|
|
1102 (t (setq mine-count2 (1+ mine-count2)))))
|
|
1103 ((= level 3)
|
|
1104 (setq mine-count1 0)
|
|
1105 (setq mine-count2 0)
|
|
1106 (cond
|
|
1107 ((= mine-level level)
|
|
1108 (sit-for 0))))))
|
|
1109
|
|
1110 (defun mine-reset-counters ()
|
|
1111 (setq mine-count1 0
|
|
1112 mine-count2 0))
|
|
1113
|
|
1114 ;;; ================================================================
|
|
1115 ;;; Messages - init board
|
|
1116
|
|
1117 (defun mine-insert-copyright ()
|
|
1118 (insert mine-version "\n\n"))
|
|
1119
|
|
1120 (defun mine-insert-remaining-mines ()
|
|
1121 (insert (format "%16s" "Remaining mines") ":")
|
|
1122 (setq mine-point-remaining-mines (point))
|
|
1123 (insert " \n"))
|
|
1124
|
|
1125 (defun mine-insert-mines-hit ()
|
|
1126 (insert (format "%16s" "mines hit") ":")
|
|
1127 (setq mine-point-mines-hit (point))
|
|
1128 (insert " \n\n"))
|
|
1129
|
|
1130 ;;; ================================================================
|
|
1131 ;;; Messages - update board
|
|
1132
|
|
1133 (defun mine-update-remaining-mines ()
|
|
1134 (let ((buffer-read-only 'nil))
|
|
1135 (save-excursion
|
|
1136 (goto-char mine-point-remaining-mines)
|
|
1137 (delete-char 3)
|
|
1138 (insert (format "%3d" mine-nb-remaining-marks)))
|
|
1139 (set-buffer-modified-p 'nil))
|
|
1140 (sit-for 0)
|
|
1141 (message "mines remaining to find...%d" mine-nb-remaining-marks)
|
|
1142 (when (and (zerop mine-nb-remaining-mines)
|
|
1143 (zerop mine-nb-remaining-marks))
|
|
1144 (mine-message 'mine-msg-win)))
|
|
1145
|
|
1146 (defun mine-update-mines-hit ()
|
|
1147 (let ((buffer-read-only 'nil))
|
|
1148 (save-excursion
|
|
1149 (goto-char mine-point-mines-hit)
|
|
1150 (delete-char 3)
|
|
1151 (insert (format "%3d" mine-nb-mines-hit)))
|
|
1152 (set-buffer-modified-p 'nil)))
|
|
1153
|
|
1154 (defun mine-update-bogus-mines (nrb nbb)
|
|
1155 (let ((buffer-read-only 'nil)
|
|
1156 (msg (format "There were %d remaining mines and %d bogus mines"
|
|
1157 nrb nbb)))
|
|
1158 (save-excursion
|
|
1159 (goto-char (point-max))
|
|
1160 (insert "\n" msg))
|
|
1161 (set-buffer-modified-p 'nil)
|
|
1162 (message msg)))
|
|
1163
|
|
1164 ;;; ================================================================
|
|
1165 ;;; Messages - write minibuffer
|
|
1166
|
|
1167 (defun mine-message (msg)
|
|
1168 (ding)
|
|
1169 (cond
|
|
1170 ((eq msg 'mine-msg-click-precisely)
|
|
1171 (message "Please, click more precisely"))
|
|
1172 ((eq msg 'mine-msg-unmark-before-hit)
|
|
1173 (message "You must unmark point before hitting it."))
|
|
1174 ((eq msg 'mine-msg-point-already-hit)
|
|
1175 (message "Point has already been hit."))
|
|
1176 ((eq msg 'mine-msg-cannot-mark)
|
|
1177 (message "Can't (un)mark point..."))
|
28
|
1178 ((eq msg 'mine-msg-lose)
|
22
|
1179 (message "Sorry... There's a mine here...")
|
|
1180 (sit-for 1)
|
|
1181 (message "Sorry... There's a mine here... You lost!"))
|
|
1182 ((eq msg 'mine-msg-win)
|
|
1183 (message "Congratulations...")
|
|
1184 (sit-for 1)
|
|
1185 (message "Congratulations... You won!"))
|
|
1186 (t
|
|
1187 (message (format "%s" msg)))))
|
|
1188
|
|
1189 (mine-init-mode-map)
|
|
1190
|
|
1191 ;;; ================================================================
|
|
1192
|
|
1193 (defun mine-print-variables (l)
|
|
1194 (let (var)
|
|
1195 (princ "(setq ")
|
|
1196 (while l
|
|
1197 (setq var (car l) l (cdr l))
|
|
1198 (cond
|
|
1199 ((stringp var) (princ (format ";; %s\n " var)))
|
|
1200 ((not (symbolp var)) (error "Not a symbol: %s" var))
|
|
1201 ((not (boundp var)) (error "Unboundp symbol: %s" var))
|
|
1202 (t (princ (format "%-20s'%s" var (symbol-value var)))
|
|
1203 (when l (princ "\n ")))))
|
|
1204 (princ "))\n")))
|
|
1205
|
|
1206 ;;; ================================================================
|
|
1207
|
|
1208 ;;(autoload 'apropos-print "apropos")
|
|
1209 ;;(autoload 'apropos-do-all "apropos")
|
|
1210
|
|
1211 (if (not (boundp 'apropos-accumulator))
|
|
1212 (load "apropos"))
|
|
1213
|
|
1214 (if (boundp 'apropos-item)
|
|
1215 ;; (Daniel.Pfeiffer's) old official version of apropos
|
|
1216 (defun apropos-symbols (l &optional do-all)
|
|
1217 (let ((ll (list)))
|
|
1218 (while l
|
|
1219 (when (not (stringp (car l)))
|
|
1220 (setq ll (cons (car l) ll)))
|
|
1221 (setq l (cdr l)))
|
|
1222 (setq apropos-accumulator (nreverse ll)))
|
|
1223 (or do-all (setq do-all apropos-do-all))
|
|
1224 (apropos-print
|
|
1225 t
|
|
1226 (lambda (p)
|
|
1227 (let (doc symbol)
|
|
1228 (while p
|
|
1229 (setcar p
|
|
1230 (list ; (s f v p)
|
|
1231 (setq symbol (car p))
|
|
1232 (if (commandp symbol)
|
|
1233 (if (setq doc (documentation symbol t))
|
|
1234 (substring doc 0 (string-match "\n" doc))
|
|
1235 "(not documented)"))
|
|
1236 (and do-all
|
|
1237 (user-variable-p symbol)
|
|
1238 (if (setq doc (documentation-property
|
|
1239 symbol 'variable-documentation t))
|
|
1240 (substring doc 0 (string-match "\n" doc))))))
|
|
1241 (setq p (cdr p)))))
|
|
1242 t)))
|
|
1243
|
|
1244 (provide 'mine)
|
|
1245
|
|
1246 ;;; mine.el ends here
|