-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.lua
380 lines (328 loc) · 13.4 KB
/
generate.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
math.randomseed(tonumber(random_seed.text))
local function list_contains(lst, x)
local isin = false
for i = 1, #lst do
if lst[i] == x then
isin = true
end
end
return isin
end
local function make_path()
-- Retry making path from this point if it gets stuck.
::redo::
-- start with an empty grid
local grid = {}
-- fill the grid with 0s
for i = 1, 6 do
grid[i] = {0, 0, 0, 0, 0, 0}
end
-- set the starting row and column
local row = 1
local column = 1
-- start path in start cell
grid[row][column] = 1
-- count completed moves
local n = 2
-- store each move as "left", "right", "up", or "down"
local completed_moves = {}
-- store the grid coordinates of the end point of each move
local path_coords = {{1, 1}}
-- work towards the bottom right of the grid (cell 6,6)
while row ~=6 or column ~= 6 do
-- gather set of possible moves available from the current cell
local mvs = {}
-- available cell to the right
if column < 6 and grid[row][column+1] == 0 then
table.insert(mvs, "right")
end
-- available cell to the left
if column > 1 and grid[row][column-1] == 0 then
table.insert(mvs, "left")
end
-- available cell below
if row < 6 and grid[row+1][column] == 0 then
table.insert(mvs, "down")
end
-- available cell above
if row > 1 and grid[row-1][column] == 0 then
table.insert(mvs, "up")
end
if #mvs == 0 then -- no possible moves
goto redo
else -- at least one possible available
-- randomly choose one of the available moves
local move = mvs[math.random(1, #mvs)]
-- make the move
if move == "right" then
column = column + 1
elseif move == "left" then
column = column - 1
elseif move == "down" then
row = row + 1
elseif move == "up" then
row = row - 1
end
-- mark the path with the number of the current move
grid[row][column] = n
-- save the coordinates and direction of the move
table.insert(path_coords, {row, column})
table.insert(completed_moves, move)
n = n + 1 -- add to move count
if (row == 6 and column == 6) and (n < 19) then
goto redo
end
end
end
return {grid = grid, moves = completed_moves, path_coords = path_coords}
end
local function fill_path(p)
local attempts = 0
::redo_fill_path::
-- create empty colour_map and image_map tables
local colour_map = {}
local image_map = {}
for i = 1, 6 do
colour_map[i] = {0, 0, 0, 0, 0, 0}
image_map[i] = {0, 0, 0, 0, 0, 0}
end
-- count redos
attempts = attempts + 1
if attempts > 5 then -- stuck
return {colour_map = colour_map, image_map = image_map, EXIT_SUCCESS = 1}
end
-- randomly choose colour and tangram for initial cell
colour_map[1][1] = math.random(1, 3)
image_map[1][1] = math.random(1, 9)
-- randomly choose whether colour or tangram should match between
-- first two adjacent cells
local match_type = math.random(1, 2)
-- fill tangrams and colours on the path
for i = 2, #p.path_coords do
local current_cell = p.path_coords[i]
local previous_cell = p.path_coords[i-1]
local two_back_cell = p.path_coords[i-2]
-- create set of all possible colours by usings table keys
local all_colours = {[1] = true, [2] = true, [3] = true}
-- create set of all possible tangrams by usings table keys
local all_tangrams = {[1] = true,
[2] = true,
[3] = true,
[4] = true,
[5] = true,
[6] = true,
[7] = true,
[8] = true,
[9] = true}
-- remove items from colour or tangram sets when they are
-- in adjacent cells to the current cell
if current_cell[1] > 1 and (current_cell[1]-1 ~= previous_cell[1] or current_cell[2] ~= previous_cell[2]) then
all_colours[colour_map[current_cell[1]-1][current_cell[2]]] = nil
all_tangrams[image_map[current_cell[1]-1][current_cell[2]]] = nil
end
if current_cell[1] < 6 and (current_cell[1]+1 ~= previous_cell[1] or current_cell[2] ~= previous_cell[2]) then
all_colours[colour_map[current_cell[1]+1][current_cell[2]]] = nil
all_tangrams[image_map[current_cell[1]+1][current_cell[2]]] = nil
end
if current_cell[2] > 1 and (current_cell[1] ~= previous_cell[1] or current_cell[2]-1 ~= previous_cell[2]) then
all_colours[colour_map[current_cell[1]][current_cell[2]-1]] = nil
all_tangrams[image_map[current_cell[1]][current_cell[2]-1]] = nil
end
if current_cell[2] < 6 and (current_cell[1] ~= previous_cell[1] or current_cell[2]+1 ~= previous_cell[2]) then
all_colours[colour_map[current_cell[1]][current_cell[2]+1]] = nil
all_tangrams[image_map[current_cell[1]][current_cell[2]+1]] = nil
end
-- construct a list of tangrams that are not used in adjacent cells
local complimentary_tangrams = {}
for tangram_idx = 1, 9 do
if all_tangrams[tangram_idx] then
table.insert(complimentary_tangrams, tangram_idx)
end
end
-- construct a list of colours that are not used in adjacent cells
local complimentary_colours = {}
for colour_idx = 1, 3 do
if all_colours[colour_idx] then
table.insert(complimentary_colours, colour_idx)
end
end
-- try again if all colours or tangrams are used in adjacent cells
if #complimentary_colours == 0 then
goto redo_fill_path
elseif #complimentary_tangrams == 0 then
goto redo_fill_path
end
-- can't match previous cell in colour or tangram due to clashes with other adj path cells
if list_contains(complimentary_colours, colour_map[previous_cell[1]][previous_cell[2]]) == false and
list_contains(complimentary_tangrams, image_map[previous_cell[1]][previous_cell[2]]) == false then
goto redo_fill_path
end
if match_type == 1 then -- match colour
if list_contains(complimentary_colours, colour_map[previous_cell[1]][previous_cell[2]]) then
colour_map[current_cell[1]][current_cell[2]] = colour_map[previous_cell[1]][previous_cell[2]] -- match colour
image_map[current_cell[1]][current_cell[2]] = complimentary_tangrams[math.random(1, #complimentary_tangrams)]
else -- can't match colour because of clash with another adjacent cell in the path
colour_map[current_cell[1]][current_cell[2]] = complimentary_colours[math.random(1, #complimentary_colours)]
image_map[current_cell[1]][current_cell[2]] = image_map[previous_cell[1]][previous_cell[2]] -- match tangram
end
else -- match tangram
if list_contains(complimentary_tangrams, image_map[previous_cell[1]][previous_cell[2]]) then
colour_map[current_cell[1]][current_cell[2]] = complimentary_colours[math.random(1, #complimentary_colours)]
image_map[current_cell[1]][current_cell[2]] = image_map[previous_cell[1]][previous_cell[2]] -- match tangram
else -- can't match tangram because of clash with another adjacent cell in the path
colour_map[current_cell[1]][current_cell[2]] = colour_map[previous_cell[1]][previous_cell[2]] -- match colour
image_map[current_cell[1]][current_cell[2]] = complimentary_tangrams[math.random(1, #complimentary_tangrams)]
end
end
-- don't allow adjacent cells in the path to match both colour and tangram
if colour_map[current_cell[1]][current_cell[2]] == colour_map[previous_cell[1]][previous_cell[2]] and
image_map[current_cell[1]][current_cell[2]] == image_map[previous_cell[1]][previous_cell[2]] then
goto redo_fill_path
end
-- don't allows runs of three cells of the same colour within the path
if two_back_cell ~= nil and
colour_map[current_cell[1]][current_cell[2]] == colour_map[previous_cell[1]][previous_cell[2]] and
colour_map[current_cell[1]][current_cell[2]] == colour_map[two_back_cell[1]][two_back_cell[2]] then
goto redo_fill_path
end
-- don't allows runs of three cells of the same tangram within the path
if two_back_cell ~= nil and
image_map[current_cell[1]][current_cell[2]] == image_map[previous_cell[1]][previous_cell[2]] and
image_map[current_cell[1]][current_cell[2]] == image_map[two_back_cell[1]][two_back_cell[2]] then
goto redo_fill_path
end
-- alternate match type at each step of the path
if match_type == 1 then
match_type = 2
else
match_type = 1
end
end
return {colour_map = colour_map, image_map = image_map, EXIT_SUCCESS = 0}
end
-- pass the output of fill_path()
local function pad_path(p)
-- Add colours and tangrams to each cell adjacent to the path.
-- These cells form a barrier around the path by ensuring that
-- neither colours nor tangrams match in cells that can be
-- moved to from the path.
local attempts = 0
::redo_pad_path::
local colour_map = {}
local image_map = {}
attempts = attempts + 1
if attempts > 9 then
return {colour_map = colour_map, image_map = image_map, EXIT_SUCCESS = 1}
end
for i = 1, 6 do
colour_map[i] = {0, 0, 0, 0, 0, 0}
image_map[i] = {0, 0, 0, 0, 0, 0}
end
for i = 1, 6 do
for j = 1, 6 do
if p.colour_map[i][j] == 0 then -- find cells not on the path
-- find cells adjacent to path
if ((i > 1) and (p.colour_map[i - 1][j] ~= 0)) or -- cell above on path
((i < 6) and (p.colour_map[i + 1][j] ~= 0)) or -- cell below on path
((j > 1) and (p.colour_map[i][j - 1] ~= 0)) or -- cell to left on path
((j < 6) and (p.colour_map[i][j + 1] ~= 0)) then -- cell to right on path
-- construct set of colours and tangrams in adjacent cells on path
-- create set of all colours and tangrams
-- create set of all possible colours by usings table keys
local possible_colours = {}
for _,v in ipairs({1, 2, 3}) do
possible_colours[v] = true
end
-- which of 9 possible tangrams can be used
-- start with all 9
local possible_tangrams = {true, true, true, true, true, true, true, true, true}
-- don't use colour or tangram from cell above
if i > 1 and p.colour_map[i - 1][j] ~= 0 then
possible_colours[p.colour_map[i - 1][j]] = nil
possible_tangrams[p.image_map[i - 1][j]] = nil
end
-- don't use colour or tangram from cell below
if i < 6 and p.colour_map[i + 1][j] ~= 0 then
possible_colours[p.colour_map[i + 1][j]] = nil
possible_tangrams[p.image_map[i + 1][j]] = nil
end
-- don't use colour or tangram from cell to left
if j > 1 and p.colour_map[i][j-1] ~= 0 then
possible_colours[p.colour_map[i][j-1]] = nil
possible_tangrams[p.image_map[i][j-1]] = nil
end
-- don't use colour or tangram from cell to right
if j < 6 and p.colour_map[i][j+1] ~= 0 then
possible_colours[p.colour_map[i][j+1]] = nil
possible_tangrams[p.image_map[i][j+1]] = nil
end
-- construct a list of tangrams that are not used in adjacent cells
local complimentary_tangrams = {}
for x = 1,9 do
if possible_tangrams[x] then
table.insert(complimentary_tangrams, x)
end
end
-- construct a list of colours that are not used in adjacent cells
local complimentary_colours = {}
for y = 1,3 do
if possible_colours[y] then
table.insert(complimentary_colours, y)
end
end
-- no colour or tangram options left
if #complimentary_colours == 0 then
goto redo_pad_path
elseif #complimentary_tangrams == 0 then
goto redo_pad_path
end
-- fill in cells adjacent to path
colour_map[i][j] = complimentary_colours[math.random(1, #complimentary_colours)]
image_map[i][j] = complimentary_tangrams[math.random(1, #complimentary_tangrams)]
else
-- fill in the rest of the blank cells
colour_map[i][j] = math.random(1, 3)
image_map[i][j] = math.random(1, 9)
end
end
end
end
return {colour_map = colour_map, image_map = image_map, EXIT_SUCCESS = 0}
end
function MakeMaps()
::redo::
local path = make_path()
local filled_path = fill_path(path)
if filled_path.EXIT_SUCCESS == 1 then
goto redo
end
local padded_path = pad_path(filled_path)
if padded_path.EXIT_SUCCESS == 1 then
goto redo
end
local colour_map = {}
local image_map = {}
for i = 1, 6 do
colour_map[i] = {0, 0, 0, 0, 0, 0}
image_map[i] = {0, 0, 0, 0, 0, 0}
end
for x = 1, 6 do
for y = 1, 6 do
if filled_path.colour_map[x][y] ~= 0 then
colour_map[x][y] = filled_path.colour_map[x][y]
elseif padded_path.colour_map[x][y] ~= 0 then
colour_map[x][y] = padded_path.colour_map[x][y]
end
if filled_path.image_map[x][y] ~= 0 then
image_map[x][y] = filled_path.image_map[x][y]
elseif padded_path.image_map[x][y] ~= 0 then
image_map[x][y] = padded_path.image_map[x][y]
end
end
end
return {path = path.grid,
moves = path.moves,
path_coords = path.path_coords,
image_map = image_map,
colour_map = colour_map}
end