pico8-0hh1/states/game.lua

92 lines
1.8 KiB
Lua
Raw Normal View History

2022-05-29 18:38:07 +02:00
function stateGame()
local board = Board.new()
2022-06-01 23:14:22 +02:00
local selectedId = 1
2022-06-02 17:55:52 +02:00
local function drawSelectedTile()
2022-06-01 23:14:22 +02:00
local x, y = board:draw_coords(selectedId)
local w = board:getTileWidth()
rect2(x-1, y-1, w+2, w+2, 6)
end
2022-06-02 17:55:52 +02:00
local function create_board()
printh("a: " .. time())
2022-05-29 18:38:07 +02:00
repeat
board:reset()
repeat
board:solveStep(true)
2022-06-02 17:55:52 +02:00
yield()
2022-05-29 18:38:07 +02:00
until board:isComplete()
until board:isValid()
2022-06-02 17:55:52 +02:00
printh("b: " .. time())
2022-05-29 18:38:07 +02:00
-- Remove tiles until randomness is unavoidable
local previous = {board:getTilesCopy()} -- initial state
local invalidcount = 0
while true do
board:setTiles(previous[#previous])
2022-06-02 17:55:52 +02:00
-- remove a random tile
2022-05-29 18:38:07 +02:00
local i = board:getRandomNonZero()
board:fill(i, 0)
add(previous, board:getTilesCopy())
-- try to solve the board
local solved = ""
2022-06-02 17:55:52 +02:00
yield()
2022-05-29 18:38:07 +02:00
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
2022-06-02 17:55:52 +02:00
if invalidcount > 50 then
2022-05-29 18:38:07 +02:00
break
end
end -- end while
2022-06-02 17:55:52 +02:00
printh("c: " .. time())
2022-05-29 18:38:07 +02:00
board:setTiles(previous[#previous])
printh(board:tostring())
2022-06-02 17:55:52 +02:00
printh(count(board:getTilesCopy(), 0).." zeroes")
end
local function _enter()
local c = cocreate(create_board)
2022-05-29 18:38:07 +02:00
end
2022-06-01 23:14:22 +02:00
local function _draw()
2022-06-02 17:55:52 +02:00
cls()
2022-06-01 23:14:22 +02:00
board:draw()
drawSelectedTile()
end
2022-05-29 18:38:07 +02:00
2022-06-01 23:14:22 +02:00
local function _update()
local size = board:getSize()
local x, y = board:idx_xy(selectedId)
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
selectedId = board:xy_idx(x, y)
end
2022-05-29 18:38:07 +02:00
2022-06-01 23:14:22 +02:00
return {
_enter = _enter,
_update = _update,
_draw = _draw,
2022-05-29 18:38:07 +02:00
}
end