Refactored board creation

This commit is contained in:
Simon Cambier 2022-06-02 20:08:20 +02:00
parent 7fcd9b4ee0
commit 4920224034
2 changed files with 10 additions and 10 deletions

View File

@ -68,6 +68,7 @@ function Board.new()
end, end,
fill = function(self, idx, color, invert) fill = function(self, idx, color, invert)
if idx > width*width then return end
if invert then if invert then
color = color == YELLOW and BLUE or YELLOW color = color == YELLOW and BLUE or YELLOW
end end

View File

@ -23,11 +23,17 @@ function stateGame()
printh("b: " .. time()) printh("b: " .. time())
-- Remove tiles until randomness is unavoidable -- Remove tiles until randomness is unavoidable
local previous = {board:getTilesCopy()} -- initial state local previous = {board:getTilesCopy()} -- initial state
local invalidcount = 0 local i = 0
while true do while true do
board:setTiles(previous[#previous]) board:setTiles(previous[#previous])
-- remove a random tile -- remove a random tile
local i = board:getRandomNonZero() repeat
i += 1
until i == 100 or board:getTilesCopy()[i] ~= 0
if i == 100 then
break
end
board:fill(i, 0) board:fill(i, 0)
add(previous, board:getTilesCopy()) add(previous, board:getTilesCopy())
@ -37,15 +43,8 @@ function stateGame()
repeat repeat
solved = board:solveStep() solved = board:solveStep()
until board:isComplete() or solved == "invalid" until board:isComplete() or solved == "invalid"
if board:isComplete() then
invalidcount = 0
end
if solved == "invalid" then if solved == "invalid" then
deli(previous) deli(previous)
invalidcount += 1
end
if invalidcount > 50 then
break
end end
end -- end while end -- end while
printh("c: " .. time()) printh("c: " .. time())
@ -56,7 +55,7 @@ function stateGame()
end end
local function _enter() local function _enter()
local c = cocreate(create_board) cocreate(create_board)
end end
local function _draw() local function _draw()