I am working on a function in a object that takes in a List of objects as a parameter and clone its contents into its own List. Modifying the new List should not affect the List that was passed in. I know the List would be passed by reference, but would the objects within the list be passed by reference or value? (sorry if this sounds dumb)
I am passing in a list of pieces (pawn, rook etc.) that extends a Piece class. I was thinking of creating a clonePiece() function in the Piece class but I don't know how to go about doing it. This is what I have so far:
public void copyPieces(List<Piece> whitePieces, List<Piece> blackPieces){
for (int i = 0; i < whitePieces.size(); i++){
this.whitePieces.add(whitePieces.get(i).clonePiece());
}
for (int i = 0; i < blackPieces.size(); i++){
this.whitePieces.add(blackPieces.get(i).clonePiece());
}
How would you go about implementing a clonePiece() function in an abstract class that creates a new instance of its inherited classes?
edit:
public abstract class Piece {
private int color;
private int x;
private int y;
public Piece (int color, int x, int y){
this.color = color;
this.y = y;
this.x = x;
}
public int getColor(){
return this.color;
}
public int getX(){
return this.x;
}
public int getY(){
return this.y;
}
public void move(int x, int y, Board board){
board.getGameTiles()[this.x][this.y].setToUnoccupied();
this.x = x;
this.y = y;
}
public abstract ArrayList<Move> getMoves(Board board);
public Piece clonePiece(){
return this;
}
}
public class Rook extends Piece{
int x, y, color;
private ArrayList<Move> moves;
public Rook(int color, int x, int y) {
super(color, x, y);
this.x = x;
this.y = y;
this.color = color;
moves = new ArrayList<>();
}
@Override
public ArrayList<Move> getMoves(Board board) {
//moves right
int a = 1;
while(UtilFunctions.isInBoundaries(x+a, y)){
if(!board.getGameTiles()[x+a][y].isTileOccupied()){
//add move type 0 for passive move
moves.add(new Move(x, y, x+a, y, 0));
}
else{
if(board.getGameTiles()[x+a][y].getPiece().getColor() != this.color){
//add move type 1 for attack move
moves.add(new Move(x, y, x+a, y, 1));
}
break;
}
a++;
}
//moves left
a = -1;
while(UtilFunctions.isInBoundaries(x+a, y)){
if(!board.getGameTiles()[x+a][y].isTileOccupied()){
//add move type 0 for passive move
moves.add(new Move(x, y, x+a, y, 0));
}
else{
if(board.getGameTiles()[x+a][y].getPiece().getColor() != this.color){
//add move type 1 for attack move
moves.add(new Move(x, y, x+a, y, 1));
}
break;
}
a++;
}
//moves up
a = 1;
while(UtilFunctions.isInBoundaries(x, y+a)){
if(!board.getGameTiles()[x][y+a].isTileOccupied()){
//add move type 0 for passive move
moves.add(new Move(x, y, x, y+a, 0));
}
else{
if(board.getGameTiles()[x][y+a].getPiece().getColor() != this.color){
//add move type 1 for attack move
moves.add(new Move(x, y, x, y+a, 1));
}
break;
}
a++;
}
//moves down
a = -1;
while(UtilFunctions.isInBoundaries(x, y+a)){
if(!board.getGameTiles()[x][y+a].isTileOccupied()){
//add move type 0 for passive move
moves.add(new Move(x, y, x, y+a, 0));
}
else{
if(board.getGameTiles()[x][y+a].getPiece().getColor() != this.color){
//add move type 1 for attack move
moves.add(new Move(x, y, x, y+a, 1));
}
break;
}
a++;
}
return moves;
}
}