*** empty log message ***
[emacs.git] / lisp / play / blackbox.el
blob002a33ec37f44415aaec3b4671064b61cafc3e88
1 ;;; blackbox.el --- blackbox game in Emacs Lisp
3 ;; Copyright (C) 1985, 1986, 1987, 1992 Free Software Foundation, Inc.
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 2, or (at your option)
10 ;; any later version.
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to
19 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
21 ; by F. Thomas May <uw-nsr!uw-warp!tom@beaver.cs.washington.edu>
22 ; doc comment by Root Boy Jim <rbj@dsys.icst.nbs.gov>, 27 Apr 89
23 ; interface improvements by Eric Raymond <eric@snark.thyrsus.com>, Dec 5 1991.
25 ; The object of the game is to find four hidden balls by shooting rays
26 ; into the black box. There are four possibilities: 1) the ray will
27 ; pass thru the box undisturbed, 2) it will hit a ball and be absorbed,
28 ; 3) it will be deflected and exit the box, or 4) be deflected immediately,
29 ; not even being allowed entry into the box.
31 ; The strange part is the method of deflection. It seems that rays will
32 ; not pass next to a ball, and change direction at right angles to avoid it.
34 ; R 3
35 ; 1 - - - - - - - - 1
36 ; - - - - - - - -
37 ; - O - - - - - - 3
38 ; 2 - - - - O - O -
39 ; 4 - - - - - - - -
40 ; 5 - - - - - - - - 5
41 ; - - - - - - - - R
42 ; H - - - - - - - O
43 ; 2 H 4 H
45 ; Rays which enter and exit are numbered. You can see that rays 1 & 5 pass
46 ; thru the box undisturbed. Ray 2 is deflected by the northwesternmost
47 ; ball. Likewise rays 3 and 4. Rays which hit balls and are absorbed are
48 ; marked with H. The bottom of the left and the right of the bottom hit
49 ; the southeastern ball directly. Rays may also hit balls after being
50 ; reflected. Consider the H on the bottom next to the 4. It bounces off
51 ; the NW-ern most ball and hits the central ball. A ray shot from above
52 ; the right side 5 would hit the SE-ern most ball. The R beneath the 5
53 ; is because the ball is returned instantly. It is not allowed into
54 ; the box if it would reflect immediately. The R on the top is a more
55 ; leisurely return. Both central balls would tend to deflect it east
56 ; or west, but it cannot go either way, so it just retreats.
58 ; At the end of the game, if you've placed guesses for as many balls as
59 ; there are in the box, the true board position will be revealed. Each
60 ; `x' is an incorrect guess of yours; `o' is the true location of a ball.
62 (defvar blackbox-mode-map nil "")
64 (if blackbox-mode-map
66 (setq blackbox-mode-map (make-keymap))
67 (suppress-keymap blackbox-mode-map t)
68 (define-key blackbox-mode-map "\C-f" 'bb-right)
69 (define-key blackbox-mode-map "\C-b" 'bb-left)
70 (define-key blackbox-mode-map "\C-p" 'bb-up)
71 (define-key blackbox-mode-map "\C-n" 'bb-down)
72 (define-key blackbox-mode-map "\C-e" 'bb-eol)
73 (define-key blackbox-mode-map "\C-a" 'bb-bol)
74 (define-key blackbox-mode-map " " 'bb-romp)
75 (define-key blackbox-mode-map "\C-m" 'bb-done)
77 ;; This is a kluge. What we really want is a general
78 ;; feature for reminding terminal keys to the functions
79 ;; corresponding to them in local maps
80 (if (featurep 'keypad)
81 (let (keys)
82 (if (setq keys (function-key-sequence ?u)) ; Up Arrow
83 (define-key blackbox-mode-map keys 'bb-up))
84 (if (setq keys (function-key-sequence ?d)) ; Down Arrow
85 (define-key blackbox-mode-map keys 'bb-down))
86 (if (setq keys (function-key-sequence ?l)) ; Left Arrow
87 (define-key blackbox-mode-map keys 'bb-left))
88 (if (setq keys (function-key-sequence ?r)) ; Right Arrow
89 (define-key blackbox-mode-map keys 'bb-right))
90 (if (setq keys (function-key-sequence ?e)) ; Enter
91 (define-key blackbox-mode-map keys 'bb-done))
92 (if (setq keys (function-key-sequence ?I)) ; Insert
93 (define-key blackbox-mode-map keys 'bb-romp))
94 )))
97 ;; Blackbox mode is suitable only for specially formatted data.
98 (put 'blackbox-mode 'mode-class 'special)
100 (defun blackbox-mode ()
101 "Major mode for playing blackbox. To learn how to play blackbox,
102 see the documentation for function `blackbox'.
104 The usual mnemonic keys move the cursor around the box.
105 \\<blackbox-mode-map>\\[bb-bol] and \\[bb-eol] move to the beginning and end of line, respectively.
107 \\[bb-romp] -- send in a ray from point, or toggle a ball at point
108 \\[bb-done] -- end game and get score
110 (interactive)
111 (kill-all-local-variables)
112 (use-local-map blackbox-mode-map)
113 (setq truncate-lines t)
114 (setq major-mode 'blackbox-mode)
115 (setq mode-name "Blackbox"))
117 ;;;###autoload
118 (defun blackbox (num)
119 "Play blackbox. Optional prefix argument is the number of balls;
120 the default is 4.
122 What is blackbox?
124 Blackbox is a game of hide and seek played on an 8 by 8 grid (the
125 Blackbox). Your opponent (Emacs, in this case) has hidden several
126 balls (usually 4) within this box. By shooting rays into the box and
127 observing where they emerge it is possible to deduce the positions of
128 the hidden balls. The fewer rays you use to find the balls, the lower
129 your score.
131 Overview of play:
133 To play blackbox, call the function `blackbox'. An optional prefix
134 argument specifies the number of balls to be hidden in the box; the
135 default is four.
137 The cursor can be moved around the box with the standard cursor
138 movement keys.
140 To shoot a ray, move the cursor to the edge of the box and press SPC.
141 The result will be determined and the playfield updated.
143 You may place or remove balls in the box by moving the cursor into the
144 box and pressing \\<bb-romp>.
146 When you think the configuration of balls you have placed is correct,
147 press \\<bb-done>. You will be informed whether you are correct or not, and
148 be given your score. Your score is the number of letters and numbers
149 around the outside of the box plus five for each incorrectly placed
150 ball. If you placed any balls incorrectly, they will be indicated
151 with `x', and their actual positions indicated with `o'.
153 Details:
155 There are three possible outcomes for each ray you send into the box:
157 Detour: the ray is deflected and emerges somewhere other than
158 where you sent it in. On the playfield, detours are
159 denoted by matching pairs of numbers -- one where the
160 ray went in, and the other where it came out.
162 Reflection: the ray is reflected and emerges in the same place
163 it was sent in. On the playfield, reflections are
164 denoted by the letter `R'.
166 Hit: the ray strikes a ball directly and is absorbed. It does
167 not emerge from the box. On the playfield, hits are
168 denoted by the letter `H'.
170 The rules for how balls deflect rays are simple and are best shown by
171 example.
173 As a ray approaches a ball it is deflected ninety degrees. Rays can
174 be deflected multiple times. In the diagrams below, the dashes
175 represent empty box locations and the letter `O' represents a ball.
176 The entrance and exit points of each ray are marked with numbers as
177 described under \"Detour\" above. Note that the entrance and exit
178 points are always interchangeable. `*' denotes the path taken by the
179 ray.
181 Note carefully the relative positions of the ball and the ninety
182 degree deflection it causes.
185 - * - - - - - - - - - - - - - - - - - - - - - -
186 - * - - - - - - - - - - - - - - - - - - - - - -
187 1 * * - - - - - - - - - - - - - - - O - - - - O -
188 - - O - - - - - - - O - - - - - - - * * * * - -
189 - - - - - - - - - - - * * * * * 2 3 * * * - - * - -
190 - - - - - - - - - - - * - - - - - - - O - * - -
191 - - - - - - - - - - - * - - - - - - - - * * - -
192 - - - - - - - - - - - * - - - - - - - - * - O -
195 As mentioned above, a reflection occurs when a ray emerges from the same point
196 it was sent in. This can happen in several ways:
199 - - - - - - - - - - - - - - - - - - - - - - - -
200 - - - - O - - - - - O - O - - - - - - - - - - -
201 R * * * * - - - - - - - * - - - - O - - - - - - -
202 - - - - O - - - - - - * - - - - R - - - - - - - -
203 - - - - - - - - - - - * - - - - - - - - - - - -
204 - - - - - - - - - - - * - - - - - - - - - - - -
205 - - - - - - - - R * * * * - - - - - - - - - - - -
206 - - - - - - - - - - - - O - - - - - - - - - - -
208 In the first example, the ray is deflected downwards by the upper
209 ball, then left by the lower ball, and finally retraces its path to
210 its point of origin. The second example is similar. The third
211 example is a bit anomalous but can be rationalized by realizing the
212 ray never gets a chance to get into the box. Alternatively, the ray
213 can be thought of as being deflected downwards and immediately
214 emerging from the box.
216 A hit occurs when a ray runs straight into a ball:
218 - - - - - - - - - - - - - - - - - - - - - - - -
219 - - - - - - - - - - - - - - - - - - - - O - - -
220 - - - - - - - - - - - - O - - - H * * * * - - - -
221 - - - - - - - - H * * * * O - - - - - - * - - - -
222 - - - - - - - - - - - - O - - - - - - O - - - -
223 H * * * O - - - - - - - - - - - - - - - - - - - -
224 - - - - - - - - - - - - - - - - - - - - - - - -
225 - - - - - - - - - - - - - - - - - - - - - - - -
227 Be sure to compare the second example of a hit with the first example of
228 a reflection."
229 (interactive "P")
230 (switch-to-buffer "*Blackbox*")
231 (blackbox-mode)
232 (setq buffer-read-only t)
233 (buffer-disable-undo (current-buffer))
234 (setq bb-board (bb-init-board (or num 4)))
235 (setq bb-balls-placed nil)
236 (setq bb-x -1)
237 (setq bb-y -1)
238 (setq bb-score 0)
239 (setq bb-detour-count 0)
240 (bb-insert-board)
241 (bb-goto (cons bb-x bb-y)))
243 (defun bb-init-board (num-balls)
244 (random t)
245 (let (board pos)
246 (while (>= (setq num-balls (1- num-balls)) 0)
247 (while
248 (progn
249 (setq pos (cons (random 8) (random 8)))
250 (bb-member pos board)))
251 (setq board (cons pos board)))
252 board))
254 (defun bb-insert-board ()
255 (let (i (buffer-read-only nil))
256 (erase-buffer)
257 (insert " \n")
258 (setq i 8)
259 (while (>= (setq i (1- i)) 0)
260 (insert " - - - - - - - - \n"))
261 (insert " \n")
262 (insert (format "\nThere are %d balls in the box" (length bb-board)))
265 (defun bb-right ()
266 (interactive)
267 (if (= bb-x 8)
269 (forward-char 2)
270 (setq bb-x (1+ bb-x))))
272 (defun bb-left ()
273 (interactive)
274 (if (= bb-x -1)
276 (backward-char 2)
277 (setq bb-x (1- bb-x))))
279 (defun bb-up ()
280 (interactive)
281 (if (= bb-y -1)
283 (previous-line 1)
284 (setq bb-y (1- bb-y))))
286 (defun bb-down ()
287 (interactive)
288 (if (= bb-y 8)
290 (next-line 1)
291 (setq bb-y (1+ bb-y))))
293 (defun bb-eol ()
294 (interactive)
295 (setq bb-x 8)
296 (bb-goto (cons bb-x bb-y)))
298 (defun bb-bol ()
299 (interactive)
300 (setq bb-x -1)
301 (bb-goto (cons bb-x bb-y)))
303 (defun bb-romp ()
304 (interactive)
305 (cond
306 ((and
307 (or (= bb-x -1) (= bb-x 8))
308 (or (= bb-y -1) (= bb-y 8))))
309 ((bb-outside-box bb-x bb-y)
310 (bb-trace-ray bb-x bb-y))
312 (bb-place-ball bb-x bb-y))))
314 (defun bb-place-ball (x y)
315 (let ((coord (cons x y)))
316 (cond
317 ((bb-member coord bb-balls-placed)
318 (setq bb-balls-placed (bb-delete coord bb-balls-placed))
319 (bb-update-board "-"))
321 (setq bb-balls-placed (cons coord bb-balls-placed))
322 (bb-update-board "O")))))
324 (defun bb-trace-ray (x y)
325 (let ((result (bb-trace-ray-2
328 (cond
329 ((= x -1) 1)
330 ((= x 8) -1)
331 (t 0))
333 (cond
334 ((= y -1) 1)
335 ((= y 8) -1)
336 (t 0)))))
337 (cond
338 ((eq result 'hit)
339 (bb-update-board "H")
340 (setq bb-score (1+ bb-score)))
341 ((equal result (cons x y))
342 (bb-update-board "R")
343 (setq bb-score (1+ bb-score)))
345 (setq bb-detour-count (1+ bb-detour-count))
346 (bb-update-board (format "%d" bb-detour-count))
347 (save-excursion
348 (bb-goto result)
349 (bb-update-board (format "%d" bb-detour-count)))
350 (setq bb-score (+ bb-score 2))))))
352 (defun bb-trace-ray-2 (first x dx y dy)
353 (cond
354 ((and (not first)
355 (bb-outside-box x y))
356 (cons x y))
357 ((bb-member (cons (+ x dx) (+ y dy)) bb-board)
358 'hit)
359 ((bb-member (cons (+ x dx dy) (+ y dy dx)) bb-board)
360 (bb-trace-ray-2 nil x (- dy) y (- dx)))
361 ((bb-member (cons (+ x dx (- dy)) (+ y dy (- dx))) bb-board)
362 (bb-trace-ray-2 nil x dy y dx))
364 (bb-trace-ray-2 nil (+ x dx) dx (+ y dy) dy))))
366 (defun bb-done ()
367 "Finish the game and report score."
368 (interactive)
369 (let (bogus-balls)
370 (cond
371 ((not (= (length bb-balls-placed) (length bb-board)))
372 (message "There %s %d hidden ball%s; you have placed %d."
373 (if (= (length bb-board) 1) "is" "are")
374 (length bb-board)
375 (if (= (length bb-board) 1) "" "s")
376 (length bb-balls-placed)))
378 (setq bogus-balls (bb-show-bogus-balls bb-balls-placed bb-board))
379 (if (= bogus-balls 0)
380 (message "Right! Your score is %d." bb-score)
381 (message "Oops! You missed %d ball%s. Your score is %d."
382 bogus-balls
383 (if (= bogus-balls 1) "" "s")
384 (+ bb-score (* 5 bogus-balls))))
385 (bb-goto '(-1 . -1))))))
387 (defun bb-show-bogus-balls (balls-placed board)
388 (bb-show-bogus-balls-2 balls-placed board "x")
389 (bb-show-bogus-balls-2 board balls-placed "o"))
391 (defun bb-show-bogus-balls-2 (list-1 list-2 c)
392 (cond
393 ((null list-1)
395 ((bb-member (car list-1) list-2)
396 (bb-show-bogus-balls-2 (cdr list-1) list-2 c))
398 (bb-goto (car list-1))
399 (bb-update-board c)
400 (1+ (bb-show-bogus-balls-2 (cdr list-1) list-2 c)))))
402 ;; blackbox.el ends here
404 (defun bb-goto (pos)
405 (goto-char (+ (* (car pos) 2) (* (cdr pos) 22) 26)))
407 (defun bb-update-board (c)
408 (let ((buffer-read-only nil))
409 (backward-char (1- (length c)))
410 (delete-char (length c))
411 (insert c)
412 (backward-char 1)))
414 (defun bb-member (elt list)
415 "Returns non-nil if ELT is an element of LIST."
416 (eval (cons 'or (mapcar (function (lambda (x) (equal x elt))) list))))
418 (defun bb-delete (item list)
419 "Deletes ITEM from LIST and returns a copy."
420 (cond
421 ((equal item (car list)) (cdr list))
422 (t (cons (car list) (bb-delete item (cdr list))))))
424 ;;; blackbox.el ends here