Cleaned some code, added comments

This commit is contained in:
Simon Cambier 2022-05-29 14:35:28 +02:00
parent b80297c45f
commit 9fd3208aaf

View File

@ -43,7 +43,7 @@ end
function slice(tbl, first, last, step) function slice(tbl, first, last, step)
local sliced = {} local sliced = {}
for i = (first or 1), (last or #tbl), (step or 1) do for i = (first or 1), (last or #tbl), (step or 1) do
sliced[(#sliced + 1)] = tbl[i] sliced[#sliced + 1] = tbl[i]
end end
return sliced return sliced
end end
@ -53,16 +53,22 @@ function rectfill(x, y, w, h, col)
_rectfill(x, y, x+w, y+h, col) _rectfill(x, y, x+w, y+h, col)
end end
local _count = count --
function count(tbl, p) -- Overrides count() to accept a callback
if type(p) != "function" then return _count(tbl, p) end --
local c = 0 -- local _count = count
for v in all(tbl) do -- function count(tbl, p)
if p(v) then c+=1 end -- if type(p) != "function" then return _count(tbl, p) end
end -- local c = 0
return c -- for v in all(tbl) do
end -- if p(v) then c+=1 end
-- end
-- return c
-- end
--
-- Returns the indices of found occurences of `o` within `tbl`
--
function find(tbl, o) function find(tbl, o)
local indices = {} local indices = {}
for k,v in ipairs(tbl) do for k,v in ipairs(tbl) do
@ -71,14 +77,16 @@ function find(tbl, o)
return indices return indices
end end
-- shallow copy --
-- Makes a shallow table copy
--
function copy(tbl) function copy(tbl)
local copy = {} return map(tbl, function (o) return o end)
for v in all(tbl) do add(copy, v) end
return copy
end end
-- shallow equality --
-- Table equality - shallow comparison
--
function equal(tbl1, tbl2) function equal(tbl1, tbl2)
for k, _ in ipairs(tbl1) do for k, _ in ipairs(tbl1) do
if tbl1[k] ~= tbl2[k] then return false end if tbl1[k] ~= tbl2[k] then return false end
@ -86,15 +94,15 @@ function equal(tbl1, tbl2)
return true return true
end end
function tostring(any) -- function tostring(any)
if (type(any)~="table") return tostr(any) -- if (type(any)~="table") return tostr(any)
local str = "{" -- local str = "{"
for k,v in pairs(any) do -- for k,v in pairs(any) do
if (str~="{") str=str.."," -- if (str~="{") str=str..","
str=str..tostring(k).."="..tostring(v) -- str=str..tostring(k).."="..tostring(v)
end -- end
return str.."}" -- return str.."}"
end -- end
-- --
-- Board -- Board
@ -127,10 +135,6 @@ function Board.new()
tiles = split(t) tiles = split(t)
end, end,
getWidth = function()
return width
end,
getTile = function(self, idx) getTile = function(self, idx)
return tiles[idx] return tiles[idx]
end, end,
@ -241,7 +245,9 @@ function Board.new()
return count return count
end, end,
--
-- Returns the index of a random zero tile -- Returns the index of a random zero tile
--
getRandomZero = function(self) getRandomZero = function(self)
assert(count(tiles, 0) > 0, "No zero left") assert(count(tiles, 0) > 0, "No zero left")
local zeroes = filter(tiles, function(v) return v == 0 end, true) local zeroes = filter(tiles, function(v) return v == 0 end, true)
@ -252,7 +258,9 @@ function Board.new()
return rnd(z) return rnd(z)
end, end,
--
-- Returns the index of a random non-zero tile -- Returns the index of a random non-zero tile
--
getRandomNonZero = function(self) getRandomNonZero = function(self)
assert(count(tiles, 0) < #tiles, "All zeroes") assert(count(tiles, 0) < #tiles, "All zeroes")
local numbers = filter(tiles, function(v) return v ~= 0 end, true) local numbers = filter(tiles, function(v) return v ~= 0 end, true)
@ -544,9 +552,9 @@ function _init()
end end
function _update60() function _update60()
if not create then -- if not create then
board:solveStep() -- board:solveStep()
end -- end
end end
function _draw() function _draw()