Example #1
0
        public void DestroyByFlood()
        {
            var area  = GetArea();
            var field = new CityField(area);

            while (true)
            {
                Boolean wasFlood = false;
                for (Int32 x = area.Left; x < area.Left + area.Width; x++)
                {
                    for (Int32 y = area.Y - area.Height; y < area.Y; y++)
                    {
                        if (field[x, y])
                        {
                            continue;
                        }
                        if (field.CanFill(x, y, Walls))
                        {
                            field.FillCell(x, y);
                            wasFlood = true;
                        }
                    }
                }
                if (!wasFlood)
                {
                    var walls = FindAllFloodedWalls(field);
                    if (walls == null || walls.Count == 0)
                    {
                        return;
                    }
                    var nodes                = walls.GetAllNodes().ToList();
                    var nodeInfoList         = new NodeInfoList(nodes.Select(n => new NodeInfo(n, walls)));
                    var wallsOfEnclosedSpace = nodeInfoList.FindWallsOfEnclosedSpace().ToList();
                    if (wallsOfEnclosedSpace.Count == 0)
                    {
                        return;
                    }
                    wallsOfEnclosedSpace.ForEach(w => Walls.Remove(w));
                }
            }
        }
        public IEnumerable <Wall> FindWallsOfEnclosedSpace()
        {
            var     tempNodeInfoList  = new NodeInfoList(this);
            Boolean existSeparateWall = true;

            while (existSeparateWall)
            {
                existSeparateWall = false;
                foreach (var nodeInfo in tempNodeInfoList)
                {
                    if (nodeInfo.RelatedWalls.Count == 1)
                    {
                        existSeparateWall = true;
                        var singleWall    = nodeInfo.RelatedWalls.First();
                        var otherNode     = singleWall.GetOtherNode(nodeInfo);
                        var otherNodeInfo = tempNodeInfoList.First(node => node.Equals(otherNode));
                        otherNodeInfo.RelatedWalls.Remove(singleWall);
                        tempNodeInfoList.Remove(nodeInfo);
                        if (otherNodeInfo.RelatedWalls.Count == 0)
                        {
                            tempNodeInfoList.Remove(otherNodeInfo);
                        }
                        break;
                    }
                }
            }
            var result = new List <Wall>();

            tempNodeInfoList.ForEach(n => n.RelatedWalls.ForEach(w => {
                if (!result.Contains(w))
                {
                    result.Add(w);
                }
            }));
            return(result);
        }