-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.lua
145 lines (132 loc) · 3.69 KB
/
common.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
--map is a common higher-order function to map over an array
function map(func, arr)
local res = {}
for i, v in ipairs(arr) do
res[i] = func(v)
end
return res
end
-- shiftvalue shifts the value and starts from the beginning if necessary
function shiftvalue(val)
if val == 'A' then
val = 'B'
elseif val == 'B' then
val = 'C'
elseif val == 'C' then
val = 'D'
elseif val == 'D' then
val = 'E'
elseif val == 'E' then
val = 'F'
elseif val == 'F' then
val = 'A'
end
return val
end
-- randomvalue gets the next random value from the list
function listrandomvalue(list, incorrectval)
local index = math.random(1, #list)
local crystal = list[index]
table.remove(list, index)
if incorrectval then
table.insert(list, incorrectval)
end
return crystal
end
-- checkback ensures that 3 cells in a row do not contain the same value
function checkback(grid, i, j, list)
if j > 2 then
while grid[i][j] == grid[i][j - 1] and grid[i][j] == grid[i][j - 2] do
if list then
grid[i][j] = listrandomvalue(list, grid[i][j])
else
grid[i][j] = shiftvalue(grid[i][j])
end
end
end
end
-- checkabove ensures that 3 cells in a column do not contain the same value
function checkabove(grid, i, j, list)
if i > 2 then
while grid[i][j] == grid[i - 1][j] and grid[i][j] == grid[i - 2][j] do
if list then
grid[i][j] = listrandomvalue(list, grid[i][j])
else
grid[i][j] = shiftvalue(grid[i][j])
end
checkback(grid, i, j)
end
end
end
-- checkcoords ensures that x and y coords are within the matrix
function checkcoords(x, y)
if not x or not y then return false end
local resx = false
local resy = false
for i = 0, 9 do
if x == i then resx = true end
if y == i then resy = true end
end
if resx and resy then return true end
return false
end
-- checkline verifies the possibility of a move
function checkline(nx, line, gridsize)
for i = 1, gridsize - 2 do
if line[i] == line[i + 1] and line[i] == line[i + 2] and nx >= i and nx <= i + 2 then
return true
end
end
return false
end
-- checkcolumn verifies the possibility of a move
function checkcolumn(nx, ny, grid, gridsize)
for i = 1, gridsize - 2 do
if grid[i][nx] == grid[i + 1][nx] and grid[i][nx] == grid[i + 2][nx] and ny >= i and ny <= i + 2 then
return true
end
end
return false
end
-- checkcomb verifies the possibility of a move (reserved for expanded combinations)
function checkcomb(ny, nx, grid, gridsize)
return false
end
-- splitstr splits a string into a table based on the space separator
function splitstr(inpstr)
local res = {}
for s in string.gmatch(inpstr, "[^ ]+") do
table.insert(res, s)
end
return res
end
-- sleep waits for a number of seconds
function sleep(sec)
local t = os.clock()
while os.clock() - t <= sec do
end
end
function startswith(str, start)
return str:sub(1, #start) == start
end
-- matrixcontainsvoids checks if matrix has removed crystals
function matrixcontainsvoids(matrix, voidchr)
for i = 1, #matrix do
for j = 1, #matrix[i] do
if matrix[i][j] == voidchr then
return true
end
end
end
return false
end
-- flattenmatrix makes one array from a 2D array
function flattenmatrix(matrix)
local list = {}
for i = 1, #matrix do
for j = 1, #matrix[i] do
table.insert(list, matrix[i][j])
end
end
return list
end