public void DetermineShipDamage() { SpaceShip = new SpaceShip("Test", 1, 15, 1, 1, 1, new Random()); Assert.That(SpaceShip.DamageStatus(), Is.EqualTo(ShipDamageEnum.Undamaged)); SpaceShip.RecieveDamage(2); Assert.That(SpaceShip.DamageStatus(), Is.EqualTo(ShipDamageEnum.LightlyDamaged)); SpaceShip.RecieveDamage(1); Assert.That(SpaceShip.DamageStatus(), Is.EqualTo(ShipDamageEnum.ModeratelyDamaged)); SpaceShip.RecieveDamage(3); Assert.That(SpaceShip.DamageStatus(), Is.EqualTo(ShipDamageEnum.HeavilyDamaged)); SpaceShip.RecieveDamage(2); Assert.That(SpaceShip.DamageStatus(), Is.EqualTo(ShipDamageEnum.HeavilyDamaged)); SpaceShip.RecieveDamage(2); Assert.That(SpaceShip.DamageStatus(), Is.EqualTo(ShipDamageEnum.HeavilyDamaged)); SpaceShip.RecieveDamage(2); Assert.That(SpaceShip.DamageStatus(), Is.EqualTo(ShipDamageEnum.VeryHeavilyDamaged)); SpaceShip.RecieveDamage(2000); Assert.That(SpaceShip.DamageStatus(), Is.EqualTo(ShipDamageEnum.VeryHeavilyDamaged)); }
/// <summary> /// Add an new Spaceship to the array /// </summary> /// <param name="spaceShips"></param> /// <param name="objectToAdd"></param> /// <returns></returns> public static SpaceShip[] AddToArray(SpaceShip[] spaceShips, SpaceShip objectToAdd) { // int previousLength = spaceShips.Length; Array.Resize(ref spaceShips, spaceShips.Length + 1); spaceShips[spaceShips.Length - 1] = objectToAdd; return spaceShips; }
public Fleet(string fleetName, SpaceShip[] spaceShips, Random randomShipTargeter) { this.fleetName = fleetName; SpaceShipsInService = spaceShips; this.randomShipTargeter = randomShipTargeter; this.shipsHitInCurrentBattle = new int[0]; DestroyedSpaceShips = new SpaceShip[0]; }
private SpaceShip[] CreateSpaceShips(int amount) { SpaceShip[] spaceShips = new SpaceShip[amount]; for (int i = 0; i < amount; i++) { spaceShips[i] = CreateSpaceShip("Test" + i); } return spaceShips; }
/// <summary> /// Each ship which is still in service and has not been hit /// in the current battle regenerates it's /// shields. /// </summary> private void RegenerateShields() { for (int index = 0; index < SpaceShipsInService.Length; index++) { if (HasShipBeenHitInCurrentBattle(index)) { continue; } SpaceShip spaceShip = SpaceShipsInService[index]; spaceShip.RegenerateShield(); } }
/// <summary> /// Fires the fleet's targeting missiles. /// </summary> /// <returns></returns> public TargetingMissile[] FireTargetingMissiles() { TargetingMissile[] attackMissiles = new TargetingMissile[SpaceShipsInService.Length]; for (int index = 0; index < SpaceShipsInService.Length; index++) { SpaceShip spaceShip = SpaceShipsInService[index]; int damagePayload = spaceShip.FireWeapons(); int enemyTargetPosition = DetermineEnemyTargetPosition(); attackMissiles[index] = new TargetingMissile(damagePayload, enemyTargetPosition); } return attackMissiles; }
/// <summary> /// Recives a incomming missile from the enemy fleet. The missile will /// deal it's specified damage to the target ship in this /// fleet. This Fleet will also update it's battle information on the /// ship that has been hit. Will have no effect if missile targets invalid ship /// </summary> /// <param name="incommingMissile"></param> public void RecieveFireFromEnemyFleet(TargetingMissile incommingMissile) { if (incommingMissile.TargetShipPosition > SpaceShipsInService.Length || incommingMissile.TargetShipPosition < 0) { return; } SpaceShip spaceShipUnderAttack = SpaceShipsInService[incommingMissile.TargetShipPosition]; spaceShipUnderAttack.RecieveDamage(incommingMissile.DamageAmount); UpdateBattleInformation(incommingMissile.TargetShipPosition); }
/// <summary> /// Updates the list of ships destroyed and still in service. /// Returns the list of ships class names which have been destroyed. /// </summary> /// <returns></returns> private string[] UpdateDestroyedShips() { string[] destroyedShipNames = {}; for (int index = 0; index < SpaceShipsInService.Length; index++) { SpaceShip spaceShip = SpaceShipsInService[index]; if (spaceShip.IsDestroyed()) { RemoveDestroyedShipFromService(spaceShip, index); destroyedShipNames = ArrayUtils.AddStringToArray(destroyedShipNames, spaceShip.ShipClassName); } } return destroyedShipNames; }
private SpaceShip[] CreateSpaceShips(int numberOfShips) { SpaceShip[] spaceShips = new SpaceShip[numberOfShips]; for (int i = 0; i < numberOfShips; i++) { spaceShips[i] = new SpaceShip("Test" + i, 10, 15, 4, 10, 5, CommonRandom); } return spaceShips; }
/// <summary> /// Cleans up the given array, by removing any null elements from the /// given array. /// </summary> /// <param name="arrayWithNulls"></param> /// <returns>New array with nulls removed</returns> public static SpaceShip[] RemoveNullsFromArray(SpaceShip[] arrayWithNulls) { SpaceShip[] spaceShips = {}; if (arrayWithNulls == null) { return spaceShips; } foreach (SpaceShip spaceShip in arrayWithNulls) { if (spaceShip != null) { spaceShips = AddToArray(spaceShips, spaceShip); } } return spaceShips; }
/// <summary> /// Sets the element at the spefified index to null. Once any further work on /// the array that requires consistent indexs, has /// been performed RemoveNullsFromArray can /// be called to clean up the array. /// </summary> /// <param name="spaceShips"></param> /// <param name="indexOfElement"></param> public static SpaceShip[] RemoveFromArray(SpaceShip[] spaceShips, int indexOfElement) { spaceShips[indexOfElement] = null; return spaceShips; }
/// <summary> /// Creates an array of spaceships /// </summary> /// <param name="expectedNumberOfShips"></param> /// <returns></returns> private SpaceShip[] CreateSpaceShips(int expectedNumberOfShips) { SpaceShip[] spaceShips = new SpaceShip[expectedNumberOfShips]; int shipsCreatedIndex = 0; int index = ShipInformationStartPostion; // while not created all ships while (shipsCreatedIndex != expectedNumberOfShips) { SpaceShip spaceShip = CreateSpaceShip(index); if (spaceShip == null) { return null; // exit as soon as there is an invalid ship } spaceShips[shipsCreatedIndex] = spaceShip; shipsCreatedIndex++; index += NumberOfPropertiesPerShip; } if (ArrayUtils.HasValidElementsInArrayRange(fleetFileContents, index, fleetFileContents.Length)) { // more content in the file than expected, reject ConsoleUtil.WriteMoreShipsThanExpected(fleetFileName); return null; } return spaceShips; }
private void RemoveDestroyedShipFromService(SpaceShip destroyedSpaceShip, int spaceShipIndex) { DestroyedSpaceShips = ArrayUtils.AddToArray(DestroyedSpaceShips, destroyedSpaceShip); SpaceShipsInService = ArrayUtils.RemoveFromArray(SpaceShipsInService, spaceShipIndex); }
public void TestShipTakesDamageAndImpactsHull() { SpaceShip = new SpaceShip("Voyager", 20, 20, 5, 10, 10, new Random()); SpaceShip.RecieveDamage(10); ExpectSpaceShipToBe(false, false); //shield should be at 10 now, 30 damage should destroy shield and destroy hull SpaceShip.RecieveDamage(30); ExpectSpaceShipToBe(true, true); }
public void SetUp() { SpaceShip = new SpaceShip("Voyager", 10, 20, 5, 5, 10, new Random()); }
public void TestShipCanReturnWeaponDamage() { this.SpaceShip = new SpaceShip("Voyager", 10, 10, 10, 5, 30, new Random(50)); Assert.That(SpaceShip.FireWeapons(), Is.EqualTo(30), "Expected the ship to return more than the base damage"); Assert.That(SpaceShip.FireWeapons(), Is.EqualTo(19), "Expected the ship to return more than the base damage"); Assert.That(SpaceShip.FireWeapons(), Is.EqualTo(26), "Expected the ship to return more than the base damage"); Assert.That(SpaceShip.FireWeapons(), Is.EqualTo(11), "Expected the ship to return more than the base damage"); }