Flood It! inspired by the original classic game

2019

I made a colour board game inspired by the original classic Flood It game.

The game is played on a grid of squares, where each square is coloured. The player can choose a colour and flood the grid with that colour. The goal is to flood the entire grid with a single colour.

Recursive Flood Fill Algorithm

The game uses a recursive flood fill algorithm to flood the grid with the selected colour.

The algorithm works by checking the colour of the current square. If the colour is the same as the selected colour, the algorithm will return. If the colour is different, the algorithm will change the colour of the current square to the selected colour and then recursively call the algorithm for the squares above, below, left and right of the current square.

Code used for the recursive flood fill algorithm:

// Flood function based on recursive flood fill algorithm
function flood(cell_row, cell_col, target_colour, replacement_colour) {
  /* This is a recursive flood fill 
    algorithm */
  if (
    cell_row >= table.grid_size ||
    cell_col >= table.grid_size ||
    cell_row < 0 ||
    cell_col < 0
  ) {
    return
  } else if (target_colour == replacement_colour) {
    return
  } else if (table.data[cell_row][cell_col].colour != target_colour) {
    return
  } else {
    table.data[cell_row][cell_col].colour = replacement_colour
  }
  flood(cell_row + 1, cell_col, target_colour, replacement_colour)
  flood(cell_row, cell_col + 1, target_colour, replacement_colour)
  flood(cell_row - 1, cell_col, target_colour, replacement_colour)
  flood(cell_row, cell_col - 1, target_colour, replacement_colour)
}