I am writing a program for my IB Math internal assessment.
I have the classes Calc, Gamble, Inventory, Player, and Test. I have a field instance variable Players (instantiated in the constructor) and the method playrounds and that is where the problem is. The source for the method is below:
ArrayList<Player> Players;
public ArrayList<ArrayList<ArrayList<Player>>> playrounds(boolean randombets, double initialmoney, double initialbet,
double betrange, int numberofrounds){
ArrayList<Player> winningPlayers = new ArrayList<>(0);
ArrayList<ArrayList<Player>> m = new ArrayList<>(0);
for(int b = 0; b < numberofrounds; b++){
if(firstround){
firstround = false;
preparePlayersfirst(randombets, initialmoney, initialbet, betrange);
winningPlayers.clear();
winningPlayers.trimToSize();
m.clear();
m.trimToSize();
}
else{
preparePlayersnext(randombets, initialbet, betrange);
}
for(int a = 0; a < playingPlayers.size(); a++){
Player u = playingPlayers.get(a);
u.rolldice();
u.checkdice();
Players.set(a, u);
playingPlayers.set(a, u);
}
Player t = getwinningPlayer();
for(int a = 0; a < playingPlayers.size(); a++){
Player u = playingPlayers.get(a);
u.setprofits(t, this);
Players.set(a, u);
playingPlayers.set(a, u);
}
distributegainslosses();
removebankruptPlayers();
winningPlayers.add(t);
m.add(Players);
for(int x = 0; x < m.size(); x++){
for(Player y: m.get(x)){
for(int z = 0; z < y.getdice().size(); z++){
System.out.print(y.getdie(z) + " ");
}
System.out.print("\t");
}
System.out.println();
}
System.out.println();
}
ArrayList<ArrayList<ArrayList<Player>>> results = new ArrayList<>(0);
ArrayList<ArrayList<Player>> t = new ArrayList<>(0);
t.add(winningPlayers);
results.add(t);
results.add(m);
return results;
}
The for loop to print the values inside the variable m gives an output like this:
2 3
6 4
6 4
5 5
5 5
5 5
6 1
6 1
6 1
6 1
The expected output is something like this:
2 3
2 3
6 4
2 3
6 4
5 5
2 3
6 4
5 5
6 1
I know that the way I print the values is not wrong. It doesn't print duplicates.
The if(firstround) and the else does not have to do with the problem.
Any help would be greatly appreciated.
-Edit-
@NPE: I tried one of the solutions from the place you suggested to look at. It still doesn't work. Here's the code for what I did:
public ArrayList<Player> clonePlayers(){
ArrayList<Player> clone = new ArrayList<>(0);
for(Player a: Players){
clone.add(new Player(a));
}
return clone;
}
Did I do something wrong?