So, to give you a quick run through, i have created a class Fighter, which represents a fighter with a combat style
ENUM: COMBAT
CLASS: FIGHTER
Now we have a class barracks, the barracks is full of fighters.
The ArrayList inside barracks that represents the barracks is:
private ArrayList<Fighter> fighters = new ArrayList<>();
now the class Barracks has a few methods.
CLASS: BARRACKS
now if you have a basic understanding of the classes, the problem i have is with this method. The problem is that the fighters will not be removed from the barracks. They will all be called by a phrase, and the list with calledfighters will be correctly filled, but they just wont be removed from the barracks if there are 2 fighters with the same initial
/**
* Calls all fighters that have initials that are in the name. Example: name = war. All fighters with initials 'w','a', or 'r' will be called and removed from the barracks.
* @param phrase The phrase with initials
* @param combatStyle The combat style the fighter is specialized in
*/
public ArrayList<Fighter> callFighters(String phrase, Combat combatStyle)
{
//make the phrase fully uppercase
phrase = phrase.toUpperCase();
//Used to check if we have enough fighters in our army
ArrayList<Fighter> tempBarracks = new ArrayList<>();
//fighters to remove from the barracks after they have been called
ArrayList<Fighter> toRemoveFightersFromBarracks = new ArrayList<>();
//list of fighters that are called from the barracks
ArrayList<Fighter> calledFighters = new ArrayList<>();
//fill the temporary barracks with the contents real barracks, so that we dont have to modify the real
barracks if we call fighters and dont have enough fighters
tempBarracks.addAll(fighters);
//Fill the arraylist with chars of the parameter name //war will be 'W','A','R'
ArrayList<Character> nameInChars = new ArrayList<>();
for(char chr: phrase.toCharArray())
nameInChars.add(chr);
//Call all fighters
int i = 0;
int removedFighters = 0;
for(Fighter fgt: tempBarracks)
{
if (nameInChars.contains(fgt.getInitial()) && fgt.getCombatStyle().equals(combatStyle) && i < nameInChars.size())
{
Fighter r = new Fighter(nameInChars.get(i), combatStyle);
calledFighters.add(r);
for (Fighter fghtr : tempBarracks)
{
if (fghtr.getInitial() == nameInChars.get(i) && fghtr.getCombatStyle().equals(combatStyle) && removedFighters == 0)
{
toRemoveFightersFromBarracks.add(fghtr);
removedFighters++;
}
}
removedFighters = 0;
i++;
}
}
//toRemoveFightersFromBarracks will now be Correctly filled with all the fighters from the given phrase
for(Fighter fgt: toRemoveFightersFromBarracks) //HERE LIES THE PROBLEM I'M HAVING.
{
if(tempBarracks.contains(fgt))
{
//Will also be FALSE if indexOfRm = -1 as described below.
}
int indexOfRm = tempBarracks.indexOf(fgt); //Will be -1 if we call 2 fighters with the same initials and combat style
tempBarracks.remove(fgt); //Wont remove object fgt from tempBarracks, probably has something to do with the
fact that the index of fgt in tempBarracks is -1
}
So basically if there is a second fighter with the same initial, the object fgt from toRemoveFightersFromBarracks will not be removable from tempBarracks, the index of that fighter in tempbarracks is -1, and contains returns false, but it does contain a fighter with that exact initial and that exact combat style
I've made a .gif to show it
if(calledFighters.size() == phrase.length())
{
//Finally: remove the fighters from the barracks!
this.fighters.clear();
this.fighters.addAll(tempBarracks);
return calledFighters;
}
else
{
System.out.println("We dont have enough fighters for this phrase");
return null;
}
}
gif:
EDIT: Images instead since this editor is annoying
callFighters