Beispiel #1
0
        private static StringBuilder CreateMap(XY sourceLocation, ClayMap clayMap, IDictionary <XY, Water> existingWater)
        {
            var topBoundary    = 0;
            var bottomBoundary = clayMap.ClayLocations.MinBy(l => l.Y).Y - 1;
            var leftBoundary   = clayMap.ClayLocations.MinBy(l => l.X).X - 5;
            var rightBoundary  = clayMap.ClayLocations.MaxBy(l => l.X).X + 5;

            var yLocations = Enumerable.Range(bottomBoundary, topBoundary - bottomBoundary + 1).Reverse();
            var xLocations = Enumerable.Range(leftBoundary, rightBoundary - leftBoundary + 1);

            var builder = new StringBuilder();

            foreach (var y in yLocations)
            {
                foreach (var x in xLocations)
                {
                    var l = new XY(x, y);

                    if (clayMap.IsClayAtLocation(l))
                    {
                        builder.Append('#');
                    }
                    else if (existingWater.ContainsKey(l))
                    {
                        if (l == sourceLocation)
                        {
                            builder.Append('+');
                        }
                        else if (existingWater[l].IsStable)
                        {
                            builder.Append('~');
                        }
                        else
                        {
                            builder.Append('|');
                        }
                    }
                    else
                    {
                        builder.Append(' ');
                    }
                }

                builder.AppendLine();
            }

            return(builder);
        }
Beispiel #2
0
        private XY[] GetFlowDirections(XY location, ClayMap clayMap)
        {
            bool CanFlowToLocation(XY loc) => !clayMap.IsClayAtLocation(loc) && !HasWaterAtLocation(loc);

            var locationBelow = location + XY.Down;

            if (CanFlowToLocation(locationBelow))
            {
                return(XY.Down.RepeatOnce().ToArray());
            }

            var isWaterBelow = TryGetWater(locationBelow, out var waterBelow);

            if (!isWaterBelow || waterBelow.IsStable)
            {
                return(Water.GetPossibleFlowDirections()
                       .Where(d => CanFlowToLocation(location + d))
                       .ToArray());
            }

            return(new XY[0]);
        }
Beispiel #3
0
 private bool IsLocationEmpty(XY location, WaterStream stream)
 {
     return(!clayMap.IsClayAtLocation(location) && !stream.HasWaterAtLocation(location));
 }