This is what I have come up with in C#:
namespace ConsoleApplication8
{
class FloodFill
{
char[,] map;
List<CaveCell> CaveCells = new List<CaveCell>();
public FloodFill(char[,] map)
{
this.map = map;
}
public List<CaveCell> GetCaveCells()
{
return CaveCells;
}
public void fill(int x, int y, char newTile, char oldTile)
{
if (x < 0) return;
if (y < 0) return;
if (x >= map.GetLength(1)) return;
if (y >= map.GetLength(0)) return;
char currentTile = map[x,y]; // get current tile
if (oldTile != currentTile) return; //
if (newTile == currentTile) return; // same
// map[x,y] = newTile; // store x,y position
CaveCell cell = new CaveCell(x, y);
CaveCells.Add(cell);
fill(x - 1, y, newTile, oldTile);
fill(x + 1, y, newTile, oldTile);
fill(x, y - 1, newTile, oldTile);
fill(x, y + 1, newTile, oldTile);
}
}
class CaveCell
{
public CaveCell(int x, int y)
{
this.x = x;
this.y = y;
}
public int x { get; set; }
public int y { get; set; }
public override string ToString()
{
return String.Format("[{0},{1}]",x,y);
}
}
class Cave
{
public Cave() {
Cells = new List<CaveCell>();
}
public List<CaveCell> Cells { get; set; }
}
}
class Program
{
static List<Cave> Caves = new List<Cave>();
static void Main(string[] args)
{
char[,] map = {
{ 'O', 'O', 'O', 'O', 'X','O','O','X' },
{ 'X', 'O', 'O', 'O', 'X','O','O','X' },
{ 'X', 'O', 'O', 'O', 'X','O','O','X' },
{ 'X', 'O', 'X', 'O', 'X','X','O','X' },
{ 'X', 'X', 'X', 'X', 'X','X','O','X' },
{ 'X', 'X', 'X', 'X', 'X','X','O','X' },
{ 'X', 'X', 'X', 'X', 'X','X','X','X' }
};
FloodFill floodFill = new FloodFill(map);
floodFill.fill(0, 0, '~', 'O');
// floodFill.fill(0, 6, '~', 'O');
for (int y = 0; y < map.GetLength(0); y++, Console.WriteLine())
for (int x = 0; x < map.GetLength(1); x++)
Console.Write(map[y, x]);
List<CaveCell> CaveCells = floodFill.GetCaveCells();
Cave Cave = new Cave();
foreach (CaveCell cell in CaveCells)
{
Cave.Cells.Add(cell);
}
Caves.Add(Cave);
foreach (Cave cave in Caves)
{
foreach (CaveCell cell in cave.Cells)
{
Console.WriteLine(cell);
}
}
}
}
So the Caves list holds list of Caves, where each Cave has a list of it’s cells, this look ok?
Thanks