1 ;;; blackbox.el --- blackbox game in Emacs Lisp
3 ;; Copyright (C) 1985, 1986, 1987, 1992 Free Software Foundation, Inc.
5 ;; Author: F. Thomas May <uw-nsr!uw-warp!tom@beaver.cs.washington.edu>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; 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 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; 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 ;; interface improvements by ESR, Dec 5 1991.
32 ;; The object of the game is to find four hidden balls by shooting rays
33 ;; into the black box. There are four possibilities: 1) the ray will
34 ;; pass thru the box undisturbed, 2) it will hit a ball and be absorbed,
35 ;; 3) it will be deflected and exit the box, or 4) be deflected immediately,
36 ;; not even being allowed entry into the box.
38 ;; The strange part is the method of deflection. It seems that rays will
39 ;; not pass next to a ball, and change direction at right angles to avoid it.
42 ;; 1 - - - - - - - - 1
47 ;; 5 - - - - - - - - 5
52 ;; 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 ;; 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 ;; 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 ;; 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 ;; 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 ;; leisurely return. Both central balls would tend to deflect it east
63 ;; or west, but it cannot go either way, so it just retreats.
65 ;; At the end of the game, if you've placed guesses for as many balls as
66 ;; 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.
71 (defvar blackbox-mode-map nil
"")
75 (setq blackbox-mode-map
(make-keymap))
76 (suppress-keymap blackbox-mode-map t
)
77 (define-key blackbox-mode-map
"\C-f" 'bb-right
)
78 (define-key blackbox-mode-map
[right] 'bb-right)
79 (define-key blackbox-mode-map "\C-b" 'bb-left)
80 (define-key blackbox-mode-map [left] 'bb-left)
81 (define-key blackbox-mode-map "\C-p" 'bb-up)
82 (define-key blackbox-mode-map [up] 'bb-up)
83 (define-key blackbox-mode-map "\C-n" 'bb-down)
84 (define-key blackbox-mode-map [down] 'bb-down)
85 (define-key blackbox-mode-map "\C-e" 'bb-eol)
86 (define-key blackbox-mode-map "\C-a" 'bb-bol)
87 (define-key blackbox-mode-map " " 'bb-romp)
88 (define-key blackbox-mode-map [insert] 'bb-romp)
89 (define-key blackbox-mode-map "\C-m" 'bb-done)
90 (define-key blackbox-mode-map [kp-enter] 'bb-done))
92 ;; Blackbox mode is suitable only for specially formatted data.
93 (put 'blackbox-mode 'mode-class 'special)
95 (defun blackbox-mode ()
96 "Major mode for playing blackbox. To learn how to play blackbox,
97 see the documentation for function `blackbox'.
99 The usual mnemonic keys move the cursor around the box.
100 \\<blackbox-mode-map>\\[bb-bol] and \\[bb-eol] move to the beginning and end of line, respectively.
102 \\[bb-romp] -- send in a ray from point, or toggle a ball at point
103 \\[bb-done] -- end game and get score
106 (kill-all-local-variables)
107 (use-local-map blackbox-mode-map)
108 (setq truncate-lines t)
109 (setq major-mode 'blackbox-mode)
110 (setq mode-name "Blackbox"))
113 (defun blackbox (num)
114 "Play blackbox. Optional prefix argument is the number of balls;
119 Blackbox is a game of hide and seek played on an 8 by 8 grid (the
120 Blackbox). Your opponent (Emacs, in this case) has hidden several
121 balls (usually 4) within this box. By shooting rays into the box and
122 observing where they emerge it is possible to deduce the positions of
123 the hidden balls. The fewer rays you use to find the balls, the lower
128 \\<blackbox-mode-map>\
129 To play blackbox, type \\[blackbox]. An optional prefix argument
130 specifies the number of balls to be hidden in the box; the default is
133 The cursor can be moved around the box with the standard cursor
136 To shoot a ray, move the cursor to the edge of the box and press SPC.
137 The result will be determined and the playfield updated.
139 You may place or remove balls in the box by moving the cursor into the
140 box and pressing \\[bb-romp].
142 When you think the configuration of balls you have placed is correct,
143 press \\[bb-done]. You will be informed whether you are correct or
144 not, and be given your score. Your score is the number of letters and
145 numbers around the outside of the box plus five for each incorrectly
146 placed ball. If you placed any balls incorrectly, they will be
147 indicated with `x', and their actual positions indicated with `o'.
151 There are three possible outcomes for each ray you send into the box:
153 Detour: the ray is deflected and emerges somewhere other than
154 where you sent it in. On the playfield, detours are
155 denoted by matching pairs of numbers -- one where the
156 ray went in, and the other where it came out.
158 Reflection: the ray is reflected and emerges in the same place
159 it was sent in. On the playfield, reflections are
160 denoted by the letter `R'.
162 Hit: the ray strikes a ball directly and is absorbed. It does
163 not emerge from the box. On the playfield, hits are
164 denoted by the letter `H'.
166 The rules for how balls deflect rays are simple and are best shown by
169 As a ray approaches a ball it is deflected ninety degrees. Rays can
170 be deflected multiple times. In the diagrams below, the dashes
171 represent empty box locations and the letter `O' represents a ball.
172 The entrance and exit points of each ray are marked with numbers as
173 described under \"Detour\" above. Note that the entrance and exit
174 points are always interchangeable. `*' denotes the path taken by the
177 Note carefully the relative positions of the ball and the ninety
178 degree deflection it causes.
181 - * - - - - - - - - - - - - - - - - - - - - - -
182 - * - - - - - - - - - - - - - - - - - - - - - -
183 1 * * - - - - - - - - - - - - - - - O - - - - O -
184 - - O - - - - - - - O - - - - - - - * * * * - -
185 - - - - - - - - - - - * * * * * 2 3 * * * - - * - -
186 - - - - - - - - - - - * - - - - - - - O - * - -
187 - - - - - - - - - - - * - - - - - - - - * * - -
188 - - - - - - - - - - - * - - - - - - - - * - O -
191 As mentioned above, a reflection occurs when a ray emerges from the same point
192 it was sent in. This can happen in several ways:
195 - - - - - - - - - - - - - - - - - - - - - - - -
196 - - - - O - - - - - O - O - - - - - - - - - - -
197 R * * * * - - - - - - - * - - - - O - - - - - - -
198 - - - - O - - - - - - * - - - - R - - - - - - - -
199 - - - - - - - - - - - * - - - - - - - - - - - -
200 - - - - - - - - - - - * - - - - - - - - - - - -
201 - - - - - - - - R * * * * - - - - - - - - - - - -
202 - - - - - - - - - - - - O - - - - - - - - - - -
204 In the first example, the ray is deflected downwards by the upper
205 ball, then left by the lower ball, and finally retraces its path to
206 its point of origin. The second example is similar. The third
207 example is a bit anomalous but can be rationalized by realizing the
208 ray never gets a chance to get into the box. Alternatively, the ray
209 can be thought of as being deflected downwards and immediately
210 emerging from the box.
212 A hit occurs when a ray runs straight into a ball:
214 - - - - - - - - - - - - - - - - - - - - - - - -
215 - - - - - - - - - - - - - - - - - - - - O - - -
216 - - - - - - - - - - - - O - - - H * * * * - - - -
217 - - - - - - - - H * * * * O - - - - - - * - - - -
218 - - - - - - - - - - - - O - - - - - - O - - - -
219 H * * * O - - - - - - - - - - - - - - - - - - - -
220 - - - - - - - - - - - - - - - - - - - - - - - -
221 - - - - - - - - - - - - - - - - - - - - - - - -
223 Be sure to compare the second example of a hit with the first example of
226 (switch-to-buffer "*Blackbox*")
228 (setq buffer-read-only t)
229 (buffer-disable-undo (current-buffer))
230 (setq bb-board (bb-init-board (or num 4)))
231 (setq bb-balls-placed nil)
235 (setq bb-detour-count 0)
237 (bb-goto (cons bb-x bb-y)))
239 (defun bb-init-board (num-balls)
242 (while (>= (setq num-balls (1- num-balls)) 0)
245 (setq pos (cons (random 8) (random 8)))
246 (bb-member pos board)))
247 (setq board (cons pos board)))
250 (defun bb-insert-board ()
251 (let (i (buffer-read-only nil))
255 (while (>= (setq i (1- i)) 0)
256 (insert " - - - - - - - - \n"))
258 (insert (format "\nThere are %d balls in the box" (length bb-board)))
266 (setq bb-x (1+ bb-x))))
273 (setq bb-x (1- bb-x))))
280 (setq bb-y (1- bb-y))))
287 (setq bb-y (1+ bb-y))))
292 (bb-goto (cons bb-x bb-y)))
297 (bb-goto (cons bb-x bb-y)))
303 (or (= bb-x -1) (= bb-x 8))
304 (or (= bb-y -1) (= bb-y 8))))
305 ((bb-outside-box bb-x bb-y)
306 (bb-trace-ray bb-x bb-y))
308 (bb-place-ball bb-x bb-y))))
310 (defun bb-place-ball (x y)
311 (let ((coord (cons x y)))
313 ((bb-member coord bb-balls-placed)
314 (setq bb-balls-placed (bb-delete coord bb-balls-placed))
315 (bb-update-board "-"))
317 (setq bb-balls-placed (cons coord bb-balls-placed))
318 (bb-update-board "O")))))
320 (defun bb-trace-ray (x y)
321 (let ((result (bb-trace-ray-2
335 (bb-update-board "H")
336 (setq bb-score (1+ bb-score)))
337 ((equal result (cons x y))
338 (bb-update-board "R")
339 (setq bb-score (1+ bb-score)))
341 (setq bb-detour-count (1+ bb-detour-count))
342 (bb-update-board (format "%d" bb-detour-count))
345 (bb-update-board (format "%d" bb-detour-count)))
346 (setq bb-score (+ bb-score 2))))))
348 (defun bb-trace-ray-2 (first x dx y dy)
351 (bb-outside-box x y))
353 ((bb-member (cons (+ x dx) (+ y dy)) bb-board)
355 ((bb-member (cons (+ x dx dy) (+ y dy dx)) bb-board)
356 (bb-trace-ray-2 nil x (- dy) y (- dx)))
357 ((bb-member (cons (+ x dx (- dy)) (+ y dy (- dx))) bb-board)
358 (bb-trace-ray-2 nil x dy y dx))
360 (bb-trace-ray-2 nil (+ x dx) dx (+ y dy) dy))))
363 "Finish the game and report score."
367 ((not (= (length bb-balls-placed) (length bb-board)))
368 (message "There %s %d hidden ball%s; you have placed %d."
369 (if (= (length bb-board) 1) "is" "are")
371 (if (= (length bb-board) 1) "" "s")
372 (length bb-balls-placed)))
374 (setq bogus-balls (bb-show-bogus-balls bb-balls-placed bb-board))
375 (if (= bogus-balls 0)
376 (message "Right! Your score is %d." bb-score)
377 (message "Oops! You missed %d ball%s. Your score is %d."
379 (if (= bogus-balls 1) "" "s")
380 (+ bb-score (* 5 bogus-balls))))
381 (bb-goto '(-1 . -1))))))
383 (defun bb-show-bogus-balls (balls-placed board)
384 (bb-show-bogus-balls-2 balls-placed board "x")
385 (bb-show-bogus-balls-2 board balls-placed "o"))
387 (defun bb-show-bogus-balls-2 (list-1 list-2 c)
391 ((bb-member (car list-1) list-2)
392 (bb-show-bogus-balls-2 (cdr list-1) list-2 c))
394 (bb-goto (car list-1))
396 (1+ (bb-show-bogus-balls-2 (cdr list-1) list-2 c)))))
398 (defun bb-outside-box (x y)
399 (or (= x -1) (= x 8) (= y -1) (= y 8)))
402 (goto-char (+ (* (car pos) 2) (* (cdr pos) 22) 26)))
404 (defun bb-update-board (c)
405 (let ((buffer-read-only nil))
406 (backward-char (1- (length c)))
407 (delete-char (length c))
411 (defun bb-member (elt list)
412 "Returns non-nil if ELT is an element of LIST."
413 (eval (cons 'or (mapcar (function (lambda (x) (equal x elt))) list))))
415 (defun bb-delete (item list)
416 "Deletes ITEM from LIST and returns a copy."
418 ((equal item (car list)) (cdr list))
419 (t (cons (car list) (bb-delete item (cdr list))))))
421 ;;; blackbox.el ends here