1 ;;; life.el --- John Horton Conway's `Life' game for GNU Emacs
3 ;; Copyright (C) 1988 Free Software Foundation, Inc.
4 ;; Contributed by Kyle Jones, talos!kjones@uunet.uu.net
6 ;; This file is part of GNU Emacs.
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)
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.
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.
22 (defconst life-patterns
24 ("@@@ @@@" "@@ @@ " "@@@ @@@")
25 ("@@@ @@@" "@@ @@" "@@@ @@@")
26 ("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")
33 ("@" "@" "@" "@" "@" "@" "@" "@" "@" "@" "@" "@" "@" "@" "@")
45 ("@@@@@@@@@" "@ @ @" "@ @@@@@ @" "@ @ @ @" "@@@ @@@"
46 "@ @ @ @" "@ @@@@@ @" "@ @ @" "@@@@@@@@@")]
47 "Vector of rectangles containing some Life startup patterns.")
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.
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.
57 ;; Don't change any of the life-* macro constants unless you thoroughly
58 ;; understand the `life-grim-reaper' function.
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
() ?\
)
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]"))
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)))
79 (defmacro increment
(variable) (list 'setq variable
(list '1+ variable
)))
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
)
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
)
91 (defconst life-current-generation nil
)
92 ;; Sadly, mode-line-format won't display numbers.
93 (defconst life-generation-string nil
)
95 (defun abs (n) (if (< n
0) (- n
) n
))
97 (defun life (&optional sleeptime
)
98 "Run Conway's Life simulation.
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
101 generations (this defaults to 1)."
103 (or sleeptime
(setq sleeptime
1))
105 (life-display-generation sleeptime
)
107 (let ((inhibit-quit t
))
109 (life-expand-plane-if-needed)
110 (life-increment-generation)
111 (life-display-generation sleeptime
))))
113 (fset 'life-mode
'life
)
114 (put 'life-mode
'mode-class
'special
)
120 (switch-to-buffer (get-buffer-create "*Life*") t
)
122 (kill-all-local-variables)
123 (setq case-fold-search nil
125 major-mode
'life-mode
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))
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))
146 ;; center the pattern vertically
147 (setq n
(/ (- (1- (window-height))
148 (count-lines (point-min) (point-max)))
150 (goto-beginning-of-buffer)
154 ;; pad lines out to fill-column
155 (goto-beginning-of-buffer)
158 (indent-to fill-column
)
159 (move-to-column fill-column
)
160 (delete-region (point) (progn (end-of-line) (point)))
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)))
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
)
176 (defun life-insert-random-pattern ()
178 (elt life-patterns
(%
(abs (random)) (length life-patterns
))))
181 (defun life-increment-generation ()
182 (increment life-current-generation
)
183 (setq life-generation-string
(int-to-string life-current-generation
)))
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
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
))
203 (subst-char-in-region point
(1+ point
) char
(1+ char
) t
))
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))
211 (subst-char-in-region pivot
(1+ pivot
)
212 (life-life-char) (life-death-char) t
))))
213 (if (null (match-beginning 0))
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
))
221 (defun life-expand-plane-if-needed ()
223 (goto-beginning-of-buffer)
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)
231 (insert (life-void-char))
233 (insert (life-void-char))
235 (setq fill-column
(+ 2 fill-column
))
237 (life-compute-neighbor-deltas)
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
)
248 (insert-char (life-void-char) fill-column
)
250 (setq life-window-start
(+ life-window-start fill-column
1))))
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
)
257 (insert-char (life-void-char) fill-column
)
259 (insert-char (life-void-char) fill-column
)
261 (setq life-window-start
(+ life-window-start fill-column
1)))))
263 (defun life-display-generation (sleeptime)
264 (goto-char life-window-start
)
268 (defun life-extinct-quit ()
269 (life-display-generation 0)
270 (signal 'life-extinct nil
))
272 (put 'life-extinct
'error-conditions
'(life-extinct quit
))
273 (put 'life-extinct
'error-message
"All life has perished")
277 ;;; life.el ends here