-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.lua
309 lines (294 loc) · 6.72 KB
/
main.lua
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
suit = require 'suit'
-- storage for text input
local speedslider={value=1,min=100,max=1,step=-1}
local cellsizeslider = {value = 0,max=100,min=5,step=5}
function love.load()
cells={}
bufferCells={}
pixels={}
width=1000
height=800
uiwidth=1000
uiheight=200
cellsize=5
cellsizeslider.value=cellsize
gutterSize=1
cellnum = 0
draggingEnabled = false
livingcells=0
speed=1
ticks=0
go = false
W=0
E=1
N=2
S=3
NW=4
NE=5
SW=6
SE=7
smallerFont = love.graphics.newFont(10)
lastCell = {["h"]=-1,["w"]=-1}
love.window.setTitle("Eli's Conway's Game Of Life")
print(width/cellsize)
print(width+((width/cellsize)*gutterSize))
print(height+((height/cellsize)*gutterSize)+uiheight)
love.window.setMode(width, height+((height/cellsize)*gutterSize)+uiheight, {resizable=true, vsync=true, minwidth=800, minheight=600})
initcells()
end
function initcells()
ticks=0
cellnum=0
for h=0,height,cellsize+gutterSize do
cells[h]={}
for w=0,width,cellsize+gutterSize do
cells[h][w]=0
cellnum=cellnum+1
end
end
end
function drawui()
-- the layout will grow down and to the right from this point
suit.layout:reset(0,height)
rows = suit.layout:rows{pos = {0,height}, min_height = 300,
{200, 25},
{200, 25}, -- the first cell will measure 200 by 30 px
{200, 25},
{200, 25},
{200, 25},
{200, 25}
}
if suit.Slider(cellsizeslider,rows.cell(2)).changed then
cellsize=cellsizeslider.value
initcells()
end
if not go then
if suit.Button("Start", rows.cell(3)).hit then
go=true
end
elseif go then
if suit.Button("Stop", rows.cell(3)).hit then
go=false
end
end
if suit.Slider(speedslider,rows.cell(4)).changed then
speed=speedslider.value
end
if suit.Button("Reset", rows.cell(5)).hit then
initcells()
end
if suit.Button("Close", rows.cell(6)).hit then
love.event.quit()
end
end
function love.update()
drawui()
end
function love.draw()
tick()
suit.draw()
love.graphics.setBackgroundColor(0,0,0,0)
for h=0,height,cellsize+gutterSize do
for w=0,width,cellsize+gutterSize do
if cells[h][w]==0 then
love.graphics.setColor(1,0,0)
else
love.graphics.setColor(0,0,1)
end
love.graphics.rectangle('fill',w,h,cellsize,cellsize)
end
end
end
function love.mousepressed(x,y,button)
draggingEnabled=true
end
function love.mousemoved(x,y,dx,dy,istouch)
if draggingEnabled then
h,w=getCell(x,y)
if lastCell.h~=h or lastCell.w~=w then
r=setCell(x,y)
if r ~= nil then
h,w=r
lastCell.h=h
lastCell.w=w
end
end
end
end
function love.mousereleased( x, y, button, istouch, presses)
setCell(x,y)
draggingEnabled = false
end
function getCell(x,y)
for h=0,height,cellsize+gutterSize do
for w=0,width,cellsize+gutterSize do
if y>=h and y<=h+cellsize then
if x>=w and x<=w+cellsize then
return h,w
end
end
end
end
end
function setCell(x,y)
h,w=getCell(x,y)
if h~=nil and h<height and w<width then
if cells[h][w]==0 then
cells[h][w]=1
livingcells=livingcells+1
else
cells[h][w]=0
livingcells=livingcells-1
end
return h,w
end
return nil
end
function alive(h,w)
if cells[h][w]==0 then
return false
elseif cells[h][w]==1 then
return true
end
end
function getNeighborStatus(direction,h,w)
-- This is how our grid is setup, so we can do the math to look around a cell
-- ww w w
-- h 00:02652104
-- h 26:02652104
-- h 52:02652104
-- cell=26,3
-- abovecell=0,3
-- where cellsize=25 in this example and guttersize=1
totalcellsize=cellsize+gutterSize
if direction==E then
if w-totalcellsize>0 then
leftcell_w=w-totalcellsize
leftalive=alive(h,leftcell_w)
return leftalive
end
elseif direction==W then
if w+totalcellsize<width then
rightcell_w=w+totalcellsize
rightalive=alive(h,rightcell_w)
return rightalive
end
elseif direction==N then
abovecell=h-totalcellsize
if abovecell>=0 then
abovealive=alive(abovecell,w)
return abovealive
end
elseif direction==S then
belowcell=h+totalcellsize
if belowcell<height then
belowalive=alive(belowcell,w)
return belowalive
end
elseif direction==NW then
-- subtract one row and one column to get NE cell
nwcell_h=h-totalcellsize
nwcell_w=w-totalcellsize
if nwcell_h>=0 and nwcell_w>=0 then
return alive(nwcell_h,nwcell_w)
end
elseif direction==NE then
-- subtract one row and add one column to get NW cell
necell_h=h-totalcellsize
necell_w=w+totalcellsize
if necell_h>=0 and necell_w<width then
return alive(necell_h,necell_w)
end
elseif direction==SW then
-- add one row and subract one column to get SW cell
swcell_h=h+totalcellsize
swcell_w=w-totalcellsize
if swcell_h<height and swcell_w>=0 then
return alive(swcell_h,swcell_w)
end
elseif direction==SE then
-- add one row and add one column to get SE cell
secell_h=h+totalcellsize
secell_w=w+totalcellsize
if secell_h<height and secell_w<width then
return alive(secell_h,secell_w)
end
end
end
function killCell(h,w)
bufferCells[h][w]=0
end
function birthCell(h,w)
bufferCells[h][w]=1
end
function copy(obj, seen)
if type(obj) ~= 'table' then return obj end
if seen and seen[obj] then return seen[obj] end
local s = seen or {}
local res = setmetatable({}, getmetatable(obj))
s[obj] = res
for k, v in pairs(obj) do res[copy(k, s)] = copy(v, s) end
return res
end
function tick()
if ticks>speed then
if go then
-- copy cells into buffercells so we can flip/flop buffer and cells before/after each tick
bufferCells=copy(cells)
for h=0,height,cellsize+gutterSize do
for w=0,width,cellsize+gutterSize do
--w=west
--e=east
--n=north
--s=south
--nw=northwest
--ne=northeast
--se=southeast
--sw=southwest
WAlive=getNeighborStatus(W,h,w)
EAlive=getNeighborStatus(E,h,w)
NAlive=getNeighborStatus(N,h,w)
SAlive=getNeighborStatus(S,h,w)
NWAlive=getNeighborStatus(NW,h,w)
NEAlive=getNeighborStatus(NE,h,w)
SEAlive=getNeighborStatus(SE,h,w)
SWAlive=getNeighborStatus(SW,h,w)
neighborsAlive=0
if WAlive then
neighborsAlive=neighborsAlive+1
end
if EAlive then
neighborsAlive=neighborsAlive+1
end
if NAlive then
neighborsAlive=neighborsAlive+1
end
if SAlive then
neighborsAlive=neighborsAlive+1
end
if NEAlive then
neighborsAlive=neighborsAlive+1
end
if NWAlive then
neighborsAlive=neighborsAlive+1
end
if SEAlive then
neighborsAlive=neighborsAlive+1
end
if SWAlive then
neighborsAlive=neighborsAlive+1
end
if (neighborsAlive<2 or neighborsAlive>3) and alive(h,w) then
killCell(h,w)
end
if neighborsAlive==3 and not alive(h,w) then
birthCell(h,w)
end
end
end
cells = copy(bufferCells)
end
ticks=0
end
ticks=ticks+1
end