-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlifeGame.pen
143 lines (129 loc) · 2.92 KB
/
lifeGame.pen
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
import Core'List
import Core'Number
import Core'String
type Context {
random \() number
}
type cellTuple {
first boolean
second boolean
third boolean
}
NewContext = \(random \() number) Context {
Context{random: random}
}
Step = \(rs [[boolean]]) [[boolean]] | error {
stepRows(
if [f, ..._] = rs {
if l = List'Last([any ...rs], none) as [boolean] {
[[boolean] l, ...rs, f()]
} else {
error("too few rows")
}
} else {
error("too few rows")
}?,
)
}
stepRows = \(rs [[boolean]]) [[boolean]] | error {
# TODO Use a generalized if-list expression.
if [r1, ...rs] = rs {
if [r2, ...rs] = rs {
if [r3, ...rs] = rs {
r = stepRow(r1(), r2(), r3())?
rs = stepRows([[boolean] r2(), r3(), ...rs])?
[[boolean] r, ...rs]
} else {
[[boolean]]
}
} else {
error("too few rows")
}
} else {
error("too few rows")
}
}
stepRow = \(upper [boolean], current [boolean], lower [boolean]) [boolean] | error {
stepColumns(loopRow(upper)?, loopRow(current)?, loopRow(lower)?)
}
loopRow = \(cs [boolean]) [boolean] | error {
if [f, ..._] = cs {
if l = List'Last([any ...cs], none) as boolean {
[boolean l, ...cs, f()]
} else {
error("too few columns")
}
} else {
error("too few columns")
}
}
stepColumns = \(upper [boolean], current [boolean], lower [boolean]) [boolean] | error {
if u = cellTuple(upper) as cellTuple {
if c = cellTuple(current) as cellTuple {
if l = cellTuple(lower) as cellTuple {
n = Number'Sum(
[number
if b() { 1 } else { 0 }
for b in [boolean
u.first,
u.second,
u.third,
c.first,
c.third,
l.first,
l.second,
l.third,
]
],
)
bs = stepColumns(tail(upper), tail(current), tail(lower))?
[boolean c.second & 2 <= n & n <= 3 | !c.second & n == 3, ...bs]
} else {
error("wrong row length")
}
} else {
error("wrong row length")
}
} else {
[boolean]
}
}
tail = \(cs [boolean]) [boolean] {
if [_, ...cs] = cs {
cs
} else {
[boolean]
}
}
cellTuple = \(cs [boolean]) cellTuple | none {
# TODO Use a generalized if-list expression.
if [c1, ...cs] = cs {
if [c2, ...cs] = cs {
if [c3, ...cs] = cs {
cellTuple{first: c1(), second: c2(), third: c3()}
} else {
none
}
} else {
none
}
} else {
none
}
}
Initialize = \(ctx Context, rows number, columns number) [[boolean]] {
[[boolean]
[boolean ctx.random() < 1 / 3 for _ in Number'Sequence(columns)]
for _ in Number'Sequence(rows)
]
}
Render = \(rs [[boolean]]) string {
String'Join(
[string
String'Concatenate([string if x() { "o" } else { "." } for x in r()])
for r in rs
],
"\n",
)
+ "\n"
}