Network and Frontend #10
|
@ -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+$");
|
||||||
|
}
|
||||||
|
}
|
|
@ -63,21 +63,21 @@ public class Ship {
|
||||||
|
|
||||||
public boolean setPosition(Point pos, List<Ship> shipsList, int boardSize) {
|
public boolean setPosition(Point pos, List<Ship> shipsList, int boardSize) {
|
||||||
// ueberpruefe boundaries
|
// 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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// bestimme die Endposition anhand der Ausrichtung
|
// bestimme die Endposition anhand der Ausrichtung
|
||||||
int endX = pos.x;
|
int endX = pos.getX();
|
||||||
int endY = pos.y;
|
int endY = pos.getY();
|
||||||
|
|
||||||
if (this.horizontal) { // rechts links
|
if (this.horizontal) { // rechts links
|
||||||
endX = pos.x + this.size - 1;
|
endX = pos.getX() + this.size - 1;
|
||||||
if (endX >= boardSize) {
|
if (endX >= boardSize) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
} else { // oben unten
|
} else { // oben unten
|
||||||
endY = pos.y + this.size - 1;
|
endY = pos.getY() + this.size - 1;
|
||||||
if (endY >= boardSize) {
|
if (endY >= boardSize) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -86,8 +86,8 @@ public class Ship {
|
||||||
// Liste an Punkten die das Schiff einnehmen wuerde
|
// Liste an Punkten die das Schiff einnehmen wuerde
|
||||||
List<Point> shipPoints = new ArrayList<>();
|
List<Point> shipPoints = new ArrayList<>();
|
||||||
for (int i = 0; i < this.size; i++) {
|
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 x = this.horizontal ? pos.getX() + i : pos.getX(); //falls horizontal dann pos.x + i ansonsten pos.x
|
||||||
int y = this.horizontal ? pos.y : pos.y + i;
|
int y = this.horizontal ? pos.getY() : pos.getY() + i;
|
||||||
shipPoints.add(new Point(x, y));
|
shipPoints.add(new Point(x, y));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -123,8 +123,8 @@ public class Ship {
|
||||||
return points;
|
return points;
|
||||||
}
|
}
|
||||||
for (int i = 0; i < this.size; i++) {
|
for (int i = 0; i < this.size; i++) {
|
||||||
int x = this.horizontal ? this.position.x + i : this.position.x;
|
int x = this.horizontal ? this.position.getX() + i : this.position.getX();
|
||||||
int y = this.horizontal ? this.position.y : this.position.y + i;
|
int y = this.horizontal ? this.position.getY() : this.position.getY() + i;
|
||||||
points.add(new Point(x, y));
|
points.add(new Point(x, y));
|
||||||
}
|
}
|
||||||
return points;
|
return points;
|
||||||
|
@ -135,8 +135,8 @@ public class Ship {
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isShipOnPos(Point pos){
|
public boolean isShipOnPos(Point pos){
|
||||||
if ((this.horizontal && pos.y == this.position.y && pos.x >= this.position.x && pos.x < this.position.x + size) ||
|
if ((this.horizontal && pos.getY() == this.position.getY() && pos.getX() >= this.position.getX() && pos.getX() < this.position.getX() + size) ||
|
||||||
(!(this.horizontal) && pos.x == this.position.x && pos.y >= this.position.y && pos.y < this.position.y + size)) {
|
(!(this.horizontal) && pos.getX() == this.position.getX() && pos.getY() >= this.position.getY() && pos.getY() < this.position.getY() + size)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -42,8 +42,8 @@ public class SpecificAiPlayerMedium extends AiPlayer{
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addAdjacentPoints(Point point) {
|
private void addAdjacentPoints(Point point) {
|
||||||
int x = point.x;
|
int x = point.getX();
|
||||||
int y = point.y;
|
int y = point.getY();
|
||||||
|
|
||||||
// Possible adjacent positions (up, down, left, right)
|
// Possible adjacent positions (up, down, left, right)
|
||||||
Point[] adjacentPoints = {
|
Point[] adjacentPoints = {
|
||||||
|
@ -66,7 +66,7 @@ public class SpecificAiPlayerMedium extends AiPlayer{
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isValidPoint(Point point) {
|
private boolean isValidPoint(Point point) {
|
||||||
return point.x >= 0 && point.x < board.getSize() &&
|
return point.getX() >= 0 && point.getX() < board.getSize() &&
|
||||||
point.y >= 0 && point.y < board.getSize();
|
point.getY() >= 0 && point.getY() < board.getSize();
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,5 +1,4 @@
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
|
||||||
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionEvent;
|
||||||
import java.awt.event.ActionListener;
|
import java.awt.event.ActionListener;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue