camelCase -> snake_case

This commit is contained in:
Simon Cambier 2022-06-02 20:18:02 +02:00
parent 4920224034
commit cb1c76cd2d
6 changed files with 100 additions and 100 deletions

View File

@ -30,17 +30,17 @@ function Board.new()
tiles = split(t) tiles = split(t)
end, end,
getTile = function(self, idx) get_tile = function(self, idx)
return tiles[idx] return tiles[idx]
end, end,
-- returns a COPY of the tiles array -- returns a COPY of the tiles array
getTilesCopy = function(self) get_tiles_copy = function(self)
return copy(tiles) return copy(tiles)
end, end,
-- overwrites the whole tiles array -- overwrites the whole tiles array
setTiles = function(self, newtiles) set_tiles = function(self, newtiles)
assert(#newtiles == #tiles, "New tiles array must have a length of " .. #tiles) assert(#newtiles == #tiles, "New tiles array must have a length of " .. #tiles)
tiles = copy(newtiles) tiles = copy(newtiles)
end, end,
@ -59,11 +59,11 @@ function Board.new()
margin + (y-1)*tile_width + (y-1)*(padding+1) margin + (y-1)*tile_width + (y-1)*(padding+1)
end, end,
getSize = function(self) get_size = function(self)
return width return width
end, end,
getTileWidth = function(self) get_tile_width = function(self)
return tile_width return tile_width
end, end,
@ -75,7 +75,7 @@ function Board.new()
tiles[idx] = color tiles[idx] = color
end, end,
getRows = function(self) get_rows = function(self)
local ret = {} local ret = {}
for i = 1, width do for i = 1, width do
add(ret, slice(tiles, ((i - 1) * width) + 1, i * width)) add(ret, slice(tiles, ((i - 1) * width) + 1, i * width))
@ -83,21 +83,21 @@ function Board.new()
return ret return ret
end, end,
getCols = function(self) get_cols = function(self)
local ret = {} local ret = {}
local rows = self.getRows(self) local rows = self.get_rows(self)
for i = 1, width do for i = 1, width do
add(ret, map(rows, function(v) return v[i] end)) add(ret, map(rows, function(v) return v[i] end))
end end
return ret return ret
end, end,
isComplete = function(self) is_complete = function(self)
return count(tiles, 0) == 0 return count(tiles, 0) == 0
end, end,
isValid = function(self) is_valid = function(self)
local rows = self:getRows() local rows = self:get_rows()
for y,row in ipairs(rows) do for y,row in ipairs(rows) do
-- check count -- check count
if count(row, BLUE) ~= count(row, YELLOW) then if count(row, BLUE) ~= count(row, YELLOW) then
@ -112,13 +112,13 @@ function Board.new()
end end
end end
-- check triples -- check triples
if self:countConsecutives(row) > 2 then if self:count_consecutives(row) > 2 then
if (debug) printh("triples") if (debug) printh("triples")
return false return false
end end
end end
local cols = self:getCols() local cols = self:get_cols()
for col in all(cols) do for col in all(cols) do
-- check count -- check count
if count(col, BLUE) ~= count(col, YELLOW) then if count(col, BLUE) ~= count(col, YELLOW) then
@ -133,7 +133,7 @@ function Board.new()
end end
end end
-- check triples -- check triples
if self:countConsecutives(col) > 2 then if self:count_consecutives(col) > 2 then
if (debug) printh("triples") if (debug) printh("triples")
return false return false
end end
@ -141,7 +141,7 @@ function Board.new()
return true return true
end, end,
countConsecutives = function(self, line) count_consecutives = function(self, line)
local count = 0 local count = 0
local last = 0 local last = 0
for v in all(line) do for v in all(line) do
@ -158,7 +158,7 @@ function Board.new()
-- --
-- Returns the index of a random zero tile -- Returns the index of a random zero tile
-- --
getRandomZero = function(self) get_random_zero = 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)
local z = {} local z = {}
@ -171,7 +171,7 @@ function Board.new()
-- --
-- Returns the index of a random non-zero tile -- Returns the index of a random non-zero tile
-- --
getRandomNonZero = function(self) get_random_non_zero = 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)
local z = {} local z = {}
@ -192,24 +192,24 @@ function Board.new()
-- Solves 1 step of the board -- Solves 1 step of the board
-- Returns "valid" if it solved it without guessing -- Returns "valid" if it solved it without guessing
-- Returns "invalid" if the board cannot be solved -- Returns "invalid" if the board cannot be solved
solveStep = function(self, random) solve_step = function(self, random)
local zeroes = count(tiles, 0) local zeroes = count(tiles, 0)
self:surroundDoubles() self:surround_doubles()
self:splitTriples() self:split_triples()
self:fillLines() self:fill_lines()
self:noIdenticalLines() self:no_identical_lines()
local changed = zeroes ~= count(tiles, 0) local changed = zeroes ~= count(tiles, 0)
if not changed and random and not self:isComplete() then if not changed and random and not self:is_complete() then
-- Set a random color -- Set a random color
local z = self:getRandomZero() local z = self:get_random_zero()
self:fill(z, rnd({BLUE, YELLOW})) self:fill(z, rnd({BLUE, YELLOW}))
if (debug) printh("!!!!!!!!!!!!!!!!! RANDOM FILL AT " .. z) if (debug) printh("!!!!!!!!!!!!!!!!! RANDOM FILL AT " .. z)
return "invalid" return "invalid"
end end
return (changed or self:isComplete()) and "valid" or "invalid" return (changed or self:is_complete()) and "valid" or "invalid"
end, end,
surroundDoubles = function(self) surround_doubles = function(self)
for idx,v in ipairs(tiles) do for idx,v in ipairs(tiles) do
local x,y = self:idx_xy(idx) local x,y = self:idx_xy(idx)
if v == 0 then if v == 0 then
@ -245,7 +245,7 @@ function Board.new()
end end
end, end,
splitTriples = function(self) split_triples = function(self)
for idx, col in ipairs(tiles) do for idx, col in ipairs(tiles) do
local x,y = self:idx_xy(idx) local x,y = self:idx_xy(idx)
if col == 0 then if col == 0 then
@ -273,17 +273,17 @@ function Board.new()
end end
end, end,
fillLines = function(self) fill_lines = function(self)
local rows = self:getRows() local rows = self:get_rows()
local cols = self:getCols() local cols = self:get_cols()
-- rows -- rows
for y,row in ipairs(rows) do for y,row in ipairs(rows) do
local a = count(row, BLUE) local a = count(row, BLUE)
local b = count(row, YELLOW) local b = count(row, YELLOW)
if a ~= b then if a ~= b then
if a == width/2 then self:fillRow(y, YELLOW) end if a == width/2 then self:fill_row(y, YELLOW) end
if b == width/2 then self:fillRow(y, BLUE) end if b == width/2 then self:fill_row(y, BLUE) end
end end
end end
@ -292,35 +292,35 @@ function Board.new()
local a = count(col, BLUE) local a = count(col, BLUE)
local b = count(col, YELLOW) local b = count(col, YELLOW)
if a ~= b then if a ~= b then
if a == width/2 then self:fillCol(x, YELLOW) end if a == width/2 then self:fill_col(x, YELLOW) end
if b == width/2 then self:fillCol(x, BLUE) end if b == width/2 then self:fill_col(x, BLUE) end
end end
end end
end, end,
fillRow = function(self, y, color) fill_row = function(self, y, color)
if (debug) printh("Filling line " .. y .. " in " .. (color == BLUE and "blue" or "yellow")) if (debug) printh("Filling line " .. y .. " in " .. (color == BLUE and "blue" or "yellow"))
local idx = self:xy_idx(1, y) local idx = self:xy_idx(1, y)
for i = idx, (idx+width-1) do for i = idx, (idx+width-1) do
if self:getTile(i) == 0 then if self:get_tile(i) == 0 then
self:fill(i, color) self:fill(i, color)
end end
end end
end, end,
fillCol = function(self, x, color) fill_col = function(self, x, color)
if (debug) printh("Filling column " .. x .. " in " .. (color == BLUE and "blue" or "yellow")) if (debug) printh("Filling column " .. x .. " in " .. (color == BLUE and "blue" or "yellow"))
local idx = self:xy_idx(x, 1) local idx = self:xy_idx(x, 1)
for i = idx, #tiles, width do for i = idx, #tiles, width do
if self:getTile(i) == 0 then if self:get_tile(i) == 0 then
self:fill(i, color) self:fill(i, color)
end end
end end
end, end,
noIdenticalLines = function(self) no_identical_lines = function(self)
-- columns -- columns
local cols = self:getCols() local cols = self:get_cols()
for x,col in ipairs(cols) do for x,col in ipairs(cols) do
if count(col, 0) == 2 and count(col, BLUE) == count(col, YELLOW) then if count(col, 0) == 2 and count(col, BLUE) == count(col, YELLOW) then
local y1, y2 = unpack(find(col, 0)) local y1, y2 = unpack(find(col, 0))
@ -353,7 +353,7 @@ function Board.new()
end end
-- rows -- rows
local rows = self:getRows() local rows = self:get_rows()
for y,row in ipairs(rows) do for y,row in ipairs(rows) do
if count(row, 0) == 2 and count(row, BLUE) == count(row, YELLOW) then if count(row, 0) == 2 and count(row, BLUE) == count(row, YELLOW) then
local x1, x2 = unpack(find(row, 0)) local x1, x2 = unpack(find(row, 0))

View File

@ -13,11 +13,11 @@ local LEFT,RIGHT,UP,DOWN,BTN_O,BTN_X = 0,1,2,3,4,5
-- Utils -- Utils
-- --
function customFont() function custom_font()
poke(0x5f58,0x81) poke(0x5f58,0x81)
end end
function standardFont(font) function standard_font(font)
poke(0x5f58,0x80) poke(0x5f58,0x80)
end end
@ -118,7 +118,7 @@ end
-- end -- end
function setState(state, ...) function set_state(state, ...)
local args = {...} local args = {...}
if gs and gs._leave then gs._leave() end if gs and gs._leave then gs._leave() end
gs = state gs = state
@ -137,24 +137,24 @@ function _init()
mouse_x = 0 mouse_x = 0
mouse_y = 0 mouse_y = 0
frameCount = 0 frame_count = 0
states = { states = {
rules = stateRules(), rules = state_rules(),
menu = stateMenu(), menu = state_menu(),
game = stateGame() game = state_game()
} }
setState(states.game) set_state(states.game)
end end
function _update60() function _update60()
frameCount += 1 frame_count += 1
_coresolve() _coresolve()
gs._update() gs._update()
-- if not create then -- if not create then
-- board:solveStep() -- board:solve_step()
-- end -- end
-- mouse -- mouse

View File

@ -1,11 +1,11 @@
function stateGame() function state_game()
local board = Board.new() local board = Board.new()
local selectedId = 1 local selected_id = 1
local function drawSelectedTile() local function drawSelectedTile()
local x, y = board:draw_coords(selectedId) local x, y = board:draw_coords(selected_id)
local w = board:getTileWidth() local w = board:get_tile_width()
rect2(x-1, y-1, w+2, w+2, 6) rect2(x-1, y-1, w+2, w+2, 6)
end end
@ -15,43 +15,43 @@ function stateGame()
repeat repeat
board:reset() board:reset()
repeat repeat
board:solveStep(true) board:solve_step(true)
yield() yield()
until board:isComplete() until board:is_complete()
until board:isValid() until board:is_valid()
printh("b: " .. time()) printh("b: " .. time())
-- Remove tiles until randomness is unavoidable -- Remove tiles until randomness is unavoidable
local previous = {board:getTilesCopy()} -- initial state local previous = {board:get_tiles_copy()} -- initial state
local i = 0 local i = 0
while true do while true do
board:setTiles(previous[#previous]) board:set_tiles(previous[#previous])
-- remove a random tile -- remove a random tile
repeat repeat
i += 1 i += 1
until i == 100 or board:getTilesCopy()[i] ~= 0 until i == 100 or board:get_tiles_copy()[i] ~= 0
if i == 100 then if i == 100 then
break break
end end
board:fill(i, 0) board:fill(i, 0)
add(previous, board:getTilesCopy()) add(previous, board:get_tiles_copy())
-- try to solve the board -- try to solve the board
local solved = "" local solved = ""
yield() yield()
repeat repeat
solved = board:solveStep() solved = board:solve_step()
until board:isComplete() or solved == "invalid" until board:is_complete() or solved == "invalid"
if solved == "invalid" then if solved == "invalid" then
deli(previous) deli(previous)
end end
end -- end while end -- end while
printh("c: " .. time()) printh("c: " .. time())
board:setTiles(previous[#previous]) board:set_tiles(previous[#previous])
printh(board:tostring()) printh(board:tostring())
printh(count(board:getTilesCopy(), 0).." zeroes") printh(count(board:get_tiles_copy(), 0).." zeroes")
end end
local function _enter() local function _enter()
@ -65,8 +65,8 @@ function stateGame()
end end
local function _update() local function _update()
local size = board:getSize() local size = board:get_size()
local x, y = board:idx_xy(selectedId) local x, y = board:idx_xy(selected_id)
if btnp(UP) then if btnp(UP) then
y -= 1 y -= 1
elseif btnp(DOWN) then elseif btnp(DOWN) then
@ -80,7 +80,7 @@ function stateGame()
if (x>size) x=1 if (x>size) x=1
if (y<1) y=size if (y<1) y=size
if (y>size) y=1 if (y>size) y=1
selectedId = board:xy_idx(x, y) selected_id = board:xy_idx(x, y)
end end
return { return {

View File

@ -1,21 +1,21 @@
function stateMenu() function state_menu()
local selected = 1 local selected = 1
local function onBtnDraw(btn) local function on_btn_draw(btn)
if selected == btn.data.i then if selected == btn.data.i then
btn.color = 7 btn.color = 7
end end
end end
local buttons = { local buttons = {
makeButton({x=10, y=10, w=30, text="play", data={i=1}, make_button({x=10, y=10, w=30, text="play", data={i=1},
onClick=function() setState(states.game) end, on_click=function() set_state(states.game) end,
onHover=function(btn) btn.color = 7 selected = 1 end, on_hover=function(btn) btn.color = 7 selected = 1 end,
onDraw=onBtnDraw}), on_draw=on_btn_draw}),
makeButton({x=10, y=20, w=30, text="rules", data={i=2}, make_button({x=10, y=20, w=30, text="rules", data={i=2},
onClick=function() setState(states.rules) end, on_click=function() set_state(states.rules) end,
onHover=function(btn) btn.color = 7 selected = 2 end, on_hover=function(btn) btn.color = 7 selected = 2 end,
onDraw=onBtnDraw}) on_draw=on_btn_draw})
} }
local function _enter() local function _enter()
@ -35,7 +35,7 @@ function stateMenu()
elseif btnp(DOWN) then elseif btnp(DOWN) then
selected += 1 selected += 1
elseif btnp(BTN_O) then elseif btnp(BTN_O) then
buttons[selected]:onClick() buttons[selected]:on_click()
end end
selected = mid(1, selected, #buttons) selected = mid(1, selected, #buttons)

View File

@ -1,4 +1,4 @@
function stateRules() function state_rules()
local fade = split"0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,4,9,10,9,4,2,1" local fade = split"0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,4,9,10,9,4,2,1"
local color = 1 local color = 1
@ -7,17 +7,17 @@ function stateRules()
rect2(x,y,w,h,fade[color]) rect2(x,y,w,h,fade[color])
end end
local function goBack() local function go_back()
setState(states.menu) set_state(states.menu)
end end
local btnBack = makeButton({x=1, y=118, w=30, h=7, text="❎menu", color=8, local btn_back = make_button({x=1, y=118, w=30, h=7, text="❎menu", color=8,
onClick=function() goBack() end, on_click=function() go_back() end,
onHover=function(btn) btn.color = 7 end}) on_hover=function(btn) btn.color = 7 end})
return { return {
_update=function() _update=function()
if frameCount%8==0 then if frame_count%8==0 then
color += 1 color += 1
end end
if color>#fade then if color>#fade then
@ -26,14 +26,14 @@ function stateRules()
-- Back to the menu -- Back to the menu
if btnp(BTN_X) then if btnp(BTN_X) then
goBack() go_back()
end end
btnBack:update() btn_back:update()
end, end,
_draw=function() _draw=function()
customFont() custom_font()
print("1) yOU CAN'T HAVE MORE THAN\n TWO (2) CONSECUTIVE TILES\n OF THE SAME COLOR", 2,2, 7) print("1) yOU CAN'T HAVE MORE THAN\n TWO (2) CONSECUTIVE TILES\n OF THE SAME COLOR", 2,2, 7)
local x = 14 local x = 14
@ -80,7 +80,7 @@ function stateRules()
-------------- --------------
btnBack:draw() btn_back:draw()
end, end,
} }
end end

18
ui.lua
View File

@ -1,4 +1,4 @@
function makeButton(options) function make_button(options)
local state = 0 -- 0 = normal, 1 = hovered, 2 = pressed local state = 0 -- 0 = normal, 1 = hovered, 2 = pressed
return { return {
x = options.x, x = options.x,
@ -7,16 +7,16 @@ function makeButton(options)
h = options.h or 6, h = options.h or 6,
data = options.data, data = options.data,
text = options.text, text = options.text,
onClick = options.onClick, on_click = options.on_click,
onHover = options.onHover, on_hover = options.on_hover,
onDraw = options.onDraw, on_draw = options.on_draw,
ogColor = options.color or 5, ogColor = options.color or 5,
color = options.color or 5, color = options.color or 5,
draw=function(self, selected) draw=function(self, selected)
standardFont() standard_font()
-- rect2(self.x, self.y, self.w, self.h, 8) -- rect2(self.x, self.y, self.w, self.h, 8)
if self.onDraw then self.onDraw(self) end if self.on_draw then self.on_draw(self) end
print(self.text, self.x+1, self.y+1, self.color) print(self.text, self.x+1, self.y+1, self.color)
end, end,
@ -24,13 +24,13 @@ function makeButton(options)
self.color = self.ogColor self.color = self.ogColor
if mouse_x >= self.x and mouse_x <= self.x + self.w and if mouse_x >= self.x and mouse_x <= self.x + self.w and
mouse_y >= self.y and mouse_y <= self.y + self.h then mouse_y >= self.y and mouse_y <= self.y + self.h then
if stat(34)&1 == 0 and state == 2 and self.onClick then if stat(34)&1 == 0 and state == 2 and self.on_click then
self.onClick(self) self.on_click(self)
end end
if stat(34)&1 == 1 then if stat(34)&1 == 1 then
state = 2 state = 2
else else
if self.onHover then self.onHover(self) end if self.on_hover then self.on_hover(self) end
state = 1 state = 1
end end
else else