public static int GetCount(this Grid board, CellPredicate predicate)
 {
     return(Engine.Fold(0, (acc, cell, brd) => {
         if (predicate(cell, brd))
         {
             return acc + 1;
         }
         else
         {
             return acc;
         }
     }, board));
 }
Beispiel #2
0
        public void ProcessCellFill(int x, int y, int z, CellCallback action, CellPredicate predicate)
        {
            if (!IsValidCell(x, y, z))
            {
                return;
            }

            bool[,,] closed = new bool[CellsPerXAxis, CellsPerYAxis, CellsPerZAxis];

            var baseCell = GetCastInfo(x, y, z);
            var open     = new Queue <CellCastInfo>();

            open.Enqueue(baseCell);
            closed[x, y, z] = true;

            CellCastInfo cell = baseCell;

            if (!predicate(cell.X, cell.Y, cell.Z, cell.Cell))
            {
                return;
            }

            action(cell.X, cell.Y, cell.Z, cell.Cell);

            while (open.Count > 0)
            {
                CellCastInfo next = open.Dequeue();

                int  west, east;
                bool match;

                // Find west
                x     = next.X;
                y     = next.Y;
                z     = next.Z;
                match = true;

                while (match)
                {
                    ++x;
                    if (IsValidCell(x, y, z))
                    {
                        cell  = GetCastInfo(x, y, z);
                        match = predicate(cell.X, cell.Y, cell.Z, cell.Cell);
                    }
                    else
                    {
                        match = false;
                    }
                }
                west = x;
                //

                // Find east
                x     = next.X;
                y     = next.Y;
                z     = next.Z;
                match = true;

                while (match)
                {
                    --x;
                    if (IsValidCell(x, y, z))
                    {
                        cell  = GetCastInfo(x, y, z);
                        match = predicate(cell.X, cell.Y, cell.Z, cell.Cell);
                    }
                    else
                    {
                        match = false;
                    }
                }
                east = x;
                //

                for (x = east + 1; x < west; ++x)
                {
                    if (closed[x, y, z])
                    {
                        continue;
                    }

                    cell  = GetCastInfo(x, y, z);
                    match = predicate(cell.X, cell.Y, cell.Z, cell.Cell);

                    closed[x, y, z] = true;

                    if (IsValidCell(x, y - 1, z))
                    {
                        cell = GetCastInfo(x, y - 1, z);
                        if (predicate(cell.X, cell.Y, cell.Z, cell.Cell))
                        {
                            open.Enqueue(cell);
                        }
                    }
                    if (IsValidCell(x, y + 1, z))
                    {
                        cell = GetCastInfo(x, y + 1, z);
                        if (predicate(cell.X, cell.Y, cell.Z, cell.Cell))
                        {
                            open.Enqueue(cell);
                        }
                    }
                }
            }
        }