-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrid.lua
More file actions
282 lines (226 loc) · 6.45 KB
/
Grid.lua
File metadata and controls
282 lines (226 loc) · 6.45 KB
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
-- Grid (of cells) class
local Util = require 'Util'
local Cell = require 'Cell'
local Dim = require 'Dim'
local Grid = {
-- prototype object
gridGroup = nil,
shapesGroup = nil,
cells = nil, -- array of Cell objects
width = nil, -- number of columns
height = nil, -- number of rows
complete = nil,
levelText = nil,
}
Grid.__index = Grid
function Grid.new(gridGroup, shapesGroup)
trace('Grid.new')
local o = {}
setmetatable(o, Grid)
o.gridGroup = gridGroup
o.shapesGroup = shapesGroup
return o
end
function Grid:reset()
-- trace('Grid:reset')
-- clear out gridGroup, shapeGroup objects
while self.shapesGroup.numChildren > 0 do
display.remove(self.shapesGroup[self.shapesGroup.numChildren])
end
while self.gridGroup.numChildren > 0 do
display.remove(self.gridGroup[self.gridGroup.numChildren])
end
do
-- local last_using = composer.getVariable('last_using')
-- if not last_using then
-- last_using = 0
-- end
local before = collectgarbage('count')
collectgarbage('collect')
local after = collectgarbage('count')
print('collected', math.floor(before - after), 'KBytes, now using', math.floor(after), 'KBytes')
-- composer.setVariable('last_using', after)
end
end
function Grid:newLevel()
-- trace('Grid:newLevel')
-- assume gridGroup, shapeGroup are created but empty
math.randomseed(_G.gameState.level + 2) -- make the first level easier and prettier
-- width runs from 4 to 7
-- (0 % 4) + 4 == 4
-- (1 % 4) + 4 == 5
-- (2 % 4) + 4 == 6
-- (3 % 4) + 4 == 7
-- (4 % 4) + 4 == 4
-- (5 % 4) + 4 == 5
self.width = ((_G.gameState.level - 1) % 3) + 3
self.height = (self.width*2) - 1 -- odd number for mirror
trace('dimensions', self.width, self.height)
-- each cell is Q * math.sqrt(3) wide
-- we need space for numX + a half
self.dim = Dim.new( display.viewableContentWidth/(self.width+0.5)/math.sqrt(3) )
self.colors, self.backgroundColor, self.completeColor = Util.chooseColors()
display.setDefault("background", unpack(self.backgroundColor))
self:createCells()
self:linkCells()
self:placeCoins()
self:colorCoins()
self:jumbleCoins()
self:createGraphics(0.1)
self.complete = false
-- create a new levelText for every level because Grid:reset() deletes all it's children
local fontSize = display.contentWidth / 7
self.levelText = display.newText({
parent = self.gridGroup,
text = tostring(_G.gameState.level),
x = display.contentCenterX,
y = display.contentHeight - fontSize,
font = native.systemFontBold,
fontSize = fontSize})
self.levelText:setFillColor(0,0,0)
self.levelText.alpha = 0.1
self:fadeIn()
end
function Grid:createCells()
self.cells = {}
for y = 1, self.height do
for x = 1, self.width do
local c = Cell.new(self, x, y)
table.insert(self.cells, c) -- push
end
end
end
function Grid:linkCells()
for _,c in ipairs(self.cells) do
-- local fc -- found cell
-- matters if x is odd or even
-- even x: cells to ne or se will be == x
-- even x cells to sw or nw will be x - 1
-- odd x: cells to se or ne will be x + 1
-- odd x: cells to sw or nw will be == x
local oddRow = math.fmod(c.y,2) == 1
local xdiffE, xdiffW
if oddRow then xdiffE = 1 else xdiffE = 0 end -- easterly
if oddRow then xdiffW = 0 else xdiffW = -1 end -- westerly
c.ne = self:findCell(c.x + xdiffE, c.y - 1)
c.e = self:findCell(c.x + 1, c.y)
c.se = self:findCell(c.x + xdiffE, c.y + 1)
c.sw = self:findCell(c.x + xdiffW, c.y + 1)
c.w = self:findCell(c.x - 1, c.y)
c.nw = self:findCell(c.x + xdiffW, c.y - 1)
end
end
function Grid:iterator(fn)
for _,c in ipairs(self.cells) do
fn(c)
end
end
function Grid:findCell(x,y)
for _,c in ipairs(self.cells) do
if c.x == x and c.y == y then
return c
end
end
return nil
end
function Grid:randomCell()
return self.cells[math.random(#self.cells)]
end
function Grid:createGraphics()
self:iterator(function(c) c:createGraphics(0.1) end)
end
function Grid:placeCoins()
local function _countCoins()
local count = 0
self:iterator(function(c) count = count + c.bitCount end)
return count
end
repeat
if math.random() < 0.5 then
self:iterator(function(c) c:placeCoin() end)
else
local yS = self.height
for yN = 1, self.height/2 do
for x = 1, self.width do
local cN = self:findCell(x,yN)
local cS = self:findCell(x,yS)
cN:placeCoin(cS)
end
yS = yS - 1
end
end
self:iterator(function(c) c:calcHammingWeight() end)
if _countCoins() == 0 then trace("WARNING: no coins") end
until _countCoins() > 0
end
function Grid:colorCoins()
assert(self.colors)
local nColor = 1
local section = 1
local c = table.find(self.cells, function(d) return d.coins ~= 0 and d.color == nil end)
while c do
c:colorConnected(self.colors[nColor], section)
nColor = nColor + 1
if nColor > #self.colors then
nColor = 1
end
section = section + 1
c = table.find(self.cells, function(d) return d.coins ~= 0 and d.color == nil end)
end
end
function Grid:jumbleCoins()
if system.getInfo('environment') == 'simulator' then
repeat
local totalMoves = 0
self:iterator(function(c) totalMoves = totalMoves + c:jumbleCoin() end)
if totalMoves == 0 then trace('WARNING: simulator repeating jumble') end
until totalMoves > 0
else
self:iterator(function(c) c:jumbleCoin() end)
end
end
function Grid:isComplete()
for n = 1, #self.cells do
if not self.cells[n]:isComplete() then
return false
end
end
self.complete = true
return true
end
function Grid:isSectionComplete(section)
local arr = table.filter(self.cells, function(c) return c.section == section end)
for n = 1, #arr do
if not arr[n]:isComplete(section) then
return false
end
end
for n = 1, #arr do
arr[n].section = 0 -- lock cell from moving
end
return true
end
function Grid:hideSection(section)
local arr = table.filter(self.cells, function(c) return c.section == section end)
for _,c in ipairs(arr) do
c:fadeOut()
end
end
--[[
function Grid:colorComplete()
self:iterator( function(c)
c:colorComplete()
c:fadeIn()
end )
end
]]
function Grid:fadeIn()
self:iterator( function(c) c:fadeIn() end )
end
function Grid:fadeOut()
self:iterator( function(c) c:fadeOut() end )
end
function Grid:destroy()
audio.stop() -- stop all channels
end
return Grid