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)
end,
getTile = function(self, idx)
get_tile = function(self, idx)
return tiles[idx]
end,
-- returns a COPY of the tiles array
getTilesCopy = function(self)
get_tiles_copy = function(self)
return copy(tiles)
end,
-- 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)
tiles = copy(newtiles)
end,
@ -59,11 +59,11 @@ function Board.new()
margin + (y-1)*tile_width + (y-1)*(padding+1)
end,
getSize = function(self)
get_size = function(self)
return width
end,
getTileWidth = function(self)
get_tile_width = function(self)
return tile_width
end,
@ -75,7 +75,7 @@ function Board.new()
tiles[idx] = color
end,
getRows = function(self)
get_rows = function(self)
local ret = {}
for i = 1, width do
add(ret, slice(tiles, ((i - 1) * width) + 1, i * width))
@ -83,21 +83,21 @@ function Board.new()
return ret
end,
getCols = function(self)
get_cols = function(self)
local ret = {}
local rows = self.getRows(self)
local rows = self.get_rows(self)
for i = 1, width do
add(ret, map(rows, function(v) return v[i] end))
end
return ret
end,
isComplete = function(self)
is_complete = function(self)
return count(tiles, 0) == 0
end,
isValid = function(self)
local rows = self:getRows()
is_valid = function(self)
local rows = self:get_rows()
for y,row in ipairs(rows) do
-- check count
if count(row, BLUE) ~= count(row, YELLOW) then
@ -112,13 +112,13 @@ function Board.new()
end
end
-- check triples
if self:countConsecutives(row) > 2 then
if self:count_consecutives(row) > 2 then
if (debug) printh("triples")
return false
end
end
local cols = self:getCols()
local cols = self:get_cols()
for col in all(cols) do
-- check count
if count(col, BLUE) ~= count(col, YELLOW) then
@ -133,7 +133,7 @@ function Board.new()
end
end
-- check triples
if self:countConsecutives(col) > 2 then
if self:count_consecutives(col) > 2 then
if (debug) printh("triples")
return false
end
@ -141,7 +141,7 @@ function Board.new()
return true
end,
countConsecutives = function(self, line)
count_consecutives = function(self, line)
local count = 0
local last = 0
for v in all(line) do
@ -158,7 +158,7 @@ function Board.new()
--
-- Returns the index of a random zero tile
--
getRandomZero = function(self)
get_random_zero = function(self)
assert(count(tiles, 0) > 0, "No zero left")
local zeroes = filter(tiles, function(v) return v == 0 end, true)
local z = {}
@ -171,7 +171,7 @@ function Board.new()
--
-- 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")
local numbers = filter(tiles, function(v) return v ~= 0 end, true)
local z = {}
@ -192,24 +192,24 @@ function Board.new()
-- Solves 1 step of the board
-- Returns "valid" if it solved it without guessing
-- Returns "invalid" if the board cannot be solved
solveStep = function(self, random)
solve_step = function(self, random)
local zeroes = count(tiles, 0)
self:surroundDoubles()
self:splitTriples()
self:fillLines()
self:noIdenticalLines()
self:surround_doubles()
self:split_triples()
self:fill_lines()
self:no_identical_lines()
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
local z = self:getRandomZero()
local z = self:get_random_zero()
self:fill(z, rnd({BLUE, YELLOW}))
if (debug) printh("!!!!!!!!!!!!!!!!! RANDOM FILL AT " .. z)
return "invalid"
end
return (changed or self:isComplete()) and "valid" or "invalid"
return (changed or self:is_complete()) and "valid" or "invalid"
end,
surroundDoubles = function(self)
surround_doubles = function(self)
for idx,v in ipairs(tiles) do
local x,y = self:idx_xy(idx)
if v == 0 then
@ -245,7 +245,7 @@ function Board.new()
end
end,
splitTriples = function(self)
split_triples = function(self)
for idx, col in ipairs(tiles) do
local x,y = self:idx_xy(idx)
if col == 0 then
@ -273,17 +273,17 @@ function Board.new()
end
end,
fillLines = function(self)
local rows = self:getRows()
local cols = self:getCols()
fill_lines = function(self)
local rows = self:get_rows()
local cols = self:get_cols()
-- rows
for y,row in ipairs(rows) do
local a = count(row, BLUE)
local b = count(row, YELLOW)
if a ~= b then
if a == width/2 then self:fillRow(y, YELLOW) end
if b == width/2 then self:fillRow(y, BLUE) end
if a == width/2 then self:fill_row(y, YELLOW) end
if b == width/2 then self:fill_row(y, BLUE) end
end
end
@ -292,35 +292,35 @@ function Board.new()
local a = count(col, BLUE)
local b = count(col, YELLOW)
if a ~= b then
if a == width/2 then self:fillCol(x, YELLOW) end
if b == width/2 then self:fillCol(x, BLUE) end
if a == width/2 then self:fill_col(x, YELLOW) end
if b == width/2 then self:fill_col(x, BLUE) 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"))
local idx = self:xy_idx(1, y)
for i = idx, (idx+width-1) do
if self:getTile(i) == 0 then
if self:get_tile(i) == 0 then
self:fill(i, color)
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"))
local idx = self:xy_idx(x, 1)
for i = idx, #tiles, width do
if self:getTile(i) == 0 then
if self:get_tile(i) == 0 then
self:fill(i, color)
end
end
end,
noIdenticalLines = function(self)
no_identical_lines = function(self)
-- columns
local cols = self:getCols()
local cols = self:get_cols()
for x,col in ipairs(cols) do
if count(col, 0) == 2 and count(col, BLUE) == count(col, YELLOW) then
local y1, y2 = unpack(find(col, 0))
@ -353,7 +353,7 @@ function Board.new()
end
-- rows
local rows = self:getRows()
local rows = self:get_rows()
for y,row in ipairs(rows) do
if count(row, 0) == 2 and count(row, BLUE) == count(row, YELLOW) then
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
--
function customFont()
function custom_font()
poke(0x5f58,0x81)
end
function standardFont(font)
function standard_font(font)
poke(0x5f58,0x80)
end
@ -118,7 +118,7 @@ end
-- end
function setState(state, ...)
function set_state(state, ...)
local args = {...}
if gs and gs._leave then gs._leave() end
gs = state
@ -137,24 +137,24 @@ function _init()
mouse_x = 0
mouse_y = 0
frameCount = 0
frame_count = 0
states = {
rules = stateRules(),
menu = stateMenu(),
game = stateGame()
rules = state_rules(),
menu = state_menu(),
game = state_game()
}
setState(states.game)
set_state(states.game)
end
function _update60()
frameCount += 1
frame_count += 1
_coresolve()
gs._update()
-- if not create then
-- board:solveStep()
-- board:solve_step()
-- end
-- mouse

View File

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

View File

@ -1,21 +1,21 @@
function stateMenu()
function state_menu()
local selected = 1
local function onBtnDraw(btn)
local function on_btn_draw(btn)
if selected == btn.data.i then
btn.color = 7
end
end
local buttons = {
makeButton({x=10, y=10, w=30, text="play", data={i=1},
onClick=function() setState(states.game) end,
onHover=function(btn) btn.color = 7 selected = 1 end,
onDraw=onBtnDraw}),
makeButton({x=10, y=20, w=30, text="rules", data={i=2},
onClick=function() setState(states.rules) end,
onHover=function(btn) btn.color = 7 selected = 2 end,
onDraw=onBtnDraw})
make_button({x=10, y=10, w=30, text="play", data={i=1},
on_click=function() set_state(states.game) end,
on_hover=function(btn) btn.color = 7 selected = 1 end,
on_draw=on_btn_draw}),
make_button({x=10, y=20, w=30, text="rules", data={i=2},
on_click=function() set_state(states.rules) end,
on_hover=function(btn) btn.color = 7 selected = 2 end,
on_draw=on_btn_draw})
}
local function _enter()
@ -35,7 +35,7 @@ function stateMenu()
elseif btnp(DOWN) then
selected += 1
elseif btnp(BTN_O) then
buttons[selected]:onClick()
buttons[selected]:on_click()
end
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 color = 1
@ -7,17 +7,17 @@ function stateRules()
rect2(x,y,w,h,fade[color])
end
local function goBack()
setState(states.menu)
local function go_back()
set_state(states.menu)
end
local btnBack = makeButton({x=1, y=118, w=30, h=7, text="❎menu", color=8,
onClick=function() goBack() end,
onHover=function(btn) btn.color = 7 end})
local btn_back = make_button({x=1, y=118, w=30, h=7, text="❎menu", color=8,
on_click=function() go_back() end,
on_hover=function(btn) btn.color = 7 end})
return {
_update=function()
if frameCount%8==0 then
if frame_count%8==0 then
color += 1
end
if color>#fade then
@ -26,14 +26,14 @@ function stateRules()
-- Back to the menu
if btnp(BTN_X) then
goBack()
go_back()
end
btnBack:update()
btn_back:update()
end,
_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)
local x = 14
@ -80,7 +80,7 @@ function stateRules()
--------------
btnBack:draw()
btn_back:draw()
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
return {
x = options.x,
@ -7,16 +7,16 @@ function makeButton(options)
h = options.h or 6,
data = options.data,
text = options.text,
onClick = options.onClick,
onHover = options.onHover,
onDraw = options.onDraw,
on_click = options.on_click,
on_hover = options.on_hover,
on_draw = options.on_draw,
ogColor = options.color or 5,
color = options.color or 5,
draw=function(self, selected)
standardFont()
standard_font()
-- 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)
end,
@ -24,13 +24,13 @@ function makeButton(options)
self.color = self.ogColor
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
if stat(34)&1 == 0 and state == 2 and self.onClick then
self.onClick(self)
if stat(34)&1 == 0 and state == 2 and self.on_click then
self.on_click(self)
end
if stat(34)&1 == 1 then
state = 2
else
if self.onHover then self.onHover(self) end
if self.on_hover then self.on_hover(self) end
state = 1
end
else