This commit is contained in:
Simon Cambier 2022-06-03 23:31:26 +02:00
parent 08f35f6d0d
commit 6d1f8bbc8b
5 changed files with 83 additions and 47 deletions

View File

@ -6,6 +6,8 @@ function Board.new()
local tile_width = 10 local tile_width = 10
local margin = 4 local margin = 4
local padding = 1 local padding = 1
local tiles = {}
local locked = {} -- list of indexes
local reset = function() local reset = function()
local t = [[ local t = [[
@ -21,6 +23,7 @@ function Board.new()
0,0,0,0,0,0,0,0,0,0 0,0,0,0,0,0,0,0,0,0
]] ]]
tiles = split(t) tiles = split(t)
locked = {}
end end
reset() reset()
@ -384,6 +387,17 @@ function Board.new()
end end
end, end,
lock_tiles = function(self)
locked = {}
for k,v in ipairs(tiles) do
if v > 0 then add(locked, k) end
end
end,
is_locked = function(self, idx)
return #find(locked, idx) > 0
end,
draw_bg = function(self) draw_bg = function(self)
local w = width local w = width
fillp() fillp()
@ -397,13 +411,23 @@ function Board.new()
self:draw_bg() self:draw_bg()
local w = width local w = width
for k,v in ipairs(tiles) do for k,v in ipairs(tiles) do
if v > 0 then self:draw_tile(k)
local x,y = self:draw_coords(k) end
local color = v == BLUE and 12 or 8 end,
if color == 1 then fillp() else fillp() end
rectfill2(x, y, w, w, color) draw_tile = function(self, idx)
local w = width
local v = tiles[idx]
if v > 0 then
local x,y = self:draw_coords(idx)
local color = v == BLUE and 12 or 8
if color == 1 then fillp() else fillp() end
rectfill2(x, y, w, w, color)
if self:is_locked(idx) then
line(x, y+w, x+w, y+w, v == BLUE and 6 or 14)
line(x+w, y, x+w, y+w, v == BLUE and 6 or 14)
end end
end end
end end,
} }
end end

View File

@ -79,33 +79,6 @@ end
-- return c -- return c
-- end -- end
--
-- Returns the indices of found occurences of `o` within `tbl`
--
function find(tbl, o)
local indices = {}
for k,v in ipairs(tbl) do
if v == o then add(indices, k) end
end
return indices
end
--
-- Makes a shallow table copy
--
function copy(tbl)
return map(tbl, function (o) return o end)
end
--
-- Table equality - shallow comparison
--
function equal(tbl1, tbl2)
for k, _ in ipairs(tbl1) do
if tbl1[k] ~= tbl2[k] then return false end
end
return true
end
-- function tostring(any) -- function tostring(any)
-- if (type(any)~="table") return tostr(any) -- if (type(any)~="table") return tostr(any)

View File

@ -13,6 +13,8 @@ function state_game()
local function _enter(_board) local function _enter(_board)
board = _board board = _board
-- lock the initial tiles
board:lock_tiles()
end end
local function _draw() local function _draw()

View File

@ -8,12 +8,14 @@ function state_loading()
"eMULATING HARDWARE", "eMULATING HARDWARE",
"uNCLOGGING MEMORY BUS", "uNCLOGGING MEMORY BUS",
"sPINNING UP ai AGENT", "sPINNING UP ai AGENT",
"bOOTING VIRTUAL MACHINE", "sIDE-STEPPING VIRTUAL MACHINE",
"cOLLECTING PIXELS TO BUILD TILES", "iNSTALLING BACKTRACKING WIZARD",
"lOADING RAY-TRACING ALGORITHM", "eXFOLIATING PIXELS",
"sCAFFOLDING RAY-TRACING ALGORITHM",
"sWEEPING PARTICLES", "sWEEPING PARTICLES",
"pRESSURIZING USER INTERFACE", "pRESSURIZING USER INTERFACE",
"rECOMPUTING BOARD MATRIX" "sERIALIZING BOARD MATRIX",
"bACKPORTING e2e ENCRYPTION",
} }
local message = "" local message = ""
@ -32,14 +34,16 @@ function state_loading()
until board:is_valid() until board:is_valid()
printh("b: " .. time()) printh("b: " .. time())
-- Remove tiles until randomness is unavoidable
-- Remove tiles that can be removed
local previous = {board:get_tiles_copy()} -- initial state local previous = {board:get_tiles_copy()} -- initial state
while true do while true do
board:set_tiles(previous[#previous]) board:set_tiles(previous[#previous])
-- remove a random tile -- remove a random tile
repeat repeat
removing_tile += 1 removing_tile += 1
until removing_tile == 100 or board:get_tiles_copy()[iremoving_tile] ~= 0 until removing_tile == 100 or board:get_tiles_copy()[removing_tile] ~= 0
if removing_tile == 100 then if removing_tile == 100 then
break break
end end
@ -67,14 +71,12 @@ function state_loading()
end) end)
local function draw_tiles(board) local function draw_tiles(board)
local w = board:get_size() local w = board:get_size()
local tiles = board:get_tiles_copy() local tiles = board:get_tiles_copy()
for k,v in ipairs(tiles) do for k,v in ipairs(tiles) do
if k<removing_tile and v > 0 then if k < removing_tile then
local x,y = board:draw_coords(k) board:draw_tile(k)
local color = v == BLUE and 12 or 8
if color == 1 then fillp() else fillp() end
rectfill2(x, y, w, w, color)
end end
end end
end end
@ -97,7 +99,9 @@ function state_loading()
end end
local function _update() local function _update()
messages_x -= 2 board:lock_tiles()
messages_x -= 5
if loaded then if loaded then
set_state(states.game, board) set_state(states.game, board)
end end
@ -105,13 +109,17 @@ function state_loading()
local function _draw() local function _draw()
cls() cls()
board:draw_bg() board:draw_bg()
draw_tiles(board) draw_tiles(board)
local l = print(message, 0, -100) local l = print(message, 0, -100)
local y = 118+removing_tile/8
local colors = {7,6,5,5}
local ci = removing_tile\20+1
local c = colors[ci]
if not loaded then if not loaded then
print(messages_str, messages_x, 50, 7, 0) rectfill2(0,y-2,127,10,0)
print(messages_str, messages_x, y, c, 0)
-- print(spinner[t()\.25%4+1], 120, 120) -- print(spinner[t()\.25%4+1], 120, 120)
end end
end end

View File

@ -25,4 +25,33 @@ function set_state(state, ...)
if gs and gs._leave then gs._leave() end if gs and gs._leave then gs._leave() end
gs = state gs = state
if gs and gs._enter then gs._enter(unpack(args)) end if gs and gs._enter then gs._enter(unpack(args)) end
end
--
-- Returns the indices of found occurences of `o` within `tbl`
--
function find(tbl, o)
local indices = {}
for k,v in ipairs(tbl) do
if v == o then add(indices, k) end
end
return indices
end
--
-- Makes a shallow table copy
--
function copy(tbl)
return map(tbl, function (o) return o end)
end
--
-- Table equality - shallow comparison
--
function equal(tbl1, tbl2)
for k, _ in ipairs(tbl1) do
if tbl1[k] ~= tbl2[k] then return false end
end
return true
end end