OOP_A02_Project/OOP_A2_Project/src/backend/Agent.java

36 lines
838 B
Java

package backend;
import java.awt.Color;
public abstract class Agent {
protected int x;
protected int y;
protected World world;
public Agent(int x, int y, World world) {
this.x = x;
this.y = y;
this.world = world;
}
public abstract void step();
public int getX() {
return x;
}
public int getY() {
return y;
}
public abstract Color getDisplayColor();
protected int[] getRandomAdjacentPosition() {
int newX = x + (int)(Math.random() * 3) - 1; // -1, 0, or 1
int newY = y + (int)(Math.random() * 3) - 1; // -1, 0, or 1
newX = Math.max(0, Math.min(world.getCols() - 1, newX));
newY = Math.max(0, Math.min(world.getRows() - 1, newY));
return new int[]{newX, newY};
}
}