Exemplo n.º 1
0
 public void PlaceShip(Ship ship)
 {
     this.OwnBoard.PlaceShip(ship);
 }
Exemplo n.º 2
0
        /// <summary>
        /// Attempt to place a ship on the board.
        /// </summary>
        /// <param name="ship">Details of the ship to place.</param>
        /// <exception cref="ShipCollisionException">Thrown if placing the ship would overlap with an existing ship.</exception>
        public void PlaceShip(Ship ship)
        {
            var newPlacedShips = new List<Ship>(placedShips);
            newPlacedShips.Add(ship);

            if (Ship.HasCollisions(newPlacedShips))
            {
                throw new ShipCollisionException("Placing this ship would overlap with an existing ship.");
            }

            placedShips = newPlacedShips;
        }
Exemplo n.º 3
0
        /// <summary>
        /// Place a ship on a players board.
        /// </summary>
        /// <param name="playerId">Identifier of the player to place the ship for.</param>
        /// <param name="ship">Details of the ship to place.</param>
        /// <exception cref="InvalidOperationException">Thrown if a ship is placed in any other state than during setup.</exception>
        /// <exception cref="ShipCollisionException">Thrown if placing the ship would overlap with an existing ship.</exception>
        public void PutShip(Guid playerId, Ship ship)
        {
            if (this.State != GameState.Setup)
            {
                throw new InvalidOperationException("Cannot put ship in a game that has already started.");
            }

            players[playerId].PlaceShip(ship);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Determine if two ships are intersecting (overlapping).
        /// </summary>
        /// <param name="a">First ship.</param>
        /// <param name="b">Second ship.</param>
        /// <returns>Boolean indicating if the ships overlap.</returns>
        public static bool Intersects(Ship a, Ship b)
        {
            // This is a little hackish, would look for a better solution,
            // but this appears to work against the tests and is not a performance bottleneck.

            switch (a.Orientation)
            {
                case ShipOrientation.Horizontal:
                    switch (b.Orientation)
                    {
                        case ShipOrientation.Horizontal:
                            return a.Location.Lat == b.Location.Lat
                                && ((a.Location.Long <= b.Location.Long && a.EndCoordinate.Long >= b.Location.Long)
                                || (b.Location.Long <= a.Location.Long && b.EndCoordinate.Long >= a.Location.Long));
                        default:
                            return (a.Location.Long <= b.Location.Long && a.EndCoordinate.Long >= b.Location.Long)
                                && (b.Location.Lat <= a.Location.Lat && b.EndCoordinate.Lat >= a.Location.Lat);
                    }
                default:
                    switch (b.Orientation)
                    {
                        case ShipOrientation.Vertical:
                            return a.Location.Long == b.Location.Long
                                && ((a.Location.Lat <= b.Location.Lat && a.EndCoordinate.Lat >= b.Location.Lat)
                                || (b.Location.Lat <= a.Location.Lat && b.EndCoordinate.Lat >= a.Location.Lat));
                        default:
                            return (a.Location.Lat <= b.Location.Lat && a.EndCoordinate.Lat >= b.Location.Lat)
                                && (b.Location.Long <= a.Location.Long && b.EndCoordinate.Long >= a.Location.Long);
                    }
            }
        }