From aaa9d57f6842d18471e65819fa29eca684320bf1 Mon Sep 17 00:00:00 2001 From: eyFlorian <45431375+eyFlorian@users.noreply.github.com> Date: Sun, 24 Nov 2024 12:47:45 +0100 Subject: [PATCH] Easy/MediumAI MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Haben eine sehr einfache und Medium KI hinzugefügt. F&F Fragenaufwerfende Methoden: https://cdn.discordapp.com/attachments/1296860613212897413/1310209916325068894/WhatsApp_Bild_2024-11-24_um_12.31.02_fb836c3d.jpg?ex=67446392&is=67431212&hm=82f37b83dc5e711622abb43abe51edd62833450dddf91ed524991da6d0981f38& --- src/AiPlayer.java | 28 ++++++-- src/Board.java | 3 + ...Player1.java => SpecificAiPlayerEasy.java} | 0 src/SpecificAiPlayerHard.java | 7 ++ src/SpecificAiPlayerMedium.java | 65 +++++++++++++++++++ 5 files changed, 99 insertions(+), 4 deletions(-) rename src/{SpecificAiPlayer1.java => SpecificAiPlayerEasy.java} (100%) create mode 100644 src/SpecificAiPlayerHard.java create mode 100644 src/SpecificAiPlayerMedium.java 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