Getting issues

This commit is contained in:
Simon Cambier 2022-07-03 19:09:15 +02:00
parent a22ab50f54
commit 14fc9693bb
4 changed files with 30 additions and 39 deletions

View File

@ -2,7 +2,7 @@ local Board = {}
function Board.new() function Board.new()
local debug = false local debug = false
local width = 10 local width = 6
local tile_width = 10 local tile_width = 10
local padding = 1 local padding = 1
local tiles = {} local tiles = {}
@ -102,19 +102,21 @@ function Board.new()
return #self:get_issues() == 0 return #self:get_issues() == 0
end, end,
-- returns a list of issues of the board's current state
get_issues = function(self, details) get_issues = function(self, details)
local rows = self:get_rows() local rows = self:get_rows()
local issues = {} local issues = {}
for y,row in ipairs(rows) do for y,row in ipairs(rows) do
local filled = count(row, 0) == 0
-- check count -- check count
if count(row, BLUE) ~= count(row, YELLOW) then if filled and count(row, BLUE) ~= count(row, YELLOW) then
add(issues, {"row", "count", row, y}) add(issues, {"row", "count", row, y})
if (debug) printh("uneven count on row "..y) if (debug) printh("uneven count on row "..y)
if (not details) return issues 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 filled and equal(other, row) and other ~= row then
add(issues, {"row", "identical", row, y, k}) add(issues, {"row", "identical", row, y, k})
if (debug) printh("equal rows "..k) if (debug) printh("equal rows "..k)
if (not details) return issues if (not details) return issues
@ -130,15 +132,17 @@ function Board.new()
local cols = self:get_cols() local cols = self:get_cols()
for x,col in ipairs(cols) do for x,col in ipairs(cols) do
local filled = count(col, 0) == 0
-- check count -- check count
if count(col, BLUE) ~= count(col, YELLOW) then if filled and count(col, BLUE) ~= count(col, YELLOW) then
add(issues, {"col", "count", col, x}) add(issues, {"col", "count", col, x})
if (debug) printh("uneven count") if (debug) printh("uneven count")
if (not details) return issues if (not details) return issues
end end
-- check identical lines -- check identical lines
for k,other in ipairs(cols) do for k,other in ipairs(cols) do
if equal(other, col) and other ~= col then if filled and equal(other, col) and other ~= col then
add(issues, {"col", "identical", col, x, k}) add(issues, {"col", "identical", col, x, k})
if (debug) printh("equal cols") if (debug) printh("equal cols")
if (not details) return issues if (not details) return issues
@ -163,7 +167,7 @@ function Board.new()
top = max(current, top) top = max(current, top)
current = 1 current = 1
last = v last = v
else elseif v~= 0 then
current += 1 current += 1
end end
end end
@ -214,6 +218,7 @@ function Board.new()
self:fill_lines() self:fill_lines()
self:no_identical_lines() self:no_identical_lines()
local changed = zeroes ~= count(tiles, 0) local changed = zeroes ~= count(tiles, 0)
if not changed and random and not self:is_complete() then if not changed and random and not self:is_complete() then
-- Set a random color -- Set a random color
local z = self:get_random_zero() local z = self:get_random_zero()
@ -333,31 +338,25 @@ function Board.new()
end end
end, end,
-- Finds "identical" lines, and fill the 2 remaining tiles with inverted colors
no_identical_lines = function(self) no_identical_lines = function(self)
-- columns -- columns
local cols = self:get_cols() local cols = self:get_cols()
for x,col in ipairs(cols) do for x,col in ipairs(cols) do
-- if the line has the corrent number of colors,
-- but missing 2 tiles
if count(col, 0) == 2 and count(col, BLUE) == count(col, YELLOW) then if count(col, 0) == 2 and count(col, BLUE) == count(col, YELLOW) then
local y1, y2 = unpack(find(col, 0)) local y1, y2 = unpack(find(col, 0)) -- get the position of the 2 missing tiles
-- create both both solutions
local ab = copy(col) ab[y1] = BLUE ab[y2] = YELLOW local ab = copy(col) ab[y1] = BLUE ab[y2] = YELLOW
local ba = copy(col) ba[y1] = YELLOW ba[y2] = BLUE local ba = copy(col) ba[y1] = YELLOW ba[y2] = BLUE
-- Check if a dupe exists -- Check if a dupe exists
for x2,col in ipairs(cols) do for x2,col in ipairs(cols) do
if equal(col, ab) then if equal(col, ab) then
if debug then
printh("No-dupe at col " .. x .. " from col " .. x2)
printh(" " .. self:xy_idx(x,y1) .. " in yellow")
printh(" " .. self:xy_idx(x,y2) .. " in blue")
end
self:fill(self:xy_idx(x,y1), YELLOW) self:fill(self:xy_idx(x,y1), YELLOW)
self:fill(self:xy_idx(x,y2), BLUE) self:fill(self:xy_idx(x,y2), BLUE)
goto continue goto continue
elseif equal(col, ba) then elseif equal(col, ba) then
if debug then
printh("No-dupe at col " .. x .. " from col " .. x2)
printh(" " .. self:xy_idx(x,y1) .. " in blue")
printh(" " .. self:xy_idx(x,y2) .. " in yellow")
end
self:fill(self:xy_idx(x,y1), BLUE) self:fill(self:xy_idx(x,y1), BLUE)
self:fill(self:xy_idx(x,y2), YELLOW) self:fill(self:xy_idx(x,y2), YELLOW)
goto continue goto continue
@ -377,20 +376,10 @@ function Board.new()
-- Check if a dupe exists -- Check if a dupe exists
for y2,row in ipairs(rows) do for y2,row in ipairs(rows) do
if equal(row, ab) then if equal(row, ab) then
if debug then
printh("No-dupe at row " .. y .. " from row " .. y2)
printh(" " .. self:xy_idx(x1,y) .. " in yellow")
printh(" " .. self:xy_idx(x2,y) .. " in blue")
end
self:fill(self:xy_idx(x1,y), YELLOW) self:fill(self:xy_idx(x1,y), YELLOW)
self:fill(self:xy_idx(x2,y), BLUE) self:fill(self:xy_idx(x2,y), BLUE)
goto continue goto continue
elseif equal(row, ba) then elseif equal(row, ba) then
if debug then
printh("No-dupe at row " .. y .. " from row " .. y2)
printh(" " .. self:xy_idx(x1,y) .. " in blue")
printh(" " .. self:xy_idx(x2,y) .. " in yellow")
end
self:fill(self:xy_idx(x1,y), BLUE) self:fill(self:xy_idx(x1,y), BLUE)
self:fill(self:xy_idx(x2,y), YELLOW) self:fill(self:xy_idx(x2,y), YELLOW)
goto continue goto continue

View File

@ -107,10 +107,9 @@ function _init()
printh(" ") printh(" ")
printh("*************") printh("*************")
printh(" ") printh(" ")
local date = stat(80)..stat(81)..stat(82) local date = stat(80)..stat(81)..stat(82)..stat(84)..stat(85)
-- srand(date) srand(date)
-- srand(20227149) printh("seed " .. date)
printh(date)
mouse_x = 0 mouse_x = 0
mouse_y = 0 mouse_y = 0

View File

@ -5,11 +5,15 @@ function state_game()
local timer_clue = 0 local timer_clue = 0
local function show_clues() local function show_clues()
-- local result = board:is_valid() local issues = board:get_issues(true)
-- if result ~= true and result ~= false then for issue in all(issues) do
-- local rule,what,where = unpack(result) local type, err, row, pos, other = unpack(issue)
-- --printh("Rule: " .. rule .. " " .. what .. " " .. where[1].. " " .. where[2]) if err == "identical" and count(row,0)>0 then
-- end goto continue
end
printh(type .. " " .. err .. " " .. pos)
::continue::
end
end end
local function draw_selected_tile() local function draw_selected_tile()

View File

@ -27,6 +27,7 @@ function state_loading()
local removing_tile = 0 local removing_tile = 0
local create_board = cocreate(function() local create_board = cocreate(function()
local start = time()
repeat repeat
board:reset() board:reset()
repeat repeat
@ -35,8 +36,6 @@ function state_loading()
until board:is_complete() until board:is_complete()
until board:is_valid() until board:is_valid()
printh("b: " .. time())
-- 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() local size = board:get_size()*board:get_size()
@ -64,7 +63,7 @@ function state_loading()
deli(previous) deli(previous)
end end
end -- end while end -- end while
printh("c: " .. time()) printh("board generated in "..time()-start.." seconds")
board:set_tiles(previous[#previous]) board:set_tiles(previous[#previous])
printh(board:tostring()) printh(board:tostring())