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

View File

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

View File

@ -2,6 +2,15 @@ function state_game()
local board
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 x, y = board:draw_coords(selected_id)
@ -17,6 +26,10 @@ function state_game()
board:lock_tiles()
end
local function _leave()
-- stopcoroutine(show_clues)
end
local function _draw()
cls()
board:draw()
@ -24,6 +37,10 @@ function state_game()
end
local function _update()
timer_clue += 1
if timer_clue % 120 == 0 then
show_clues()
end
local size = board:get_size()
local x, y = board:idx_xy(selected_id)
if btnp(UP) then
@ -51,5 +68,6 @@ function state_game()
_enter = _enter,
_update = _update,
_draw = _draw,
_leave = _leave
}
end