Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618pico-8 cartridge // http://www.pico-8.comversion 43__lua__-- power*grid-- by m4ttbit
-- initialize global variablesplayer_grid = {}target = {}row_hints = {}col_hints = {}grid_size = 8cx = 1 -- cursor x position (1-based)cy = 1 -- cursor y position (1-based)message = ""msg_timer = 0powers = 3start = truehelp = falsepuzzles_completed = 0correct_cells = {}start_anim_timer = 0game_over_sound_played = false
-- screen width and heightlocal screen_width = 128local screen_height = 128
-- calculate horizontal and vertical centerslocal center_x = screen_width / 2local center_y = screen_height / 2
-- calculate grid dimensionslocal grid_width = grid_size * 8local grid_height = grid_size * 8
-- calculate grid offsets to center itlocal grid_offset_x = center_x - grid_width / 2local grid_offset_y = center_y - grid_height / 2
-- calculate clue offsetslocal clue_offset = 8
function calculate_hints() row_hints = {} col_hints = {}
-- rows for r = 1, grid_size do row_hints[r] = {} local count = 0 for c = 1, grid_size do local index = (r - 1) * grid_size + c if target[index] == 1 then count = count + 1 else if count > 0 then add(row_hints[r], count) end count = 0 end end if count > 0 then add(row_hints[r], count) end end
-- columns for c = 1, grid_size do col_hints[c] = {} local count = 0 for r = 1, grid_size do local index = (r - 1) * grid_size + c if target[index] == 1 then count = count + 1 else if count > 0 then add(col_hints[c], count) end count = 0 end end if count > 0 then add(col_hints[c], count) end endend
function place_random_xs() local empty_cells = {} for i = 1, grid_size * grid_size do if target[i] == 0 then add(empty_cells, i) end end
-- shuffle the list of empty cells for i = #empty_cells, 2, -1 do local j = flr(rnd(i)) + 1 empty_cells[i], empty_cells[j] = empty_cells[j], empty_cells[i] end
-- mark at least half of them with 'X' local num_to_mark = flr(#empty_cells / 3) for i = 1, num_to_mark do player_grid[empty_cells[i]] = 4 -- 4 is for locked 'X' endend
-- helper to copy a tablefunction cpy(t) local t2 = {} for k,v in pairs(t) do t2[k] = v end return t2end
-- helper to get all possible starting positions for hints in a linefunction get_line_possibilities(line, hints) local n = #line local m = #hints if m == 0 then return {{}} end
local total_hint_len = 0 for h in all(hints) do total_hint_len += h end local total_len = total_hint_len + m - 1
local possibilities = {} local current_pos = {}
function find_perms(hint_idx, start_pos) if hint_idx > m then add(possibilities, cpy(current_pos)) return end
local hint = hints[hint_idx] local remaining_len = 0 for i = hint_idx + 1, m do remaining_len += hints[i] + 1 end
for p = start_pos, n - remaining_len - hint + 1 do -- check if this placement is valid against known cells local possible = true -- check for 'x' in the block for i = 0, hint - 1 do if line[p + i] == 3 or line[p+i] == 4 then possible = false break end end if not possible then goto next_p end
-- check for filled cells before this block if hint_idx == 1 then for i=1,p-1 do if line[i] == 1 or line[i] == 2 then possible = false break end end else for i=current_pos[hint_idx-1] + hints[hint_idx-1] + 1, p-1 do if line[i] == 1 or line[i] == 2 then possible = false break end end end if not possible then goto next_p end
-- check for filled cells after last block if hint_idx == m then for i=p+hint,n do if line[i] == 1 or line[i] == 2 then possible = false break end end end if not possible then goto next_p end
current_pos[hint_idx] = p find_perms(hint_idx + 1, p + hint + 1)
::next_p:: end end
find_perms(1, 1) return possibilitiesend
-- solver to check for unique solutionfunction has_unique_solution() local temp_grid = {} for i=1, #player_grid do temp_grid[i] = player_grid[i] end
local changed = true while changed do changed = false
-- row logic for r = 1, grid_size do local line = {} for c=1,grid_size do add(line, temp_grid[(r-1)*grid_size+c]) end local possibilities = get_line_possibilities(line, row_hints[r])
if #possibilities == 0 then return false end -- no solution
if #possibilities > 0 then local intersect_fill = {} local intersect_x = {} for i=1,grid_size do intersect_fill[i] = true; intersect_x[i] = true end
for p_idx, p in ipairs(possibilities) do local line_fill = {} for i=1, #row_hints[r] do local start = p[i] local len = row_hints[r][i] for j=start, start+len-1 do line_fill[j] = true end end for i=1,grid_size do if not line_fill[i] then intersect_fill[i] = false end if line_fill[i] then intersect_x[i] = false end end end
for c=1,grid_size do local index = (r-1)*grid_size+c if temp_grid[index] == 0 then if intersect_fill[c] then temp_grid[index] = 1; changed=true end if intersect_x[c] then temp_grid[index] = 3; changed=true end end end end end
-- column logic for c = 1, grid_size do local line = {} for r=1,grid_size do add(line, temp_grid[(r-1)*grid_size+c]) end local possibilities = get_line_possibilities(line, col_hints[c])
if #possibilities == 0 then return false end -- no solution
if #possibilities > 0 then local intersect_fill = {} for i=1,grid_size do intersect_fill[i] = true end
for r=1,grid_size do local index = (r-1)*grid_size+c if temp_grid[index] == 0 then local all_fill = true for _,p in ipairs(possibilities) do local is_in_block = false for i=1,#col_hints[c] do if r >= p[i] and r < p[i]+col_hints[c][i] then is_in_block=true break end end if not is_in_block then all_fill=false break end end if all_fill then temp_grid[index] = 1; changed=true end end end end end end
-- check if solved for i=1, #temp_grid do if temp_grid[i] == 0 then return false -- not fully solved end end
return trueend
-- initialize start of gamefunction init_game() local unique = false while not unique do if puzzles_completed == 2 then local pat = "01001101001101000101010001010100010000100100100101010100" for i = 1, grid_size * grid_size do if i <= #pat then target[i] = tonum(sub(pat, i, i)) else target[i] = 0 end player_grid[i] = 0 end else for i = 1, grid_size * grid_size do target[i] = flr(rnd(2)) player_grid[i] = 0 end end calculate_hints() place_random_xs() if puzzles_completed == 2 then unique = true else unique = has_unique_solution() end end correct_cells = {} message = "" msg_timer = 0 game_over_sound_played = falseend
function center_text(text) return 64 - #text * 2end
function start_render() local y_start = 21 local title_part1 = "power" local title_part2 = "grid" local sprite_width = 8 local total_width = (#title_part1 * 4) + sprite_width + (#title_part2 * 4) local start_x = (128 - total_width) / 2 local flicker_sprite = flr(start_anim_timer / 4) % 2 == 0 and 1 or 3 print(title_part1, start_x, y_start, 7) spr(flicker_sprite, start_x + #title_part1 * 4, y_start - 1) print(title_part2, start_x + #title_part1 * 4 + sprite_width, y_start, 7) -- center and draw the row of 6 sprites local sprite_sequence = {16, 17, 18, 17, 16, 17, 18} local start_x = (128 - #sprite_sequence * 8) / 2 for i=1, #sprite_sequence do spr(sprite_sequence[i], start_x + (i-1)*8, y_start + 18) end press_x='❎ start' print(press_x, 32, y_start + 42, 7) press_z='🅾️/z how to play' print(press_z, 32, y_start + 52, 7) made='game by m4ttbit' print(made, center_text(made), y_start + 80, 7)end
function help_render() local y_start = 12 title='-- how to play --' print(title, center_text(title), y_start, 7) press_back='🅾️/z exit' print(press_back, center_text(press_back), y_start + 94, 7) spr(19, 8, y_start + 12, 1, 1) story_one="use hammer to build" print(story_one, 30, y_start + 14, 7) clue_num = "213" print(clue_num, 8, y_start + 26, 7) story_two="buildings in a row" print(story_two, 30, y_start + 26, 7) z_toggle="🅾️/z" print(z_toggle, 8, y_start + 38, 7) story_three="build, x, or clear" print(story_three,30,y_start + 38,7) x_check="❎" print(x_check, 8, y_start + 50, 7) story_four="check grid accuracy" print(story_four,30,y_start + 50,7) spr(1,8,y_start + 60,1,1) story_five="energy remaining" print(story_five,30,y_start + 62,7) story_six="movement controls: ⬆️⬇️⬅️➡️ " print(story_six,center_text(story_six),y_start + 79,7)end
function start_update() start_anim_timer += 1 if btn(5) then start=false init_game() end if btnp(4) then start=false help=true endend
function help_update() if btnp(4) then help=false start=true endend
-- main game loopfunction _update() if start then start_update() elseif help then help_update() else if msg_timer > 0 then msg_timer -= 1 if msg_timer == 0 then if message == "game over" then -- game over timeout finished, return to start start = true powers = 3 puzzles_completed = 0 start_anim_timer = 0 message = "" elseif message == "grid online!" then puzzles_completed += 1 init_game() elseif message == "grid offline" and powers <= 0 then msg_timer = 120 -- set timer for game over screen sfx(2) game_over_sound_played = true message = "game over" else correct_cells = {} message = "" end end return end
-- handle hammer placement when cursor moves local old_cx, old_cy = cx, cy
-- direction buttons if btnp(0) then cx = max(1, cx - 1) end -- left if btnp(1) then cx = min(grid_size, cx + 1) end -- right if btnp(2) then cy = max(1, cy - 1) end -- up if btnp(3) then cy = min(grid_size, cy + 1) end -- down
if cx ~= old_cx or cy ~= old_cy then local old_index = (old_cy - 1) * grid_size + old_cx if player_grid[old_index] == 5 then -- if the old cell was a hammer if rnd(1) < 0.5 then player_grid[old_index] = 1 -- place a house else player_grid[old_index] = 2 -- place a building end end end
-- toggle cell ('z' - button 4) local current_index = (cy - 1) * grid_size + cx if btnp(4) then local current_val = player_grid[current_index] if current_val ~= 4 then -- check if not a locked 'X' if current_val == 0 then player_grid[current_index] = 5 -- empty -> hammer elseif current_val == 5 then player_grid[current_index] = 3 -- hammer -> x else player_grid[current_index] = 0 -- x, house, or building -> empty end correct_cells[current_index] = nil sfx(0) end end
-- check solution ('x' - button 5) if btnp(5) then local win = true correct_cells = {} -- clear previous correct cell highlights for i = 1, grid_size * grid_size do if (player_grid[i] == 1 or player_grid[i] == 2) ~= (target[i] == 1) then win = false else if player_grid[i] == 1 or player_grid[i] == 2 then correct_cells[i] = true -- mark correct cells end end end
-- play sound based on win/loss if win then sfx(3) -- win sound else sfx(1) -- lose sound end message = win and "grid online!" or "grid offline" msg_timer = 90 if (win == false) then powers -= 1 end end endend
-- main draw functionfunction _draw() cls() if start then start_render() elseif help then help_render() -- check for game over state elseif message == "game over" then local text1 = "grid overloaded" local text2 = "game over" -- calculate vertical center for 3 lines local text_y_start = screen_height / 2 - (3 * 6 / 2) print(text1, center_text(text1), text_y_start, 8) print(text2, center_text(text2), text_y_start + 12, 8) return -- stop drawing the rest of the game else
-- draw grid (centered vertically and horizontally) for y = 1, grid_size do for x = 1, grid_size do local index = (y - 1) * grid_size + x local cell_x = grid_offset_x + (x - 1) * 8 local cell_y = grid_offset_y + (y - 1) * 8 rectfill(cell_x, cell_y, cell_x + 7, cell_y + 7, 5) -- draw default background
if correct_cells[index] then spr(1, cell_x, cell_y) end
if msg_timer == 0 then if player_grid[index] == 1 then -- draw sprite 16 spr(16, cell_x, cell_y) elseif player_grid[index] == 2 then -- draw sprite 18 spr(18, cell_x, cell_y) elseif player_grid[index] == 5 then -- hammer spr(19, cell_x, cell_y) end end
if player_grid[index] == 3 or player_grid[index] == 4 then -- 'X' or locked 'X' line(cell_x + 1, cell_y + 1, cell_x + 6, cell_y + 6, 7) line(cell_x + 1, cell_y + 6, cell_x + 6, cell_y + 1, 7) end
-- cursor border if x == cx and y == cy then rect(cell_x, cell_y, cell_x + 7, cell_y + 7, 8) end end end
-- draw row clues (aligned with centered grid) for r = 1, grid_size do local hints = row_hints[r] local x_cursor = grid_offset_x - clue_offset if #hints == 0 then print("0", x_cursor - 1, grid_offset_y + (r - 1) * 8 + 1, 7) else for i = #hints, 1, -1 do local s = ""..hints[i] local w = #s * 4 - 1 print(s, x_cursor - w, grid_offset_y + (r - 1) * 8 + 1, 7) x_cursor = x_cursor - w - 4 end end end
-- draw column clues (aligned with centered grid) for c = 1, grid_size do local hints = col_hints[c] local y_cursor = grid_offset_y - clue_offset + 2 if #hints == 0 then print("0", grid_offset_x + (c - 1) * 8 + 2, y_cursor - 5, 7) else for i = #hints, 1, -1 do local s = ""..hints[i] local x = grid_offset_x + (c - 1) * 8 + 2 if #s > 1 then x = x - 2 end print(s, x, y_cursor - 5, 7) y_cursor = y_cursor - 6 end end end
-- draw puzzles completed info local text_x_center = (grid_offset_x + grid_width + screen_width) / 2 local text_y_start = screen_height / 2 - (4 * 6 / 2) -- center 4 lines vertically -- center and print each line print("grids", text_x_center - (#"grids" * 2), text_y_start, 11) print("online", text_x_center - (#"online" * 2), text_y_start + 6, 11) print(puzzles_completed, text_x_center - (#(""..puzzles_completed) * 2), text_y_start + 18, 11)
-- draw message (centered horizontally and vertically below grid) if message != "" then print(message, center_text(message), 102, 10) end
-- draw remaining power sprites for i=1, powers do spr(1, 52 + (i-1)*8, 112) end
-- if incorrect, flicker the lost power sprite if powers >= 0 then if message == "grid offline" and msg_timer > 0 then local flicker_sprite = flr(msg_timer/4)%2 == 0 and 3 or 1 local flicker_x = 52 + powers * 8 spr(flicker_sprite, flicker_x, 112) end end endend__gfx__000000000000aa00000077000000550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000aa00000077000000550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000aa00000077000000550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000aaaaaa00777777005555550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000aaaaaa00777777005555550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000aa00000077000000550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000aa00000077000000550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000aa0000007700000055000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000544044500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000066666600004000500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000707707005444500006660000006660000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007777770500400500666660000004600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000070770700004000567777760000406600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000777777000040000077707b000400660000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000bb7b0b7b00040000b7077bbb0400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000__sfx__000200000955000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800001115017152201500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001200001f150141500a1500215000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a00001c250232501c2502f25030200222502825028250000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000