diff --git a/src/Point.java b/src/Point.java new file mode 100644 index 0000000..bf2b127 --- /dev/null +++ b/src/Point.java @@ -0,0 +1,45 @@ +public class Point { + private int x; + private int y; + + public Point (int x, int y) { + this.setX(x); + this.setY(y); + } + public Point (String str) { + if (Point.isValidSyntax(str)) { + this.setX(str.charAt(0)); + this.setY(Integer.parseInt(str.substring(1))); + } else { + throw new IllegalArgumentException("String ist keine gültige Koordinate"); + } + } + + @Override + public String toString() { + return (char) ('A' + this.x) + String.valueOf(this.y); + } + + public int getX() { + return x; + } + + public int getY() { + return y; + } + + public void setX(int x) { + this.x = x; + } + + public void setY(int y) { + this.y = y; + } + public void setX(char c) { + this.x = c - 'A'; + } + + public static boolean isValidSyntax(String str) { + return str.matches("^[A-Z]\\d+$"); + } +} diff --git a/src/Ship.java b/src/Ship.java index dd2006d..0c3c7f6 100644 --- a/src/Ship.java +++ b/src/Ship.java @@ -63,21 +63,21 @@ public class Ship { public boolean setPosition(Point pos, List shipsList, int boardSize) { // ueberpruefe boundaries - if (pos.x < 0 || pos.y < 0 || pos.x >= boardSize || pos.y >= boardSize) { + if (pos.getX() < 0 || pos.getY() < 0 || pos.getX() >= boardSize || pos.getY() >= boardSize) { return false; } // bestimme die Endposition anhand der Ausrichtung - int endX = pos.x; - int endY = pos.y; + int endX = pos.getX(); + int endY = pos.getY(); if (this.horizontal) { // rechts links - endX = pos.x + this.size - 1; + endX = pos.getX() + this.size - 1; if (endX >= boardSize) { return false; } } else { // oben unten - endY = pos.y + this.size - 1; + endY = pos.getY() + this.size - 1; if (endY >= boardSize) { return false; } @@ -86,8 +86,8 @@ public class Ship { // Liste an Punkten die das Schiff einnehmen wuerde List 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; + int x = this.horizontal ? pos.getX() + i : pos.getX(); //falls horizontal dann pos.x + i ansonsten pos.x + int y = this.horizontal ? pos.getY() : pos.getY() + i; shipPoints.add(new Point(x, y)); } @@ -123,8 +123,8 @@ public class Ship { 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; + int x = this.horizontal ? this.position.getX() + i : this.position.getX(); + int y = this.horizontal ? this.position.getY() : this.position.getY() + i; points.add(new Point(x, y)); } return points; @@ -135,8 +135,8 @@ public class Ship { } public boolean isShipOnPos(Point pos){ - if ((this.horizontal && pos.y == this.position.y && pos.x >= this.position.x && pos.x < this.position.x + size) || - (!(this.horizontal) && pos.x == this.position.x && pos.y >= this.position.y && pos.y < this.position.y + size)) { + if ((this.horizontal && pos.getY() == this.position.getY() && pos.getX() >= this.position.getX() && pos.getX() < this.position.getX() + size) || + (!(this.horizontal) && pos.getX() == this.position.getX() && pos.getY() >= this.position.getY() && pos.getY() < this.position.getY() + size)) { return true; } return false; diff --git a/src/SpecificAiPlayerMedium.java b/src/SpecificAiPlayerMedium.java index 78cdae0..1676eb4 100644 --- a/src/SpecificAiPlayerMedium.java +++ b/src/SpecificAiPlayerMedium.java @@ -42,8 +42,8 @@ public class SpecificAiPlayerMedium extends AiPlayer{ } private void addAdjacentPoints(Point point) { - int x = point.x; - int y = point.y; + int x = point.getX(); + int y = point.getY(); // Possible adjacent positions (up, down, left, right) Point[] adjacentPoints = { @@ -66,7 +66,7 @@ public class SpecificAiPlayerMedium extends AiPlayer{ } private boolean isValidPoint(Point point) { - return point.x >= 0 && point.x < board.getSize() && - point.y >= 0 && point.y < board.getSize(); + return point.getX() >= 0 && point.getX() < board.getSize() && + point.getY() >= 0 && point.getY() < board.getSize(); } } \ No newline at end of file diff --git a/src/startLocalGame.java b/src/startLocalGame.java index 8dcc513..ff3ca3c 100644 --- a/src/startLocalGame.java +++ b/src/startLocalGame.java @@ -1,5 +1,4 @@ import javax.swing.*; -import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;