Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Roblox
GitHub Repository: Roblox/luau
Path: blob/master/bench/tests/life.lua
2725 views
1
local function prequire(name) local success, result = pcall(require, name); return success and result end
2
local bench = script and require(script.Parent.bench_support) or prequire("bench_support") or require("../bench_support")
3
4
function test()
5
6
-- life.lua
7
-- original by Dave Bollinger <[email protected]> posted to lua-l
8
-- modified to use ANSI terminal escape sequences
9
-- modified to use for instead of while
10
11
-- local write=io.write
12
13
ALIVE="O" DEAD="-"
14
15
--function delay() -- NOTE: SYSTEM-DEPENDENT, adjust as necessary
16
-- for i=1,10000 do end
17
-- local i=os.clock()+1 while(os.clock()<i) do end
18
--end
19
20
function ARRAY2D(w,h)
21
local t = {w=w,h=h}
22
for y=1,h do
23
t[y] = {}
24
for x=1,w do
25
t[y][x]=0
26
end
27
end
28
return t
29
end
30
31
_CELLS = {}
32
33
-- give birth to a "shape" within the cell array
34
function _CELLS:spawn(shape,left,top)
35
for y=0,shape.h-1 do
36
for x=0,shape.w-1 do
37
self[top+y][left+x] = shape[y*shape.w+x+1]
38
end
39
end
40
end
41
42
-- run the CA and produce the next generation
43
function _CELLS:evolve(next)
44
local ym1,y,yp1,yi=self.h-1,self.h,1,self.h
45
while yi > 0 do
46
local xm1,x,xp1,xi=self.w-1,self.w,1,self.w
47
while xi > 0 do
48
local sum = self[ym1][xm1] + self[ym1][x] + self[ym1][xp1] +
49
self[y][xm1] + self[y][xp1] +
50
self[yp1][xm1] + self[yp1][x] + self[yp1][xp1]
51
next[y][x] = ((sum==2) and self[y][x]) or ((sum==3) and 1) or 0
52
xm1,x,xp1,xi = x,xp1,xp1+1,xi-1
53
end
54
ym1,y,yp1,yi = y,yp1,yp1+1,yi-1
55
end
56
end
57
58
-- output the array to screen
59
--function _CELLS:draw()
60
-- local out="" -- accumulate to reduce flicker
61
-- for y=1,self.h do
62
-- for x=1,self.w do
63
-- out=out..(((self[y][x]>0) and ALIVE) or DEAD)
64
-- end
65
-- out=out.."\n"
66
-- end
67
-- write(out)
68
--end
69
70
-- constructor
71
function CELLS(w,h)
72
local c = ARRAY2D(w,h)
73
c.spawn = _CELLS.spawn
74
c.evolve = _CELLS.evolve
75
c.draw = _CELLS.draw
76
return c
77
end
78
79
--
80
-- shapes suitable for use with spawn() above
81
--
82
HEART = { 1,0,1,1,0,1,1,1,1; w=3,h=3 }
83
GLIDER = { 0,0,1,1,0,1,0,1,1; w=3,h=3 }
84
EXPLODE = { 0,1,0,1,1,1,1,0,1,0,1,0; w=3,h=4 }
85
FISH = { 0,1,1,1,1,1,0,0,0,1,0,0,0,0,1,1,0,0,1,0; w=5,h=4 }
86
BUTTERFLY = { 1,0,0,0,1,0,1,1,1,0,1,0,0,0,1,1,0,1,0,1,1,0,0,0,1; w=5,h=5 }
87
88
-- the main routine
89
function LIFE(w,h)
90
-- create two arrays
91
local thisgen = CELLS(w,h)
92
local nextgen = CELLS(w,h)
93
94
-- create some life
95
-- about 1000 generations of fun, then a glider steady-state
96
thisgen:spawn(GLIDER,5,4)
97
thisgen:spawn(EXPLODE,25,10)
98
thisgen:spawn(FISH,4,12)
99
100
-- run until break
101
local gen=1
102
-- write("\027[2J") -- ANSI clear screen
103
while 1 do
104
thisgen:evolve(nextgen)
105
thisgen,nextgen = nextgen,thisgen
106
--write("\027[H") -- ANSI home cursor
107
--thisgen:draw()
108
--write("Life - generation ",gen,"\n")
109
gen=gen+1
110
if gen>1000 then break end
111
--delay() -- no delay
112
end
113
end
114
115
116
local ts0 = os.clock()
117
LIFE(40,20)
118
local ts1 = os.clock()
119
120
return ts1 - ts0
121
end
122
123
bench.runCode(test, "life")
124