Compare commits
No commits in common. "a381c8d6602dd65fc24891dd51448f1afb10b642" and "d00e12e72ff06b91069bff5029dcb201210df3f0" have entirely different histories.
a381c8d660
...
d00e12e72f
|
@ -8,14 +8,14 @@ public abstract class AiPlayer extends Player {
|
||||||
}
|
}
|
||||||
public Point RandomPoint() {
|
public Point RandomPoint() {
|
||||||
Random random = new Random(); // Pseudo Random für zufallszahlen
|
Random random = new Random(); // Pseudo Random für zufallszahlen
|
||||||
int posx = random.nextInt(super.board.getSize()); // Generiert 0 - 13
|
int posx = random.nextInt(super.board.size); // Generiert 0 - 13
|
||||||
int posy = random.nextInt(super.board.getSize()); //
|
int posy = random.nextInt(super.board.size); //
|
||||||
return new Point(posx,posy);
|
return new Point(posx,posy);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AiSetShips() {
|
public void AiSetShips() {
|
||||||
for(int i = 0; i < super.board.getShips().size(); i++) { // Interiert durch alle Shiffe
|
for(int i = 0; i < super.board.ships.size(); i++) { // Interiert durch alle Shiffe
|
||||||
while(!super.board.getShips().get(i).setPosition(RandomPoint(), super.board.getShips(), super.board.getSize())) {}
|
while(!super.board.ships.get(i).setPosition(RandomPoint()))
|
||||||
} // Versucht das Aktuelle Shiff zu setzten und wiederholt solange bis es funktioniert
|
} // Versucht das Aktuelle Shiff zu setzten und wiederholt solange bis es funktioniert
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -68,7 +68,7 @@ public class Board {
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getSize() {
|
public int getSize() {
|
||||||
return this.size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,15 +7,8 @@ public class GameController {
|
||||||
|
|
||||||
public void startLocalGame(Class<? extends LocalPlayer> localPlayerClass, Class<? extends AiPlayer> enemyClass, int size) throws InstantiationException, IllegalAccessException {
|
public void startLocalGame(Class<? extends LocalPlayer> localPlayerClass, Class<? extends AiPlayer> enemyClass, int size) throws InstantiationException, IllegalAccessException {
|
||||||
|
|
||||||
LocalPlayer localPlayer;
|
LocalPlayer localPlayer = localPlayerClass.newInstance();
|
||||||
AiPlayer aiPlayer;
|
AiPlayer aiPlayer = enemyClass.newInstance();
|
||||||
try {
|
|
||||||
localPlayer = localPlayerClass.getDeclaredConstructor().newInstance();
|
|
||||||
aiPlayer = enemyClass.getDeclaredConstructor().newInstance();
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
localPlayer.setEnemy(aiPlayer);
|
localPlayer.setEnemy(aiPlayer);
|
||||||
aiPlayer.setEnemy(localPlayer);
|
aiPlayer.setEnemy(localPlayer);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,20 +1,5 @@
|
||||||
import java.io.BufferedInputStream;
|
|
||||||
import java.io.BufferedReader;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.OutputStream;
|
|
||||||
import java.io.PrintWriter;
|
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
|
|
||||||
public abstract class OnlinePlayer extends Player{
|
public abstract class OnlinePlayer extends Player{
|
||||||
private Socket socket;
|
private Socket socket;
|
||||||
private BufferedReader netIn;
|
|
||||||
private PrintWriter netOut;
|
|
||||||
|
|
||||||
public OnlinePlayer(int size, Socket socket, BufferedReader netIn) throws IOException {
|
|
||||||
super(size);
|
|
||||||
this.socket = socket;
|
|
||||||
this.netIn = netIn;
|
|
||||||
this.netOut = new PrintWriter(socket.getOutputStream(), true);
|
|
||||||
//TODO Auto-generated constructor stub
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,10 @@
|
||||||
|
|
||||||
|
|
||||||
import java.awt.Point;
|
import java.awt.Point;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
record ShipData (int size, String name){}
|
record ShipData (int size, String name){
|
||||||
|
}
|
||||||
|
|
||||||
public class Ship {
|
public class Ship {
|
||||||
static List<List<ShipData>> semeterList = Arrays.asList(Arrays.asList(
|
static List<List<ShipData>> semeterList = Arrays.asList(Arrays.asList(
|
||||||
|
@ -57,79 +56,16 @@ public class Ship {
|
||||||
public Ship (int size, String name) {
|
public Ship (int size, String name) {
|
||||||
this.size = size;
|
this.size = size;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.horizontal = false; //true = von Punkt aus nach rechts; false = von Punkt aus nach unten
|
this.horizontal = false;
|
||||||
this.position = null;
|
this.position = null;
|
||||||
this.hitsOnMe = 0;
|
this.hitsOnMe = 0;
|
||||||
this.sunk = false;
|
this.sunk = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean setPosition(Point pos, List<Ship> shipsList, int boardSize) {
|
public boolean setPosition(Point pos) {
|
||||||
// ueberpruefe boundaries
|
//TODO Conte abrfrage ob shif da sein darf
|
||||||
if (pos.x < 0 || pos.y < 0 || pos.x >= boardSize || pos.y >= boardSize) {
|
this.position = pos;
|
||||||
return false;
|
return true;
|
||||||
}
|
|
||||||
|
|
||||||
// bestimme die Endposition anhand der Ausrichtung
|
|
||||||
int endX = pos.x;
|
|
||||||
int endY = pos.y;
|
|
||||||
|
|
||||||
if (this.horizontal) { // rechts links
|
|
||||||
endX = pos.x + this.size - 1;
|
|
||||||
if (endX >= boardSize) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
} else { // oben unten
|
|
||||||
endY = pos.y + this.size - 1;
|
|
||||||
if (endY >= boardSize) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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;
|
|
||||||
shipPoints.add(new Point(x, y));
|
|
||||||
}
|
|
||||||
|
|
||||||
// ueberlappen mit anderen Schiffen pruefen
|
|
||||||
for (Ship otherShip : shipsList) {
|
|
||||||
// eigenes Schiff ueberspringen
|
|
||||||
if (otherShip == this) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
// ueberspringe falls noch nicht gesetzt
|
|
||||||
if (otherShip.position == null) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
// Punkte die das andere Schiff besetzt
|
|
||||||
List<Point> otherShipPoints = otherShip.getOccupiedPoints();
|
|
||||||
// ueberlappen checken
|
|
||||||
for (Point p : shipPoints) {
|
|
||||||
if (otherShipPoints.contains(p)) {
|
|
||||||
// ueberlappen entdeckt
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// kein ueberlappen also setze das Schiff
|
|
||||||
this.position = pos;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<Point> getOccupiedPoints() {
|
|
||||||
List<Point> points = new ArrayList<>();
|
|
||||||
if (this.position == null) {
|
|
||||||
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;
|
|
||||||
points.add(new Point(x, y));
|
|
||||||
}
|
|
||||||
return points;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Point getPosition() {
|
public Point getPosition() {
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
|
|
||||||
public class SpecificAiPlayerEasy extends AiPlayer{
|
public class SpecificAiPlayer1 extends AiPlayer{
|
||||||
|
|
||||||
public SpecificAiPlayerEasy(int size) {
|
public SpecificAiPlayer1(int size) {
|
||||||
super(size);
|
super(size);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
|
|
||||||
public class SpecificAiPlayerHard extends AiPlayer{
|
public class SpecificAiPlayer1 extends AiPlayer{
|
||||||
|
|
||||||
public SpecificAiPlayerHard(int size) {
|
public SpecificAiPlayer1(int size) {
|
||||||
super(size);
|
super(size);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,3 @@
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.awt.Point;
|
|
||||||
|
|
||||||
public class SpecificAiPlayerMedium extends AiPlayer{
|
public class SpecificAiPlayerMedium extends AiPlayer{
|
||||||
|
|
||||||
|
@ -16,7 +13,7 @@ public class SpecificAiPlayerMedium extends AiPlayer{
|
||||||
// Shoot at the enemy and receive the hit response
|
// Shoot at the enemy and receive the hit response
|
||||||
enemy.receiveShoot(nextShot);
|
enemy.receiveShoot(nextShot);
|
||||||
|
|
||||||
HitResponse hitResponse = enemy.board.getHitResponsOnPoint(nextShot);
|
HitResponse hitResponse = enemy.board.getHitResponsOnPoint(nextShot)
|
||||||
// If it's a hit or sunk, add adjacent cells to the hitsQueue
|
// If it's a hit or sunk, add adjacent cells to the hitsQueue
|
||||||
if (hitResponse.getHitResponse() == HitResponseType.HIT) {
|
if (hitResponse.getHitResponse() == HitResponseType.HIT) {
|
||||||
addAdjacentPoints(nextShot);
|
addAdjacentPoints(nextShot);
|
||||||
|
@ -61,11 +58,6 @@ public class SpecificAiPlayerMedium extends AiPlayer{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean alreadyShot(Point p) {
|
|
||||||
// TODO Auto-generated method stub
|
|
||||||
throw new UnsupportedOperationException("Unimplemented method 'alreadyShot'");
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean isValidPoint(Point point) {
|
private boolean isValidPoint(Point point) {
|
||||||
return point.x >= 0 && point.x < board.getSize() &&
|
return point.x >= 0 && point.x < board.getSize() &&
|
||||||
point.y >= 0 && point.y < board.getSize();
|
point.y >= 0 && point.y < board.getSize();
|
||||||
|
|
Loading…
Reference in New Issue