lucasjoshua #14

Merged
lgc merged 13 commits from lucasjoshua into main 2024-12-15 13:27:15 +00:00
4 changed files with 26 additions and 21 deletions
Showing only changes of commit 88ec23c5e5 - Show all commits

View File

@ -26,12 +26,13 @@ public class BoardDisplay extends JPanel {
this.fields = new JButton[gridSize][gridSize];
this.ships = new ArrayList<>();
this.player = player;
this.gridSize = gridSize;
// Erstellung von Spielfeld
for (int i = 0; i <= gridSize; i++) {
for (int j = 0; j <= gridSize; j++) {
final int x = i; // Temporäre Variable
final int y = j; // Temporäre Variable
final int x = i - 1; // Temporäre Variable
final int y = j - 1; // Temporäre Variable
if (i == 0 && j == 0) {
add(new JLabel(" "));
} else if (i == 0) {
@ -65,13 +66,10 @@ public class BoardDisplay extends JPanel {
// field.setBackground(Color.LIGHT_GRAY);
// }
// });
int finalI = i;
int finalJ = j;
field.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
Point o= new Point(finalI, finalJ);
System.out.println(o);
Point o= new Point(x, y);
handleFieldClick(o);
}
});
@ -148,17 +146,13 @@ public class BoardDisplay extends JPanel {
return true;
}
private void selectShip(MouseEvent e) {
Ship current = (Ship) e.getSource();
}
/**
* TODO Funktion beschreiben etc.
* @param o
*/
private void handleFieldClick(Point o) {
this.currentShip.setPosition(o,player.getBoard().getShips(),gridSize);
if (!this.currentShip.setPosition(o,player.getBoard().getShips(),this.gridSize)) {
}
paintFields();
// Beispiel: Setze ein Schiff bei einem Klick
//if (setShip(new Ship(3, "TestShip"), o, true,player)) {
@ -167,8 +161,8 @@ public class BoardDisplay extends JPanel {
}
public void paintFields() {
for(int i = 0; i <= gridSize; i++) {
for(int j = 0; j <= gridSize; j++) {
for(int i = 0; i < gridSize; i++) {
for(int j = 0; j < gridSize; j++) {
if(fields[i][j] == null) {
continue;
}

View File

@ -42,4 +42,18 @@ public class Point {
public static boolean isValidSyntax(String str) {
return str.matches("^[A-Z]\\d+$");
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || this.getClass() != o.getClass()) return false;
Point p = (Point)o;
return p.getX() == this.getX() && p.getY() == this.getY();
}
public boolean neighbours(Point other) {
if (other == null) return false;
return (int)Math.abs(this.getX() - other.getX()) <= 1 && (int)Math.abs(this.getY() - other.getY()) <= 1;
}
}

View File

@ -105,10 +105,9 @@ public class Ship {
List<Point> otherShipPoints = otherShip.getOccupiedPoints();
// ueberlappen checken
for (Point p : shipPoints) {
if (otherShipPoints.contains(p)) {
// ueberlappen entdeckt
return false;
}
for (Point otherPoint : otherShipPoints) {
if (otherPoint.neighbours(p)) return false;
}
}
}

View File

@ -1,6 +1,4 @@
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ShipButton extends JButton {
Ship ship;
@ -9,7 +7,7 @@ public class ShipButton extends JButton {
super(ship.getName());
this.ship = ship;
this.addActionListener((e)->{
boardDisplay.selectCurrentShip(ship);
boardDisplay.selectCurrentShip(this.ship);
});
}