-
Notifications
You must be signed in to change notification settings - Fork 0
/
life.cl
436 lines (383 loc) · 9.93 KB
/
life.cl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
(* The Game of Life
Tendo Kayiira, Summer '95
With code taken from /private/cool/class/examples/cells.cl
This introduction was taken off the internet. It gives a brief
description of the Game Of Life. It also gives the rules by which
this particular game follows.
Introduction
John Conway's Game of Life is a mathematical amusement, but it
is also much more: an insight into how a system of simple
cellualar automata can create complex, odd, and often aesthetically
pleasing patterns. It is played on a cartesian grid of cells
which are either 'on' or 'off' The game gets it's name from the
similarity between the behaviour of these cells and the behaviour
of living organisms.
The Rules
The playfield is a cartesian grid of arbitrary size. Each cell in
this grid can be in an 'on' state or an 'off' state. On each 'turn'
(called a generation,) the state of each cell changes simultaneously
depending on it's state and the state of all cells adjacent to it.
For 'on' cells,
If the cell has 0 or 1 neighbours which are 'on', the cell turns
'off'. ('dies of loneliness')
If the cell has 2 or 3 neighbours which are 'on', the cell stays
'on'. (nothing happens to that cell)
If the cell has 4, 5, 6, 7, 8, or 9 neighbours which are 'on',
the cell turns 'off'. ('dies of overcrowding')
For 'off' cells,
If the cell has 0, 1, 2, 4, 5, 6, 7, 8, or 9 neighbours which
are 'on', the cell stays 'off'. (nothing happens to that cell)
If the cell has 3 neighbours which are 'on', the cell turns
'on'. (3 neighbouring 'alive' cells 'give birth' to a fourth.)
Repeat for as many generations as desired.
*)
class Board inherits IO {
rows : Int;
columns : Int;
board_size : Int;
size_of_board(initial : String) : Int {
initial.length()
};
board_init(start : String) : SELF_TYPE {
(let size :Int <- size_of_board(start) in
{
if size = 15 then
{
rows <- 3;
columns <- 5;
board_size <- size;
}
else if size = 16 then
{
rows <- 4;
columns <- 4;
board_size <- size;
}
else if size = 20 then
{
rows <- 4;
columns <- 5;
board_size <- size;
}
else if size = 21 then
{
rows <- 3;
columns <- 7;
board_size <- size;
}
else if size = 25 then
{
rows <- 5;
columns <- 5;
board_size <- size;
}
else if size = 28 then
{
rows <- 7;
columns <- 4;
board_size <- size;
}
else -- If none of the above fit, then just give
{ -- the configuration of the most common board
rows <- 5;
columns <- 5;
board_size <- size;
}
fi fi fi fi fi fi;
self;
}
)
};
};
class CellularAutomaton inherits Board {
population_map : String;
init(map : String) : SELF_TYPE {
{
population_map <- map;
board_init(map);
self;
}
};
print() : SELF_TYPE {
(let i : Int <- 0 in
(let num : Int <- board_size in
{
out_string("\n");
while i < num loop
{
out_string(population_map.substr(i,columns));
out_string("\n");
i <- i + columns;
}
pool;
out_string("\n");
self;
}
) )
};
num_cells() : Int {
population_map.length()
};
cell(position : Int) : String {
if board_size - 1 < position then
" "
else
population_map.substr(position, 1)
fi
};
north(position : Int): String {
if (position - columns) < 0 then
" "
else
cell(position - columns)
fi
};
south(position : Int): String {
if board_size < (position + columns) then
" "
else
cell(position + columns)
fi
};
east(position : Int): String {
if (((position + 1) /columns ) * columns) = (position + 1) then
" "
else
cell(position + 1)
fi
};
west(position : Int): String {
if position = 0 then
" "
else
if ((position / columns) * columns) = position then
" "
else
cell(position - 1)
fi fi
};
northwest(position : Int): String {
if (position - columns) < 0 then
" "
else if ((position / columns) * columns) = position then
" "
else
north(position - 1)
fi fi
};
northeast(position : Int): String {
if (position - columns) < 0 then
" "
else if (((position + 1) /columns ) * columns) = (position + 1) then
" "
else
north(position + 1)
fi fi
};
southeast(position : Int): String {
if board_size < (position + columns) then
" "
else if (((position + 1) /columns ) * columns) = (position + 1) then
" "
else
south(position + 1)
fi fi
};
southwest(position : Int): String {
if board_size < (position + columns) then
" "
else if ((position / columns) * columns) = position then
" "
else
south(position - 1)
fi fi
};
neighbors(position: Int): Int {
{
if north(position) = "X" then 1 else 0 fi
+ if south(position) = "X" then 1 else 0 fi
+ if east(position) = "X" then 1 else 0 fi
+ if west(position) = "X" then 1 else 0 fi
+ if northeast(position) = "X" then 1 else 0 fi
+ if northwest(position) = "X" then 1 else 0 fi
+ if southeast(position) = "X" then 1 else 0 fi
+ if southwest(position) = "X" then 1 else 0 fi;
}
};
(* A cell will live if 2 or 3 of it's neighbors are alive. It dies
otherwise. A cell is born if only 3 of it's neighbors are alive. *)
cell_at_next_evolution(position : Int) : String {
if neighbors(position) = 3 then
"X"
else
if neighbors(position) = 2 then
if cell(position) = "X" then
"X"
else
"-"
fi
else
"-"
fi fi
};
evolve() : SELF_TYPE {
(let position : Int <- 0 in
(let num : Int <- num_cells() in
(let temp : String in
{
while position < num loop
{
temp <- temp.concat(cell_at_next_evolution(position));
position <- position + 1;
}
pool;
population_map <- temp;
self;
}
) ) )
};
(* This is where the background pattern is detremined by the user. More
patterns can be added as long as whoever adds keeps the board either
3x5, 4x5, 5x5, 3x7, 7x4, 4x4 with the row first then column. *)
option(): String {
{
(let num : Int in
{
out_string("\nPlease chose a number:\n");
out_string("\t1: A cross\n");
out_string("\t2: A slash from the upper left to lower right\n");
out_string("\t3: A slash from the upper right to lower left\n");
out_string("\t4: An X\n");
out_string("\t5: A greater than sign \n");
out_string("\t6: A less than sign\n");
out_string("\t7: Two greater than signs\n");
out_string("\t8: Two less than signs\n");
out_string("\t9: A 'V'\n");
out_string("\t10: An inverse 'V'\n");
out_string("\t11: Numbers 9 and 10 combined\n");
out_string("\t12: A full grid\n");
out_string("\t13: A 'T'\n");
out_string("\t14: A plus '+'\n");
out_string("\t15: A 'W'\n");
out_string("\t16: An 'M'\n");
out_string("\t17: An 'E'\n");
out_string("\t18: A '3'\n");
out_string("\t19: An 'O'\n");
out_string("\t20: An '8'\n");
out_string("\t21: An 'S'\n");
out_string("Your choice => ");
num <- in_int();
out_string("\n");
if num = 1 then
" XX XXXX XXXX XX "
else if num = 2 then
" X X X X X "
else if num = 3 then
"X X X X X"
else if num = 4 then
"X X X X X X X X X"
else if num = 5 then
"X X X X X "
else if num = 6 then
" X X X X X"
else if num = 7 then
"X X X XX X "
else if num = 8 then
" X XX X X X "
else if num = 9 then
"X X X X X "
else if num = 10 then
" X X X X X"
else if num = 11 then
"X X X X X X X X"
else if num = 12 then
"XXXXXXXXXXXXXXXXXXXXXXXXX"
else if num = 13 then
"XXXXX X X X X "
else if num = 14 then
" X X XXXXX X X "
else if num = 15 then
"X X X X X X X "
else if num = 16 then
" X X X X X X X"
else if num = 17 then
"XXXXX X XXXXX X XXXX"
else if num = 18 then
"XXX X X X X XXXX "
else if num = 19 then
" XX X XX X XX "
else if num = 20 then
" XX X XX X XX X XX X XX "
else if num = 21 then
" XXXX X XX X XXXX "
else
" "
fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi fi;
}
);
}
};
prompt() : Bool {
{
(let ans : String in
{
out_string("Would you like to continue with the next generation? \n");
out_string("Please use lowercase y or n for your answer [y]: ");
ans <- in_string();
out_string("\n");
if ans = "n" then
false
else
true
fi;
}
);
}
};
prompt2() : Bool {
(let ans : String in
{
out_string("\n\n");
out_string("Would you like to choose a background pattern? \n");
out_string("Please use lowercase y or n for your answer [n]: ");
ans <- in_string();
if ans = "y" then
true
else
false
fi;
}
)
};
};
class Main inherits CellularAutomaton {
cells : CellularAutomaton;
main() : SELF_TYPE {
{
(let continue : Bool in
(let choice : String in
{
out_string("Welcome to the Game of Life.\n");
out_string("There are many initial states to choose from. \n");
while prompt2() loop
{
continue <- true;
choice <- option();
cells <- (new CellularAutomaton).init(choice);
cells.print();
while continue loop
if prompt() then
{
cells.evolve();
cells.print();
}
else
continue <- false
fi
pool;
}
pool;
self;
} ) ); }
};
};