check_surroundings - 1Fr3aK2/Cub3d GitHub Wiki
📝 check_surroundings
This function checks the surroundings of a map to ensure that all open spaces and player positions are properly enclosed using a flood-fill algorithm.
⚙️ Parameters
| Parameter | Type | Description |
|---|---|---|
map |
t_map* |
A pointer to the map structure containing the map data and temporary flags. |
🔁 Returns
| Return value | Description |
|---|---|
bool |
Returns true if all surroundings are valid (properly enclosed), or false if an error or invalid area is detected. |
📖 Description
The check_surroundings function iterates through each cell of the map to verify that all empty spaces ('0') and player positions are enclosed and accessible. The function uses a flood-fill algorithm implemented in flood_fill_map to mark visited positions in map->temp_map.
- If the input
mapisNULL, the function immediately returnsfalse. - For each cell in the map, it checks if the cell is an empty space ('0') or a valid player position and whether it has already been visited.
- If a cell meets these conditions, the function calls
flood_fill_mapto recursively mark the connected area. Ifflood_fill_mapfails, an error message is printed and the function returnsfalse. - The function returns
trueif all areas are valid and properly enclosed.
💡 Example Usage
t_map map = initialize_map(); // Assume this function sets up the map structure
if (check_surroundings(&map)) {
printf("All map surroundings are valid!\n");
} else {
printf("Invalid map surroundings detected.\n");
}