FloA #19

Merged
lgc merged 3 commits from FloA into main 2024-12-20 15:59:51 +00:00
2 changed files with 54 additions and 16 deletions
Showing only changes of commit 3c03e63899 - Show all commits

View File

@ -5,7 +5,7 @@ public class SpecificAiPlayerHard extends AiPlayer{
private int gridSize;
private boolean[][] shotsFired;
private final ArrayList<int[]> hitQueue;
private final ArrayList<Point> hitQueue;
private final Random random;
private int nextChessRow;
private int nextChessCol;
@ -23,24 +23,21 @@ public class SpecificAiPlayerHard extends AiPlayer{
}
// Checks if a position has already been shot at
public boolean alreadyShot(int row, int col) {
return shotsFired[row][col];
public boolean alreadyShot(Point p) {
return shotsFired[p.getX()][p.getY()];
}
// Generates the next move for the AI
public int[] getNextMove() {
public Point getNextMove() {
if(gridSize == 0) {
this.gridSize = super.board.getSize();
this.shotsFired = new boolean[gridSize][gridSize];
}
// If there are hits to process, prioritize those
while (!hitQueue.isEmpty()) {
int[] target = hitQueue.remove(0);
int row = target[0];
int col = target[1];
Point target = hitQueue.remove(0);
if (!alreadyShot(row, col)) {
shotsFired[row][col] = true;
if (!alreadyShot(target)) {
return target;
}
}
@ -48,7 +45,7 @@ public class SpecificAiPlayerHard extends AiPlayer{
// Otherwise, use chess pattern targeting
int row = nextChessRow;
int col = nextChessCol;
while (alreadyShot(row, col)) {
while (alreadyShot(new Point(row, col))) {
advanceChessPattern();
row = nextChessRow;
col = nextChessCol;
@ -56,7 +53,45 @@ public class SpecificAiPlayerHard extends AiPlayer{
shotsFired[row][col] = true;
advanceChessPattern();
return new int[]{row, col};
return new Point(row, col);
}
@Override
public synchronized void receiveHit(HitResponse hitResponse) {
super.receiveHit(hitResponse);
// If it's a hit or sunk, add adjacent cells to the hitsQueue
if (hitResponse.getHitResponse() == HitResponseType.HIT) {
addAdjacentPoints(hitResponse.getPoint());
}
}
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) && !hitQueue.contains(p)) {
hitQueue.add(p);
}
}
}
private boolean isValidPoint(Point point) {
return point.getX() >= 0 && point.getX() < gridSize &&
point.getY() >= 0 && point.getY() < gridSize;
}
@Override
public void aiShoot() {
this.enemy.receiveShoot(getNextMove());
}
// Advances the chess pattern to the next cell
@ -73,7 +108,7 @@ public class SpecificAiPlayerHard extends AiPlayer{
}
// Adds adjacent cells to the hit queue
public void processHit(int row, int col) {
/* public void processHit(int row, int col) {
if (row > 0 && !alreadyShot(row - 1, col)) {
hitQueue.add(new int[]{row - 1, col});
}
@ -86,5 +121,5 @@ public class SpecificAiPlayerHard extends AiPlayer{
if (col < gridSize - 1 && !alreadyShot(row, col + 1)) {
hitQueue.add(new int[]{row, col + 1});
}
}
}*/
}

View File

@ -15,11 +15,14 @@ public class SpecificAiPlayerMedium extends AiPlayer{
Point nextShot = ComputeNextShot();
// Shoot at the enemy and receive the hit response
enemy.receiveShoot(nextShot);
}
HitResponse hitResponse = enemy.board.getHitResponseOnPoint(nextShot);
@Override
public synchronized void receiveHit(HitResponse hitResponse) {
super.receiveHit(hitResponse);
// If it's a hit or sunk, add adjacent cells to the hitsQueue
if (hitResponse.getHitResponse() == HitResponseType.HIT) {
addAdjacentPoints(nextShot);
addAdjacentPoints(hitResponse.getPoint());
}
}