示例#1
0
 public Ship(ShipKind kind)
 {
     /* The type of ship */
     Kind = kind;
     /* Set Squares to null*/
     Squares = null;
 }
示例#2
0
 protected bool Equals(ShipKind other)
 {
     if (ReferenceEquals(other, null))
     {
         return(false);
     }
     return(Code == other.Code);
 }
示例#3
0
        public Result TryMoveShipTo(ShipKind kind, GridCoordinate[] segmentCoordinates, IGrid grid)
        {
            if (!_allShips.TryGetValue(kind, out var ship))
            {
                return(Result.CreateFailureResult($"Ship of kind {kind.Name} not found"));
            }

            if (ship.Kind.Size != segmentCoordinates.Length)
            {
                return(Result.CreateFailureResult($"Ship is not as big as the amount of GridSquares you are trying to position it in"));
            }

            if (segmentCoordinates.HasAnyOutOfBounds(grid.Size))
            {
                return(Result.CreateFailureResult($"SegmentCoordinates are out of bounds"));
            }

            if (!segmentCoordinates.AreAligned())
            {
                return(Result.CreateFailureResult($"SegmentCoordinates are not aligned"));
            }

            if (!segmentCoordinates.AreLinked())
            {
                return(Result.CreateFailureResult($"SegmentCoordinates are not linked"));
            }

            var otherPositionedShips = _allShips
                                       .Where(kvp => kvp.Key != kind && kvp.Value.Squares != null).Select(kvp => kvp.Value);

            foreach (var otherPositionedShip in otherPositionedShips)
            {
                foreach (var segmentCoordinate in segmentCoordinates)
                {
                    if (otherPositionedShip.CanBeFoundAtCoordinate(segmentCoordinate))
                    {
                        return(Result.CreateFailureResult($"SegmentCoordinate '{segmentCoordinate}' collides with '{otherPositionedShip.Kind.Name}'"));
                    }
                }
            }

            ship.PositionOnGrid(segmentCoordinates.Select(sc => grid.GetSquareAt(sc)).ToArray());

            return(Result.CreateSuccessResult());
        }
示例#4
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());
 }
示例#5
0
 public Ship(ShipKind kind)
 {
 }
示例#6
0
 public Ship(ShipKind kind)
 {
     Kind = kind;
 }
示例#7
0
 public Ship(ShipKind kind)
 {
     this.Kind = kind;
 }
示例#8
0
 public Result TryMoveShipTo(ShipKind kind, GridCoordinate[] segmentCoordinates, IGrid grid)
 {
     throw new NotImplementedException("TryMoveShipTo method of Fleet class is not implemented");
 }
        public Result TryMoveShipTo(ShipKind kind, GridCoordinate[] segmentCoordinates, IGrid grid)
        {
            /* Check if the segmentcoordinates have the same length as the ship */
            if (segmentCoordinates.Length != kind.Size)
            {
                return(Result.CreateFailureResult("Ship length is not ok"));
            }

            /* Check if the segmentcoordinates are vertically or horizontally aligned */
            if (this.allowDeformedShips == false)
            {
                if ((segmentCoordinates.AreHorizontallyAligned() == false) && (segmentCoordinates.AreVerticallyAligned() == false))
                {
                    return(Result.CreateFailureResult("Ship coordinates are not aligned"));
                }
            }
            else
            {
                if ((segmentCoordinates.AreHorizontallyAligned() == false) && (segmentCoordinates.AreVerticallyAligned() == false) && (segmentCoordinates.AreDiagonallyAligned() == false))
                {
                    return(Result.CreateFailureResult("Ship coordinates are not aligned"));
                }
            }
            /* Check if the segmentcoordinates are linked */
            if (segmentCoordinates.AreLinked() == false)
            {
                return(Result.CreateFailureResult("Ship coordinates are not linked"));
            }
            /* Check if the segmentcoordinates are not out of bound (ship not fully in grid)*/
            for (int index = 0; index < kind.Size; index++)
            {
                if (segmentCoordinates[index].IsOutOfBounds(grid.Size) == true)
                {
                    return(Result.CreateFailureResult("Ship out of bound"));
                }
            }
            /* Check if this ship collides with another */
            foreach (KeyValuePair <ShipKind, IShip> otherShip in this.shipList)
            {
                /* Check that this ship is different from the others */
                if (otherShip.Value.Kind != kind)
                {
                    /* Go through the segmentcoordinates of this ship and check if the others have been placed on the same coordinates */
                    for (int index = 0; index < segmentCoordinates.Length; index++)
                    {
                        if (otherShip.Value.CanBeFoundAtCoordinate(segmentCoordinates[index]) == true)
                        {
                            return(Result.CreateFailureResult("Other ship at same coordinates"));
                        }
                    }
                }
            }

            /* Assign the segmentcoordinates to the ship = place the ship on the grid.*/
            IGridSquare[] gridsquares = new IGridSquare[kind.Size];
            for (int index = 0; index < kind.Size; index++)
            {
                /* Get the grid square with the right coordinates from the grid object.*/
                gridsquares[index] = grid.GetSquareAt(segmentCoordinates[index]);
            }

            /*Assign the grid squares to the ship */
            this.shipList[kind].PositionOnGrid(gridsquares);

            /* Everything went well, return success */
            return(Result.CreateSuccessResult());
        }