-
Notifications
You must be signed in to change notification settings - Fork 5
/
LIFE.REXX
85 lines (77 loc) · 1.93 KB
/
LIFE.REXX
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
/* Conway's Game of Life in REXX for z/VM */
/* COPYRIGHT 2024 BY MOSHIX */
/* COMPATIBLE WITH VM AND Z/VM REXX */
/* V 0.1 ALL IN GO, WORKS BUT CAN BE IMPROVED */
version = "0.1"
rows = 10
cols = 10
generations = 5
/* Initialize world with random data */
do row = 1 to rows
do col = 1 to cols
world.row.col = random(0, 1)
end
end
/* Display the initial world */
say "Conway Game of Life - Version: "version
say 'Initial World:'
call DisplayWorld
/* Run through generations */
do gen = 1 to generations
say 'Generation:' gen
call NextGeneration
call DisplayWorld
end
exit
NextGeneration:
/* Temporary world for calculations */
do row = 1 to rows
do col = 1 to cols
neighbors = CountNeighbors(row, col)
/* Apply rules of the Game */
if world.row.col = 1 then do
if neighbors < 2 | neighbors > 3 then
temp.row.col = 0
else
temp.row.col = 1
end
else do
if neighbors = 3 then
temp.row.col = 1
else
temp.row.col = 0
end
end
end
/* Copy the temp world to the main world */
do row = 1 to rows
do col = 1 to cols
world.row.col = temp.row.col
end
end
return
CountNeighbors:
parse arg row, col
neighbors = 0
do dr = -1 to 1
do dc = -1 to 1
if dr = 0 & dc = 0 then iterate
nr = row + dr
nc = col + dc
if nr > 0 & nr <= rows & nc > 0 & nc <= cols then
neighbors = neighbors + world.nr.nc
end
end
return neighbors
DisplayWorld:
do row = 1 to rows
line = ''
do col = 1 to cols
if world.row.col = 1 then
line = line || '*'
else
line = line || ' '
end
say line
end
return