Within your method shift
there are several problems
public Coordinate shift(int dx, int dy) {
pos = pos.shift(this.x + dx, this.y + dy);
}
- This method refers to a variable pos, pos has not been declared and so does not exist. It also seems to serve no purpose as all the co-ordinate shifting can be done without it.
- Method
shift
has no return statement, meaning that no newCoordinate
is returned - If
pos
was a variable of typeCoordinate
then shift would call pos.shift would call pos.shift would call pos.shift etc forever until a StackOverflowException occured
A corrected version of this method is
public Coordinate shift(int dx, int dy) {
return new Coordinate(this.x + dx, this.y + dy);
}
Note how:
- A new
Coordinate
is created using the constructor - This new
Coordinate
is passed out of the method
Method checkBoundaries(int xSize, int ySize)
This method does not impact the specific problem mentioned. It doesn't however behave how you think. 0|1 is a bitwise operator, it does not give multiple options for a condition. What you wanted was several conditions seperated by the logical operator ||
public boolean checkBoundaries(int xSize, int ySize) {
boolean check;
if ((xSize==0 || xSize ==1 || xSize ==2) && (ySize==0 || ySize ==1 || ySize ==2)) {
check = true;
} else {
check = false;
}
return check;
}