2022-05-29 18:38:07 +02:00
|
|
|
poke(0x5F2D, 1)
|
2022-05-28 17:32:43 +02:00
|
|
|
|
2022-05-28 11:44:09 +02:00
|
|
|
--
|
|
|
|
-- constants
|
|
|
|
--
|
|
|
|
|
|
|
|
local BLUE = 1
|
|
|
|
local YELLOW = 2
|
|
|
|
|
2022-05-29 22:55:43 +02:00
|
|
|
-- 🅾️ Z[C]N ❎ [X]VM
|
|
|
|
-- X and C have the same position on QWERTY and AZERTY
|
|
|
|
local LEFT,RIGHT,UP,DOWN,BTN_O,BTN_X = 0,1,2,3,4,5
|
|
|
|
|
2022-05-28 11:44:09 +02:00
|
|
|
--
|
|
|
|
-- Utils
|
|
|
|
--
|
|
|
|
|
|
|
|
function idx_xy(idx, width)
|
2022-05-28 12:34:08 +02:00
|
|
|
return (idx - 1) % width + 1, (idx - 1) \ width + 1
|
2022-05-28 11:44:09 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function xy_idx(x, y, width)
|
2022-05-28 12:34:08 +02:00
|
|
|
return ((y - 1) * width) + x
|
2022-05-28 11:44:09 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function map(tbl, f)
|
2022-05-28 12:34:08 +02:00
|
|
|
local t = {}
|
|
|
|
for k, v in pairs(tbl) do
|
|
|
|
t[k] = f(v)
|
|
|
|
end
|
|
|
|
return t
|
2022-05-28 11:44:09 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function filter(tbl, f, keepindex)
|
2022-05-28 12:34:08 +02:00
|
|
|
local ret = {}
|
|
|
|
for k, v in pairs(tbl) do
|
|
|
|
if f(v) then
|
|
|
|
if keepindex
|
|
|
|
then ret[k] = v
|
|
|
|
else add(ret, v)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return ret
|
2022-05-28 11:44:09 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function slice(tbl, first, last, step)
|
2022-05-28 12:34:08 +02:00
|
|
|
local sliced = {}
|
|
|
|
for i = (first or 1), (last or #tbl), (step or 1) do
|
2022-05-29 14:35:28 +02:00
|
|
|
sliced[#sliced + 1] = tbl[i]
|
2022-05-28 12:34:08 +02:00
|
|
|
end
|
|
|
|
return sliced
|
2022-05-28 11:44:09 +02:00
|
|
|
end
|
|
|
|
|
2022-05-30 22:44:22 +02:00
|
|
|
function rectfill2(x, y, w, h, col)
|
|
|
|
rectfill(x, y, x+w, y+h, col)
|
|
|
|
end
|
|
|
|
|
|
|
|
function rect2(x, y, w, h, col)
|
|
|
|
rect(x, y, x+w, y+h, col)
|
2022-05-28 11:44:09 +02:00
|
|
|
end
|
|
|
|
|
2022-05-29 14:35:28 +02:00
|
|
|
--
|
|
|
|
-- Overrides count() to accept a callback
|
|
|
|
--
|
|
|
|
-- local _count = count
|
|
|
|
-- function count(tbl, p)
|
|
|
|
-- if type(p) != "function" then return _count(tbl, p) end
|
|
|
|
-- local c = 0
|
|
|
|
-- for v in all(tbl) do
|
|
|
|
-- if p(v) then c+=1 end
|
|
|
|
-- end
|
|
|
|
-- return c
|
|
|
|
-- end
|
2022-05-28 11:44:09 +02:00
|
|
|
|
2022-05-29 14:35:28 +02:00
|
|
|
--
|
|
|
|
-- Returns the indices of found occurences of `o` within `tbl`
|
|
|
|
--
|
2022-05-28 15:02:55 +02:00
|
|
|
function find(tbl, o)
|
|
|
|
local indices = {}
|
|
|
|
for k,v in ipairs(tbl) do
|
|
|
|
if v == o then add(indices, k) end
|
|
|
|
end
|
|
|
|
return indices
|
|
|
|
end
|
|
|
|
|
2022-05-29 14:35:28 +02:00
|
|
|
--
|
|
|
|
-- Makes a shallow table copy
|
|
|
|
--
|
2022-05-28 15:02:55 +02:00
|
|
|
function copy(tbl)
|
2022-05-29 14:35:28 +02:00
|
|
|
return map(tbl, function (o) return o end)
|
2022-05-28 15:02:55 +02:00
|
|
|
end
|
|
|
|
|
2022-05-29 14:35:28 +02:00
|
|
|
--
|
|
|
|
-- Table equality - shallow comparison
|
|
|
|
--
|
2022-05-28 15:02:55 +02:00
|
|
|
function equal(tbl1, tbl2)
|
|
|
|
for k, _ in ipairs(tbl1) do
|
|
|
|
if tbl1[k] ~= tbl2[k] then return false end
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2022-05-29 14:35:28 +02:00
|
|
|
-- function tostring(any)
|
|
|
|
-- if (type(any)~="table") return tostr(any)
|
|
|
|
-- local str = "{"
|
|
|
|
-- for k,v in pairs(any) do
|
|
|
|
-- if (str~="{") str=str..","
|
|
|
|
-- str=str..tostring(k).."="..tostring(v)
|
|
|
|
-- end
|
|
|
|
-- return str.."}"
|
|
|
|
-- end
|
2022-05-28 23:58:00 +02:00
|
|
|
|
2022-05-29 19:49:59 +02:00
|
|
|
function makeButton(x, y, w, h, text, onClick, onHover)
|
2022-05-29 18:38:07 +02:00
|
|
|
local state = 0 -- 0 = normal, 1 = hovered, 2 = pressed
|
2022-05-28 11:44:09 +02:00
|
|
|
return {
|
2022-05-29 18:38:07 +02:00
|
|
|
x = x,
|
|
|
|
y = y,
|
|
|
|
w = w,
|
|
|
|
h = h,
|
|
|
|
text = text,
|
2022-05-29 19:49:59 +02:00
|
|
|
onClick = onClick,
|
|
|
|
onHover = onHover,
|
2022-05-29 18:38:07 +02:00
|
|
|
|
2022-05-29 19:49:59 +02:00
|
|
|
draw=function(self, selected)
|
2022-05-30 22:44:22 +02:00
|
|
|
-- rectfillw(self.x, self.y, self.w, self.h, 8)
|
2022-05-29 19:49:59 +02:00
|
|
|
local color = selected and 7 or 5
|
|
|
|
print(self.text, self.x+1, self.y+1, color)
|
2022-05-29 18:38:07 +02:00
|
|
|
end,
|
|
|
|
|
|
|
|
update=function(self)
|
|
|
|
if mouse_x >= self.x and mouse_x <= self.x + self.w and
|
|
|
|
mouse_y >= self.y and mouse_y <= self.y + self.h then
|
2022-05-29 19:49:59 +02:00
|
|
|
if stat(34)&1 == 0 and state == 2 and self.onClick then
|
|
|
|
self.onClick()
|
2022-05-28 17:32:43 +02:00
|
|
|
end
|
2022-05-29 18:38:07 +02:00
|
|
|
if stat(34)&1 == 1 then
|
|
|
|
state = 2
|
2022-05-28 17:32:43 +02:00
|
|
|
else
|
2022-05-29 19:49:59 +02:00
|
|
|
if onHover then onHover() end
|
2022-05-29 18:38:07 +02:00
|
|
|
state = 1
|
2022-05-28 15:02:55 +02:00
|
|
|
end
|
2022-05-29 18:38:07 +02:00
|
|
|
else
|
|
|
|
state = 0
|
2022-05-28 11:44:09 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2022-05-29 18:38:07 +02:00
|
|
|
function setState(state, ...)
|
|
|
|
local args = {...}
|
|
|
|
if gs and gs._leave then gs._leave() end
|
|
|
|
gs = state
|
|
|
|
if gs and gs._enter then gs._enter(unpack(args)) end
|
|
|
|
end
|
|
|
|
|
2022-05-28 11:44:09 +02:00
|
|
|
--
|
|
|
|
-- main loop
|
|
|
|
--
|
|
|
|
|
2022-05-28 23:58:00 +02:00
|
|
|
local create = true
|
2022-05-28 11:44:09 +02:00
|
|
|
|
|
|
|
function _init()
|
2022-05-28 23:58:00 +02:00
|
|
|
cls()
|
|
|
|
printh(" ")
|
|
|
|
printh(" ")
|
2022-05-29 18:38:07 +02:00
|
|
|
mouse_x = 0
|
|
|
|
mouse_y = 0
|
2022-05-28 23:58:00 +02:00
|
|
|
|
2022-05-30 22:44:22 +02:00
|
|
|
frameCount = 0
|
|
|
|
|
2022-05-29 18:38:07 +02:00
|
|
|
states = {
|
2022-05-29 22:55:43 +02:00
|
|
|
rules = stateRules(),
|
2022-05-29 18:38:07 +02:00
|
|
|
menu = stateMenu(),
|
|
|
|
game = stateGame()
|
|
|
|
}
|
2022-05-28 23:58:00 +02:00
|
|
|
|
2022-05-29 22:55:43 +02:00
|
|
|
setState(states.rules)
|
2022-05-28 23:58:00 +02:00
|
|
|
|
2022-05-28 11:44:09 +02:00
|
|
|
end
|
|
|
|
|
2022-05-28 17:32:43 +02:00
|
|
|
function _update60()
|
2022-05-30 22:44:22 +02:00
|
|
|
frameCount += 1
|
2022-05-29 18:38:07 +02:00
|
|
|
gs._update()
|
2022-05-29 14:35:28 +02:00
|
|
|
-- if not create then
|
|
|
|
-- board:solveStep()
|
|
|
|
-- end
|
2022-05-29 18:38:07 +02:00
|
|
|
|
|
|
|
-- mouse
|
|
|
|
mouse_x = stat(32)
|
|
|
|
mouse_y = stat(33)
|
2022-05-28 11:44:09 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function _draw()
|
2022-05-28 12:34:08 +02:00
|
|
|
cls()
|
2022-05-29 18:38:07 +02:00
|
|
|
gs._draw()
|
2022-05-29 19:49:59 +02:00
|
|
|
spr(1, stat(32), stat(33))
|
2022-05-28 11:44:09 +02:00
|
|
|
end
|