Mercurial > emacs
view lisp/play/blackbox.el @ 1710:26054080a152
* frame.h (struct frame): New fields `can_have_scrollbars' and
`has_vertical_scrollbars'.
(FRAME_CAN_HAVE_SCROLLBARS, FRAME_HAS_VERTICAL_SCROLLBARS): New
accessors, for both the MULTI_FRAME and non-MULTI_FRAME.
(VERTICAL_SCROLLBAR_WIDTH, WINDOW_VERTICAL_SCROLLBAR,
WINDOW_VERTICAL_SCROLLBAR_COLUMN,
WINDOW_VERTICAL_SCROLLBAR_HEIGHT): New macros.
* window.h (struct window): New field `vertical_scrollbar'.
* xterm.h (struct x_display): vertical_scrollbars,
judge_timestamp, vertical_scrollbar_extra: New fields.
(struct scrollbar): New struct.
(VERTICAL_SCROLLBAR_PIXEL_WIDTH, VERTICAL_SCROLLBAR_PIXEL_HEIGHT,
VERTICAL_SCROLLBAR_LEFT_BORDER, VERTICAL_SCROLLBAR_RIGHT_BORDER,
VERTICAL_SCROLLBAR_TOP_BORDER, VERTICAL_SCROLLBAR_BOTTOM_BORDER,
CHAR_TO_PIXEL_WIDTH, CHAR_TO_PIXEL_HEIGHT, PIXEL_TO_CHAR_WIDTH,
PIXEL_TO_CHAR_HEIGHT): New accessors and macros.
* frame.c (make_frame): Initialize the `can_have_scrollbars' and
`has_vertical_scrollbars' fields of the frame.
* term.c (term_init): Note that TERMCAP terminals don't support
scrollbars.
(mouse_position_hook): Document new args.
(set_vertical_scrollbar_hook, condemn_scrollbars_hook,
redeem_scrollbar_hook, judge_scrollbars_hook): New hooks.
* termhooks.h: Declare and document them.
(enum scrollbar_part): New type.
(struct input_event): Describe the new form of the scrollbar_click
event type. Change `part' from a Lisp_Object to an enum
scrollbar_part. Add a new field `scrollbar'.
* keyboard.c (kbd_buffer_get_event): Pass appropriate new
parameters to *mouse_position_hook, and make_lispy_movement.
* xfns.c (x_set_vertical_scrollbar): New function.
(x_figure_window_size): Use new macros to calculate frame size.
(Fx_create_frame): Note that X Windows frames do support scroll
bars. Default to "yes".
* xterm.c: #include <X11/cursorfont.h> and "window.h".
(x_vertical_scrollbar_cursor): New variable.
(x_term_init): Initialize it.
(last_mouse_bar, last_mouse_bar_frame, last_mouse_part,
last_mouse_scroll_range_start, last_mouse_scroll_range_end): New
variables.
(XTmouse_position): Use them to return scrollbar movement events.
Take new arguments, for that purpose.
(x_window_to_scrollbar, x_scrollbar_create,
x_scrollbar_set_handle, x_scrollbar_remove, x_scrollbar_move,
XTset_scrollbar, XTcondemn_scrollbars, XTredeem_scrollbar,
XTjudge_scrollbars, x_scrollbar_expose,
x_scrollbar_background_expose, x_scrollbar_handle_click,
x_scrollbar_handle_motion): New functions to implement scrollbars.
(x_term_init): Set the termhooks.h hooks to point to them.
(x_set_window_size): Use new macros to calculate frame size. Set
vertical_scrollbar_extra field.
(x_make_frame_visible): Use the frame accessor
FRAME_HAS_VERTICAL_SCROLLBARS to decide if we need to map the
frame's subwindows as well.
(XTread_socket): Use new size-calculation macros from xterm.h when
processing ConfigureNotify events.
(x_wm_set_size_hint): Use PIXEL_TO_CHAR_WIDTH and
PIXEL_TO_CHAR_HEIGHT macros.
* ymakefile (xdisp.o): This now depends on termhooks.h.
(xterm.o): This now depends on window.h.
Change the meaning of focus redirection to make switching windows
work properly. Fredirect_frame_focus has the details.
* frame.h (focus_frame): Doc fix.
[not MULTI_FRAME] (FRAME_FOCUS_FRAME): Make this Qnil, which
indicates no focus redirection, instead of zero, which is
selected_frame.
* frame.c (make_frame): Initialize f->focus_frame to Qnil, rather
than making it point to frame itself.
(Fselect_frame): If changing the selected frame from FOO to BAR,
make all redirections to FOO shift to BAR as well. Doc fix.
(Fredirect_frame_focus): Doc fix. Accept nil as a valid
redirection, not just as a default for FRAME.
(Fframe_focus): Doc fix.
* keyboard.c (kbd_buffer_store_event, kbd_buffer_get_event): Deal
with focus redirections being nil.
* xterm.c (XTframe_rehighlight): Doc fix. Deal with focus
redirections being nil.
It's a pain to remember that you can't assign to FRAME->visible.
Let's change all references to the `visible' member of struct
frame to use the accessor macros, and then write a setter for the
`visible' field that does the right thing.
* frame.h (FRAME_VISIBLE_P): Make this not an l-value.
(FRAME_SET_VISIBLE): New macro.
* frame.c (make_terminal_frame, Fdelete_frame): Use FRAME_SET_VISIBLE.
(Fframe_visible_p, Fvisible_frame_list): Use FRAME_VISIBLE_P and
FRAME_ICONIFIED_P.
* dispnew.c (Fredraw_display): Use the FRAME_VISIBLE_P and
FRAME_GARBAGED_P accessors.
* xdisp.c (redisplay): Use the FRAME_VISIBLE_P accessor.
* xfns.c (x_set_foreground_color, x_set_background_color,
x_set_cursor_color, x_set_border_pixel, x_set_icon_type): Use the
FRAME_VISIBLE_P accessor.
(Fx_create_frame): Use FRAME_SET_VISIBILITY.
* xterm.c (clear_cursor, x_display_bar_cursor,
x_display_box_cursor): Use FRAME_SET_VISIBILITY.
author | Jim Blandy <jimb@redhat.com> |
---|---|
date | Thu, 24 Dec 1992 06:07:02 +0000 |
parents | 213978acbc1e |
children | 507f64624555 |
line wrap: on
line source
;;; blackbox.el --- blackbox game in Emacs Lisp ;; Copyright (C) 1985, 1986, 1987, 1992 Free Software Foundation, Inc. ;; Author: F. Thomas May <uw-nsr!uw-warp!tom@beaver.cs.washington.edu> ;; Adapted-By: ESR ;; Keywords: games ;; This file is part of GNU Emacs. ;; GNU Emacs is free software; you can redistribute it and/or modify ;; it under the terms of the GNU General Public License as published by ;; the Free Software Foundation; either version 2, or (at your option) ;; any later version. ;; GNU Emacs is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;; GNU General Public License for more details. ;; You should have received a copy of the GNU General Public License ;; along with GNU Emacs; see the file COPYING. If not, write to ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. ;;; Commentary: ; by F. Thomas May <uw-nsr!uw-warp!tom@beaver.cs.washington.edu> ; doc comment by Root Boy Jim <rbj@dsys.icst.nbs.gov>, 27 Apr 89 ; interface improvements by ESR, Dec 5 1991. ; The object of the game is to find four hidden balls by shooting rays ; into the black box. There are four possibilities: 1) the ray will ; pass thru the box undisturbed, 2) it will hit a ball and be absorbed, ; 3) it will be deflected and exit the box, or 4) be deflected immediately, ; not even being allowed entry into the box. ; ; The strange part is the method of deflection. It seems that rays will ; not pass next to a ball, and change direction at right angles to avoid it. ; ; R 3 ; 1 - - - - - - - - 1 ; - - - - - - - - ; - O - - - - - - 3 ; 2 - - - - O - O - ; 4 - - - - - - - - ; 5 - - - - - - - - 5 ; - - - - - - - - R ; H - - - - - - - O ; 2 H 4 H ; ; Rays which enter and exit are numbered. You can see that rays 1 & 5 pass ; thru the box undisturbed. Ray 2 is deflected by the northwesternmost ; ball. Likewise rays 3 and 4. Rays which hit balls and are absorbed are ; marked with H. The bottom of the left and the right of the bottom hit ; the southeastern ball directly. Rays may also hit balls after being ; reflected. Consider the H on the bottom next to the 4. It bounces off ; the NW-ern most ball and hits the central ball. A ray shot from above ; the right side 5 would hit the SE-ern most ball. The R beneath the 5 ; is because the ball is returned instantly. It is not allowed into ; the box if it would reflect immediately. The R on the top is a more ; leisurely return. Both central balls would tend to deflect it east ; or west, but it cannot go either way, so it just retreats. ; ; At the end of the game, if you've placed guesses for as many balls as ; there are in the box, the true board position will be revealed. Each ; `x' is an incorrect guess of yours; `o' is the true location of a ball. ;;; Code: (defvar blackbox-mode-map nil "") (if blackbox-mode-map () (setq blackbox-mode-map (make-keymap)) (suppress-keymap blackbox-mode-map t) (define-key blackbox-mode-map "\C-f" 'bb-right) (define-key blackbox-mode-map "\C-b" 'bb-left) (define-key blackbox-mode-map "\C-p" 'bb-up) (define-key blackbox-mode-map "\C-n" 'bb-down) (define-key blackbox-mode-map "\C-e" 'bb-eol) (define-key blackbox-mode-map "\C-a" 'bb-bol) (define-key blackbox-mode-map " " 'bb-romp) (define-key blackbox-mode-map [insert] 'bb-romp) (define-key blackbox-mode-map "\C-m" 'bb-done) (define-key blackbox-mode-map [kp-enter] 'bb-done) ;; This is a kluge. What we really want is a general ;; feature for reminding terminal keys to the functions ;; corresponding to them in local maps. (mapcar (function (lambda (funk) (mapcar (function (lambda (key) (define-key blackbox-mode-map key funk))) (where-is-internal funk)))) '(previous-line next-line backward-character forward-character))) ;; Blackbox mode is suitable only for specially formatted data. (put 'blackbox-mode 'mode-class 'special) (defun blackbox-mode () "Major mode for playing blackbox. To learn how to play blackbox, see the documentation for function `blackbox'. The usual mnemonic keys move the cursor around the box. \\<blackbox-mode-map>\\[bb-bol] and \\[bb-eol] move to the beginning and end of line, respectively. \\[bb-romp] -- send in a ray from point, or toggle a ball at point \\[bb-done] -- end game and get score " (interactive) (kill-all-local-variables) (use-local-map blackbox-mode-map) (setq truncate-lines t) (setq major-mode 'blackbox-mode) (setq mode-name "Blackbox")) ;;;###autoload (defun blackbox (num) "Play blackbox. Optional prefix argument is the number of balls; the default is 4. What is blackbox? Blackbox is a game of hide and seek played on an 8 by 8 grid (the Blackbox). Your opponent (Emacs, in this case) has hidden several balls (usually 4) within this box. By shooting rays into the box and observing where they emerge it is possible to deduce the positions of the hidden balls. The fewer rays you use to find the balls, the lower your score. Overview of play: \\<blackbox-mode-map>\ To play blackbox, type \\[blackbox]. An optional prefix argument specifies the number of balls to be hidden in the box; the default is four. The cursor can be moved around the box with the standard cursor movement keys. To shoot a ray, move the cursor to the edge of the box and press SPC. The result will be determined and the playfield updated. You may place or remove balls in the box by moving the cursor into the box and pressing \\[bb-romp]. When you think the configuration of balls you have placed is correct, press \\[bb-done]. You will be informed whether you are correct or not, and be given your score. Your score is the number of letters and numbers around the outside of the box plus five for each incorrectly placed ball. If you placed any balls incorrectly, they will be indicated with `x', and their actual positions indicated with `o'. Details: There are three possible outcomes for each ray you send into the box: Detour: the ray is deflected and emerges somewhere other than where you sent it in. On the playfield, detours are denoted by matching pairs of numbers -- one where the ray went in, and the other where it came out. Reflection: the ray is reflected and emerges in the same place it was sent in. On the playfield, reflections are denoted by the letter `R'. Hit: the ray strikes a ball directly and is absorbed. It does not emerge from the box. On the playfield, hits are denoted by the letter `H'. The rules for how balls deflect rays are simple and are best shown by example. As a ray approaches a ball it is deflected ninety degrees. Rays can be deflected multiple times. In the diagrams below, the dashes represent empty box locations and the letter `O' represents a ball. The entrance and exit points of each ray are marked with numbers as described under \"Detour\" above. Note that the entrance and exit points are always interchangeable. `*' denotes the path taken by the ray. Note carefully the relative positions of the ball and the ninety degree deflection it causes. 1 - * - - - - - - - - - - - - - - - - - - - - - - - * - - - - - - - - - - - - - - - - - - - - - - 1 * * - - - - - - - - - - - - - - - O - - - - O - - - O - - - - - - - O - - - - - - - * * * * - - - - - - - - - - - - - * * * * * 2 3 * * * - - * - - - - - - - - - - - - - * - - - - - - - O - * - - - - - - - - - - - - - * - - - - - - - - * * - - - - - - - - - - - - - * - - - - - - - - * - O - 2 3 As mentioned above, a reflection occurs when a ray emerges from the same point it was sent in. This can happen in several ways: - - - - - - - - - - - - - - - - - - - - - - - - - - - - O - - - - - O - O - - - - - - - - - - - R * * * * - - - - - - - * - - - - O - - - - - - - - - - - O - - - - - - * - - - - R - - - - - - - - - - - - - - - - - - - * - - - - - - - - - - - - - - - - - - - - - - - * - - - - - - - - - - - - - - - - - - - - R * * * * - - - - - - - - - - - - - - - - - - - - - - - - O - - - - - - - - - - - In the first example, the ray is deflected downwards by the upper ball, then left by the lower ball, and finally retraces its path to its point of origin. The second example is similar. The third example is a bit anomalous but can be rationalized by realizing the ray never gets a chance to get into the box. Alternatively, the ray can be thought of as being deflected downwards and immediately emerging from the box. A hit occurs when a ray runs straight into a ball: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - O - - - - - - - - - - - - - - - O - - - H * * * * - - - - - - - - - - - - H * * * * O - - - - - - * - - - - - - - - - - - - - - - - O - - - - - - O - - - - H * * * O - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Be sure to compare the second example of a hit with the first example of a reflection." (interactive "P") (switch-to-buffer "*Blackbox*") (blackbox-mode) (setq buffer-read-only t) (buffer-disable-undo (current-buffer)) (setq bb-board (bb-init-board (or num 4))) (setq bb-balls-placed nil) (setq bb-x -1) (setq bb-y -1) (setq bb-score 0) (setq bb-detour-count 0) (bb-insert-board) (bb-goto (cons bb-x bb-y))) (defun bb-init-board (num-balls) (random t) (let (board pos) (while (>= (setq num-balls (1- num-balls)) 0) (while (progn (setq pos (cons (random 8) (random 8))) (bb-member pos board))) (setq board (cons pos board))) board)) (defun bb-insert-board () (let (i (buffer-read-only nil)) (erase-buffer) (insert " \n") (setq i 8) (while (>= (setq i (1- i)) 0) (insert " - - - - - - - - \n")) (insert " \n") (insert (format "\nThere are %d balls in the box" (length bb-board))) )) (defun bb-right () (interactive) (if (= bb-x 8) () (forward-char 2) (setq bb-x (1+ bb-x)))) (defun bb-left () (interactive) (if (= bb-x -1) () (backward-char 2) (setq bb-x (1- bb-x)))) (defun bb-up () (interactive) (if (= bb-y -1) () (previous-line 1) (setq bb-y (1- bb-y)))) (defun bb-down () (interactive) (if (= bb-y 8) () (next-line 1) (setq bb-y (1+ bb-y)))) (defun bb-eol () (interactive) (setq bb-x 8) (bb-goto (cons bb-x bb-y))) (defun bb-bol () (interactive) (setq bb-x -1) (bb-goto (cons bb-x bb-y))) (defun bb-romp () (interactive) (cond ((and (or (= bb-x -1) (= bb-x 8)) (or (= bb-y -1) (= bb-y 8)))) ((bb-outside-box bb-x bb-y) (bb-trace-ray bb-x bb-y)) (t (bb-place-ball bb-x bb-y)))) (defun bb-place-ball (x y) (let ((coord (cons x y))) (cond ((bb-member coord bb-balls-placed) (setq bb-balls-placed (bb-delete coord bb-balls-placed)) (bb-update-board "-")) (t (setq bb-balls-placed (cons coord bb-balls-placed)) (bb-update-board "O"))))) (defun bb-trace-ray (x y) (let ((result (bb-trace-ray-2 t x (cond ((= x -1) 1) ((= x 8) -1) (t 0)) y (cond ((= y -1) 1) ((= y 8) -1) (t 0))))) (cond ((eq result 'hit) (bb-update-board "H") (setq bb-score (1+ bb-score))) ((equal result (cons x y)) (bb-update-board "R") (setq bb-score (1+ bb-score))) (t (setq bb-detour-count (1+ bb-detour-count)) (bb-update-board (format "%d" bb-detour-count)) (save-excursion (bb-goto result) (bb-update-board (format "%d" bb-detour-count))) (setq bb-score (+ bb-score 2)))))) (defun bb-trace-ray-2 (first x dx y dy) (cond ((and (not first) (bb-outside-box x y)) (cons x y)) ((bb-member (cons (+ x dx) (+ y dy)) bb-board) 'hit) ((bb-member (cons (+ x dx dy) (+ y dy dx)) bb-board) (bb-trace-ray-2 nil x (- dy) y (- dx))) ((bb-member (cons (+ x dx (- dy)) (+ y dy (- dx))) bb-board) (bb-trace-ray-2 nil x dy y dx)) (t (bb-trace-ray-2 nil (+ x dx) dx (+ y dy) dy)))) (defun bb-done () "Finish the game and report score." (interactive) (let (bogus-balls) (cond ((not (= (length bb-balls-placed) (length bb-board))) (message "There %s %d hidden ball%s; you have placed %d." (if (= (length bb-board) 1) "is" "are") (length bb-board) (if (= (length bb-board) 1) "" "s") (length bb-balls-placed))) (t (setq bogus-balls (bb-show-bogus-balls bb-balls-placed bb-board)) (if (= bogus-balls 0) (message "Right! Your score is %d." bb-score) (message "Oops! You missed %d ball%s. Your score is %d." bogus-balls (if (= bogus-balls 1) "" "s") (+ bb-score (* 5 bogus-balls)))) (bb-goto '(-1 . -1)))))) (defun bb-show-bogus-balls (balls-placed board) (bb-show-bogus-balls-2 balls-placed board "x") (bb-show-bogus-balls-2 board balls-placed "o")) (defun bb-show-bogus-balls-2 (list-1 list-2 c) (cond ((null list-1) 0) ((bb-member (car list-1) list-2) (bb-show-bogus-balls-2 (cdr list-1) list-2 c)) (t (bb-goto (car list-1)) (bb-update-board c) (1+ (bb-show-bogus-balls-2 (cdr list-1) list-2 c))))) (defun bb-outside-box (x y) (or (= x -1) (= x 8) (= y -1) (= y 8))) (defun bb-goto (pos) (goto-char (+ (* (car pos) 2) (* (cdr pos) 22) 26))) (defun bb-update-board (c) (let ((buffer-read-only nil)) (backward-char (1- (length c))) (delete-char (length c)) (insert c) (backward-char 1))) (defun bb-member (elt list) "Returns non-nil if ELT is an element of LIST." (eval (cons 'or (mapcar (function (lambda (x) (equal x elt))) list)))) (defun bb-delete (item list) "Deletes ITEM from LIST and returns a copy." (cond ((equal item (car list)) (cdr list)) (t (cons (car list) (bb-delete item (cdr list)))))) ;;; blackbox.el ends here