pico-8 cartridge // http://www.pico-8.com version 43 __lua__ -- power*grid -- by m4ttbit -- initialize global variables player_grid = {} target = {} row_hints = {} col_hints = {} grid_size = 8 cx = 1 -- cursor x position (1-based) cy = 1 -- cursor y position (1-based) message = "" msg_timer = 0 powers = 3 start = true help = false puzzles_completed = 0 correct_cells = {} start_anim_timer = 0 game_over_sound_played = false -- screen width and height local screen_width = 128 local screen_height = 128 -- calculate horizontal and vertical centers local center_x = screen_width / 2 local center_y = screen_height / 2 -- calculate grid dimensions local grid_width = grid_size * 8 local grid_height = grid_size * 8 -- calculate grid offsets to center it local grid_offset_x = center_x - grid_width / 2 local grid_offset_y = center_y - grid_height / 2 -- calculate clue offsets local 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 end end 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' end end -- helper to copy a table function cpy(t) local t2 = {} for k,v in pairs(t) do t2[k] = v end return t2 end -- helper to get all possible starting positions for hints in a line function 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 possibilities end -- solver to check for unique solution function 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 true end -- initialize start of game function init_game() local unique = false while not unique do for i = 1, grid_size * grid_size do target[i] = flr(rnd(2)) player_grid[i] = 0 end calculate_hints() place_random_xs() unique = has_unique_solution() end correct_cells = {} message = "" msg_timer = 0 game_over_sound_played = false end function center_text(text) return 64 - #text * 2 end 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 end end function help_update() if btnp(4) then help=false start=true end end -- main game loop function _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 end end -- main draw function function _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 end end __gfx__ 000000000000aa000000770000005500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000aa0000007700000055000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000aa00000077000000550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000aaaaaa00777777005555550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000aaaaaa00777777005555550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000aa000000770000005500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000aa0000007700000055000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000aa00000077000000550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000544044500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 06666660000400050000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 07077070054445000066600000066600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 07777770500400500666660000004600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 07077070000400056777776000040660000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0777777000040000077707b000400660000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 bb7b0b7b00040000b7077bbb04000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 __sfx__ 000200000955000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000800001115017152201500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 001200001f150141500a1500215000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000a00001c250232501c2502f25030200222502825028250000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000