From 14fc9693bbcc5a51b451c355d970185b202aadb2 Mon Sep 17 00:00:00 2001 From: Simon Cambier Date: Sun, 3 Jul 2022 19:09:15 +0200 Subject: [PATCH] Getting issues --- board.lua | 43 ++++++++++++++++--------------------------- main.lua | 7 +++---- states/game.lua | 14 +++++++++----- states/loading.lua | 5 ++--- 4 files changed, 30 insertions(+), 39 deletions(-) diff --git a/board.lua b/board.lua index 1826b95..20f6d0a 100644 --- a/board.lua +++ b/board.lua @@ -2,7 +2,7 @@ local Board = {} function Board.new() local debug = false - local width = 10 + local width = 6 local tile_width = 10 local padding = 1 local tiles = {} @@ -102,19 +102,21 @@ function Board.new() return #self:get_issues() == 0 end, + -- returns a list of issues of the board's current state get_issues = function(self, details) local rows = self:get_rows() local issues = {} for y,row in ipairs(rows) do + local filled = count(row, 0) == 0 -- 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}) if (debug) printh("uneven count on row "..y) if (not details) return issues end -- check identical lines 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}) if (debug) printh("equal rows "..k) if (not details) return issues @@ -130,15 +132,17 @@ function Board.new() local cols = self:get_cols() for x,col in ipairs(cols) do + local filled = count(col, 0) == 0 + -- 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}) if (debug) printh("uneven count") if (not details) return issues end -- check identical lines 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}) if (debug) printh("equal cols") if (not details) return issues @@ -163,7 +167,7 @@ function Board.new() top = max(current, top) current = 1 last = v - else + elseif v~= 0 then current += 1 end end @@ -214,6 +218,7 @@ function Board.new() self:fill_lines() self:no_identical_lines() local changed = zeroes ~= count(tiles, 0) + if not changed and random and not self:is_complete() then -- Set a random color local z = self:get_random_zero() @@ -333,31 +338,25 @@ function Board.new() end end, + -- Finds "identical" lines, and fill the 2 remaining tiles with inverted colors no_identical_lines = function(self) -- columns local cols = self:get_cols() 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 - 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 ba = copy(col) ba[y1] = YELLOW ba[y2] = BLUE -- Check if a dupe exists for x2,col in ipairs(cols) do 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,y2), BLUE) goto continue 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,y2), YELLOW) goto continue @@ -377,20 +376,10 @@ function Board.new() -- Check if a dupe exists for y2,row in ipairs(rows) do 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(x2,y), BLUE) goto continue 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(x2,y), YELLOW) goto continue diff --git a/main.lua b/main.lua index 2706dd9..a5e3f9b 100644 --- a/main.lua +++ b/main.lua @@ -107,10 +107,9 @@ function _init() printh(" ") printh("*************") printh(" ") - local date = stat(80)..stat(81)..stat(82) - -- srand(date) - -- srand(20227149) - printh(date) + local date = stat(80)..stat(81)..stat(82)..stat(84)..stat(85) + srand(date) + printh("seed " .. date) mouse_x = 0 mouse_y = 0 diff --git a/states/game.lua b/states/game.lua index fcf7baf..39b29f3 100644 --- a/states/game.lua +++ b/states/game.lua @@ -5,11 +5,15 @@ function state_game() 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 + local issues = board:get_issues(true) + for issue in all(issues) do + local type, err, row, pos, other = unpack(issue) + if err == "identical" and count(row,0)>0 then + goto continue + end + printh(type .. " " .. err .. " " .. pos) + ::continue:: + end end local function draw_selected_tile() diff --git a/states/loading.lua b/states/loading.lua index b5f8176..876761e 100644 --- a/states/loading.lua +++ b/states/loading.lua @@ -27,6 +27,7 @@ function state_loading() local removing_tile = 0 local create_board = cocreate(function() + local start = time() repeat board:reset() repeat @@ -35,8 +36,6 @@ function state_loading() until board:is_complete() until board:is_valid() - printh("b: " .. time()) - -- Remove tiles that can be removed local previous = {board:get_tiles_copy()} -- initial state local size = board:get_size()*board:get_size() @@ -64,7 +63,7 @@ function state_loading() deli(previous) end end -- end while - printh("c: " .. time()) + printh("board generated in "..time()-start.." seconds") board:set_tiles(previous[#previous]) printh(board:tostring())