public void AddShip(Ship vessel) { if (placedShips.Contains(vessel)) { throw new ShipAlreadyPlacedException(); } if (placedShips.Exists(x => x.GetType().Equals(vessel.GetType()))) { throw new ShipAlreadyPlacedException(); } AddShipToGrid(vessel); placedShips.Add(vessel); }
private string SimulateAttack() { int attackerIndex = Rand.Next(0, this.ships.Count); int defenderIndex = Rand.Next(0, this.ships.Count); while (defenderIndex == attackerIndex) { defenderIndex = Rand.Next(0, this.ships.Count); } Ship attacker = this.ships[attackerIndex]; Ship defender = this.ships[defenderIndex]; if (!attacker.IsBattleship) { return("Attacking ship cannot attack other ships."); } if (attacker.IsDestroyed) { return("Attacking ship is destroyed."); } if (defender.IsDestroyed) { return("Defending ship is already destroyed."); } switch (attacker.GetType().Name) { case "AircraftCarrier": return("We bombed them from the sky!"); case "Destroyer": return("They didn't see us coming!"); case "Warship": return("Victory is ours!"); default: return("Invalid ship type"); } }