From c8b3f4f2db0f33273ea9716cf9ff0eb11704b5a7 Mon Sep 17 00:00:00 2001 From: FlorianAlexy Date: Tue, 26 Nov 2024 16:43:25 +0100 Subject: [PATCH] Schiffe setzen Validierung hinzugefuegt Florian und Florian haben eine schoene Validierung zum setzen der Position eines Schiffes hinzugefuegt. --- src/AiPlayer.java | 8 ++--- src/Board.java | 2 +- src/Ship.java | 78 ++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 76 insertions(+), 12 deletions(-) diff --git a/src/AiPlayer.java b/src/AiPlayer.java index 05fc045..ca8c82d 100644 --- a/src/AiPlayer.java +++ b/src/AiPlayer.java @@ -8,14 +8,14 @@ public abstract class AiPlayer extends Player { } 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); // + int posx = random.nextInt(super.board.getSize()); // Generiert 0 - 13 + int posy = random.nextInt(super.board.getSize()); // 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())) + for(int i = 0; i < super.board.getShips().size(); i++) { // Interiert durch alle Shiffe + while(!super.board.getShips().get(i).setPosition(RandomPoint(), super.board.getShips(), super.board.getSize())) {} } // Versucht das Aktuelle Shiff zu setzten und wiederholt solange bis es funktioniert return; } diff --git a/src/Board.java b/src/Board.java index 5cec0cd..ae205a3 100644 --- a/src/Board.java +++ b/src/Board.java @@ -68,7 +68,7 @@ public class Board { } public int getSize() { - return size; + return this.size; } } diff --git a/src/Ship.java b/src/Ship.java index 08db6b6..de6cbff 100644 --- a/src/Ship.java +++ b/src/Ship.java @@ -1,10 +1,11 @@ + + import java.awt.Point; import java.util.ArrayList; import java.util.Arrays; import java.util.List; -record ShipData (int size, String name){ -} +record ShipData (int size, String name){} public class Ship { static List> semeterList = Arrays.asList(Arrays.asList( @@ -56,16 +57,79 @@ public class Ship { public Ship (int size, String name) { this.size = size; this.name = name; - this.horizontal = false; + this.horizontal = false; //true = von Punkt aus nach rechts; false = von Punkt aus nach unten this.position = null; this.hitsOnMe = 0; this.sunk = false; } - public boolean setPosition(Point pos) { - //TODO Conte abrfrage ob shif da sein darf - this.position = pos; - return true; + public boolean setPosition(Point pos, List shipsList, int boardSize) { + // ueberpruefe boundaries + if (pos.x < 0 || pos.y < 0 || pos.x >= boardSize || pos.y >= boardSize) { + return false; + } + + // bestimme die Endposition anhand der Ausrichtung + int endX = pos.x; + int endY = pos.y; + + if (this.horizontal) { // rechts links + endX = pos.x + this.size - 1; + if (endX >= boardSize) { + return false; + } + } else { // oben unten + endY = pos.y + this.size - 1; + if (endY >= boardSize) { + return false; + } + } + + // Liste an Punkten die das Schiff einnehmen wuerde + List shipPoints = new ArrayList<>(); + for (int i = 0; i < this.size; i++) { + int x = this.horizontal ? pos.x + i : pos.x; //falls horizontal dann pos.x + i ansonsten pos.x + int y = this.horizontal ? pos.y : pos.y + i; + shipPoints.add(new Point(x, y)); + } + + // ueberlappen mit anderen Schiffen pruefen + for (Ship otherShip : shipsList) { + // eigenes Schiff ueberspringen + if (otherShip == this) { + continue; + } + // ueberspringe falls noch nicht gesetzt + if (otherShip.position == null) { + continue; + } + // Punkte die das andere Schiff besetzt + List otherShipPoints = otherShip.getOccupiedPoints(); + // ueberlappen checken + for (Point p : shipPoints) { + if (otherShipPoints.contains(p)) { + // ueberlappen entdeckt + return false; + } + } + } + + // kein ueberlappen also setze das Schiff + this.position = pos; + return true; + } + + public List getOccupiedPoints() { + List points = new ArrayList<>(); + if (this.position == null) { + return points; + } + for (int i = 0; i < this.size; i++) { + int x = this.horizontal ? this.position.x + i : this.position.x; + int y = this.horizontal ? this.position.y : this.position.y + i; + points.add(new Point(x, y)); + } + return points; } public Point getPosition() { -- 2.40.1