Skip to content
Snippets Groups Projects
Commit 5c6b7029 authored by Tomas Beranek's avatar Tomas Beranek
Browse files

Merge branch 'main' into 'Simulation'

# Conflicts:
#   .gitignore
#   pom.xml
#   src/test/java/UnitTest.java
parents ff8a7385 af848ea4
No related branches found
No related tags found
1 merge request!5Merging simulation branch with main branch
Showing
with 1180 additions and 0 deletions
# Default ignored files
/shelf/
/workspace.xml
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
# Editor-based HTTP Client requests
/httpRequests/
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="WarGame" />
</profile>
</annotationProcessing>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
</component>
</project>
\ No newline at end of file
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="DuplicatedCode" enabled="true" level="WEAK WARNING" enabled_by_default="true">
<Languages>
<language minSize="47" name="Java" />
</Languages>
</inspection_tool>
</profile>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="https://repo.maven.apache.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_16" default="true" project-jdk-name="16" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RunConfigurationProducerService">
<option name="ignoredProducers">
<set>
<option value="com.android.tools.idea.compose.preview.runconfiguration.ComposePreviewRunConfigurationProducer" />
</set>
</option>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
package BattleSimulation;
import SpecificUnits.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
/**
* Class representing a full army, with different methods for handling the mechanics of an army
*/
public class Army {
private List<Unit> units;
private String name;
/**
* A constructor with a list, and the name of that list
* @param name army name
*/
public Army(String name) {
units = new ArrayList<>();
this.name = name;
}
/**
* A constructor created with a list and a name of the army object created
* @param name name of army
* @param units list of units
*/
public Army(String name, List<Unit> units){
this.name = name;
this.units = units;
}
/**
* the get method for the name of the army
* @return String with the name of the army
*/
public String getName() {
return name;
}
/**
* method for adding a unit to the army
* @param unit chosen unit to add to army
*/
public void add(Unit unit){
units.add(unit);
}
/**
* method for adding multiple units at one
* @param newUnits the list of the new units
*/
public void addAll(List<Unit> newUnits){
units.addAll(newUnits);
}
/**
* method for removing a specific unit in the army
* @param unit the unit to remove
*/
public void remove(Unit unit){
units.remove(unit);
}
/**
* method that return true if a list is empty or not
* @return true if the list is empty, false if it has units
*/
public boolean hasUnits(){
if(units.size() > 0){
return true;
}
return false;
}
/**
* method for getting all units
* @return a list of the unit
*/
public List<Unit> getAllUnits(){
return units;
}
/**
* method for getting a random unit for a list
* @return a random unit
*/
public Unit getRandom(){
java.util.Random random = new java.util.Random();
int randomNum = random.nextInt(units.size());
return units.get(randomNum);
}
/**
* this is a getmethod for getting the list of all units in an army
* @return list of unit in the army
*/
public List<Unit> getUnits() {
return units;
}
/**
* methods for getting all infantryUnits in a army
* @return a new list of all infantryUnits
*/
public List<Unit> getInfantryUnits(){
return getUnits().stream()
.filter(u -> u instanceof InfantryUnit)
.collect(Collectors.toCollection(ArrayList::new));
}
/**
* methods for getting all RangeUnitsUnits in a army
* @return a new list of all RangeUnits
*/
public List<Unit> getRangeUnits(){
return getUnits().stream()
.filter(u -> u instanceof RangedUnit)
.collect(Collectors.toCollection(ArrayList::new));
}
/**
* methods for getting all CavalryUnits in a army
* @return a new list of all CavalryUnits
*/
public List<Unit> getCavalryUnits(){
return getUnits().stream()
.filter(u -> u instanceof CavalryUnit && !(u instanceof CommanderUnit))
.collect(Collectors.toCollection(ArrayList::new));
}
/**
* methods for getting all CommanderUnits in a army
* @return a new list of all CommanderUnits
*/
public List<Unit> getCommanderUnits(){
return getUnits().stream()
.filter(u -> u instanceof CommanderUnit)
.collect(Collectors.toCollection(ArrayList::new));
}
/**
* if a object is the same as a different unit
* @param o object to check
* @return true if the units are equal, false if not
*/
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Army)) return false;
Army army = (Army) o;
if (!Objects.equals(units, army.units)) return false;
return getName() != null ? getName().equals(army.getName()) : army.getName() == null;
}
/**
* method that makes a hashcode for an object
* @return int as a hashcode for the different objects
*/
@Override
public int hashCode() {
int result = units != null ? units.hashCode() : 0;
result = 31 * result + (getName() != null ? getName().hashCode() : 0);
return result;
}
/**
* a toString method for printing a units list
* @return a string of units
*/
@Override
public String toString() {
return name +
"\nunits=" + units;
}
}
package BattleSimulation;
import SpecificUnits.Unit;
import java.util.Random;
/**
* this class represents the simulation of a battle
* the battle is going to be between two armies from the army class
* this class consists of different methods and variables
*/
public class Battle {
Army attackingArmy;
Army defendingArmy;
Army armyOne;
Army armyTwo;
/**
* this is a constructor for the battle class that takes two classes.
* @param armyOne
* @param armyTwo
*/
public Battle(Army armyOne, Army armyTwo) {
this.armyOne = armyOne;
this.armyTwo = armyTwo;
}
/**
* this method is the simulation of the battle, two armies attack each other, and one wins
* @return return the winning army as an Army object
*/
public Army simulate(){
Army winningArmy;
Random random = new Random();
boolean randomBool = random.nextBoolean();
if (randomBool){
attackingArmy = armyOne;
defendingArmy = armyTwo;
} else {
attackingArmy = armyTwo;
defendingArmy = armyOne;
}
boolean winner;
while (armyOne.hasUnits() && armyTwo.hasUnits()){
Unit attackingUnit = attackingArmy.getRandom();
Unit defendingUnit = defendingArmy.getRandom();
winner = false;
while (!winner){
attackingUnit.attack(defendingUnit);
if (defendingUnit.getHealth() <= 0){
defendingArmy.remove(defendingUnit);
winner = true;
}
else {
defendingUnit.attack(attackingUnit);
if (attackingUnit.getHealth() <= 0){
attackingArmy.remove(attackingUnit);
winner = true;
}
}
}
}
if(armyOne.hasUnits()){
winningArmy = armyOne;
} else {
winningArmy = armyTwo;
}
return winningArmy;
}
/**
* this method chooses the starting/attacking and defending army
*/
public void whoStarts(){
}
/**
* the toString method that print out both armies
* @return both armies units
*/
@Override
public String toString() {
return armyOne.getName() +"\n"+ armyOne.getAllUnits() +
"\n\n" + armyTwo.getName() +"\n"+ armyTwo.getAllUnits();
}
}
\ No newline at end of file
package BattleSimulation;
import SpecificUnits.*;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class armyFiles {
/**
* method that takes a unit and converts it to a string of csv type
* @param u the unit you want to convert
* @return string of csv type of the specific unit
*/
public static String makeCSVString(Unit u){
return u.getClass().getSimpleName() +","+ u.getName()+","+u.getHealth()+","+u.getAttack()+","+u.getArmor()
+","+u.getMelee()+"\n";
}
/**
* method that creates a file from an army with the armys name
* @param army the army you want to create a file of
* @return File with the new army
* @throws IOException if file not found, or file is wrong throws exception
*/
public static File makeCSVFile(Army army) throws IOException{
//makes a new file in the armyFile folder with the army name
File armyFile = new File("src/main/resources/armyFiles/"+army.getName().trim());
//checks if file is already in registry
if(armyFile.exists() && !armyFile.isDirectory()) {
throw new IllegalArgumentException("This ArmyFile is already in the registry");
}
Writer writer = new FileWriter(armyFile.getAbsolutePath());
writer.write(armyFile.getName()+"\n");
// puts all units in a army to a specific list
army.getUnits()
.forEach(unit -> {
try {
writer.write(makeCSVString(unit));
} catch (IOException e) {
e.printStackTrace();
}
});
writer.close();
return armyFile;
}
/**
* method that takes a file and creates an army form its context
* @param armyName the file you are looking for also the name of the army
* @return the new Army created from the file
* @throws IOException any mistakes with the file will throw an exception
*/
public static Army readFromCSV(String armyName) throws IOException {
// finds path to wanted file
Path path = Path.of("src/main/resources/armyFiles/"+armyName);
if(Path.of("src/main/resources/armyFiles/"+armyName).getFileName() == null) throw new NoSuchFileException("File not found");
Army newArmy = new Army(armyName);
ArrayList<Unit> newUnits = new ArrayList<>();
Files.lines(path)
.skip(1)
.forEach(line -> {
//checkForCharacters(line);
checkForUnit(line, newUnits);
});
if (newUnits.size() == 0){
throw new NullPointerException("This file doesnt contain any units");
}
newArmy.addAll(newUnits);
return newArmy;
}
/**
* method that checks which unitType a line from a file is
* @param line line from a file
* @param newUnits arrayList with the new Units created
*/
public static void checkForUnit(String line, ArrayList<Unit> newUnits){
String[] column = line.split(",");
if(column.length < 6) throw new IllegalArgumentException("File is not readable");
// all 4 different unit types passed to an arraylist of units
String unitType = column[0];
if(Objects.equals(unitType, "InfantryUnit")){
newUnits.add(new InfantryUnit(column[1], Integer.parseInt(column[2]), Integer.parseInt(column[2])
,Integer.parseInt(column[3]),Integer.parseInt(column[4])));
} else if(Objects.equals(unitType, "RangedUnit")){
newUnits.add(new RangedUnit(column[1], Integer.parseInt(column[2]), Integer.parseInt(column[2])
,Integer.parseInt(column[3]),Integer.parseInt(column[4])));
} else if(Objects.equals(unitType, "CavalryUnit")){
newUnits.add(new CavalryUnit(column[1], Integer.parseInt(column[2]), Integer.parseInt(column[2])
,Integer.parseInt(column[3]),Integer.parseInt(column[4])));
}else if(Objects.equals(unitType, "CommanderUnit")){
newUnits.add(new CommanderUnit(column[1], Integer.parseInt(column[2]), Integer.parseInt(column[2])
,Integer.parseInt(column[3]),Integer.parseInt(column[4])));
}
}
/*
public static void checkForCharacters(String lines) throws IllegalArgumentException{
String removeCharacters = "[\\d]";
Pattern pt = Pattern.compile(removeCharacters);
Matcher mt = pt.matcher(lines);
boolean result = mt.matches();
if (result) throw new IllegalArgumentException("Illegal characters found in file");
}
*/
}
\ No newline at end of file
import SpecificUnits.*;
import BattleSimulation.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import static javax.swing.JOptionPane.*;
/**
* this is the client for my warGame
*/
public class GameHub {
public static List<Unit> unitList = new ArrayList<>();
public static List<Army> armyList = new ArrayList<>();
/**
* the main method that starts the game
* @param args
*/
public static void main(String[] args) {
start();
}
public static boolean finished = true;
/**
* the start f the game, where you can choose what you want to do in the game
*/
public static void start() {
List<Army> armyList = new ArrayList<>();
while(finished){
try {
String[] menuInput =
{
"Register new Unit",
"Register new Army",
"Add all units",
"Multiply unit count",
"Remove all units registered",
"Run simulation",
"See all Units",
"End Game",
};
final int REGISTER_NEW = 0;
final int REGISTER_ARMY = 1;
final int ADD_ALL = 2;
final int MULTIPY_UNIT = 3;
final int REMOVE_ALL = 4;
final int RUN_SIMULATION = 5;
final int SHOW_ALL_UNITS = 6;
final int EXIT = 7;
int menuSelection = showOptionDialog(null, "****WarGames****" + "\n Choose function",
"Project Idatt2001", YES_NO_OPTION, INFORMATION_MESSAGE, null, menuInput, menuInput[0]);
switch (menuSelection) {
case REGISTER_NEW -> {
REGISTERNEW();
}
case REGISTER_ARMY -> {
REGISTERARMY();
}
case ADD_ALL -> {
ADDALL();
}
case MULTIPY_UNIT -> {
MULTIPYUNIT();
}
case REMOVE_ALL -> {
REMOVEALL();
}
case RUN_SIMULATION -> {
RUNSIMULATION();
}
case SHOW_ALL_UNITS -> {
SHOWALLUNITS();
}
case EXIT -> {
System.out.println("""
Thanks of using this Game
Bye
""");
finished = false;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* method where you register new units
*/
private static void REGISTERNEW() {
String typeRead = showInputDialog("what type of unit is it\n" +
"1. Infantry" +
"\n2. Range" +
"\n3. Cavalry" +
"\n4. Commander");
int type = Integer.parseInt(typeRead);
String newName = showInputDialog("What is the name of the unit");
String newDamageRead = showInputDialog("How much damage does it deal");
int newDamage = Integer.parseInt(newDamageRead);
String newArmorRead = showInputDialog("How much Armor does it have");
int newArmor = Integer.parseInt(newArmorRead);
String newHealthRead = showInputDialog("How much health does it have");
int newHealth = Integer.parseInt(newHealthRead);
String newMeleeRead = showInputDialog("How much melee damage does it deal");
int newMelee = Integer.parseInt(newMeleeRead);
if (type == 1){
unitList.add(new InfantryUnit(newName, newHealth, newDamage, newArmor, newMelee));
}
else if(type == 2){
unitList.add(new RangedUnit(newName, newHealth, newDamage, newArmor, newMelee));
}
else if(type == 3){
unitList.add(new CavalryUnit(newName, newHealth, newDamage, newArmor, newMelee));
}
else if(type == 4){
unitList.add(new CommanderUnit(newName, newHealth, newDamage, newArmor, newMelee));
}
}
/**
* method for registering new army
*/
private static void REGISTERARMY() {
String newName = showInputDialog("Write the name of the army");
Army army = new Army(newName);
armyList.add(army);
}
/**
* method for adding all units to an army
*/
private static void ADDALL() {
String armyName = showInputDialog("what is the name of the army you want to add all these units to");
for (Army a:armyList) {
if(Objects.equals(a.getName(), armyName)){
a.addAll(unitList);
}
}
}
/**
* method for multiplying a specific unit based on type
*/
private static void MULTIPYUNIT() {
String typeRead = showInputDialog("what type of unit is it\n" +
"1. Infantry" +
"\n2. Range" +
"\n3. Cavalry" +
"\n4. Commander");
int type = Integer.parseInt(typeRead);
String nameMultiple = showInputDialog("What is the name of unit you want to multiple");
String howManyTimes = showInputDialog("How many do you want");
int multipleNum = Integer.parseInt(howManyTimes);
if (type == 1) {
for (Unit inf : unitList) {
if (Objects.equals(inf.getName(), nameMultiple)) {
for (int i = 0; i <= multipleNum; i++) {
unitList.add(new InfantryUnit(inf.getName(), inf.getHealth(), inf.getAttack(), inf.getArmor(), inf.getMelee()));
}
}
}
}
else if (type == 2){
for (Unit rng: unitList) {
if (Objects.equals(rng.getName(), nameMultiple)){
for (int i = 0; i < multipleNum; i++) {
unitList.add(new RangedUnit(rng.getName(), rng.getHealth(), rng.getAttack(), rng.getArmor(), rng.getMelee()));
}
}
}
}
else if (type == 3){
for (Unit cav : unitList){
if (Objects.equals(cav.getName(), nameMultiple)){
for (int i = 0; i <= multipleNum; i++) {
unitList.add(new CavalryUnit(cav.getName(), cav.getHealth(), cav.getAttack(), cav.getArmor(), cav.getMelee()));
}
}
}
}
else if (type == 4){
for (Unit com : unitList){
if (Objects.equals(com.getName(), nameMultiple)){
for (int i = 0; i <= multipleNum; i++) {
unitList.add(new CommanderUnit(com.getName(), com.getHealth(), com.getAttack(), com.getArmor(), com.getMelee()));
}
}
}
}
}
/**
* remove all units in the list, so you can make a new army
*/
private static void REMOVEALL() {
unitList.clear();
}
/**
* mathod that runs the simulation of the battle
*/
private static void RUNSIMULATION() {
Battle battle = new Battle(armyList.get(0), armyList.get(1));
System.out.println(battle.simulate());
}
/**
* shows all units registered
*/
private static void SHOWALLUNITS() {
System.out.println(unitList);
}
}
package SpecificUnits;
/**
* Cavalry class that represents the cavalry unit with special features
*/
public class CavalryUnit extends Unit {
private boolean firstCharge = true;
/**
* this is the constructor for all units
* @param name
* @param health
* @param attack damage
* @param armor protection
* @param melee melee damage
* @throws IllegalArgumentException if health sett < 0 or name is empty thr exception
*/
public CavalryUnit(String name, int health, int attack, int armor,int melee) throws IllegalArgumentException {
super(name, health, attack, armor, melee);
}
/**
* an easier constructor with predefined attack and armor and melee
* @param name
* @param health
* @throws IllegalArgumentException if health sett < 0 or name is empty thr exception
*/
public CavalryUnit(String name, int health) throws IllegalArgumentException {
this(name, health, 20, 12, 2);
}
/**
*this method represents the attackBonuses of a cavalry unit
* the first attack of this unit will it will get a charge bonus +3
* later on it will only have the melee attack as main attackBonus
* @return ether +5 or +2 as a attackBonus, depends on the firstCharge bonus
*/
@Override
int getAttackBonus() {
int charge = 3;
if(firstCharge){
firstCharge = false;
return this.getMelee() + charge;
}
return this.getMelee();
}
/**
* this unit has a better protection than basic infantry, and has a bonus of +3 to resistance
* @return bonus of 3 as resistance
*/
@Override
int getResistBonus() {
int cavalry = 3;
return cavalry;
}
}
package SpecificUnits;
/**
* Commander class that represents all the variables and methods of a commander unit
*/
public class CommanderUnit extends CavalryUnit{
/**
* this is the constructor for all units
* @param name
* @param health
* @param attack damage
* @param armor protection
* @param melee melee damage
* @throws IllegalArgumentException if health sett < 0 or name is empty thr exception
*/
public CommanderUnit(String name, int health, int attack, int armor, int melee) throws IllegalArgumentException {
super(name, health, attack, armor, melee);
}
/**
* an easier constructor with predefined attack and armor
* @param name
* @param health
* @throws IllegalArgumentException if health sett < 0 or name is empty thr exception
*/
public CommanderUnit(String name, int health) throws IllegalArgumentException {
this(name, health, 25, 15, 2);
}
/**
*this method represents the attackBonuses of a cavalry unit
* the first attack of this unit will it will get a charge bonus +3
* later on it will only have the melee attack as main attackBonus
* @return ether +5 or +2 as a attackBonus, depends on the firstCharge bonus
*/
@Override
int getAttackBonus() {
return super.getAttackBonus();
}
/**
* this unit has a better protection than basic infantry, and has a bonus of +3 to resistance
* @return bonus of 3 as resistance
*/
@Override
int getResistBonus() {
return super.getResistBonus();
}
}
package SpecificUnits;
/**
* Infantry class that represents all the variables and methods of an infantry unit
*/
public class InfantryUnit extends Unit{
/**
* this is the constructor for all units
* @param name
* @param health
* @param attack damage
* @param armor protection
* @param melee melee damage
* @throws IllegalArgumentException if health sett < 0 or name is empty thr exception
*/
public InfantryUnit(String name, int health, int attack, int armor, int melee) throws IllegalArgumentException {
super(name, health, attack, armor, melee);
}
/**
* a constructor with predefined attack and armor
* @param name
* @param health
* @throws IllegalArgumentException if health sett < 0 or name is empty thr exception
*/
public InfantryUnit(String name, int health) throws IllegalArgumentException {
this(name, health, 15, 10, 2);
}
/**
* this unit is a melee unit
* @return which means that it gets a +2 as a AttackBonus
*/
@Override
int getAttackBonus() {
return this.getMelee();
}
/**
* infantry unit has only a small armor bonus
* @return because of small armor bonus add 1 as a ResistBonus
*/
@Override
int getResistBonus() {
return 1;
}
}
package SpecificUnits;
/**
* Range class that represents all the variables and methods of a ranger unit
*/
public class RangedUnit extends Unit {
private int distanceDamage = 0;
private int distanceResist = 0;
/**
* this is the constructor for all units
* @param name
* @param health
* @param attack damage
* @param armor protection
* @param melee melee damage
* @throws IllegalArgumentException if health sett < 0 or name is empty thr exception
*/
public RangedUnit(String name, int health, int attack, int armor, int melee) throws IllegalArgumentException {
super(name, health, attack, armor, melee);
}
/**
* an easier constructor with predefined attack and armor
* @param name
* @param health
* @throws IllegalArgumentException if health sett < 0 or name is empty thr exception
*/
public RangedUnit(String name, int health) throws IllegalArgumentException {
this(name, health, 15, 8, 2);
}
/**
* Range unit has a range attack bonus
* @return because this unit is range, this unit gets +3 as AttackBonus
*/
@Override
int getAttackBonus() {
if(distanceDamage >= 3){
return this.getMelee();
}
distanceDamage++;
return 3;
}
/**
* Range units has an uniq resistantBonus that works as distance between him and his opponent
* @return because of this the range unit returns a bonus based on the variable multiplier (distance)
*/
@Override
int getResistBonus() {
int resistBonus = 6 - 2 * distanceResist;
if(resistBonus <= 2){
resistBonus = 2;
}
distanceResist++;
return resistBonus;
}
}
package SpecificUnits;
/**
* SpecificUnits.Unit class that works as a blueprint for all future specific unitclasses
* <p>
* has variables as name, health, DP and CP and to abstract methods
* which talks about bonus Damage or Armor
* </p>
*/
public abstract class Unit {
private String name;
private int health;
private int attack;
private int armor;
private int melee;
/**
* this is the constructor for all units
* @param name
* @param health
* @param attack damage
* @param armor protection
* @param melee this is implemented as all my units will be using this melee variable
* @throws IllegalArgumentException if health sett < 0 thr or name is empty exception
*/
public Unit(String name, int health, int attack, int armor, int melee) throws IllegalArgumentException {
if(name.isBlank()){
throw new IllegalArgumentException("Name no blank bad");
}
this.name = name;
if(health < 0){
throw new IllegalArgumentException("health less zero bad");
}
this.health = health;
//I think that attack and armor can be < 0 because in a war something can go wrong or very well
// Eg: a catapult can malfunction, or a units armor can ble less and less efficient
this.attack = attack;
this.armor = armor;
this.melee = melee;
}
/**
* this is a attack method that takes simulates a unit attacking the opponent
* @param opponent the one this unit attacks
*/
public void attack(Unit opponent){
int newHealth = opponent.health - (this.attack + this.getAttackBonus()) + (opponent.armor + opponent.getResistBonus());
if(newHealth < opponent.getHealth()){
opponent.setHealth(newHealth);
}
}
/**
* get method for name of the unit
* @return the name of this unit
*/
public String getName() {
return name;
}
/**
* get method for health of the unit
* @return the health of this unit
*/
public int getHealth() {
return health;
}
/**
* get method for attacking damage of this unit
* @return the damage or DP of this unit
*/
public int getAttack() {
return attack;
}
/**
* get method for armor protection of the unit
* @return the protection or AP of this unit
*/
public int getArmor() {
return armor;
}
/**
* get method for when a unit changes to melee combat
* @return the bonus of using melee
*/
public int getMelee() {
return melee;
}
/**
* set method that changes the health of a unit if it gets damaged
* @param newHealth the newhealth of this unit after it gets damaged
*/
public void setHealth(int newHealth) {
this.health = newHealth;
}
/**
* an abstract method for showing an attacking bonus
* <p>
* an abstract method which wil be declared,and further used in subclasses
* </p>
* @return attackBonus as an int
*/
abstract int getAttackBonus();
/**
* an abstract method for showing an armor bonus
* <p>
* an abstract method which wil be declared,and further used in subclasses
* </p>
* @return ResistBonus as an int
*/
abstract int getResistBonus();
/**
* a to string method that print out a units statistics in an order
* @return a string of all statistics
*/
@Override
public String toString() {
return "\n\nThe statistics of unit: \n" +name+
"\nHealth:\n" + health+
"\nDamage/DP:\n" + attack+
"\nArmor/AP \n" + armor;
}
}
File added
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment