Network and Frontend #10

Merged
lgc merged 29 commits from ole into main 2024-12-03 12:13:28 +00:00
4 changed files with 60 additions and 16 deletions
Showing only changes of commit 058d069ff2 - Show all commits

45
src/Point.java Normal file
View File

@ -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+$");
}
}

View File

@ -63,21 +63,21 @@ public class Ship {
public boolean setPosition(Point pos, List<Ship> 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<Point> 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;

View File

@ -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();
}
}

View File

@ -1,5 +1,4 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;