68 lines
2.0 KiB
Java
68 lines
2.0 KiB
Java
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
public class SpecificAiPlayerMedium extends AiPlayer{
|
|
|
|
private List<Point> hitsQueue = new ArrayList<>();
|
|
|
|
@Override
|
|
public void AiShoot() {
|
|
Point nextShot = ComputeNextShot();
|
|
// Shoot at the enemy and receive the hit response
|
|
enemy.receiveShoot(nextShot);
|
|
|
|
HitResponse hitResponse = enemy.board.getHitResponsOnPoint(nextShot);
|
|
// If it's a hit or sunk, add adjacent cells to the hitsQueue
|
|
if (hitResponse.getHitResponse() == HitResponseType.HIT) {
|
|
addAdjacentPoints(nextShot);
|
|
}
|
|
}
|
|
|
|
public Point ComputeNextShot() {
|
|
Point nextShot;
|
|
|
|
// Prioritize shots from the hitsQueue
|
|
while (!hitsQueue.isEmpty()) {
|
|
nextShot = hitsQueue.remove(0);
|
|
if (!alreadyShot(nextShot)) {
|
|
return nextShot;
|
|
}
|
|
}
|
|
|
|
// If hitsQueue is empty, pick a random point that hasn't been shot
|
|
do {
|
|
nextShot = RandomPoint();
|
|
} while (alreadyShot(nextShot));
|
|
|
|
return nextShot;
|
|
}
|
|
|
|
private void addAdjacentPoints(Point point) {
|
|
int x = point.getX();
|
|
int y = point.getY();
|
|
|
|
// Possible adjacent positions (up, down, left, right)
|
|
Point[] adjacentPoints = {
|
|
new Point(x, y - 1),
|
|
new Point(x, y + 1),
|
|
new Point(x - 1, y),
|
|
new Point(x + 1, y)
|
|
};
|
|
|
|
for (Point p : adjacentPoints) {
|
|
if (isValidPoint(p) && !alreadyShot(p) && !hitsQueue.contains(p)) {
|
|
hitsQueue.add(p);
|
|
}
|
|
}
|
|
}
|
|
|
|
private boolean alreadyShot(Point p) {
|
|
// TODO Auto-generated method stub
|
|
throw new UnsupportedOperationException("Unimplemented method 'alreadyShot'");
|
|
}
|
|
|
|
private boolean isValidPoint(Point point) {
|
|
return point.getX() >= 0 && point.getX() < board.getSize() &&
|
|
point.getY() >= 0 && point.getY() < board.getSize();
|
|
}
|
|
} |