annotate lisp/games/life.el @ 4:b82b59fe008d r19-15b3

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