comparison lisp/play/blackbox.el @ 88155:d7ddb3e565de

sync with trunk
author Henrik Enberg <henrik.enberg@telia.com>
date Mon, 16 Jan 2006 00:03:54 +0000
parents 0d8b17d428b5
children
comparison
equal deleted inserted replaced
88154:8ce476d3ba36 88155:d7ddb3e565de
1 ;;; blackbox.el --- blackbox game in Emacs Lisp 1 ;;; blackbox.el --- blackbox game in Emacs Lisp
2 2
3 ;; Copyright (C) 1985, 1986, 1987, 1992, 2001, 2002 Free Software Foundation, Inc. 3 ;; Copyright (C) 1985, 1986, 1987, 1992, 2001, 2002, 2003, 2004,
4 ;; 2005 Free Software Foundation, Inc.
4 5
5 ;; Author: F. Thomas May <uw-nsr!uw-warp!tom@beaver.cs.washington.edu> 6 ;; Author: F. Thomas May <uw-nsr!uw-warp!tom@beaver.cs.washington.edu>
6 ;; Adapted-By: ESR 7 ;; Adapted-By: ESR
7 ;; Keywords: games 8 ;; Keywords: games
8 9
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details. 20 ;; GNU General Public License for more details.
20 21
21 ;; You should have received a copy of the GNU General Public License 22 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the 23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, 24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02111-1307, USA. 25 ;; Boston, MA 02110-1301, USA.
25 26
26 ;;; Commentary: 27 ;;; Commentary:
27 28
28 ;; by F. Thomas May <uw-nsr!uw-warp!tom@beaver.cs.washington.edu> 29 ;; by F. Thomas May <uw-nsr!uw-warp!tom@beaver.cs.washington.edu>
29 ;; doc comment by Root Boy Jim <rbj@dsys.icst.nbs.gov>, 27 Apr 89 30 ;; doc comment by Root Boy Jim <rbj@dsys.icst.nbs.gov>, 27 Apr 89
48 ;; - - - - - - - - R 49 ;; - - - - - - - - R
49 ;; H - - - - - - - O 50 ;; H - - - - - - - O
50 ;; 2 H 4 H 51 ;; 2 H 4 H
51 ;; 52 ;;
52 ;; Rays which enter and exit are numbered. You can see that rays 1 & 5 pass 53 ;; Rays which enter and exit are numbered. You can see that rays 1 & 5 pass
53 ;; thru the box undisturbed. Ray 2 is deflected by the northwesternmost 54 ;; thru the box undisturbed. Ray 2 is deflected by the northwesternmost
54 ;; ball. Likewise rays 3 and 4. Rays which hit balls and are absorbed are 55 ;; ball. Likewise rays 3 and 4. Rays which hit balls and are absorbed are
55 ;; marked with H. The bottom of the left and the right of the bottom hit 56 ;; marked with H. The bottom of the left and the right of the bottom hit
56 ;; the southeastern ball directly. Rays may also hit balls after being 57 ;; the southeastern ball directly. Rays may also hit balls after being
57 ;; reflected. Consider the H on the bottom next to the 4. It bounces off 58 ;; reflected. Consider the H on the bottom next to the 4. It bounces off
58 ;; the NW-ern most ball and hits the central ball. A ray shot from above 59 ;; the NW-ern most ball and hits the central ball. A ray shot from above
59 ;; the right side 5 would hit the SE-ern most ball. The R beneath the 5 60 ;; the right side 5 would hit the SE-ern most ball. The R beneath the 5
60 ;; is because the ball is returned instantly. It is not allowed into 61 ;; is because the ball is returned instantly. It is not allowed into
61 ;; the box if it would reflect immediately. The R on the top is a more 62 ;; the box if it would reflect immediately. The R on the top is a more
62 ;; leisurely return. Both central balls would tend to deflect it east 63 ;; leisurely return. Both central balls would tend to deflect it east
66 ;; there are in the box, the true board position will be revealed. Each 67 ;; there are in the box, the true board position will be revealed. Each
67 ;; `x' is an incorrect guess of yours; `o' is the true location of a ball. 68 ;; `x' is an incorrect guess of yours; `o' is the true location of a ball.
68 69
69 ;;; Code: 70 ;;; Code:
70 71
71 (defvar blackbox-mode-map nil "")
72
73 (defvar bb-board nil 72 (defvar bb-board nil
74 "Blackbox board.") 73 "Blackbox board.")
75 74
76 (defvar bb-x -1 75 (defvar bb-x -1
77 "Current x-position.") 76 "Current x-position.")
86 "Number of detours.") 85 "Number of detours.")
87 86
88 (defvar bb-balls-placed nil 87 (defvar bb-balls-placed nil
89 "List of already placed balls.") 88 "List of already placed balls.")
90 89
91 (unless blackbox-mode-map 90 ;; This is used below to remap existing bindings for cursor motion to
92 (setq blackbox-mode-map (make-keymap)) 91 ;; blackbox-specific bindings in blackbox-mode-map. This is so that
93 (suppress-keymap blackbox-mode-map t) 92 ;; users who prefer non-default key bindings for cursor motion don't
94 (define-key blackbox-mode-map "\C-f" 'bb-right) 93 ;; lose that when they play Blackbox.
95 (define-key blackbox-mode-map [right] 'bb-right) 94 (defun blackbox-redefine-key (map oldfun newfun)
96 (define-key blackbox-mode-map "\C-b" 'bb-left) 95 "Redefine keys that run the function OLDFUN to run NEWFUN instead."
97 (define-key blackbox-mode-map [left] 'bb-left) 96 (define-key map (vector 'remap oldfun) newfun))
98 (define-key blackbox-mode-map "\C-p" 'bb-up) 97
99 (define-key blackbox-mode-map [up] 'bb-up) 98
100 (define-key blackbox-mode-map "\C-n" 'bb-down) 99 (defvar blackbox-mode-map
101 (define-key blackbox-mode-map [down] 'bb-down) 100 (let ((map (make-keymap)))
102 (define-key blackbox-mode-map "\C-e" 'bb-eol) 101 (suppress-keymap map t)
103 (define-key blackbox-mode-map "\C-a" 'bb-bol) 102 (blackbox-redefine-key map 'backward-char 'bb-left)
104 (define-key blackbox-mode-map " " 'bb-romp) 103 (blackbox-redefine-key map 'forward-char 'bb-right)
105 (define-key blackbox-mode-map [insert] 'bb-romp) 104 (blackbox-redefine-key map 'previous-line 'bb-up)
106 (define-key blackbox-mode-map "\C-m" 'bb-done) 105 (blackbox-redefine-key map 'next-line 'bb-down)
107 (define-key blackbox-mode-map [kp-enter] 'bb-done)) 106 (blackbox-redefine-key map 'move-end-of-line 'bb-eol)
107 (blackbox-redefine-key map 'move-beginning-of-line 'bb-bol)
108 (define-key map " " 'bb-romp)
109 (define-key map [insert] 'bb-romp)
110 (blackbox-redefine-key map 'newline 'bb-done)
111 map))
108 112
109 ;; Blackbox mode is suitable only for specially formatted data. 113 ;; Blackbox mode is suitable only for specially formatted data.
110 (put 'blackbox-mode 'mode-class 'special) 114 (put 'blackbox-mode 'mode-class 'special)
111 115
112 (defun blackbox-mode () 116 (defun blackbox-mode ()
115 119
116 The usual mnemonic keys move the cursor around the box. 120 The usual mnemonic keys move the cursor around the box.
117 \\<blackbox-mode-map>\\[bb-bol] and \\[bb-eol] move to the beginning and end of line, respectively. 121 \\<blackbox-mode-map>\\[bb-bol] and \\[bb-eol] move to the beginning and end of line, respectively.
118 122
119 \\[bb-romp] -- send in a ray from point, or toggle a ball at point 123 \\[bb-romp] -- send in a ray from point, or toggle a ball at point
120 \\[bb-done] -- end game and get score 124 \\[bb-done] -- end game and get score"
121 "
122 (interactive) 125 (interactive)
123 (kill-all-local-variables) 126 (kill-all-local-variables)
124 (use-local-map blackbox-mode-map) 127 (use-local-map blackbox-mode-map)
125 (setq truncate-lines t) 128 (setq truncate-lines t)
126 (setq major-mode 'blackbox-mode) 129 (setq major-mode 'blackbox-mode)
127 (setq mode-name "Blackbox")) 130 (setq mode-name "Blackbox")
131 (run-mode-hooks 'blackbox-mode-hook))
128 132
129 ;;;###autoload 133 ;;;###autoload
130 (defun blackbox (num) 134 (defun blackbox (num)
131 "Play blackbox. 135 "Play blackbox.
132 Optional prefix argument is the number of balls; the default is 4. 136 Optional prefix argument is the number of balls; the default is 4.
333 (t 337 (t
334 (setq bb-balls-placed (cons coord bb-balls-placed)) 338 (setq bb-balls-placed (cons coord bb-balls-placed))
335 (bb-update-board (propertize "O" 'help-echo "Placed ball")))))) 339 (bb-update-board (propertize "O" 'help-echo "Placed ball"))))))
336 340
337 (defun bb-trace-ray (x y) 341 (defun bb-trace-ray (x y)
338 (let ((result (bb-trace-ray-2 342 (when (= (following-char) 32)
339 t 343 (let ((result (bb-trace-ray-2
340 x 344 t
341 (cond 345 x
342 ((= x -1) 1) 346 (cond
343 ((= x 8) -1) 347 ((= x -1) 1)
344 (t 0)) 348 ((= x 8) -1)
345 y 349 (t 0))
346 (cond 350 y
347 ((= y -1) 1) 351 (cond
348 ((= y 8) -1) 352 ((= y -1) 1)
349 (t 0))))) 353 ((= y 8) -1)
350 (cond 354 (t 0)))))
351 ((eq result 'hit) 355 (cond
352 (bb-update-board (propertize "H" 'help-echo "Hit")) 356 ((eq result 'hit)
353 (setq bb-score (1+ bb-score))) 357 (bb-update-board (propertize "H" 'help-echo "Hit"))
354 ((equal result (cons x y)) 358 (setq bb-score (1+ bb-score)))
355 (bb-update-board (propertize "R" 'help-echo "Reflection")) 359 ((equal result (cons x y))
356 (setq bb-score (1+ bb-score))) 360 (bb-update-board (propertize "R" 'help-echo "Reflection"))
357 (t 361 (setq bb-score (1+ bb-score)))
358 (setq bb-detour-count (1+ bb-detour-count)) 362 (t
359 (bb-update-board (propertize (format "%d" bb-detour-count) 363 (setq bb-detour-count (1+ bb-detour-count))
360 'help-echo "Detour")) 364 (bb-update-board (propertize (format "%d" bb-detour-count)
361 (save-excursion 365 'help-echo "Detour"))
362 (bb-goto result) 366 (save-excursion
363 (bb-update-board (propertize (format "%d" bb-detour-count) 367 (bb-goto result)
364 'help-echo "Detour"))) 368 (bb-update-board (propertize (format "%d" bb-detour-count)
365 (setq bb-score (+ bb-score 2)))))) 369 'help-echo "Detour")))
370 (setq bb-score (+ bb-score 2)))))))
366 371
367 (defun bb-trace-ray-2 (first x dx y dy) 372 (defun bb-trace-ray-2 (first x dx y dy)
368 (cond 373 (cond
369 ((and (not first) 374 ((and (not first)
370 (bb-outside-box x y)) 375 (bb-outside-box x y))
427 (insert c) 432 (insert c)
428 (backward-char 1))) 433 (backward-char 1)))
429 434
430 (provide 'blackbox) 435 (provide 'blackbox)
431 436
437 ;; arch-tag: 6c474c62-5617-4b10-9b44-ac430168c0e2
432 ;;; blackbox.el ends here 438 ;;; blackbox.el ends here