lucasjoshua #14
|
@ -26,12 +26,13 @@ public class BoardDisplay extends JPanel {
|
||||||
this.fields = new JButton[gridSize][gridSize];
|
this.fields = new JButton[gridSize][gridSize];
|
||||||
this.ships = new ArrayList<>();
|
this.ships = new ArrayList<>();
|
||||||
this.player = player;
|
this.player = player;
|
||||||
|
this.gridSize = gridSize;
|
||||||
|
|
||||||
// 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 - 1; // Temporäre Variable
|
||||||
final int y = j; // Temporäre Variable
|
final int y = j - 1; // 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) {
|
||||||
|
@ -65,13 +66,10 @@ public class BoardDisplay extends JPanel {
|
||||||
// field.setBackground(Color.LIGHT_GRAY);
|
// field.setBackground(Color.LIGHT_GRAY);
|
||||||
// }
|
// }
|
||||||
// });
|
// });
|
||||||
int finalI = i;
|
|
||||||
int finalJ = j;
|
|
||||||
field.addMouseListener(new MouseAdapter() {
|
field.addMouseListener(new MouseAdapter() {
|
||||||
@Override
|
@Override
|
||||||
public void mouseClicked(MouseEvent e) {
|
public void mouseClicked(MouseEvent e) {
|
||||||
Point o= new Point(finalI, finalJ);
|
Point o= new Point(x, y);
|
||||||
System.out.println(o);
|
|
||||||
handleFieldClick(o);
|
handleFieldClick(o);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -148,17 +146,13 @@ public class BoardDisplay extends JPanel {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void selectShip(MouseEvent e) {
|
|
||||||
Ship current = (Ship) e.getSource();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO Funktion beschreiben etc.
|
* TODO Funktion beschreiben etc.
|
||||||
* @param o
|
* @param o
|
||||||
*/
|
*/
|
||||||
private void handleFieldClick(Point 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();
|
paintFields();
|
||||||
// Beispiel: Setze ein Schiff bei einem Klick
|
// Beispiel: Setze ein Schiff bei einem Klick
|
||||||
//if (setShip(new Ship(3, "TestShip"), o, true,player)) {
|
//if (setShip(new Ship(3, "TestShip"), o, true,player)) {
|
||||||
|
@ -167,8 +161,8 @@ public class BoardDisplay extends JPanel {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void paintFields() {
|
public void paintFields() {
|
||||||
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++) {
|
||||||
if(fields[i][j] == null) {
|
if(fields[i][j] == null) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,4 +42,18 @@ public class Point {
|
||||||
public static boolean isValidSyntax(String str) {
|
public static boolean isValidSyntax(String str) {
|
||||||
return str.matches("^[A-Z]\\d+$");
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -105,9 +105,8 @@ public class Ship {
|
||||||
List<Point> otherShipPoints = otherShip.getOccupiedPoints();
|
List<Point> otherShipPoints = otherShip.getOccupiedPoints();
|
||||||
// ueberlappen checken
|
// ueberlappen checken
|
||||||
for (Point p : shipPoints) {
|
for (Point p : shipPoints) {
|
||||||
if (otherShipPoints.contains(p)) {
|
for (Point otherPoint : otherShipPoints) {
|
||||||
// ueberlappen entdeckt
|
if (otherPoint.neighbours(p)) return false;
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,4 @@
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.event.ActionEvent;
|
|
||||||
import java.awt.event.ActionListener;
|
|
||||||
|
|
||||||
public class ShipButton extends JButton {
|
public class ShipButton extends JButton {
|
||||||
Ship ship;
|
Ship ship;
|
||||||
|
@ -9,7 +7,7 @@ public class ShipButton extends JButton {
|
||||||
super(ship.getName());
|
super(ship.getName());
|
||||||
this.ship = ship;
|
this.ship = ship;
|
||||||
this.addActionListener((e)->{
|
this.addActionListener((e)->{
|
||||||
boardDisplay.selectCurrentShip(ship);
|
boardDisplay.selectCurrentShip(this.ship);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue