Commit 341f2414 authored by Carl Johan Gützkow's avatar Carl Johan Gützkow 🎮
Browse files

fix: deprecated simulationattack

parent 1cf559d5
......@@ -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";
}
}
......@@ -5,12 +5,18 @@ import edu.ntnu.idatt2001.carljgu.units.Army;
import edu.ntnu.idatt2001.carljgu.units.Unit;
/**
* This class is deprecated.
* A class for keeping track of which unit
* has attacked which unit. Helpful when
* logging out this information.
* Deprecated as it provided no extra functionality
* and made the program harder to read. Additionally,
* it had high unnecessary coupling with Army and Unit.
* Battle use an arraylist of String so this class is not useful.
*
* @author Carl Gützkow
* @version 1.2 09.04.2022
* @deprecated 06.05.2022
*/
public class SimulationAttack {
......@@ -123,7 +129,7 @@ public class SimulationAttack {
* from the attack. In addition, the
* defender's health after the attack is shown
*
* @return
* @return String - string representation of an attack
*/
@Override
public String toString() {
......
......@@ -167,6 +167,16 @@ public class Army {
* Overrides the toString() method from Object.
* iterates over all units and calls their toString() method.
* Uses StringBuilder to avoid making new Strings.
*
* Example of a string representation:
* Army Human army
* ------------------------
* Infantry: 10
* Ranged: 23
* Cavalry: 4
* Commander: 0
* ------------------------
*
* @return String - representation of the army.
*/
@Override
......
......@@ -58,7 +58,7 @@ public abstract class Unit {
public int attack(Unit unit, Terrain terrain) {
int totalDamage = this.getAttack() + this.getAttackBonus(terrain) - unit.getArmor() - unit.getResistBonus(terrain);
if (totalDamage > 0) {
unit.setHealth(unit.getHealth() - totalDamage);
unit.setHealth(Math.max(unit.getHealth() - totalDamage, 0));
return totalDamage;
}
return 0;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment