pico8-0hh1/states/game.lua

91 lines
1.7 KiB
Lua

function state_game()
local board = Board.new()
local selected_id = 1
local function drawSelectedTile()
local x, y = board:draw_coords(selected_id)
local w = board:get_tile_width()
rect2(x-1, y-1, w+2, w+2, 6)
end
local function create_board()
printh("a: " .. time())
repeat
board:reset()
repeat
board:solve_step(true)
yield()
until board:is_complete()
until board:is_valid()
printh("b: " .. time())
-- Remove tiles until randomness is unavoidable
local previous = {board:get_tiles_copy()} -- initial state
local i = 0
while true do
board:set_tiles(previous[#previous])
-- remove a random tile
repeat
i += 1
until i == 100 or board:get_tiles_copy()[i] ~= 0
if i == 100 then
break
end
board:fill(i, 0)
add(previous, board:get_tiles_copy())
-- try to solve the board
local solved = ""
yield()
repeat
solved = board:solve_step()
until board:is_complete() or solved == "invalid"
if solved == "invalid" then
deli(previous)
end
end -- end while
printh("c: " .. time())
board:set_tiles(previous[#previous])
printh(board:tostring())
printh(count(board:get_tiles_copy(), 0).." zeroes")
end
local function _enter()
cocreate(create_board)
end
local function _draw()
cls()
board:draw()
drawSelectedTile()
end
local function _update()
local size = board:get_size()
local x, y = board:idx_xy(selected_id)
if btnp(UP) then
y -= 1
elseif btnp(DOWN) then
y += 1
elseif btnp(LEFT) then
x -= 1
elseif btnp(RIGHT) then
x += 1
end
if (x<1) x=size
if (x>size) x=1
if (y<1) y=size
if (y>size) y=1
selected_id = board:xy_idx(x, y)
end
return {
_enter = _enter,
_update = _update,
_draw = _draw,
}
end