Mercurial > emacs
annotate lisp/play/life.el @ 659:505130d1ddf8
*** empty log message ***
author | Eric S. Raymond <esr@snark.thyrsus.com> |
---|---|
date | Sat, 30 May 1992 22:12:04 +0000 |
parents | 4cd7543be581 |
children | 4f28bd14272c |
rev | line source |
---|---|
659
505130d1ddf8
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
584
diff
changeset
|
1 ;;; life.el --- John Horton Conway's `Life' game for GNU Emacs |
505130d1ddf8
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
584
diff
changeset
|
2 |
53 | 3 ;; Copyright (C) 1988 Free Software Foundation, Inc. |
4 ;; Contributed by Kyle Jones, talos!kjones@uunet.uu.net | |
5 | |
6 ;; This file is part of GNU Emacs. | |
7 | |
8 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
9 ;; it under the terms of the GNU General Public License as published by | |
10 ;; the Free Software Foundation; either version 1, or (at your option) | |
11 ;; any later version. | |
12 | |
13 ;; GNU Emacs is distributed in the hope that it will be useful, | |
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 ;; GNU General Public License for more details. | |
17 | |
18 ;; You should have received a copy of the GNU General Public License | |
19 ;; along with GNU Emacs; see the file COPYING. If not, write to | |
20 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
21 | |
22 (defconst life-patterns | |
23 [("@@@" " @@" "@@@") | |
24 ("@@@ @@@" "@@ @@ " "@@@ @@@") | |
25 ("@@@ @@@" "@@ @@" "@@@ @@@") | |
26 ("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@") | |
27 ("@@@@@@@@@@") | |
28 (" @@@@@@@@@@ " | |
29 " @@@@@@@@@@ " | |
30 " @@@@@@@@@@ " | |
31 "@@@@@@@@@@ " | |
32 "@@@@@@@@@@ ") | |
33 ("@" "@" "@" "@" "@" "@" "@" "@" "@" "@" "@" "@" "@" "@" "@") | |
34 ("@ @" "@ @" "@ @" | |
35 "@ @" "@ @" "@ @" | |
36 "@ @" "@ @" "@ @" | |
37 "@ @" "@ @" "@ @" | |
38 "@ @" "@ @" "@ @") | |
39 ("@@ " " @@ " " @@ " | |
40 " @@ " " @@ " " @@ " | |
41 " @@ " " @@ " " @@ " | |
42 " @@ " " @@ " " @@ " | |
43 " @@ " " @@ " " @@ " | |
44 " @@") | |
45 ("@@@@@@@@@" "@ @ @" "@ @@@@@ @" "@ @ @ @" "@@@ @@@" | |
46 "@ @ @ @" "@ @@@@@ @" "@ @ @" "@@@@@@@@@")] | |
47 "Vector of rectangles containing some Life startup patterns.") | |
48 | |
49 ;; Macros are used macros for manifest constants instead of variables | |
50 ;; because the compiler will convert them to constants, which should | |
51 ;; eval faster than symbols. | |
52 ;; | |
53 ;; The (require) wrapping forces the compiler to eval these macros at | |
54 ;; compile time. This would not be necessary if we did not use macros | |
55 ;; inside of macros, which the compiler doesn't seem to check for. | |
56 ;; | |
57 ;; Don't change any of the life-* macro constants unless you thoroughly | |
58 ;; understand the `life-grim-reaper' function. | |
59 (require | |
60 (progn | |
61 (defmacro life-life-char () ?@) | |
62 (defmacro life-death-char () (1+ (life-life-char))) | |
63 (defmacro life-birth-char () 3) | |
64 (defmacro life-void-char () ?\ ) | |
65 | |
66 (defmacro life-life-string () (char-to-string (life-life-char))) | |
67 (defmacro life-death-string () (char-to-string (life-death-char))) | |
68 (defmacro life-birth-string () (char-to-string (life-birth-char))) | |
69 (defmacro life-void-string () (char-to-string (life-void-char))) | |
70 (defmacro life-not-void-regexp () (concat "[^" (life-void-string) "\n]")) | |
71 | |
72 ;; try to optimize the (goto-char (point-min)) & (goto-char (point-max)) | |
73 ;; idioms. This depends on goto-char's not griping if we underrshoot | |
74 ;; or overshoot beginning or end of buffer. | |
75 (defmacro goto-beginning-of-buffer () '(goto-char 1)) | |
76 (defmacro maxint () (lsh (lsh (lognot 0) 1) -1)) | |
77 (defmacro goto-end-of-buffer () '(goto-char (maxint))) | |
78 | |
79 (defmacro increment (variable) (list 'setq variable (list '1+ variable))) | |
80 'life)) | |
81 | |
82 ;; list of numbers that tell how many characters to move to get to | |
83 ;; each of a cell's eight neighbors. | |
84 (defconst life-neighbor-deltas nil) | |
85 | |
86 ;; window display always starts here. Easier to deal with than | |
87 ;; (scroll-up) and (scroll-down) when trying to center the display. | |
88 (defconst life-window-start nil) | |
89 | |
90 ;; For mode line | |
91 (defconst life-current-generation nil) | |
92 ;; Sadly, mode-line-format won't display numbers. | |
93 (defconst life-generation-string nil) | |
94 | |
95 (defun abs (n) (if (< n 0) (- n) n)) | |
96 | |
97 (defun life (&optional sleeptime) | |
98 "Run Conway's Life simulation. | |
215 | 99 The starting pattern is randomly selected. Prefix arg (optional first |
100 arg non-nil from a program) is the number of seconds to sleep between | |
53 | 101 generations (this defaults to 1)." |
102 (interactive "p") | |
103 (or sleeptime (setq sleeptime 1)) | |
104 (life-setup) | |
105 (life-display-generation sleeptime) | |
106 (while t | |
107 (let ((inhibit-quit t)) | |
108 (life-grim-reaper) | |
109 (life-expand-plane-if-needed) | |
110 (life-increment-generation) | |
111 (life-display-generation sleeptime)))) | |
112 | |
113 (fset 'life-mode 'life) | |
114 (put 'life-mode 'mode-class 'special) | |
115 | |
116 (random t) | |
117 | |
118 (defun life-setup () | |
119 (let (n) | |
120 (switch-to-buffer (get-buffer-create "*Life*") t) | |
121 (erase-buffer) | |
122 (kill-all-local-variables) | |
123 (setq case-fold-search nil | |
124 mode-name "Life" | |
125 major-mode 'life-mode | |
126 truncate-lines t | |
127 life-current-generation 0 | |
128 life-generation-string "0" | |
129 mode-line-buffer-identification '("Life: generation " | |
130 life-generation-string) | |
131 fill-column (1- (window-width)) | |
132 life-window-start 1) | |
133 (buffer-disable-undo (current-buffer)) | |
134 ;; stuff in the random pattern | |
135 (life-insert-random-pattern) | |
136 ;; make sure (life-life-char) is used throughout | |
137 (goto-beginning-of-buffer) | |
138 (while (re-search-forward (life-not-void-regexp) nil t) | |
139 (replace-match (life-life-string) t t)) | |
140 ;; center the pattern horizontally | |
141 (goto-beginning-of-buffer) | |
142 (setq n (/ (- fill-column (save-excursion (end-of-line) (point))) 2)) | |
143 (while (not (eobp)) | |
144 (indent-to n) | |
145 (forward-line)) | |
146 ;; center the pattern vertically | |
147 (setq n (/ (- (1- (window-height)) | |
148 (count-lines (point-min) (point-max))) | |
149 2)) | |
150 (goto-beginning-of-buffer) | |
151 (newline n) | |
152 (goto-end-of-buffer) | |
153 (newline n) | |
154 ;; pad lines out to fill-column | |
155 (goto-beginning-of-buffer) | |
156 (while (not (eobp)) | |
157 (end-of-line) | |
158 (indent-to fill-column) | |
159 (move-to-column fill-column) | |
160 (delete-region (point) (progn (end-of-line) (point))) | |
161 (forward-line)) | |
162 ;; expand tabs to spaces | |
163 (untabify (point-min) (point-max)) | |
164 ;; before starting be sure the automaton has room to grow | |
165 (life-expand-plane-if-needed) | |
166 ;; compute initial neighbor deltas | |
167 (life-compute-neighbor-deltas))) | |
168 | |
169 (defun life-compute-neighbor-deltas () | |
170 (setq life-neighbor-deltas | |
171 (list -1 (- fill-column) | |
172 (- (1+ fill-column)) (- (+ 2 fill-column)) | |
173 1 fill-column (1+ fill-column) | |
174 (+ 2 fill-column)))) | |
175 | |
176 (defun life-insert-random-pattern () | |
177 (insert-rectangle | |
178 (elt life-patterns (% (abs (random)) (length life-patterns)))) | |
179 (insert ?\n)) | |
180 | |
181 (defun life-increment-generation () | |
182 (increment life-current-generation) | |
183 (setq life-generation-string (int-to-string life-current-generation))) | |
184 | |
185 (defun life-grim-reaper () | |
186 ;; Clear the match information. Later we check to see if it | |
187 ;; is still clear, if so then all the cells have died. | |
188 (store-match-data nil) | |
189 (goto-beginning-of-buffer) | |
190 ;; For speed declare all local variable outside the loop. | |
191 (let (point char pivot living-neighbors list) | |
192 (while (search-forward (life-life-string) nil t) | |
193 (setq list life-neighbor-deltas | |
194 living-neighbors 0 | |
195 pivot (1- (point))) | |
196 (while list | |
197 (setq point (+ pivot (car list)) | |
198 char (char-after point)) | |
199 (cond ((eq char (life-void-char)) | |
200 (subst-char-in-region point (1+ point) | |
201 (life-void-char) 1 t)) | |
202 ((< char 3) | |
203 (subst-char-in-region point (1+ point) char (1+ char) t)) | |
204 ((< char 9) | |
205 (subst-char-in-region point (1+ point) char 9 t)) | |
206 ((>= char (life-life-char)) | |
207 (increment living-neighbors))) | |
208 (setq list (cdr list))) | |
209 (if (memq living-neighbors '(2 3)) | |
210 () | |
211 (subst-char-in-region pivot (1+ pivot) | |
212 (life-life-char) (life-death-char) t)))) | |
213 (if (null (match-beginning 0)) | |
214 (life-extinct-quit)) | |
215 (subst-char-in-region 1 (point-max) 9 (life-void-char) t) | |
216 (subst-char-in-region 1 (point-max) 1 (life-void-char) t) | |
217 (subst-char-in-region 1 (point-max) 2 (life-void-char) t) | |
218 (subst-char-in-region 1 (point-max) (life-birth-char) (life-life-char) t) | |
219 (subst-char-in-region 1 (point-max) (life-death-char) (life-void-char) t)) | |
220 | |
221 (defun life-expand-plane-if-needed () | |
222 (catch 'done | |
223 (goto-beginning-of-buffer) | |
224 (while (not (eobp)) | |
225 ;; check for life at beginning or end of line. If found at | |
226 ;; either end, expand at both ends, | |
227 (cond ((or (eq (following-char) (life-life-char)) | |
228 (eq (progn (end-of-line) (preceding-char)) (life-life-char))) | |
229 (goto-beginning-of-buffer) | |
230 (while (not (eobp)) | |
231 (insert (life-void-char)) | |
232 (end-of-line) | |
233 (insert (life-void-char)) | |
234 (forward-char)) | |
235 (setq fill-column (+ 2 fill-column)) | |
236 (scroll-left 1) | |
237 (life-compute-neighbor-deltas) | |
238 (throw 'done t))) | |
239 (forward-line))) | |
240 (goto-beginning-of-buffer) | |
241 ;; check for life within the first two lines of the buffer. | |
242 ;; If present insert two lifeless lines at the beginning.. | |
243 (cond ((search-forward (life-life-string) | |
244 (+ (point) fill-column fill-column 2) t) | |
245 (goto-beginning-of-buffer) | |
246 (insert-char (life-void-char) fill-column) | |
247 (insert ?\n) | |
248 (insert-char (life-void-char) fill-column) | |
249 (insert ?\n) | |
250 (setq life-window-start (+ life-window-start fill-column 1)))) | |
251 (goto-end-of-buffer) | |
252 ;; check for life within the last two lines of the buffer. | |
253 ;; If present insert two lifeless lines at the end. | |
254 (cond ((search-backward (life-life-string) | |
255 (- (point) fill-column fill-column 2) t) | |
256 (goto-end-of-buffer) | |
257 (insert-char (life-void-char) fill-column) | |
258 (insert ?\n) | |
259 (insert-char (life-void-char) fill-column) | |
260 (insert ?\n) | |
261 (setq life-window-start (+ life-window-start fill-column 1))))) | |
262 | |
263 (defun life-display-generation (sleeptime) | |
264 (goto-char life-window-start) | |
265 (recenter 0) | |
266 (sit-for sleeptime)) | |
267 | |
268 (defun life-extinct-quit () | |
269 (life-display-generation 0) | |
270 (signal 'life-extinct nil)) | |
271 | |
272 (put 'life-extinct 'error-conditions '(life-extinct quit)) | |
273 (put 'life-extinct 'error-message "All life has perished") | |
584 | 274 |
275 (provide 'life) | |
276 | |
659
505130d1ddf8
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
584
diff
changeset
|
277 ;;; life.el ends here |