WIP showing clues

This commit is contained in:
Simon Cambier 2022-07-02 20:45:11 +02:00
parent 6f54ba9477
commit bc8b1be6cd
3 changed files with 44 additions and 15 deletions

View File

@ -99,48 +99,59 @@ function Board.new()
end, end,
is_valid = function(self) is_valid = function(self)
return #self:get_issues() == 0
end,
get_issues = function(self, details)
local rows = self:get_rows() local rows = self:get_rows()
local issues = {}
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
add(issues, {"row", "count", row, y})
if (debug) printh("uneven count on row "..y) if (debug) printh("uneven count on row "..y)
return false if (not details) return issues
end end
-- check identical lines -- check identical lines
for k,other in ipairs(rows) do for k,other in ipairs(rows) do
if equal(other, row) and other ~= row then if equal(other, row) and other ~= row then
add(issues, {"row", "identical", row, y, k})
if (debug) printh("equal rows "..k) if (debug) printh("equal rows "..k)
return false if (not details) return issues
end end
end end
-- check triples -- check triples
if self:count_consecutives(row) > 2 then if self:count_consecutives(row) > 2 then
add(issues, {"row", "triples", row, y})
if (debug) printh("triples") if (debug) printh("triples")
return false if (not details) return issues
end end
end end
local cols = self:get_cols() local cols = self:get_cols()
for col in all(cols) do for x,col in ipairs(cols) do
-- check count -- check count
if count(col, BLUE) ~= count(col, YELLOW) then if count(col, BLUE) ~= count(col, YELLOW) then
add(issues, {"col", "count", col, x})
if (debug) printh("uneven count") if (debug) printh("uneven count")
return false if (not details) return issues
end end
-- check identical lines -- check identical lines
for other in all(cols) do for k,other in ipairs(cols) do
if equal(other, col) and other ~= col then if equal(other, col) and other ~= col then
add(issues, {"col", "identical", col, x, k})
if (debug) printh("equal cols") if (debug) printh("equal cols")
return false if (not details) return issues
end end
end end
-- check triples -- check triples
if self:count_consecutives(col) > 2 then if self:count_consecutives(col) > 2 then
add(issues, {"col", "triples", col, x})
if (debug) printh("triples") if (debug) printh("triples")
return false if (not details) return issues
end end
end end
return true return issues
end, end,
count_consecutives = function(self, line) count_consecutives = function(self, line)

View File

@ -1,14 +1,12 @@
local coroutines={} local coroutines={}
-- starts a coroutine and returns its key -- starts a coroutine and saves it for future reference
function startcoroutine(fn, key) function startcoroutine(fn)
assert(tostr(fn) == "[thread]") -- make sure that fn is a coroutine, the return value of cocreate()
add(coroutines, fn) add(coroutines, fn)
end end
function stepcoroutine(fn) -- stops the the coroutine
coresume(fn)
end
function stopcoroutine(fn) function stopcoroutine(fn)
del(coroutines, fn) del(coroutines, fn)
end end
@ -23,10 +21,12 @@ function _coresolve()
end end
end end
-- to be used inside a coroutine
function wait(seconds) function wait(seconds)
wait_frames(seconds*60) wait_frames(seconds*60)
end end
-- to be used inside a coroutine
function wait_frames(frames) function wait_frames(frames)
for i=1,frames do for i=1,frames do
yield() yield()

View File

@ -2,6 +2,15 @@ function state_game()
local board local board
local selected_id = 1 local selected_id = 1
local timer_clue = 0
local function show_clues()
-- local result = board:is_valid()
-- if result ~= true and result ~= false then
-- local rule,what,where = unpack(result)
-- --printh("Rule: " .. rule .. " " .. what .. " " .. where[1].. " " .. where[2])
-- end
end
local function draw_selected_tile() local function draw_selected_tile()
local x, y = board:draw_coords(selected_id) local x, y = board:draw_coords(selected_id)
@ -17,6 +26,10 @@ function state_game()
board:lock_tiles() board:lock_tiles()
end end
local function _leave()
-- stopcoroutine(show_clues)
end
local function _draw() local function _draw()
cls() cls()
board:draw() board:draw()
@ -24,6 +37,10 @@ function state_game()
end end
local function _update() local function _update()
timer_clue += 1
if timer_clue % 120 == 0 then
show_clues()
end
local size = board:get_size() local size = board:get_size()
local x, y = board:idx_xy(selected_id) local x, y = board:idx_xy(selected_id)
if btnp(UP) then if btnp(UP) then
@ -51,5 +68,6 @@ function state_game()
_enter = _enter, _enter = _enter,
_update = _update, _update = _update,
_draw = _draw, _draw = _draw,
_leave = _leave
} }
end end