From 4920224034c33beb24835f7464b34f3f51c2e85d Mon Sep 17 00:00:00 2001 From: Simon Cambier Date: Thu, 2 Jun 2022 20:08:20 +0200 Subject: [PATCH] Refactored board creation --- board.lua | 1 + states/game.lua | 19 +++++++++---------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/board.lua b/board.lua index 0a20d1a..0e83a10 100644 --- a/board.lua +++ b/board.lua @@ -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 diff --git a/states/game.lua b/states/game.lua index 43fc9e4..fe9e17d 100644 --- a/states/game.lua +++ b/states/game.lua @@ -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()