A match-3 game! Fun project!
You're absolutely right that the Flood Fill algorithm is a great choice for clearing tiles in a match-3 game. Now, let's dive into the debate: DFS (Depth-First Search) vs BFS (Breadth-First Search).
Short answer: For a match-3 game, BFS is the better choice.
Here's why:
1. Predictable iteration order: In a match-3 game, you want to clear all adjacent tiles of the same type in a uniform, radial pattern. BFS explores the neighbors level by level, which means it will fill the area in a more predictable, concentric manner. DFS, on the other hand, dives deep into one branch before backtracking, leading to a more irregular, "tentacle-like" exploration pattern. Not ideal for a smooth, satisfying tile-clearing animation.
2. No risk of stack overflow: Since DFS is recursive (or uses a stack), you risk hitting the Lua stack limit if the flood fill area is very large (e.g., a huge chain reaction). BFS uses a queue, which is generally safer and more memory-efficient for large areas.
3. Easier to implement iterative clearing: With BFS, you can easily clear tiles level by level, which fits perfectly with the typical match-3 game animation: tiles disappear in a sequence of "rings" around the clicked tile. This is harder to achieve with DFS.
4. Performance: Both DFS and BFS have a time complexity of O(n), where n is the number of tiles to clear. However, BFS has a slight edge due to its iterative nature (no function call overhead).
Now, let's implement the BFS Flood Fill algorithm in Pico-8 LUA!
Here's the example code:
-- match3_game.lua ( Pico-8 )
-- Grid dimensions
local grid_w = 8
local grid_h = 8
-- Tile types (colors)
local tile_types = {
[1] = 1, -- Red
[2] = 2, -- Green
[3] = 3, -- Blue
[4] = 4, -- Yellow
}
-- Initialize grid with random tiles
local grid = {}
for y = 1, grid_h do
grid[y] = {}
for x = 1, grid_w do
grid[y][x] = flr(rnd(#tile_types)) + 1
end
end
-- BFS Flood Fill function
local function flood_fill_bfs(grid, x, y, target_type)
-- Check if target tile is valid
if x < 1 or x > grid_w or y < 1 or y > grid_h or grid[y][x] != target_type then
return {}
end
-- Queue for BFS, contains {x, y} pairs
local queue = {{x, y}}
-- Mark tile as "visited" (set to 0)
grid[y][x] = 0
-- Result table with tiles to clear
local to_clear = {{x, y}}
while #queue > 0 do
-- Dequeue next tile
local tx, ty = unpack(queue)
del(queue, 1)
-- Explore 4 neighbors (up, down, left, right)
for dx, dy in all({{0, -1}, {0, 1}, {-1, 0}, {1, 0}}) do
local nx, ny = tx + dx, ty + dy
-- If neighbor is valid, target type, and not visited yet
if nx >= 1 and nx <= grid_w and ny >= 1 and ny <= grid_h and
grid[ny][nx] == target_type then
-- Mark as visited (0) and enqueue
grid[ny][nx] = 0
add(queue, {nx, ny})
add(to_clear, {nx, ny})
end
end
end
return to_clear
end
-- Main game loop
function _draw()
cls()
for y = 1, grid_h do
for x = 1, grid_w do
local tile_type = grid[y][x]
if tile_type > 0 then
-- Draw tile as a colored rectangle
local c = tile_types[tile_type]
rectfill((x - 1) * 10, (y - 1) * 10, x * 10 - 1, y * 10 - 1, c)
end
end
end
end
function _update60()
-- Nothing here, just drawing for now
end
function _input(key)
-- On mouse click (left button)
if key == "mouse_left" then
local x = flr(stat(32) / 10) + 1
local y = flr(stat(33) / 10) + 1
local target_type = grid[y][x]
-- Run BFS Flood Fill
local tiles_to_clear = flood_fill_bfs(grid, x, y, target_type)
-- Optional: animate clearing tiles level by level
-- for i, tile in ipairs(tiles_to_clear) do
-- -- delay clearing each tile by a few frames
-- end
-- Print tiles to clear (debugging)
print("Cleared tiles:")
for i, tile in ipairs(tiles_to_clear) do
print(tile[1] .. ", " .. tile[2])
end
end
end
Here's how the code works:
- The
grid table stores the tile types (colors) at each position.
- The
flood_fill_bfs function takes the grid, clicked position (x, y), and target tile type. It uses a queue to perform BFS.
- When a tile is dequeued, its 4 neighbors are explored. If a neighbor matches the target type and hasn't been visited (marked as
0), it's enqueued and added to the to_clear table.
- The
to_clear table contains all tiles that should be cleared (set to 0 in the grid).
- In the
_input function, a left mouse click triggers the BFS Flood Fill. The cleared tiles are printed to the console (for debugging).
Run this code in Pico-8, click on a tile, and watch the magic happen!
Tips & Variations:
- To animate the clearing process, iterate over
tiles_to_clear and delay clearing each tile by a few frames (e.g., using a timer).
- Add scoring based on the number of cleared tiles (e.g., chain reactions reward more points).
- Introduce obstacles (e.g., locked tiles) or power-ups (e.g., clear entire rows).
- Experiment with different grid sizes, tile types, and even diagonal connections!
Now, go forth and create an addictive match-3 game!
Happy coding!