Code formatting

This commit is contained in:
2023-10-07 19:15:47 +02:00
parent bc408aeeb3
commit cb74bdf740
16 changed files with 1066 additions and 1067 deletions
+43 -40
View File
@@ -1,51 +1,54 @@
function state_endgame()
local board
local message = "웃웃 yay! 웃웃"
local message_x = 0
local board
local message = "웃웃 yay! 웃웃"
local message_x = 0
local function go_to_menu()
set_state(states.menu)
end
local function go_to_menu()
set_state(states.menu)
end
local btn_back = make_button({x=1, y=118, h=7, text="🅾️/C bACK TO mENU", color=8,
on_click=function() go_to_menu() end,
on_hover=function(btn) btn.color = 7 end})
local btn_back = make_button({
x = 1, y = 118, h = 7, text = "🅾️/C bACK TO mENU", color = 8,
on_click = function() go_to_menu() end,
on_hover = function(btn) btn.color = 7 end
})
local function _enter(_board)
board = _board
message_x = (128 - print(message,0,-100))/2
end
local function _enter(_board)
board = _board
message_x = (128 - print(message, 0, -100)) / 2
end
local function _update()
btn_back:update()
if btnp(BTN_O) then
go_to_menu()
end
end
local function _update()
btn_back:update()
if btnp(BTN_O) then
go_to_menu()
end
end
local function _draw()
cls()
board:draw()
local function _draw()
cls()
board:draw()
-- wavy message
local offset=0
for i = 1, #message, 1 do -- loop through every letter
letter = sub(message, i, i) -- grab this letter
local st = t() + 0.125 * i -- create a modified time for this letter
print(letter, message_x + offset, 20 + sin(st*0.5)*10, 7) -- draw this letter
offset += print(letter,0,-100)
end
-- wavy message
local offset = 0
for i = 1, #message, 1 do
-- loop through every letter
letter = sub(message, i, i) -- grab this letter
local st = t() + 0.125 * i -- create a modified time for this letter
print(letter, message_x + offset, 20 + sin(st * 0.5) * 10, 7) -- draw this letter
offset += print(letter, 0, -100)
end
btn_back:draw()
end
btn_back:draw()
end
local function _leave()
end
local function _leave()
end
return {
_enter = _enter,
_update = _update,
_draw = _draw,
_leave = _leave
}
return {
_enter = _enter,
_update = _update,
_draw = _draw,
_leave = _leave
}
end
+115 -116
View File
@@ -1,134 +1,133 @@
function state_game()
local board
local selected_id = 1
local timer_clue = 0
local mx, mx = 0, 0
local board
local selected_id = 1
local timer_clue = 0
local mx,mx = 0,0
local clues = {}
local function show_clues()
local issues = board:get_issues(true)
clues = {}
for issue in all(issues) do
local type, err, row, pos, other = unpack(issue)
printh(type .. " " .. err .. " " .. pos)
add(clues, { type = type, pos = pos, t = 0 })
if err == "identical" then
add(clues, { type = type, pos = other, t = 0 })
end
end
end
local clues = {}
local function show_clues()
local issues = board:get_issues(true)
clues = {}
for issue in all(issues) do
local type, err, row, pos, other = unpack(issue)
printh(type .. " " .. err .. " " .. pos)
add(clues, {type=type, pos=pos, t=0})
if err == "identical" then
add(clues, {type=type, pos=other, t=0})
end
end
end
local function draw_selected_tile()
local x, y = board:draw_coords(selected_id)
local w = board:get_tile_width()
-- fillp(▒)
rect2(x - 1, y - 1, w, w, 6)
line()
fillp(█)
end
local function draw_selected_tile()
local x, y = board:draw_coords(selected_id)
local w = board:get_tile_width()
-- fillp(▒)
rect2(x-1, y-1, w, w, 6)
line()
fillp(█)
end
local function update_mouse()
-- update mouse position
if mx == mouse_x and my == mouse_y then return end
mx, my = mouse_x, mouse_y
local board_x, board_y = board:draw_coords(1)
local tw = board:get_tile_width()
local bw = board:get_size()
-- pixels coords to grid coords
local x = mid(1, (mouse_x - board_x) \ tw + 1, bw)
local y = mid(1, (mouse_y - board_y) \ tw + 1, bw)
selected_id = board:xy_idx(x, y)
end
local function update_mouse()
-- update mouse position
if mx == mouse_x and my == mouse_y then return end
mx,my = mouse_x, mouse_y
local board_x, board_y = board:draw_coords(1)
local tw = board:get_tile_width()
local bw = board:get_size()
-- pixels coords to grid coords
local x = mid(1, (mouse_x - board_x) \ tw + 1, bw)
local y = mid(1, (mouse_y - board_y) \ tw + 1, bw)
selected_id = board:xy_idx(x,y)
end
local function check_endgame()
if board:is_complete() and board:is_valid() then
set_state(states.endgame, board)
end
end
local function check_endgame()
if board:is_complete() and board:is_valid() then
set_state(states.endgame, board)
end
end
local function _enter(_board)
-- mouse bound to buttons
poke(0x5F2D, 3)
local function _enter(_board)
-- mouse bound to buttons
poke(0x5F2D, 3)
board = _board
-- lock the initial tiles
board:lock_tiles()
end
board = _board
-- lock the initial tiles
board:lock_tiles()
end
local function _leave()
-- stopcoroutine(show_clues)
end
local function _leave()
-- stopcoroutine(show_clues)
end
local function _update()
update_mouse()
local function _update()
update_mouse()
local size = board:get_size()
local x, y = board:idx_xy(selected_id)
local moved = false
local size = board:get_size()
local x, y = board:idx_xy(selected_id)
local moved = false
if btnp(UP) then
moved = true
y -= 1
elseif btnp(DOWN) then
moved = true
y += 1
elseif btnp(LEFT) then
moved = true
x -= 1
elseif btnp(RIGHT) then
moved = true
x += 1
end
if moved then
selected_id = board:xy_idx(x, y)
end
if btnp(UP) then
moved = true
y -= 1
elseif btnp(DOWN) then
moved = true
y += 1
elseif btnp(LEFT) then
moved = true
x -= 1
elseif btnp(RIGHT) then
moved = true
x += 1
end
if moved then
selected_id = board:xy_idx(x, y)
end
if btnp(BTN_X) then
board:try_flip_tile(selected_id)
show_clues()
end
if btnp(BTN_X) then
board:try_flip_tile(selected_id)
show_clues()
end
if (x < 1) x = size
if (x > size) x = 1
if (y < 1) y = size
if (y > size) y = 1
check_endgame()
end
if (x<1) x=size
if (x>size) x=1
if (y<1) y=size
if (y>size) y=1
local function _draw()
cls()
local x, y = board:draw_coords(1)
draw_animated_bg(x)
check_endgame()
end
board:draw()
draw_selected_tile()
local function _draw()
cls()
local x,y = board:draw_coords(1)
draw_animated_bg(x)
-- draw clues
for clue in all(clues) do
palt(0, false)
palt(5, true)
local x, y
if clue.type == "row" then
x, y = board:draw_coords((clue.pos - 1) * board:get_size() + 1)
x = -32
spr(19, x + clue.t % 144, y + 1 + sin(t()) * 2)
else
-- col
x, y = board:draw_coords(clue.pos)
y = -32
spr(19, x + 2 + sin(t()) * 2, y + clue.t % 144)
end
pset(x, y, 5)
clue.t += 1
end
palt()
end
board:draw()
draw_selected_tile()
-- draw clues
for clue in all(clues) do
palt(0,false)
palt(5,true)
local x,y
if clue.type == "row" then
x,y = board:draw_coords((clue.pos-1) * board:get_size()+1)
x=-32
spr(19, x+clue.t%144, y+1+sin(t())*2)
else -- col
x,y = board:draw_coords(clue.pos)
y=-32
spr(19, x+2+sin(t())*2, y+clue.t%144)
end
pset(x, y, 5)
clue.t += 1
end
palt()
end
return {
_enter = _enter,
_update = _update,
_draw = _draw,
_leave = _leave
}
return {
_enter = _enter,
_update = _update,
_draw = _draw,
_leave = _leave
}
end
+140 -138
View File
@@ -1,158 +1,160 @@
function state_loading()
local board
local size
local og_rt
local removing_tile
local board
local size
local og_rt
local removing_tile
local spinner = split"-,\\,|,/"
local loading_messages = {
"rETICULATING SPLINES",
"aLLOCATING RESSOURCES",
"eMULATING HARDWARE",
"uNCLOGGING MEMORY BUS",
"sPINNING UP ai AGENT",
"sIDE-STEPPING VIRTUAL MACHINE",
"iNSTALLING BACKTRACKING WIZARD",
"eXFOLIATING PIXELS",
"sCAFFOLDING RAY-TRACING ALGORITHM",
"sWEEPING PARTICLES",
"pRESSURIZING USER INTERFACE",
"sHAKING RED AND BLUE PAINT BUCKETS",
"gATHERING GRAVITY",
"sERIALIZING BOARD MATRIX",
"bACKPORTING e2e ENCRYPTION",
}
local spinner = split "-,\\,|,/"
local loading_messages = {
"rETICULATING SPLINES",
"aLLOCATING RESSOURCES",
"eMULATING HARDWARE",
"uNCLOGGING MEMORY BUS",
"sPINNING UP ai AGENT",
"sIDE-STEPPING VIRTUAL MACHINE",
"iNSTALLING BACKTRACKING WIZARD",
"eXFOLIATING PIXELS",
"sCAFFOLDING RAY-TRACING ALGORITHM",
"sWEEPING PARTICLES",
"pRESSURIZING USER INTERFACE",
"sHAKING RED AND BLUE PAINT BUCKETS",
"gATHERING GRAVITY",
"sERIALIZING BOARD MATRIX",
"bACKPORTING e2e ENCRYPTION"
}
local message = ""
local messages_str = ""
local messages_x = 128
local loaded = false
local done = {}
local message = ""
local messages_str = ""
local messages_x = 128
local loaded = false
local done = {}
local create_board = function()
printh("creating")
local start = time()
repeat
board:reset()
repeat
board:solve_step(true)
yield()
until board:is_complete()
until board:is_valid()
local create_board = function()
printh("creating")
local start = time()
repeat
board:reset()
repeat
board:solve_step(true)
yield()
until board:is_complete()
until board:is_valid()
-- Remove tiles that can be removed
local previous = {board:get_tiles_copy()} -- initial state
done = {}
while true do
-- Remove tiles that can be removed
local previous = { board:get_tiles_copy() }
-- initial state
done = {}
while true do
board:set_tiles(previous[#previous])
-- remove a random tile
repeat
removing_tile += 1
if removing_tile > size then
removing_tile = 1
end
until removing_tile == size or board:get_tiles_copy()[removing_tile] ~= 0
if removing_tile == og_rt then
break
end
board:set_tiles(previous[#previous])
-- remove a random tile
repeat
removing_tile += 1
if removing_tile > size then
removing_tile = 1
end
until removing_tile == size or board:get_tiles_copy()[removing_tile] ~= 0
if removing_tile == og_rt then
break
end
board:fill(removing_tile, 0)
add(done, removing_tile)
add(previous, board:get_tiles_copy())
board:fill(removing_tile, 0)
add(done, removing_tile)
add(previous, board:get_tiles_copy())
-- try to solve the board
local solved = ""
yield()
repeat
solved = board:solve_step()
if solved == "valid" then
amplitude -= .125
end
until board:is_complete() or solved == "invalid"
-- try to solve the board
local solved = ""
yield()
repeat
solved = board:solve_step()
if solved == "valid" then
amplitude -= .125
end
until board:is_complete() or solved == "invalid"
if solved == "invalid" then
deli(previous)
end
end
-- end while
printh("board generated in " .. time() - start .. " seconds")
startcoroutine(cocreate(function()
repeat
amplitude -= .5
yield()
until amplitude <= 0
end))
if solved == "invalid" then
deli(previous)
end
end -- end while
printh("board generated in "..time()-start.." seconds")
startcoroutine(cocreate(function()
repeat
amplitude -= .5
yield()
until amplitude <= 0
end))
board:set_tiles(previous[#previous])
printh(board:tostring())
printh(count(board:get_tiles_copy(), 0) .. " zeroes")
board:set_tiles(previous[#previous])
printh(board:tostring())
printh(count(board:get_tiles_copy(), 0).." zeroes")
loaded = true
end
loaded = true
end
local function draw_tiles(board)
local w = board:get_size()
local tiles = board:get_tiles_copy()
for k, v in ipairs(tiles) do
if v == 0 or contains(done, k) then
board:draw_tile(k)
end
end
end
local function draw_tiles(board)
local w = board:get_size()
local tiles = board:get_tiles_copy()
for k,v in ipairs(tiles) do
if v==0 or contains(done, k) then
board:draw_tile(k)
end
end
end
local function _enter()
printh("enter loading")
board = Board:new()
size = board:get_size() * board:get_size()
og_rt = rnd(size) \ 1 + 1
-- original removing tile
removing_tile = og_rt
local function _enter()
printh("enter loading")
board = Board:new()
size = board:get_size()*board:get_size()
og_rt = rnd(size)\1+1 -- original removing tile
removing_tile = og_rt
amplitude = 64
loaded = false
done = {}
amplitude = 64
loaded = false
done = {}
local copy_messages = copy(loading_messages)
messages_str = ""
messages_x = 128
while #copy_messages > 0 do
local m = rnd(copy_messages)
messages_str ..= m .. " "
del(copy_messages, m)
end
messages_str ..= "tHANK YOU FOR YOUR PATIENCE"
local copy_messages = copy(loading_messages)
messages_str = ""
messages_x = 128
while #copy_messages > 0 do
local m = rnd(copy_messages)
messages_str ..= m .. " "
del(copy_messages, m)
end
messages_str ..= "tHANK YOU FOR YOUR PATIENCE"
startcoroutine(cocreate(create_board))
end
startcoroutine(cocreate(create_board))
end
local function _update()
board:lock_tiles()
local function _update()
board:lock_tiles()
messages_x -= 4
if loaded then
set_state(states.game, board)
end
end
messages_x -= 4
if loaded then
set_state(states.game, board)
end
end
local function _draw()
cls()
local x,y = board:draw_coords(1)
draw_animated_bg(x)
-- board:draw_bg()
draw_tiles(board)
local s = board:get_size()*board:get_size()
local l = print(message, 0, -100)
local y = 118+(removing_tile/s)*100/8
local ci = removing_tile\20+1
-- if not loaded then
-- rectfill2(0,y-2,127,10,0)
-- print(messages_str, messages_x, y, 3, 0)
-- -- print(spinner[t()\.25%4+1], 120, 120)
-- end
end
local function _draw()
cls()
local x, y = board:draw_coords(1)
draw_animated_bg(x)
-- board:draw_bg()
draw_tiles(board)
local s = board:get_size() * board:get_size()
local l = print(message, 0, -100)
local y = 118 + removing_tile / s * 100 / 8
local ci = removing_tile \ 20 + 1
-- if not loaded then
-- rectfill2(0,y-2,127,10,0)
-- print(messages_str, messages_x, y, 3, 0)
-- -- print(spinner[t()\.25%4+1], 120, 120)
-- end
end
return {
_enter = _enter,
_update = _update,
_draw = _draw,
}
return {
_enter = _enter,
_update = _update,
_draw = _draw
}
end
+66 -65
View File
@@ -1,78 +1,79 @@
function state_menu()
local selected = 1
local selected = 1
local buttons = {}
-- local buttons = {
-- make_button({x=10, y=10, w=30, text="pLAY", data={i=1},
-- on_click=function() set_state(states.loading) end,
-- on_hover=function(btn) selected=1 end,
-- }),
-- }
local buttons = {}
-- local buttons = {
-- make_button({x=10, y=10, w=30, text="pLAY", data={i=1},
-- on_click=function() set_state(states.loading) end,
-- on_hover=function(btn) selected=1 end,
-- }),
-- }
local game_sizes = {
{"mINI bOARD - 4X4", 4},
{"sMALL bOARD - 6X6", 6},
{"mEDIUM bOARD - 8X8", 8},
{"lARGE bOARD - 10X10", 10}
}
for k, item in ipairs(game_sizes) do
add(buttons,
make_button({
x=10, y=10+k*10,
selected_color=7,
text=item[1],
on_click=function() board_size=item[2] set_state(states.loading) end,
on_hover=function() selected=k end,
})
)
end
local game_sizes = {
{ "mINI bOARD - 4X4", 4 },
{ "sMALL bOARD - 6X6", 6 },
{ "mEDIUM bOARD - 8X8", 8 },
{ "lARGE bOARD - 10X10", 10 }
}
for k, item in ipairs(game_sizes) do
add(
buttons,
make_button({
x = 10, y = 10 + k * 10,
selected_color = 7,
text = item[1],
on_click = function() board_size = item[2] set_state(states.loading) end,
on_hover = function() selected = k end
})
)
end
local rulesId = #buttons + 1
add(
buttons,
make_button({
x = 10, y = 20 + (#game_sizes + 1) * 10, w = 30, text = "rULES",
color = 13, selected_color = 12,
on_click = function() set_state(states.rules) end,
on_hover = function() selected = rulesId end
})
)
local rulesId = #buttons+1
add(buttons,
make_button({x=10, y=20+(#game_sizes+1)*10, w=30, text="rULES",
color=13, selected_color=12,
on_click=function() set_state(states.rules) end,
on_hover=function() selected=rulesId end,
})
)
local function _enter()
-- mouse not bound to buttons
poke(0x5F2D, 1)
end
local function _enter()
-- mouse not bound to buttons
poke(0x5F2D, 1)
end
local function _draw()
cls()
draw_bg_menu()
local function _draw()
cls()
draw_bg_menu()
print("pLAY", 10, 8, 8)
print("pLAY", 10, 8, 8)
for k,button in ipairs(buttons) do
button:draw(selected == k)
end
print("pRESS ❎/X TO CONTINUE", 8, 120, 7)
end
for k, button in ipairs(buttons) do
button:draw(selected == k)
end
print("pRESS ❎/X TO CONTINUE", 8, 120, 7)
end
return {
_enter = _enter,
return {
_enter = _enter,
_update = function()
for button in all(buttons) do
button:update()
end
_update = function()
for button in all(buttons) do
button:update()
end
if btnp(UP) then
selected -= 1
elseif btnp(DOWN) then
selected += 1
elseif btnp(BTN_X) then
buttons[selected]:on_click()
end
selected = mid(1, selected, #buttons)
if btnp(UP) then
selected -= 1
elseif btnp(DOWN) then
selected += 1
elseif btnp(BTN_X) then
buttons[selected]:on_click()
end
selected = mid(1, selected, #buttons)
end,
end,
_draw = _draw
}
_draw = _draw
}
end
+75 -75
View File
@@ -1,94 +1,94 @@
function state_rules()
local fade = split "0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,4,9,10,9,4,2,1"
local color = 1
local fade = split"0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,4,9,10,9,4,2,1"
local color = 1
local function blink(x, y, w, h)
rect2(x, y, w, h, fade[color])
end
local function blink(x,y,w,h)
rect2(x,y,w,h,fade[color])
end
local function go_back()
set_state(states.menu)
end
local function go_back()
set_state(states.menu)
end
local btn_back = make_button({
x = 1, y = 118, w = 30, h = 7, text = "🅾️/C mENU", color = 8,
on_click = function() go_back() end,
on_hover = function(btn) btn.color = 7 end
})
local btn_back = make_button({x=1, y=118, w=30, h=7, text="🅾️/C mENU", color=8,
on_click=function() go_back() end,
on_hover=function(btn) btn.color = 7 end})
return {
_enter = function()
end,
return {
_enter = function()
end,
_exit = function()
-- standard_font()
end,
_update=function()
-- custom_font()
if frame_count%8==0 then
color += 1
end
if color>#fade then
color = 1
end
_exit = function()
-- standard_font()
end,
-- Back to the menu
if btnp(BTN_O) then
go_back()
end
_update = function()
-- custom_font()
if frame_count % 8 == 0 then
color += 1
end
if color > #fade then
color = 1
end
btn_back:update()
end,
-- Back to the menu
if btnp(BTN_O) then
go_back()
end
_draw=function()
cls()
btn_back:update()
end,
print("1) yOU CAN'T HAVE MORE THAN\n TWO (2) CONSECUTIVE TILES\n OF THE SAME COLOR", 2,2, 7)
local x = 14
local y = 28
sspr(0,32, 12,12, x,y)
spr(2, x+14, y+1)
sspr(13,32, 12,12, x+26,y)
blink(x-1, y-1, 9, 3)
blink(x+25, y-1, 9, 3)
blink(x-1, y+8, 12, 3)
blink(x+25, y+8, 12, 3)
_draw = function()
cls()
x = 75
sspr(26,32, 12,12, x,y)
spr(2, x+14, y+1)
sspr(39,32, 12,12, x+26,y)
blink(x+5, y+2, 3, 9)
blink(x+31, y+2, 3, 9)
print("1) yOU CAN'T HAVE MORE THAN\n TWO (2) CONSECUTIVE TILES\n OF THE SAME COLOR", 2, 2, 7)
local x = 14
local y = 28
sspr(0, 32, 12, 12, x, y)
spr(2, x + 14, y + 1)
sspr(13, 32, 12, 12, x + 26, y)
blink(x - 1, y - 1, 9, 3)
blink(x + 25, y - 1, 9, 3)
blink(x - 1, y + 8, 12, 3)
blink(x + 25, y + 8, 12, 3)
x = 75
sspr(26, 32, 12, 12, x, y)
spr(2, x + 14, y + 1)
sspr(39, 32, 12, 12, x + 26, y)
blink(x + 5, y + 2, 3, 9)
blink(x + 31, y + 2, 3, 9)
--------------
--------------
x = 44
y = 50
print("2) eACH LINE CONTAINS AN EQUAL\n NUMBER OF EACH COLOR", 2, y, 7)
sspr(52,32, 12,12, x,y+20)
spr(2, x+14, y+21)
sspr(65,32, 12,12, x+26,y+20)
blink(x+5, y+22, 6, 3)
blink(x+31, y+22, 6, 3)
blink(x-1, y+19, 3, 6)
blink(x+25, y+19, 3, 6)
x = 44
y = 50
print("2) eACH LINE CONTAINS AN EQUAL\n NUMBER OF EACH COLOR", 2, y, 7)
sspr(52, 32, 12, 12, x, y + 20)
spr(2, x + 14, y + 21)
sspr(65, 32, 12, 12, x + 26, y + 20)
blink(x + 5, y + 22, 6, 3)
blink(x + 31, y + 22, 6, 3)
blink(x - 1, y + 19, 3, 6)
blink(x + 25, y + 19, 3, 6)
--------------
--------------
x = 44
y = 90
print("3) aLL ROWS AND COLUMNS\n ARE DIFFERENT", 2, y, 7)
sspr(78,32, 12,12, x,y+20)
spr(2, x+14, y+21)
sspr(91,32, 12,12, x+26,y+20)
blink(x-1, y+25, 12, 6)
blink(x+25, y+25, 12, 6)
x = 44
y = 90
print("3) aLL ROWS AND COLUMNS\n ARE DIFFERENT", 2, y, 7)
sspr(78, 32, 12, 12, x, y + 20)
spr(2, x + 14, y + 21)
sspr(91, 32, 12, 12, x + 26, y + 20)
blink(x - 1, y + 25, 12, 6)
blink(x + 25, y + 25, 12, 6)
--------------
--------------
btn_back:draw()
end,
}
btn_back:draw()
end
}
end