pieces instance is a List that contains class Piece. Piece object contains two instance variables that resemble coordinate int x and int y. However, when I tried these methods, the second method does not return true if the parameter piece is already inside the pieces object. I have generated an equal method on the class Piece. not sure why the second method does not work.
public boolean alreadyContainsCoordinate1(Piece piece) {
for (int i = 0; i < getLength(); i++) {
if (pieces.get(i).getX() == piece.getX() && pieces.get(i).getY() == piece.getY()) {
return true;
}
}
return false;
}
public boolean alreadyContainsThisCoordinate2(Piece piece) {
for (Piece body : pieces) {
if (body.equals(piece)) {
return true;
}
}
return false;
}