pico8-0hh1/states/game.lua

91 lines
1.7 KiB
Lua
Raw Normal View History

2022-06-02 20:18:02 +02:00
function state_game()
2022-05-29 18:38:07 +02:00
local board = Board.new()
2022-06-02 20:18:02 +02:00
local selected_id = 1
2022-06-01 23:14:22 +02:00
2022-06-02 17:55:52 +02:00
local function drawSelectedTile()
2022-06-02 20:18:02 +02:00
local x, y = board:draw_coords(selected_id)
local w = board:get_tile_width()
2022-06-01 23:14:22 +02:00
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
2022-06-02 20:18:02 +02:00
board:solve_step(true)
2022-06-02 17:55:52 +02:00
yield()
2022-06-02 20:18:02 +02:00
until board:is_complete()
until board:is_valid()
2022-05-29 18:38:07 +02:00
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
2022-06-02 20:18:02 +02:00
local previous = {board:get_tiles_copy()} -- initial state
2022-06-02 20:08:20 +02:00
local i = 0
2022-05-29 18:38:07 +02:00
while true do
2022-06-02 20:18:02 +02:00
board:set_tiles(previous[#previous])
2022-06-02 17:55:52 +02:00
-- remove a random tile
2022-06-02 20:08:20 +02:00
repeat
i += 1
2022-06-02 20:18:02 +02:00
until i == 100 or board:get_tiles_copy()[i] ~= 0
2022-06-02 20:08:20 +02:00
if i == 100 then
break
end
2022-05-29 18:38:07 +02:00
board:fill(i, 0)
2022-06-02 20:18:02 +02:00
add(previous, board:get_tiles_copy())
2022-05-29 18:38:07 +02:00
-- 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
2022-06-02 20:18:02 +02:00
solved = board:solve_step()
until board:is_complete() or solved == "invalid"
2022-05-29 18:38:07 +02:00
if solved == "invalid" then
deli(previous)
end
end -- end while
2022-06-02 17:55:52 +02:00
printh("c: " .. time())
2022-05-29 18:38:07 +02:00
2022-06-02 20:18:02 +02:00
board:set_tiles(previous[#previous])
2022-05-29 18:38:07 +02:00
printh(board:tostring())
2022-06-02 20:18:02 +02:00
printh(count(board:get_tiles_copy(), 0).." zeroes")
2022-06-02 17:55:52 +02:00
end
local function _enter()
2022-06-02 20:08:20 +02:00
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()
2022-06-02 20:18:02 +02:00
local size = board:get_size()
local x, y = board:idx_xy(selected_id)
2022-06-01 23:14:22 +02:00
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
2022-06-02 20:18:02 +02:00
selected_id = board:xy_idx(x, y)
2022-06-01 23:14:22 +02:00
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