I want to make a match-3 game that uses clicks to clear the tiles. The algorithm suits best for clearing tiles is Flood fill algorithm. But there are 2 choices: DFS and BFS. Which one is the best? After you choose, make an example of it in Pico 8 LUA
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Both Depth-First Search (DFS) and Breadth-First Search (BFS) can be used to implement the Flood Fill algorithm. The choice between them depends on the specific requirements of your match-3 game.
DFS vs BFS
DFS:
Uses a stack (or recursion) to explore the grid.
Can be more memory-efficient for very large grids with many isolated regions, as it only stores the current path.
May cause a stack overflow for very large connected regions if implemented recursively.
BFS:
Uses a queue to explore the grid.
More suitable for match-3 games, as it explores the grid level by level, which can be more intuitive for tile clearing animations.
Generally more predictable and less prone to stack overflows.
For a match-3 game, BFS is a better choice because it explores the grid in a more predictable, level-by-level manner, making it easier to animate the tile clearing process.
Example Implementation in Pico-8 LUA using BFS
Here's an example implementation of the Flood Fill algorithm using BFS in Pico-8 LUA:
lua
function flood_fill_bfs(grid, x, y, target_color)
-- Check if the target position is within the grid and has the target color
if x < 1 or x > #grid[1] or y < 1 or y > #grid or grid[y][x] ~= target_color then
return
end
-- Create a queue for BFS
local queue = {{x, y}}
-- Mark the target position as visited (by setting it to a different color)
grid[y][x] = -1
-- Define the possible directions (up, down, left, right)
local directions = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}}
-- Perform BFS
while #queue > 0 do
local curr_x, curr_y = queue[1][1], queue[1][2]
table.remove(queue, 1)
-- Explore neighbors
for _, dir in pairs(directions) do
local new_x, new_y = curr_x + dir[1], curr_y + dir[2]
-- Check if the neighbor is within the grid and has the target color
if new_x >= 1 and new_x <= #grid[1] and new_y >= 1 and new_y <= #grid and grid[new_y][new_x] == target_color then
-- Mark the neighbor as visited
grid[new_y][new_x] = -1
table.insert(queue, {new_x, new_y})
end
end
end
-- Clear the visited tiles (marked as -1)
for y = 1, #grid do
for x = 1, #grid[y] do
if grid[y][x] == -1 then
grid[y][x] = 0 -- 0 represents an empty tile
end
end
end
end
-- Example usage:
function _init()
grid = {
{1, 1, 1, 2, 2},
{1, 1, 1, 2, 2},
{1, 1, 1, 1, 2},
{2, 2, 1, 1, 1},
{2, 2, 2, 1, 1}
}
end
function _draw()
cls()
for y = 1, #grid do
for x = 1, #grid[y] do
local color = grid[y][x]
if color == 1 then
rectfill((x - 1) * 10, (y - 1) * 10, x * 10 - 1, y * 10 - 1, 8)
elseif color == 2 then
rectfill((x - 1) * 10, (y - 1) * 10, x * 10 - 1, y * 10 - 1, 12)
end
end
end
end
function _update60()
if btnp(❎) then
local x = flr(rnd(#grid[1])) + 1
local y = flr(rnd(#grid)) + 1
local target_color = grid[y][x]
flood_fill_bfs(grid, x, y, target_color)
end
end
In this example, pressing the ❎ button will randomly select a tile and clear all connected tiles of the same color using the BFS-based Flood Fill algorithm. The grid is displayed on the screen, with different colors representing different tile types.
User
Where is the cursor to select the tile to remove? Pico 8 doesn't have clicks (except devkit mouse mode but we aren't using that)
Assistant
Pico-8 does not support mouse clicks directly. To select a tile, we can use the D-pad to move a cursor around the grid and press a button to select the tile.
Here's an updated version of the code that includes a cursor:
lua
function flood_fill_bfs(grid, x, y, target_color)
-- Check if the target position is within the grid and has the target color
if x < 1 or x > #grid[1] or y < 1 or y > #grid or grid[y][x] ~= target_color then
return
end
-- Create a queue for BFS
local queue = {{x, y}}
-- Mark the target position as visited (by setting it to a different color)
grid[y][x] = -1
-- Define the possible directions (up, down, left, right)
local directions = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}}
-- Perform BFS
while #queue > 0 do
local curr_x, curr_y = queue[1][1], queue[1][2]
table.remove(queue, 1)
-- Explore neighbors
for _, dir in pairs(directions) do
local new_x, new_y = curr_x + dir[1], curr_y + dir[2]
-- Check if the neighbor is within the grid and has the target color
if new_x >= 1 and new_x <= #grid[1] and new_y >= 1 and new_y <= #grid and grid[new_y][new_x] == target_color then
-- Mark the neighbor as visited
grid[new_y][new_x] = -1
table.insert(queue, {new_x, new_y})
end
end
end
-- Clear the visited tiles (marked as -1)
for y = 1, #grid do
for x = 1, #grid[y] do
if grid[y][x] == -1 then
grid[y][x] = 0 -- 0 represents an empty tile
end
end
end
end
-- Example usage:
function _init()
grid = {
{1, 1, 1, 2, 2},
{1, 1, 1, 2, 2},
{1, 1, 1, 1, 2},
{2, 2, 1, 1, 1},
{2, 2, 2, 1, 1}
}
cursor_x = 1
cursor_y = 1
end
function _draw()
cls()
for y = 1, #grid do
for x = 1, #grid[y] do
local color = grid[y][x]
if x == cursor_x and y == cursor_y then
rect((x - 1) * 10, (y - 1) * 10, x * 10 - 1, y * 10 - 1, 7)
end
if color == 1 then
rectfill((x - 1) * 10 + 1, (y - 1) * 10 + 1, x * 10 - 2, y * 10 - 2, 8)
elseif color == 2 then
rectfill((x - 1) * 10 + 1, (y - 1) * 10 + 1, x * 10 - 2, y * 10 - 2, 12)
end
end
end
end
function _update60()
if btnp(➡️) then
cursor_x = min(cursor_x + 1, #grid[1])
elseif btnp(⬅️) then
cursor_x = max(cursor_x - 1, 1)
elseif btnp(⬇️) then
cursor_y = min(cursor_y + 1, #grid)
elseif btnp(⬆️) then
cursor_y = max(cursor_y - 1, 1)
elseif btnp(❎) then
local target_color = grid[cursor_y][cursor_x]
if target_color ~= 0 then
flood_fill_bfs(grid, cursor_x, cursor_y, target_color)
end
end
end
In this updated version, a cursor is displayed on the grid, and you can move it around using the D-pad. Pressing the ❎ button will clear the tiles connected to the selected tile.