Esempio n. 1
0
        private Coordinate[] GetLocationOfLiveCells(WorldSize ws)
        {
            var coordsofLife = PromptUntilValidLocations(ws);

            if (String.IsNullOrEmpty(coordsofLife))
            {
                return(new Coordinate[0]);
            }
            var coords        = coordsofLife.Split(".");
            var liveLocations = new List <Coordinate>();

            foreach (var c in coords)
            {
                var coord = c.Split(",").Select(int.Parse).ToArray();
                if (coord.Length == 2)
                {
                    liveLocations.Add(new Coordinate(coord[0], coord[1], 0));
                }
                if (coord.Length == 3)
                {
                    liveLocations.Add(new Coordinate(coord[0], coord[1], coord[2]));
                }
            }
            return(liveLocations.ToArray());
        }
Esempio n. 2
0
        public override bool Equals(object obj)
        {
            WorldSize otherWorldSize = obj as WorldSize;

            if (otherWorldSize == null)
            {
                return(false);
            }
            return(Width == otherWorldSize.Width &&
                   Height == otherWorldSize.Height &&
                   Depth == otherWorldSize.Depth);
        }
Esempio n. 3
0
        private string PromptUntilValidLocations(WorldSize ws)
        {
            PromptLiveLocations();
            var lifeCoords = reader.ReadLine();

            while (!Validation.ValidCoords(lifeCoords, ws))
            {
                PromptLiveLocations();
                lifeCoords = reader.ReadLine();
            }

            return(lifeCoords);
        }
Esempio n. 4
0
 public World(WorldSize worldSize = default, Coordinate[] livingCoords = default)
 {
     if (worldSize == default)
     {
         worldSize = new WorldSize(5, 5, 1);
     }
     if (livingCoords == default)
     {
         livingCoords = new Coordinate[0];
     }
     this.Size = worldSize;
     InitialiseWorld();
     PopulateWorld(livingCoords);
 }
Esempio n. 5
0
        private static bool LegitCoords(string coordString, WorldSize worldSize)
        {
            var coordStringArray = coordString.Split(coordinateSeparator);

            foreach (var coordPair in coordStringArray)
            {
                var coord = coordPair.Split(coordinatePairSeparator);
                if (!(coord.All(IsValidInt) && SizeIs2DOr3D(coord)))
                {
                    return(false);
                }
                var        coordIntPair = coord.Select(int.Parse).ToArray();
                Coordinate coordinate   = new Coordinate(
                    coordIntPair[0],
                    coordIntPair[1],
                    coord.Length == 3 ? coordIntPair[2] : 0
                    );
                if (OutOfBounds(coordinate, worldSize))
                {
                    return(false);
                }
            }
            return(true);
        }
Esempio n. 6
0
 private static bool OutOfBounds(Coordinate c, WorldSize upperBound) =>
 c.X < 0 || c.X >= upperBound.Width ||
 c.Y < 0 || c.Y >= upperBound.Height ||
 c.Z < 0 || c.Z >= upperBound.Depth;
Esempio n. 7
0
 public static bool ValidCoords(string coords, WorldSize worldSize) =>
 (String.IsNullOrEmpty(coords)) ||
 (FormatCoords(coords).All(ValidCoordChars) &&
  LegitCoords(FormatCoords(coords), worldSize) &&
  !coords.Any(c => c == '-'));
Esempio n. 8
0
 public Coordinate WrapCoordinate(WorldSize upperBound) =>
 new Coordinate(
     WrapNumber(X, 0, upperBound.Height),
     WrapNumber(Y, 0, upperBound.Width),
     WrapNumber(Z, 0, upperBound.Height)
     );