Easy/MediumAI
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&
This commit is contained in:
parent
ce4995fb94
commit
aaa9d57f68
|
@ -1,7 +1,27 @@
|
||||||
|
import java.util.Random;
|
||||||
|
import java.awt.Point;
|
||||||
|
|
||||||
public abstract class AiPlayer extends Player {
|
public abstract class AiPlayer extends Player {
|
||||||
|
|
||||||
public AiPlayer(int size) {
|
public AiPlayer(int size) {
|
||||||
super(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;
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -67,6 +67,9 @@ public class Board {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getSize() {
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
|
||||||
|
public class SpecificAiPlayer1 extends AiPlayer{
|
||||||
|
|
||||||
|
public SpecificAiPlayer1(int size) {
|
||||||
|
super(size);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,65 @@
|
||||||
|
|
||||||
|
public class SpecificAiPlayerMedium extends AiPlayer{
|
||||||
|
|
||||||
|
private List<Point> 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();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue