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