From 9fd3208aaf7ae14fb5adaf5f166723d7178f03ba Mon Sep 17 00:00:00 2001 From: Simon Cambier Date: Sun, 29 May 2022 14:35:28 +0200 Subject: [PATCH] Cleaned some code, added comments --- main.lua | 70 +++++++++++++++++++++++++++++++------------------------- 1 file changed, 39 insertions(+), 31 deletions(-) diff --git a/main.lua b/main.lua index f704ef2..e6b5bf5 100644 --- a/main.lua +++ b/main.lua @@ -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()