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,
fill = function(self, idx, color, invert)
if idx > width*width then return end
if invert then
color = color == YELLOW and BLUE or YELLOW
end

View File

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