-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlife.zero
163 lines (124 loc) · 4.95 KB
/
life.zero
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
#@ life.zero
def newMatrix(nx, ny)
newArray(replicate(ny, newArray(replicate(nx, 0))))
def getCell(ix, iy, matrix)
row := matrix.get(iy) ?? abort("internal error")
row.get(ix) ?? abort("internal error")
def getDimensions(matrix)
ny := getSize(matrix)
nx := getSize(matrix.get(0) ?? newArray([]))
(nx, ny)
def crossProduct(xs, ys)
match xs
case [] -> []
case x :: xs'
ys.map(y -> (x, y)) ++ crossProduct(xs', ys)
def getCoordinates(matrix)
(nx, ny) := getDimensions(matrix)
xs := (0 ...)[0, nx]
ys := (0 ...)[0, ny]
crossProduct(xs, ys)
def toggleCell(ix, iy, matrix)
row := matrix.get(iy) ?? abort("internal error")
value := row.get(ix) ?? abort("internal error")
matrix.set(iy, row.set(ix, 1 -- value))
def clickCell(x, y, pixelsPerCell, matrix)
ix := x // pixelsPerCell
iy := y // pixelsPerCell
toggleCell(ix, iy, matrix)
def createMask(x, y, nx, ny)
dx := (x + nx -- 1) % nx
ux := (x + 1) % nx
dy := (y + ny -- 1) % ny
uy := (y + 1) % ny
[(dx, dy), (dx, y), (dx, uy),
(x, dy), (x, uy),
(ux, dy), (ux, y), (ux, uy)]
def isAlive(matrix, (ix, iy))
getCell(ix, iy, matrix) =/= 0
def countNeighbors(matrix, ix, iy)
(nx, ny) := getDimensions(matrix)
createMask(ix, iy, nx, ny).count(isAlive(matrix))
def stepCell(matrix, matrix', (ix, iy))
alive := isAlive(matrix, (ix, iy))
neighbors := countNeighbors(matrix, ix, iy)
alive' := if alive then neighbors = 2 or neighbors = 3 else neighbors = 3
if alive xor alive' then toggleCell(ix, iy, matrix') else matrix'
def stepMatrix(matrix)
getCoordinates(matrix).cascade(stepCell(matrix), matrix)
def updateGridCell(contextID, windowID, pixelsPerCell, (ix, iy))
x1 := ix * pixelsPerCell
y1 := iy * pixelsPerCell
fillRectangle(contextID, windowID, x1, y1, pixelsPerCell, pixelsPerCell)
def flattenMatrix(matrix)
# matrix to [((ix, iy), value), ...]
zip((0 ...), toList(matrix)).map((iy, row) ->
zip(zip((0 ...), repeat(iy)), toList(row))).join
def updateCells(updateCell, matrix, matrix')
cells := flattenMatrix(matrix)
cells' := flattenMatrix(matrix')
coordinates := (zip(cells, cells') |:
(x -> x.first.second =/= x.second.second)).map(first <> first)
coordinates.map(updateCell).join
def runForever(updateCell, matrix)
matrix' := stepMatrix(matrix)
updateCells(updateCell, matrix, matrix') ++ runForever(updateCell, matrix')
def handleEvent(event, state)
(contextID, pixelsPerCell, matrix) := state
eventCode := event.lookup("code") ?? 0
windowID := event.lookup("event") ?? abort("internal error")
if eventCode = ERROR
abort(showEvent(event))
if eventCode = KEY_PRESS
keysym := event.lookup("keysym") ?? 0
updateCell := updateGridCell(contextID, windowID, pixelsPerCell)
if keysym = BACKSPACE_KEY
(nx, ny) := getDimensions(matrix)
matrix' := newMatrix(nx, ny)
state' := (contextID, pixelsPerCell, matrix')
Just((updateCells(updateCell, matrix, matrix'), state'))
if keysym = RETURN_KEY
Just((runForever(updateCell, matrix), state))
if keysym = ' '
matrix' := stepMatrix(matrix)
state' := (contextID, pixelsPerCell, matrix')
Just((updateCells(updateCell, matrix, matrix'), state'))
if keysym = ESCAPE_KEY
Void
Just((NO_RESPONSE, state))
if eventCode = BUTTON_PRESS
x := event.lookup("event-x") ?? 0
y := event.lookup("event-y") ?? 0
matrix' := clickCell(x, y, pixelsPerCell, matrix)
state' := (contextID, pixelsPerCell, matrix')
d := pixelsPerCell
x1 := (x // d) * d
y1 := (y // d) * d
Just((fillRectangle(contextID, windowID, x1, y1, d, d), state'))
Just((NO_RESPONSE, state))
def initialize(nx, ny, pixelsPerCell, serverData)
width := nx * pixelsPerCell
height := ny * pixelsPerCell
(info, vendor, formats, screens) := serverData
windowID := info.lookup("resource-id-base") ?? 0
if screens is screen :: _
rootID := screen.lookup("root") ?? 0
screenWidth := screen.lookup("width-in-pixels") ?? 0
screenHeight := screen.lookup("height-in-pixels") ?? 0
x := screenWidth // 2 -- width // 2
y := screenHeight // 2 -- height // 2
contextID := windowID + 1
background := screen.lookup("white-pixel") ??
parseBytes([255, 255, 255])
foreground := screen.lookup("black-pixel") ?? 0
response :=
createWindow(windowID, rootID, x, y, width, height) ++
wmHint(windowID) ++
mapWindow(windowID) ++
createGC(contextID, windowID, INVERT, foreground, background)
matrix := newMatrix(nx, ny)
(response, (contextID, pixelsPerCell, matrix))
abort("Error: no screens")
def main(input)
XWindow(initialize(12, 12, 50), handleEvent, input)
#@