Merge pull request 'Schiffe setzen Validierung hinzugefuegt' (#8) from oleFundF into main

Reviewed-on: #8
Reviewed-by: flo <florian.hantzschel@stud.hs-hannover.de>
This commit is contained in:
alexy 2024-11-26 16:00:08 +00:00
commit f3d56b37c7
3 changed files with 76 additions and 12 deletions

View File

@ -8,14 +8,14 @@ public abstract class AiPlayer extends Player {
} }
public Point RandomPoint() { public Point RandomPoint() {
Random random = new Random(); // Pseudo Random für zufallszahlen Random random = new Random(); // Pseudo Random für zufallszahlen
int posx = random.nextInt(super.board.size); // Generiert 0 - 13 int posx = random.nextInt(super.board.getSize()); // Generiert 0 - 13
int posy = random.nextInt(super.board.size); // int posy = random.nextInt(super.board.getSize()); //
return new Point(posx,posy); return new Point(posx,posy);
} }
public void AiSetShips() { public void AiSetShips() {
for(int i = 0; i < super.board.ships.size(); i++) { // Interiert durch alle Shiffe for(int i = 0; i < super.board.getShips().size(); i++) { // Interiert durch alle Shiffe
while(!super.board.ships.get(i).setPosition(RandomPoint())) 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 } // Versucht das Aktuelle Shiff zu setzten und wiederholt solange bis es funktioniert
return; return;
} }

View File

@ -68,7 +68,7 @@ public class Board {
} }
public int getSize() { public int getSize() {
return size; return this.size;
} }
} }

View File

@ -1,10 +1,11 @@
import java.awt.Point; import java.awt.Point;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
record ShipData (int size, String name){ record ShipData (int size, String name){}
}
public class Ship { public class Ship {
static List<List<ShipData>> semeterList = Arrays.asList(Arrays.asList( static List<List<ShipData>> semeterList = Arrays.asList(Arrays.asList(
@ -56,16 +57,79 @@ public class Ship {
public Ship (int size, String name) { public Ship (int size, String name) {
this.size = size; this.size = size;
this.name = name; 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.position = null;
this.hitsOnMe = 0; this.hitsOnMe = 0;
this.sunk = false; this.sunk = false;
} }
public boolean setPosition(Point pos) { public boolean setPosition(Point pos, List<Ship> shipsList, int boardSize) {
//TODO Conte abrfrage ob shif da sein darf // ueberpruefe boundaries
this.position = pos; if (pos.x < 0 || pos.y < 0 || pos.x >= boardSize || pos.y >= boardSize) {
return true; 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<Point> 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<Point> 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<Point> getOccupiedPoints() {
List<Point> 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() { public Point getPosition() {