-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgol.lua
227 lines (211 loc) · 6.56 KB
/
gol.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
--[[
MIT License
Copyright (c) 2020 Vladislav Gorskii
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
]]
local component = require("component")
local computer = require("computer")
local event = require("event")
local gpu = component.gpu
local keyboard = require("keyboard")
local width, height = gpu.maxResolution()
local black = 0x000000
local white = 0xFFFFFF
local red = 0xC80000
local green = 0x00C800
local blue = 0x0000C8
local isSimLaunched = false
gpu.setBackground(black)
gpu.setForeground(white)
gpu.fill(1, 1, width, height, " ")
-- Game field
local field = {}
--Next generation container
local nfield = {}
-- Control hints
--[[
[ - previous generation \ - start/stop simulation ` - exit
] - next generation / - restart Use LMB to change cell's state
]]
gpu.setBackground(blue)
gpu.setForeground(black)
gpu.fill(1, height-2, width, 1, " ")
gpu.setBackground(white)
gpu.fill(1, height-1, width, 2, " ")
gpu.set(1, height-1, "[ - previous generation | \\ - start/stop simulation | ` - exit")
gpu.set(1, height, "] - next generation | / - restart | LMB - invert cell's state")
-- Field clearing/generation
local function clearField()
gpu.setBackground(blue)
gpu.fill(1, height-2, width, 1, " ")
--Making the field array circular
setmetatable(field, {
__index = function(t,i)
local index = i%width
index = index == 0 and width or index
return t[index] end
})
--
for gx=1,width do
field[gx] = {}
nfield[gx] = {}
--Making the field array circular pt.2
setmetatable(field[gx], {
__index = function(t,i)
local index = i%height-3
index = index == 0 and height-3 or index
return t[index] end
})
--
for gy=1,height-3 do
field[gx][gy] = 0
-- PROGRESS
if gx*gy==width*(height-3) then
gpu.setBackground(green)
else
gpu.setBackground(red)
end
gpu.setForeground(white)
gpu.set(2, height-2, "CURRENT PROCESS: CLEARING "..gx..":"..gy..", PROGRESS: "..gx*gy.."/"..width*(height-3))
end
end
computer.beep()
end
--Field redraw
local function drawField(cont)
gpu.setBackground(blue)
gpu.fill(1, height-2, width, 1, " ")
for x=1, width do
for y=1,height-3 do
if cont[x][y]==1 then
gpu.setBackground(white)
gpu.setForeground(black)
else
gpu.setBackground(black)
gpu.setForeground(white)
end
gpu.fill(x, y, 1, 1, " ")
-- PROGRESS
if x*y==width*(height-3) then
gpu.setBackground(green)
else
gpu.setBackground(red)
end
gpu.setForeground(white)
gpu.set(2, height-2, "CURRENT PROCESS: DRAWING "..x..":"..y..", PROGRESS: "..x*y.."/"..width*(height-3))
end
end
computer.beep()
end
--- Calculating next generation
local function nextGen()
-- Going through all the field
for x=1,width do
for y=1,height-3 do
local livingCells = 0
-- Calculation of living cells around x:y
for i=x-1,x+1 do
for j=y-1,y+1 do
livingCells = livingCells + field[i][j]
-- PROGRESS
if i*j==width*(height-3) then
gpu.setBackground(green)
else
gpu.setBackground(red)
end
gpu.setForeground(white)
gpu.set(2, height-2, "CURRENT PROCESS: CHECKING NEIGHBOUR "..i..":"..j.." AT "..x..":"..y..", PROGRESS: "..i*j.."/"..width*(height-3))
end
end
-- In case it's alive
livingCells = livingCells - field[x][y]
-- Spawning a new cell
if field[x][y]==0 and livingCells==3 then
nfield[x][y] = 1
-- Killing a cell
elseif field[x][y]==1 and (livingCells < 2 or livingCells > 3) then
nfield[x][y] = 0
else
nfield[x][y] = field[x][y]
end
end
end
computer.beep()
return true
end
-- Touch & Keyboard
local function filter(name, ...)
if name ~= "key_down" and name ~= "touch" then
return false
end
return true
end
-- Game loop
clearField()
while true do
local lastEvent = {event.pullFiltered(filter)}
if lastEvent[1] == "touch" and lastEvent[5] == 0 then
--State invertion
if field[lastEvent[3]][lastEvent[4]]==1 then
field[lastEvent[3]][lastEvent[4]] = 0
gpu.setBackground(black)
gpu.setForeground(white)
else
field[lastEvent[3]][lastEvent[4]] = 1
gpu.setBackground(white)
gpu.setForeground(black)
end
gpu.fill(lastEvent[3], lastEvent[4], 1, 1, " ")
elseif lastEvent[1] == "key_down" then
if lastEvent[4] == keyboard.keys.lbracket then
drawField(field)
elseif lastEvent[4] == keyboard.keys.rbracket then
gpu.setBackground(blue)
gpu.fill(1, height-2, width, 1, " ")
nextGen()
drawField(nfield)
for i=1,width do
for j=1,height-3 do
field[i][j] = nfield[i][j]
end
end
elseif lastEvent[4] == keyboard.keys.backslash then
isSimLaunched = not isSimLaunched
while true do
os.sleep(2)
if isSimLaunched then
if not nextGen() then
break
end
drawField(nfield)
for i=1,width do
for j=1,height-3 do
field[i][j] = nfield[i][j]
end
end
else
break
end
end
elseif lastEvent[4] == keyboard.keys.slash then
clearField()
isSimLaunched = false
elseif lastEvent[4] == keyboard.keys.grave then
return nil
end
end
end