Last objected added to the ArrayList overrides all elements in the ArrayList and I can't figure it out why.. even after reading all problem related topics on web.
I create new object each iteration? and all fields (move, board, color) are protected (I can't change that since I'm using a chessboard framework).
I haven't found the solution on web because my fields ain't static and I also create a new object after each iteration.
public String solve(String fen) {
Chessboard cb = Chessboard.getChessboardFromFEN(fen);
return solve(cb);
}
private String solve(Chessboard cb){
ArrayList<Chessboard> prioritetna = new ArrayList<>();
for(int i = 0; i < 10; i++){
cb.makeMove(cb.getMoves().get(i));
prioritetna.add(new Chessboard(cb.getBoard(), cb.getColor(),
cb.getMovesLeft()));
cb.reverseMove(cb.getMoves().get(i));
}
return "";
}
And I use this framework that is made of three classes for Chessboard implementation: 1. Chessboard: https://pastebin.com/CqgHLCXM 2. Move: https://pastebin.com/g2wr16TP 3. Rules: https://pastebin.com/HEznyECY
I've also rewrote code with no framework dependencies and it works.. so i guess this is a framework problem.
Framework is also available as a JAR: https://ufile.io/c7n3o
public static void main(String[] args) {
ArrayList<Object> list = new ArrayList<>();
Random rand = new Random();
for(int i = 0; i < 5; i++){
list.add(new Object(rand.nextInt(50) + 1, rand.nextInt(50) + 1));
}
for(int i = 0; i < 5; i++){
System.out.println(list.get(i).getOne());
}
}
public class Object {
protected int one;
protected int two;
public Object(int one, int two){
this.one = one;
this.two = two;
}
public int getOne() {
return one;
}
public void setOne(int one) {
this.one = one;
}
public int getTwo() {
return two;
}
public void setTwo(int two) {
this.two = two;
}
}