1 ;;; life.el --- John Horton Conway's `Life' game for GNU Emacs
3 ;; Copyright (C) 1988, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Kyle Jones <kyleuunet.uu.net>
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 3, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
29 ;; A demonstrator for John Horton Conway's "Life" cellular automaton
30 ;; in Emacs Lisp. Picks a random one of a set of interesting Life
31 ;; patterns and evolves it according to the familiar rules.
37 ("@@@ @@@" "@@ @@ " "@@@ @@@")
38 ("@@@ @@@" "@@ @@" "@@@ @@@")
39 ("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")
46 ("@" "@" "@" "@" "@" "@" "@" "@" "@" "@" "@" "@" "@" "@" "@")
58 ("@@@@@@@@@" "@ @ @" "@ @@@@@ @" "@ @ @ @" "@@@ @@@"
59 "@ @ @ @" "@ @@@@@ @" "@ @ @" "@@@@@@@@@")
80 ("@@@@@@@@ @@@@@ @@@ @@@@@@@ @@@@@")]
81 "Vector of rectangles containing some Life startup patterns.")
83 ;; Macros are used macros for manifest constants instead of variables
84 ;; because the compiler will convert them to constants, which should
85 ;; eval faster than symbols.
87 ;; Don't change any of the life-* macro constants unless you thoroughly
88 ;; understand the `life-grim-reaper' function.
90 (defmacro life-life-char
() ?
@)
91 (defmacro life-death-char
() (1+ (life-life-char)))
92 (defmacro life-birth-char
() 3)
93 (defmacro life-void-char
() ?\
)
95 (defmacro life-life-string
() (char-to-string (life-life-char)))
96 (defmacro life-death-string
() (char-to-string (life-death-char)))
97 (defmacro life-birth-string
() (char-to-string (life-birth-char)))
98 (defmacro life-void-string
() (char-to-string (life-void-char)))
99 (defmacro life-not-void-regexp
() (concat "[^" (life-void-string) "\n]"))
101 (defmacro life-increment
(variable) (list 'setq variable
(list '1+ variable
)))
104 ;; list of numbers that tell how many characters to move to get to
105 ;; each of a cell's eight neighbors.
106 (defvar life-neighbor-deltas nil
)
108 ;; window display always starts here. Easier to deal with than
109 ;; (scroll-up) and (scroll-down) when trying to center the display.
110 (defvar life-window-start nil
)
113 (defvar life-current-generation nil
)
114 ;; Sadly, mode-line-format won't display numbers.
115 (defvar life-generation-string nil
)
117 (defvar life-initialized nil
118 "Non-nil if `life' has been run at least once.")
121 (defun life (&optional sleeptime
)
122 "Run Conway's Life simulation.
123 The starting pattern is randomly selected. Prefix arg (optional first
124 arg non-nil from a program) is the number of seconds to sleep between
125 generations (this defaults to 1)."
129 (setq life-initialized t
)
130 (or sleeptime
(setq sleeptime
1))
134 (let ((inhibit-quit t
))
135 (life-display-generation sleeptime
)
137 (life-expand-plane-if-needed)
138 (life-increment-generation)))))
140 (defalias 'life-mode
'life
)
141 (put 'life-mode
'mode-class
'special
)
145 (switch-to-buffer (get-buffer-create "*Life*") t
)
147 (kill-all-local-variables)
148 (setq case-fold-search nil
150 major-mode
'life-mode
152 show-trailing-whitespace nil
153 life-current-generation
0
154 life-generation-string
"0"
155 mode-line-buffer-identification
'("Life: generation "
156 life-generation-string
)
157 fill-column
(1- (window-width))
159 (buffer-disable-undo (current-buffer))
160 ;; stuff in the random pattern
161 (life-insert-random-pattern)
162 ;; make sure (life-life-char) is used throughout
163 (goto-char (point-min))
164 (while (re-search-forward (life-not-void-regexp) nil t
)
165 (replace-match (life-life-string) t t
))
166 ;; center the pattern horizontally
167 (goto-char (point-min))
168 (setq n
(/ (- fill-column
(save-excursion (end-of-line) (point))) 2))
172 ;; center the pattern vertically
173 (setq n
(/ (- (1- (window-height))
174 (count-lines (point-min) (point-max)))
176 (goto-char (point-min))
178 (goto-char (point-max))
180 ;; pad lines out to fill-column
181 (goto-char (point-min))
184 (indent-to fill-column
)
185 (move-to-column fill-column
)
186 (delete-region (point) (progn (end-of-line) (point)))
188 ;; expand tabs to spaces
189 (untabify (point-min) (point-max))
190 ;; before starting be sure the automaton has room to grow
191 (life-expand-plane-if-needed)
192 ;; compute initial neighbor deltas
193 (life-compute-neighbor-deltas)))
195 (defun life-compute-neighbor-deltas ()
196 (setq life-neighbor-deltas
197 (list -
1 (- fill-column
)
198 (- (1+ fill-column
)) (- (+ 2 fill-column
))
199 1 fill-column
(1+ fill-column
)
202 (defun life-insert-random-pattern ()
204 (elt life-patterns
(random (length life-patterns
))))
207 (defun life-increment-generation ()
208 (life-increment life-current-generation
)
209 (setq life-generation-string
(int-to-string life-current-generation
)))
211 (defun life-grim-reaper ()
212 ;; Clear the match information. Later we check to see if it
213 ;; is still clear, if so then all the cells have died.
215 (goto-char (point-min))
216 ;; For speed declare all local variable outside the loop.
217 (let (point char pivot living-neighbors list
)
218 (while (search-forward (life-life-string) nil t
)
219 (setq list life-neighbor-deltas
223 (setq point
(+ pivot
(car list
))
224 char
(char-after point
))
225 (cond ((eq char
(life-void-char))
226 (subst-char-in-region point
(1+ point
)
227 (life-void-char) 1 t
))
229 (subst-char-in-region point
(1+ point
) char
(1+ char
) t
))
231 (subst-char-in-region point
(1+ point
) char
9 t
))
232 ((>= char
(life-life-char))
233 (life-increment living-neighbors
)))
234 (setq list
(cdr list
)))
235 (if (memq living-neighbors
'(2 3))
237 (subst-char-in-region pivot
(1+ pivot
)
238 (life-life-char) (life-death-char) t
))))
239 (if (null (match-beginning 0))
241 (subst-char-in-region 1 (point-max) 9 (life-void-char) t
)
242 (subst-char-in-region 1 (point-max) 1 (life-void-char) t
)
243 (subst-char-in-region 1 (point-max) 2 (life-void-char) t
)
244 (subst-char-in-region 1 (point-max) (life-birth-char) (life-life-char) t
)
245 (subst-char-in-region 1 (point-max) (life-death-char) (life-void-char) t
))
247 (defun life-expand-plane-if-needed ()
249 (goto-char (point-min))
251 ;; check for life at beginning or end of line. If found at
252 ;; either end, expand at both ends,
253 (cond ((or (eq (following-char) (life-life-char))
254 (eq (progn (end-of-line) (preceding-char)) (life-life-char)))
255 (goto-char (point-min))
257 (insert (life-void-char))
259 (insert (life-void-char))
261 (setq fill-column
(+ 2 fill-column
))
263 (life-compute-neighbor-deltas)
266 (goto-char (point-min))
267 ;; check for life within the first two lines of the buffer.
268 ;; If present insert two lifeless lines at the beginning..
269 (cond ((search-forward (life-life-string)
270 (+ (point) fill-column fill-column
2) t
)
271 (goto-char (point-min))
272 (insert-char (life-void-char) fill-column
)
274 (insert-char (life-void-char) fill-column
)
276 (setq life-window-start
(+ life-window-start fill-column
1))))
277 (goto-char (point-max))
278 ;; check for life within the last two lines of the buffer.
279 ;; If present insert two lifeless lines at the end.
280 (cond ((search-backward (life-life-string)
281 (- (point) fill-column fill-column
2) t
)
282 (goto-char (point-max))
283 (insert-char (life-void-char) fill-column
)
285 (insert-char (life-void-char) fill-column
)
287 (setq life-window-start
(+ life-window-start fill-column
1)))))
289 (defun life-display-generation (sleeptime)
290 (goto-char life-window-start
)
293 ;; Redisplay; if the user has hit a key, exit the loop.
294 (or (and (sit-for sleeptime
) (< 0 sleeptime
))
295 (not (input-pending-p))
296 (throw 'life-exit nil
)))
298 (defun life-extinct-quit ()
299 (life-display-generation 0)
300 (signal 'life-extinct nil
))
302 (put 'life-extinct
'error-conditions
'(life-extinct quit
))
303 (put 'life-extinct
'error-message
"All life has perished")
307 ;;; arch-tag: e9373544-755e-42f5-a9a1-4d4c422bb97a
308 ;;; life.el ends here