start game stuff - both online and offline #13

Merged
lgc merged 24 commits from start-game into main 2024-12-13 16:27:43 +00:00
1 changed files with 34 additions and 6 deletions
Showing only changes of commit f397803b64 - Show all commits

View File

@ -13,11 +13,12 @@ public class BoardDisplay extends JPanel {
public BoardDisplay(int gridSize, Player player) { public BoardDisplay(int gridSize, Player player) {
super(new GridLayout(gridSize + 1, gridSize + 1)); // +1 wegen extra Zeile/Splate super(new GridLayout(gridSize + 1, gridSize + 1)); // +1 wegen extra Zeile/Splate
this.fields = new JButton[gridSize][gridSize]; this.fields = new JButton[gridSize][gridSize];
this.ships = new ArrayList<>();
// Erstellung von Spielfeld // Erstellung von Spielfeld
for (int i = 0; i <= gridSize; i++) { for (int i = 0; i <= gridSize; i++) {
for (int j = 0; j <= gridSize; j++) { for (int j = 0; j <= gridSize; j++) {
final int x = i; // Temporäre Variable //final int x = i; // Temporäre Variable
final int y = j; // Temporäre Variable // final int y = j; // Temporäre Variable
if (i == 0 && j == 0) { if (i == 0 && j == 0) {
add(new JLabel(" ")); add(new JLabel(" "));
} else if (i == 0) { } else if (i == 0) {
@ -57,6 +58,7 @@ public class BoardDisplay extends JPanel {
@Override @Override
public void mouseClicked(MouseEvent e) { public void mouseClicked(MouseEvent e) {
Point o= new Point(finalI, finalJ); Point o= new Point(finalI, finalJ);
System.out.println(o);
handleFieldClick(field, o,player); handleFieldClick(field, o,player);
} }
}); });
@ -70,12 +72,15 @@ public class BoardDisplay extends JPanel {
private boolean setShip(Ship ship, Point o, boolean horizontal,Player player) { private boolean setShip(Ship ship, Point o, boolean horizontal,Player player) {
//boolean a = true; //boolean a = true;
if (placeable(ship, ship.getPosition(), horizontal)) { 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); ship.setHorizontal(horizontal);
ships.add(ship); ships.add(ship);
List<Point> occupied = ship.getOccupiedPoints(); List<Point> occupied = ship.getOccupiedPoints();
for(Point p: occupied) { 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; return true;
}else{ }else{
@ -83,6 +88,16 @@ public class BoardDisplay extends JPanel {
} }
} }
/*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) { private boolean placeable(Ship ship, Point o, boolean horizontal) {
if (horizontal && (o.getX() + ship.getSize() > gridSize)) { if (horizontal && (o.getX() + ship.getSize() > gridSize)) {
return false; return false;
@ -90,9 +105,22 @@ public class BoardDisplay extends JPanel {
if (!horizontal && (o.getY() + ship.getSize() > gridSize)) { if (!horizontal && (o.getY() + ship.getSize() > gridSize)) {
return false; 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; return true;
} }
private void selectShip(MouseEvent e) { private void selectShip(MouseEvent e) {
Ship current = (Ship) e.getSource(); Ship current = (Ship) e.getSource();
} }