diff --git a/src/AiPlayer.java b/src/AiPlayer.java index 06db3f0..05fc045 100644 --- a/src/AiPlayer.java +++ b/src/AiPlayer.java @@ -1,7 +1,27 @@ +import java.util.Random; +import java.awt.Point; public abstract class AiPlayer extends Player { - public AiPlayer(int size) { - super(size); - } -} + public AiPlayer(int size) { + super(size); + } + public Point RandomPoint() { + Random random = new Random(); // Pseudo Random für zufallszahlen + int posx = random.nextInt(super.board.size); // Generiert 0 - 13 + int posy = random.nextInt(super.board.size); // + return new Point(posx,posy); + } + + public void AiSetShips() { + for(int i = 0; i < super.board.ships.size(); i++) { // Interiert durch alle Shiffe + while(!super.board.ships.get(i).setPosition(RandomPoint())) + } // Versucht das Aktuelle Shiff zu setzten und wiederholt solange bis es funktioniert + return; + } + + public void AiShoot() { + super.board.hit(RandomPoint()); + return; + } +} \ No newline at end of file diff --git a/src/Board.java b/src/Board.java index c2c1112..5cec0cd 100644 --- a/src/Board.java +++ b/src/Board.java @@ -67,6 +67,9 @@ public class Board { return null; } + public int getSize() { + return size; + } } diff --git a/src/SpecificAiPlayer1.java b/src/SpecificAiPlayerEasy.java similarity index 100% rename from src/SpecificAiPlayer1.java rename to src/SpecificAiPlayerEasy.java diff --git a/src/SpecificAiPlayerHard.java b/src/SpecificAiPlayerHard.java new file mode 100644 index 0000000..0bcce8c --- /dev/null +++ b/src/SpecificAiPlayerHard.java @@ -0,0 +1,7 @@ + +public class SpecificAiPlayer1 extends AiPlayer{ + + public SpecificAiPlayer1(int size) { + super(size); + } +} diff --git a/src/SpecificAiPlayerMedium.java b/src/SpecificAiPlayerMedium.java new file mode 100644 index 0000000..b808ff9 --- /dev/null +++ b/src/SpecificAiPlayerMedium.java @@ -0,0 +1,65 @@ + +public class SpecificAiPlayerMedium extends AiPlayer{ + + private List hitsQueue = new ArrayList<>(); + + public SpecificAiPlayerMedium(int size) { + super(size); + } + + @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.x; + int y = point.y; + + // 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 isValidPoint(Point point) { + return point.x >= 0 && point.x < board.getSize() && + point.y >= 0 && point.y < board.getSize(); + } +} \ No newline at end of file