FloA #19
|
@ -5,7 +5,7 @@ public class SpecificAiPlayerHard extends AiPlayer{
|
||||||
|
|
||||||
private int gridSize;
|
private int gridSize;
|
||||||
private boolean[][] shotsFired;
|
private boolean[][] shotsFired;
|
||||||
private final ArrayList<int[]> hitQueue;
|
private final ArrayList<Point> hitQueue;
|
||||||
private final Random random;
|
private final Random random;
|
||||||
private int nextChessRow;
|
private int nextChessRow;
|
||||||
private int nextChessCol;
|
private int nextChessCol;
|
||||||
|
@ -23,24 +23,21 @@ public class SpecificAiPlayerHard extends AiPlayer{
|
||||||
}
|
}
|
||||||
|
|
||||||
// Checks if a position has already been shot at
|
// Checks if a position has already been shot at
|
||||||
public boolean alreadyShot(int row, int col) {
|
public boolean alreadyShot(Point p) {
|
||||||
return shotsFired[row][col];
|
return shotsFired[p.getX()][p.getY()];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generates the next move for the AI
|
// Generates the next move for the AI
|
||||||
public int[] getNextMove() {
|
public Point getNextMove() {
|
||||||
if(gridSize == 0) {
|
if(gridSize == 0) {
|
||||||
this.gridSize = super.board.getSize();
|
this.gridSize = super.board.getSize();
|
||||||
this.shotsFired = new boolean[gridSize][gridSize];
|
this.shotsFired = new boolean[gridSize][gridSize];
|
||||||
}
|
}
|
||||||
// If there are hits to process, prioritize those
|
// If there are hits to process, prioritize those
|
||||||
while (!hitQueue.isEmpty()) {
|
while (!hitQueue.isEmpty()) {
|
||||||
int[] target = hitQueue.remove(0);
|
Point target = hitQueue.remove(0);
|
||||||
int row = target[0];
|
|
||||||
int col = target[1];
|
|
||||||
|
|
||||||
if (!alreadyShot(row, col)) {
|
if (!alreadyShot(target)) {
|
||||||
shotsFired[row][col] = true;
|
|
||||||
return target;
|
return target;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -48,7 +45,7 @@ public class SpecificAiPlayerHard extends AiPlayer{
|
||||||
// Otherwise, use chess pattern targeting
|
// Otherwise, use chess pattern targeting
|
||||||
int row = nextChessRow;
|
int row = nextChessRow;
|
||||||
int col = nextChessCol;
|
int col = nextChessCol;
|
||||||
while (alreadyShot(row, col)) {
|
while (alreadyShot(new Point(row, col))) {
|
||||||
advanceChessPattern();
|
advanceChessPattern();
|
||||||
row = nextChessRow;
|
row = nextChessRow;
|
||||||
col = nextChessCol;
|
col = nextChessCol;
|
||||||
|
@ -56,7 +53,45 @@ public class SpecificAiPlayerHard extends AiPlayer{
|
||||||
|
|
||||||
shotsFired[row][col] = true;
|
shotsFired[row][col] = true;
|
||||||
advanceChessPattern();
|
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
|
// Advances the chess pattern to the next cell
|
||||||
|
@ -73,7 +108,7 @@ public class SpecificAiPlayerHard extends AiPlayer{
|
||||||
}
|
}
|
||||||
|
|
||||||
// Adds adjacent cells to the hit queue
|
// 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)) {
|
if (row > 0 && !alreadyShot(row - 1, col)) {
|
||||||
hitQueue.add(new int[]{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)) {
|
if (col < gridSize - 1 && !alreadyShot(row, col + 1)) {
|
||||||
hitQueue.add(new int[]{row, col + 1});
|
hitQueue.add(new int[]{row, col + 1});
|
||||||
}
|
}
|
||||||
}
|
}*/
|
||||||
}
|
}
|
|
@ -15,11 +15,14 @@ public class SpecificAiPlayerMedium extends AiPlayer{
|
||||||
Point nextShot = ComputeNextShot();
|
Point nextShot = ComputeNextShot();
|
||||||
// Shoot at the enemy and receive the hit response
|
// Shoot at the enemy and receive the hit response
|
||||||
enemy.receiveShoot(nextShot);
|
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 it's a hit or sunk, add adjacent cells to the hitsQueue
|
||||||
if (hitResponse.getHitResponse() == HitResponseType.HIT) {
|
if (hitResponse.getHitResponse() == HitResponseType.HIT) {
|
||||||
addAdjacentPoints(nextShot);
|
addAdjacentPoints(hitResponse.getPoint());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue