Skip to content
Snippets Groups Projects

fix: deprecated simulationattack

Merged Carl Gützkow requested to merge fix/composition-simulationattack into master
4 files
+ 72
7
Compare changes
  • Side-by-side
  • Inline
Files
4
@@ -11,7 +11,7 @@ import java.util.Random;
* Class Battle that simulates a battle between two armies.
*
* @author Carl Gützkow
* @version 1.3 30.04.2022
* @version 1.4 01.05.2022
*/
public class Battle {
@@ -39,7 +39,7 @@ public class Battle {
* if armies are equal or if terrain is not defined
* @throws NullPointerException thrown if one of the armies are null object
*/
public Battle(Army armyOne, Army armyTwo, Terrain terrain) throws IllegalArgumentException {
public Battle(Army armyOne, Army armyTwo, Terrain terrain) throws IllegalArgumentException, NullPointerException {
if (armyOne == null || armyTwo == null) throw new NullPointerException("Army is not defined");
if (!armyOne.hasUnits() || !armyTwo.hasUnits()) throw new IllegalArgumentException("Army can not be empty");
if (Objects.equals(armyOne, armyTwo)) throw new IllegalArgumentException("Armies can not be the same");
@@ -53,6 +53,7 @@ public class Battle {
/**
* Gets the winner of the army
* or a null object
*
* @return winner - Army - winner of the simulated battle
*/
public Army getWinner() {
@@ -116,21 +117,69 @@ public class Battle {
Unit attacker = attackingArmy.getRandom();
Unit defender = defendingArmy.getRandom();
attackLog.add(new SimulationAttack(attackingArmy, attacker, defendingArmy, defender, terrain).toString());
int damageDealt = attacker.attack(defender, terrain);
if (defender.getHealth() <= 0) defendingArmy.remove(defender);
attackLog.add(attackStringRepresentation(damageDealt, attacker, defender, attackingArmy, defendingArmy));
}
winner = (armyOne.hasUnits()) ? armyOne : armyTwo;
return winner;
}
/**
* Information displayed is the
* name of the attacking and defending army,
* unit information like name and class
* as well as how much damage was outputted
* from the attack. In addition, the
* defender's health after the attack is shown
*
* Example of an attack's representation:
* "InfantryUnit knight from Human army
* did 10 damage to
* RangedUnit crossbow orc from Orc army
* Current health of defender is now at 0"
*
* @return String - string representation of an attack
*/
public String attackStringRepresentation(int damageDealt, Unit attacker, Unit defender, Army attackingArmy, Army defendingArmy) {
return (
attacker.getClass().getSimpleName() + " " + attacker.getName() + " from " + attackingArmy.getName() + "\n" +
"did " + damageDealt + " damage to\n" +
defender.getClass().getSimpleName() + " " + defender.getName() + " from " + defendingArmy.getName() + ".\n" +
"Current health of the defender is now at " + defender.getHealth()
);
}
/**
* Overrides toString() method from Object.
* Returns different strings depending on who
* won the battle or if the battle is not simulated yet.
*
* Example:
* Winner:
* Army Human army
* ------------------------
* Infantry: 10
* Ranged: 23
* Cavalry: 4
* Commander: 0
* ------------------------
* Loser:
* Army Orc army
* ------------------------
* Infantry: 3
* Ranged: 10
* Cavalry: 4
* Commander: 3
* ------------------------
*
* @return String - string representation of the battle.
*/
@Override
public String toString() {
if (winner == null) return ("Army one:\n" + armyOne + "\n Army two: \n" + armyTwo + "\n");
if (winner.equals(armyOne)) return ("Winner:\n" + armyOne + "\n Looser: \n" + armyTwo + "\n");
return "Winner:\n" + armyTwo + "\n Looser: \n" + armyOne + "\n";
if (winner.equals(armyOne)) return ("Winner:\n" + armyOne + "\n Loser: \n" + armyTwo + "\n");
return "Winner:\n" + armyTwo + "\n Loser: \n" + armyOne + "\n";
}
}
Loading