java.lang.Object
edu.ntnu.idatt2001.carljgu.battle.Battle
Class Battle that simulates a battle between two armies.
- Version:
- 1.4 01.05.2022
- Author:
- Carl Gützkow
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionattackStringRepresentation
(int damageDealt, Unit attacker, Unit defender, Army attackingArmy, Army defendingArmy) 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.Returns the log of attacks to easily hold the information about each attack.Gets the terrain where the battle occurs.Gets the winner of the army or a null object.simulate()
Simulate a battle.toString()
Overrides toString() method from Object.
-
Field Details
-
armyOne
-
armyTwo
-
winner
-
terrain
-
attackLog
-
-
Constructor Details
-
Battle
public Battle(Army armyOne, Army armyTwo, Terrain terrain) throws IllegalArgumentException, NullPointerException Instantiates a new Battle.- Parameters:
armyOne
- Army - the first armyarmyTwo
- Army - the second armyterrain
- Terrain - on what terrain the battle is fought on.- Throws:
IllegalArgumentException
- thrown if one of the armies are empty, if armies are equal or if terrain is not definedNullPointerException
- thrown if one of the armies are null object
-
-
Method Details
-
getWinner
Gets the winner of the army or a null object.- Returns:
- winner - Army - winner of the simulated battle
-
getAttackLog
Returns the log of attacks to easily hold the information about each attack.- Returns:
- attackLog - List - a list of all the attacks performed in the simulation.
-
getTerrain
Gets the terrain where the battle occurs.- Returns:
- terrain - Terrain - on what terrain the battle is fought on.
-
simulate
Simulate a battle. The attacking army is chosen at random. One random unit from the attacking army attacks a random unit from the defending army. Then, if the defender survives, it attacks the first attacker. If the defender has no health left, it is removed. The simulations run until one army has no units left.- Returns:
- winner - Army - the army that won.
-
attackStringRepresentation
public String attackStringRepresentation(int damageDealt, Unit attacker, Unit defender, Army attackingArmy, Army defendingArmy) 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"- Parameters:
damageDealt
- int - how much health the defending unit lostattacker
- Unit - the attacking unitdefender
- Unit - the defending unitattackingArmy
- Army - the attacking armydefendingArmy
- Army - the defending army- Returns:
- String - string representation of an attack
-
toString
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 ------------------------
-