diff --git a/src/BoardDisplay.java b/src/BoardDisplay.java index c1db6b4..99f24c1 100644 --- a/src/BoardDisplay.java +++ b/src/BoardDisplay.java @@ -13,11 +13,12 @@ public class BoardDisplay extends JPanel { public BoardDisplay(int gridSize, Player player) { super(new GridLayout(gridSize + 1, gridSize + 1)); // +1 wegen extra Zeile/Splate this.fields = new JButton[gridSize][gridSize]; + this.ships = new ArrayList<>(); // 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; // Temporäre Variable + // final int y = j; // Temporäre Variable if (i == 0 && j == 0) { add(new JLabel(" ")); } else if (i == 0) { @@ -57,6 +58,7 @@ public class BoardDisplay extends JPanel { @Override public void mouseClicked(MouseEvent e) { Point o= new Point(finalI, finalJ); + System.out.println(o); handleFieldClick(field, o,player); } }); @@ -70,12 +72,15 @@ public class BoardDisplay extends JPanel { private boolean setShip(Ship ship, Point o, boolean horizontal,Player player) { //boolean a = true; if (placeable(ship, ship.getPosition(), horizontal)) { - ship.setPosition(new Point(o.getX(),o.getY()),player.getBoard().getShips(),gridSize); + // ship.setPosition(new Point(o.getX(), o.getY()), player.getBoard().getShips(), gridSize); + ship.setPosition(o, player.getBoard().getShips(), gridSize); + + ship.setHorizontal(horizontal); ships.add(ship); List occupied = ship.getOccupiedPoints(); for(Point p: occupied) { - fields[p.getX()][p.getY()].setBackground(Color.LIGHT_GRAY); + fields[(int) p.getX()][(int)p.getY()].setBackground(Color.LIGHT_GRAY); } return true; }else{ @@ -83,16 +88,39 @@ public class BoardDisplay extends JPanel { } } - private boolean placeable(Ship ship,Point o, boolean horizontal) { - if (horizontal && (o.getX() + ship.getSize() > gridSize)) { + /*private boolean placeable(Ship ship,Point o, boolean horizontal) { + if (horizontal && (o.getX() + ship.getSize() > gridSize)) { // Fehler return false; } if (!horizontal && (o.getY() + ship.getSize() > gridSize)) { return false; } + return true; + }*/ + + private boolean placeable(Ship ship, Point o, boolean horizontal) { + if (horizontal && (o.getX() + ship.getSize() > gridSize)) { + return false; + } + if (!horizontal && (o.getY() + ship.getSize() > gridSize)) { + return false; + } + + // Prüfen auf Kollision mit bestehenden Schiffen + for (Ship other : ships) { + for (Point p : other.getOccupiedPoints()) { + for (Point newP : ship.getOccupiedPoints()) { + if (p.equals(newP)) { + return false; // Überschneidung gefunden + } + } + } + } + return true; } + private void selectShip(MouseEvent e) { Ship current = (Ship) e.getSource(); }