Commit before algo change

This commit is contained in:
Simon Cambier 2022-06-02 17:55:52 +02:00
parent 65871f1789
commit 7fcd9b4ee0
5 changed files with 63 additions and 10 deletions

View File

@ -10,6 +10,7 @@ poke4(0x5700,unpack(split"0x0000.0000,0x0000.0000,0x0202.0202,0x0000.0200,0x0000
#include main.lua #include main.lua
#include board.lua #include board.lua
#include coroutines.lua
#include ui.lua #include ui.lua
#include states/menu.lua #include states/menu.lua
#include states/game.lua #include states/game.lua

43
coroutines.lua Normal file
View File

@ -0,0 +1,43 @@
local coroutines={}
local _cocounter = 0
local _cocreate = cocreate
function cocreate(fn)
_cocounter += 1
k = k or _cocounter
coroutines[_cocounter] = _cocreate(fn, k)
return coroutines[_cocounter], _cocounter
end
function getcoroutine(key)
return coroutines[key]
end
function stepcoroutine(key)
coresume(coroutines[key])
end
function stopcoroutine(key)
coroutines[key] = nil
end
function _coresolve()
for k,co in pairs(coroutines) do
if costatus(co)!='dead' then
coresume(co)
else
stopcoroutine(k)
end
end
end
function wait(seconds)
wait_frames(seconds*60)
end
function wait_frames(frames)
for i=1,frames do
yield()
end
end

View File

@ -132,7 +132,6 @@ end
local create = true local create = true
function _init() function _init()
cls()
printh(" ") printh(" ")
printh(" ") printh(" ")
mouse_x = 0 mouse_x = 0
@ -146,12 +145,13 @@ function _init()
game = stateGame() game = stateGame()
} }
setState(states.menu) setState(states.game)
end end
function _update60() function _update60()
frameCount += 1 frameCount += 1
_coresolve()
gs._update() gs._update()
-- if not create then -- if not create then
-- board:solveStep() -- board:solveStep()
@ -163,7 +163,6 @@ function _update60()
end end
function _draw() function _draw()
cls()
gs._draw() gs._draw()
spr(1, stat(32), stat(33)) spr(1, stat(32), stat(33))
end end

View File

@ -3,33 +3,37 @@ function stateGame()
local selectedId = 1 local selectedId = 1
function drawSelectedTile() local function drawSelectedTile()
local x, y = board:draw_coords(selectedId) local x, y = board:draw_coords(selectedId)
local w = board:getTileWidth() local w = board:getTileWidth()
rect2(x-1, y-1, w+2, w+2, 6) rect2(x-1, y-1, w+2, w+2, 6)
end end
local function _enter() local function create_board()
-- Create a board printh("a: " .. time())
repeat repeat
board:reset() board:reset()
repeat repeat
board:solveStep(true) board:solveStep(true)
yield()
until board:isComplete() until board:isComplete()
until board:isValid() until board:isValid()
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 invalidcount = 0
while true do while true do
-- remove a random tile
board:setTiles(previous[#previous]) board:setTiles(previous[#previous])
-- remove a random tile
local i = board:getRandomNonZero() local i = board:getRandomNonZero()
board:fill(i, 0) board:fill(i, 0)
add(previous, board:getTilesCopy()) add(previous, board:getTilesCopy())
-- try to solve the board -- try to solve the board
local solved = "" local solved = ""
yield()
repeat repeat
solved = board:solveStep() solved = board:solveStep()
until board:isComplete() or solved == "invalid" until board:isComplete() or solved == "invalid"
@ -40,16 +44,23 @@ function stateGame()
deli(previous) deli(previous)
invalidcount += 1 invalidcount += 1
end end
if invalidcount == 100 then if invalidcount > 50 then
break break
end end
end -- end while end -- end while
printh("c: " .. time())
board:setTiles(previous[#previous]) board:setTiles(previous[#previous])
printh(board:tostring()) printh(board:tostring())
printh(count(board:getTilesCopy(), 0).." zeroes")
end
local function _enter()
local c = cocreate(create_board)
end end
local function _draw() local function _draw()
cls()
board:draw() board:draw()
drawSelectedTile() drawSelectedTile()
end end
@ -66,7 +77,6 @@ function stateGame()
elseif btnp(RIGHT) then elseif btnp(RIGHT) then
x += 1 x += 1
end end
printh(x.." "..y)
if (x<1) x=size if (x<1) x=size
if (x>size) x=1 if (x>size) x=1
if (y<1) y=size if (y<1) y=size

View File

@ -11,7 +11,7 @@ function stateRules()
setState(states.menu) setState(states.menu)
end end
local btnBack = makeButton({x=1, y=118, w=30, h=7, text=" mENU", color=8, local btnBack = makeButton({x=1, y=118, w=30, h=7, text="menu", color=8,
onClick=function() goBack() end, onClick=function() goBack() end,
onHover=function(btn) btn.color = 7 end}) onHover=function(btn) btn.color = 7 end})