Beispiel #1
0
 public Result TryMoveShipTo(ShipKind kind, GridCoordinate[] segmentCoordinates, IGrid grid)
 {
     if (!GridCoordinateArrayExtensions.AreAligned(segmentCoordinates))
     {
         return(Result.CreateFailureResult("Coordinates are not aligned!"));
     }
     if (!GridCoordinateArrayExtensions.AreLinked(segmentCoordinates))
     {
         return(Result.CreateFailureResult("Coordinates are not linked!"));
     }
     if (segmentCoordinates.Length != kind.Size)
     {
         return(Result.CreateFailureResult("SegmentCoordinates do not match length of ShipKind!"));
     }
     if (segmentCoordinates.HasAnyOutOfBounds(grid.Size))
     {
         return(Result.CreateFailureResult("1 or More Coordinate(s) are outside of the grid!"));
     }
     foreach (IShip ship in _Dictionary.Values)
     {
         for (int i = 0; i < segmentCoordinates.Length; i++)
         {
             if (ship.Kind != kind)
             {
                 if (ship.CanBeFoundAtCoordinate(segmentCoordinates[i]))
                 {
                     return(Result.CreateFailureResult("1 or more Coordinate(s) collide with another ship"));
                 }
             }
         }
     }
     foreach (IShip ship in _Dictionary.Values)
     {
         if (ship.Kind == kind)
         {
             IGridSquare[] converted = ConvertGridCoordinateToGridSquare(segmentCoordinates, grid);
             ship.PositionOnGrid(converted);
         }
     }
     return(Result.CreateSuccessResult());
 }
Beispiel #2
0
        /// <summary>
        /// Randomly generates an array of possible ship segment coordinates for the kind of ship.
        /// This method can be used to position a ship at random on a grid.
        /// </summary>
        /// <param name="gridSize">The coordinates will be within this grid size</param>
        /// <param name="allowDeformedShips">
        /// If false (=default), the coordinates will be horizontally or vertically aligned and will touch each other.
        /// If true, the coordinates will touch each other, but may possibly not be aligned (this is an EXTRA).
        /// </param>
        public GridCoordinate[] GenerateRandomSegmentCoordinates(int gridSize, bool allowDeformedShips = false)
        {
            bool outOfBounds = true;

            while (outOfBounds)
            {
                GridCoordinate   generatePosition  = GridCoordinate.CreateRandom(gridSize);
                GridCoordinate[] output            = new GridCoordinate[Size];
                Direction        generateDirection = Direction.CreateRandomly(allowDeformedShips);
                output[0] = generatePosition;
                for (int i = 1; i < Size; i++)
                {
                    output[i] = new GridCoordinate(output[i - 1].Row + generateDirection.YStep, output[i - 1].Column + generateDirection.XStep);
                }
                outOfBounds = GridCoordinateArrayExtensions.HasAnyOutOfBounds(output, gridSize);
                if (!outOfBounds)
                {
                    return(output);
                }
            }
            return(null);
        }