Commit before fixes

This commit is contained in:
Simon Cambier 2022-07-01 19:50:08 +02:00
parent 6d1f8bbc8b
commit 0276d70de3
5 changed files with 42 additions and 24 deletions

View File

@ -10,19 +10,10 @@ function Board.new()
local locked = {} -- list of indexes local locked = {} -- list of indexes
local reset = function() local reset = function()
local t = [[ tiles = {}
0,0,0,0,0,0,0,0,0,0, for i = 1, width * width do
0,0,0,0,0,0,0,0,0,0, tiles[i] = 0
0,0,0,0,0,0,0,0,0,0, end
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0
]]
tiles = split(t)
locked = {} locked = {}
end end
@ -76,6 +67,16 @@ function Board.new()
tiles[idx] = color tiles[idx] = color
end, end,
try_flip_tile = function(self, id)
if locked[id] then return end
if tiles[id] == 0 then
tiles[id] = YELLOW
elseif tiles[id] == YELLOW then
tiles[id] = BLUE
else tiles[id] = 0
end
end,
get_rows = function(self) get_rows = function(self)
local ret = {} local ret = {}
for i = 1, width do for i = 1, width do
@ -390,16 +391,16 @@ function Board.new()
lock_tiles = function(self) lock_tiles = function(self)
locked = {} locked = {}
for k,v in ipairs(tiles) do for k,v in ipairs(tiles) do
if v > 0 then add(locked, k) end if v > 0 then locked[k] = true end
end end
end, end,
is_locked = function(self, idx) is_locked = function(self, idx)
return #find(locked, idx) > 0 return locked[idx]
end, end,
draw_bg = function(self) draw_bg = function(self)
local w = width local w = tile_width
fillp() fillp()
for k,v in ipairs(tiles) do for k,v in ipairs(tiles) do
local x,y = self:draw_coords(k) local x,y = self:draw_coords(k)
@ -409,14 +410,14 @@ function Board.new()
draw = function(self) draw = function(self)
self:draw_bg() self:draw_bg()
local w = width local w = tile_width
for k,v in ipairs(tiles) do for k,v in ipairs(tiles) do
self:draw_tile(k) self:draw_tile(k)
end end
end, end,
draw_tile = function(self, idx) draw_tile = function(self, idx)
local w = width local w = tile_width
local v = tiles[idx] local v = tiles[idx]
if v > 0 then if v > 0 then
local x,y = self:draw_coords(idx) local x,y = self:draw_coords(idx)

View File

@ -99,7 +99,12 @@ local create = true
function _init() function _init()
printh(" ") printh(" ")
printh("*************")
printh(" ") printh(" ")
local date = stat(80)..stat(81)..stat(82)..stat(85)
-- srand(date)
srand(20227149)
printh(date)
mouse_x = 0 mouse_x = 0
mouse_y = 0 mouse_y = 0

View File

@ -3,7 +3,7 @@ function state_game()
local board local board
local selected_id = 1 local selected_id = 1
local function drawSelectedTile() local function draw_selected_tile()
local x, y = board:draw_coords(selected_id) local x, y = board:draw_coords(selected_id)
local w = board:get_tile_width() local w = board:get_tile_width()
fillp() fillp()
@ -20,7 +20,7 @@ function state_game()
local function _draw() local function _draw()
cls() cls()
board:draw() board:draw()
drawSelectedTile() draw_selected_tile()
end end
local function _update() local function _update()
@ -35,6 +35,11 @@ function state_game()
elseif btnp(RIGHT) then elseif btnp(RIGHT) then
x += 1 x += 1
end end
if btnp(BTN_O) then
board:try_flip_tile(selected_id)
end
if (x<1) x=size if (x<1) x=size
if (x>size) x=1 if (x>size) x=1
if (y<1) y=size if (y<1) y=size

View File

@ -37,14 +37,15 @@ function state_loading()
-- Remove tiles that can be removed -- Remove tiles that can be removed
local previous = {board:get_tiles_copy()} -- initial state local previous = {board:get_tiles_copy()} -- initial state
local size = board:get_size()*board:get_size()
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()[removing_tile] ~= 0 until removing_tile == size or board:get_tiles_copy()[removing_tile] ~= 0
if removing_tile == 100 then if removing_tile == size then
break break
end end
@ -111,9 +112,9 @@ function state_loading()
cls() cls()
board:draw_bg() board:draw_bg()
draw_tiles(board) draw_tiles(board)
local s = board:get_size()*board:get_size()
local l = print(message, 0, -100) local l = print(message, 0, -100)
local y = 118+removing_tile/8 local y = 118+(removing_tile/s)*100/8
local colors = {7,6,5,5} local colors = {7,6,5,5}
local ci = removing_tile\20+1 local ci = removing_tile\20+1
local c = colors[ci] local c = colors[ci]

View File

@ -39,6 +39,12 @@ function find(tbl, o)
return indices return indices
end end
function contains(tbl, o)
for v in all(tbl) do
if v == o then return true end
end
return false
end
-- --
-- Makes a shallow table copy -- Makes a shallow table copy
-- --