Skip to content
Snippets Groups Projects

refactor: removed view and changed filehandler and controller

Merged Carl Gützkow requested to merge refactor/lower-coupling into master
6 files
+ 302
309
Compare changes
  • Side-by-side
  • Inline
Files
6
@@ -2,9 +2,10 @@ package edu.ntnu.idatt2001.carljgu.client;
import edu.ntnu.idatt2001.carljgu.Battle;
import edu.ntnu.idatt2001.carljgu.FileExtensionException;
import edu.ntnu.idatt2001.carljgu.FileHandler;
import edu.ntnu.idatt2001.carljgu.ArmyFileHandler;
import edu.ntnu.idatt2001.carljgu.units.Army;
import edu.ntnu.idatt2001.carljgu.units.Unit;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
@@ -19,10 +20,10 @@ import javafx.scene.text.Text;
* A controller class which
* handles events in the fxml file.
*
* @version 1.3 01.04.2022
* @version 1.4 08.04.2022
* @author Carl Gützkow
*/
public class Controller implements Initializable {
public class BattleController implements Initializable {
@FXML
private Text armyOneImportPath, armyOneName, armyOneToString;
@@ -50,6 +51,8 @@ public class Controller implements Initializable {
private final String[] filePaths = new String[2];
private final int[] scores = new int[2];
private Battle battle;
/**
* Helper method to import an army and
* display its information in the
@@ -59,21 +62,16 @@ public class Controller implements Initializable {
* 0 for army one and 1 for army two
*/
private void importArmy(int army) {
ArmyFileHandler fileHandler = new ArmyFileHandler();
try {
filePaths[army] = View.getFilePath();
filePaths[army] = fileHandler.getFilePath();
if (filePaths[army] == null)
return;
armies[army] = FileHandler.readArmyFromFile(filePaths[army]);
View.displayArmy(
armyNames[army],
armySummaries[army],
armyUnitListViews.get(army),
armyImportPaths[army],
armies[army],
filePaths[army]
);
armies[army] = fileHandler.readArmyFromFile(filePaths[army]);
displayArmy(army, armies[army]);
} catch (IOException | FileExtensionException e) {
System.out.println("An error occurred");
}
@@ -102,6 +100,38 @@ public class Controller implements Initializable {
importArmy(1);
}
/**
* Run when clicking on reset armies.
* Deep copies the armies and creates a new battle.
* Also resets the labels and texts.
*/
@FXML
void resetArmies() {
try {
ArrayList<Unit> unitsOne =
armies[0].getAllUnits()
.stream()
.map(Unit::deepCopyUnit)
.collect(Collectors.toCollection(ArrayList::new));
ArrayList<Unit> unitsTwo =
armies[1].getAllUnits()
.stream()
.map(Unit::deepCopyUnit)
.collect(Collectors.toCollection(ArrayList::new));
armyOne = new Army(armies[0].getName(), unitsOne);
armyTwo = new Army(armies[1].getName(), unitsTwo);
battle = new Battle(armyOne, armyTwo);
attackList.getItems().clear();
displayArmy(0, armyOne);
displayArmy(1, armyTwo);
} catch (IllegalArgumentException | NullPointerException e) {
System.out.println(e.getMessage());
}
}
/**
* Run when clicking on the button reset and run.
* Creates deep copies of the armies and runs the
@@ -109,52 +139,52 @@ public class Controller implements Initializable {
* to display that information.
*/
@FXML
void resetAndRun() {
void runSimulation() {
try {
ArrayList<Unit> unitsOne =
armies[0].getAllUnits()
.stream()
.map(Unit::deepCopyUnit)
.collect(Collectors.toCollection(ArrayList::new));
ArrayList<Unit> unitsTwo =
armies[1].getAllUnits()
.stream()
.map(Unit::deepCopyUnit)
.collect(Collectors.toCollection(ArrayList::new));
Army armyOne = new Army(armies[0].getName(), unitsOne);
Army armyTwo = new Army(armies[1].getName(), unitsTwo);
Battle battle = new Battle(armyOne, armyTwo);
resetArmies();
Army winningArmy = battle.simulate();
attackList.getItems().clear();
attackList.getItems().addAll(battle.getAttackLog());
if (winningArmy.equals(armyOne)) scores[0]++; else scores[1]++;
if (winningArmy.equals(armyOne)) scores[0]++;
else scores[1]++;
score.setText(scores[0] + " - " + scores[1]);
lastSimulation.setText(winningArmy.getName());
View.displayArmy(
armyNames[0],
armySummaries[0],
armyUnitListViews.get(0),
armyImportPaths[0],
armyOne,
filePaths[0]
);
View.displayArmy(
armyNames[1],
armySummaries[1],
armyUnitListViews.get(1),
armyImportPaths[1],
armyTwo,
filePaths[1]
);
} catch (IllegalArgumentException | NullPointerException e) {
displayArmy(0, armyOne);
displayArmy(1, armyTwo);
} catch (UnsupportedOperationException | NullPointerException e) {
System.out.println(e.getMessage());
}
}
/**
* Updates the information about an army
* in the chosen elements..
*
* @param armySide int - 0 or 1 depending on which elements to use to display an army.
* @param army Army - the army to display.
* @throws NullPointerException thrown if the army is null
*/
public void displayArmy(int armySide, Army army) throws NullPointerException {
if (army == null) throw new NullPointerException("Army is a a null object");
armyNames[armySide].setText(army.getName());
String armyRepresentation =
"Infantry units: " + army.getInfantryUnits().size() + "\n" +
"Ranged units: " + army.getRangedUnits().size() + "\n" +
"Cavalry units: " + army.getCavalryUnits().size() + "\n" +
"Commander units: " + army.getCommanderUnits().size() + "\n";
armySummaries[armySide].setText(armyRepresentation);
armyImportPaths[armySide].setText(filePaths[armySide]);
armyUnitListViews.get(armySide).getItems().clear();
armyUnitListViews.get(armySide).getItems().addAll(army.getAllUnits());
}
/**
* Run when the fxml file is first.
* Fills in the tables of the armies.
Loading