Schiffe setzen Validierung hinzugefuegt

Florian und Florian haben eine schoene Validierung zum setzen der Position eines Schiffes hinzugefuegt.
This commit is contained in:
FlorianAlexy 2024-11-26 16:43:25 +01:00
parent ec789dd562
commit c8b3f4f2db
3 changed files with 76 additions and 12 deletions

View File

@ -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;
}

View File

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

View File

@ -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<List<ShipData>> 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<Ship> 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<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() {