Compare commits
No commits in common. "70749c2c6292030a7455c8aeaf004f7651585578" and "bfb25dfe2c2189d1033ca8c41ad9ffb1ab07a75b" have entirely different histories.
70749c2c62
...
bfb25dfe2c
|
@ -1,8 +1,3 @@
|
||||||
/**
|
|
||||||
* Die Klasse AiPlayer ist die Basis für alle Ki Spieler und jede Spezifische Ki erweitert diese Klasse.
|
|
||||||
* @author Florian und Florian
|
|
||||||
* */
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -11,23 +6,12 @@ import java.util.Random;
|
||||||
|
|
||||||
public abstract class AiPlayer extends LocalPlayer implements Runnable {
|
public abstract class AiPlayer extends LocalPlayer implements Runnable {
|
||||||
|
|
||||||
/**
|
|
||||||
* Liste aller erstellten ShootThreads
|
|
||||||
*/
|
|
||||||
List<Thread> shootThreads;
|
List<Thread> shootThreads;
|
||||||
|
|
||||||
/**
|
|
||||||
* Konstruktor
|
|
||||||
*/
|
|
||||||
public AiPlayer() {
|
public AiPlayer() {
|
||||||
this.setName("AI Player");
|
this.setName("AI Player");
|
||||||
this.shootThreads = new ArrayList<>();
|
this.shootThreads = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Gibt einen zufälligen Punkt im Spielfeld zurück.
|
|
||||||
* @return Ein zufälliger Punkt
|
|
||||||
*/
|
|
||||||
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.getSize()); // Generiert 0 - 13
|
||||||
|
@ -35,10 +19,6 @@ public abstract class AiPlayer extends LocalPlayer implements Runnable {
|
||||||
return new Point(posx,posy);
|
return new Point(posx,posy);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Initialisiert das Board.
|
|
||||||
* @param size größe des Boards
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public void createBoard(int size) {
|
public void createBoard(int size) {
|
||||||
super.createBoard(size);
|
super.createBoard(size);
|
||||||
|
@ -46,32 +26,23 @@ public abstract class AiPlayer extends LocalPlayer implements Runnable {
|
||||||
this.ready();
|
this.ready();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Ki Methode zum zufälligen Setzten der Schiffe
|
|
||||||
*/
|
|
||||||
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.getShips().size(); i++) { // Interiert durch alle Shiffe
|
||||||
//TODO: set horizontal
|
//TODO: set horizontal
|
||||||
while(!super.board.getShips().get(i).setPosition(RandomPoint(), true, super.board.getShips(), super.board.getSize())) {}
|
while(!super.board.getShips().get(i).setPosition(RandomPoint(), true, super.board.getShips(), super.board.getSize())) {}
|
||||||
} // Versucht das aktuelle Schiff zu setzten und wiederholt solange bis es funktioniert
|
} // Versucht das Aktuelle Shiff zu setzten und wiederholt solange bis es funktioniert
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
/**
|
|
||||||
* Ki Methode zum zufälligen Schießen auf das gegnerische Board.
|
|
||||||
*/
|
|
||||||
public void aiShoot() {
|
public void aiShoot() {
|
||||||
if (!this.myTurn) return;
|
|
||||||
this.enemy.receiveShoot(RandomPoint());
|
this.enemy.receiveShoot(RandomPoint());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void receiveHit(HitResponse hitResponse) {
|
public synchronized void receiveHit(HitResponse hitResponse) {
|
||||||
// Eltern-Klasse LocalPlayer updatet myTurn
|
|
||||||
super.receiveHit(hitResponse);
|
super.receiveHit(hitResponse);
|
||||||
if (this.myTurn) {
|
if (this.myTurn) {
|
||||||
// Neuer Schuss wird erstellt und gestartet.
|
|
||||||
Thread t = new Thread(this);
|
Thread t = new Thread(this);
|
||||||
t.start();
|
t.start();
|
||||||
this.shootThreads.add(t);
|
this.shootThreads.add(t);
|
||||||
|
@ -89,10 +60,7 @@ public abstract class AiPlayer extends LocalPlayer implements Runnable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Wird aufgerufen, wenn in determineCoinToss festgestellt wurde das die Ki anfängt.
|
|
||||||
* Erster Schuss wird gestartet.
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public void beginTurn() {
|
public void beginTurn() {
|
||||||
Thread t = new Thread(this);
|
Thread t = new Thread(this);
|
||||||
|
@ -100,10 +68,6 @@ public abstract class AiPlayer extends LocalPlayer implements Runnable {
|
||||||
this.shootThreads.add(t);
|
this.shootThreads.add(t);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Closes past threads and tries firing a shot.
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public void run() {
|
public void run() {
|
||||||
Iterator<Thread> i = this.shootThreads.iterator();
|
Iterator<Thread> i = this.shootThreads.iterator();
|
||||||
while(i.hasNext()) {
|
while(i.hasNext()) {
|
||||||
|
@ -121,7 +85,7 @@ public abstract class AiPlayer extends LocalPlayer implements Runnable {
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Thread.sleep(250);
|
Thread.sleep(300);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
// TODO Auto-generated catch block
|
// TODO Auto-generated catch block
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
@ -129,24 +93,4 @@ public abstract class AiPlayer extends LocalPlayer implements Runnable {
|
||||||
|
|
||||||
this.aiShoot();
|
this.aiShoot();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Closes all running threads and does some cleanup work so the garbage collector will delete the player
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void destroy() {
|
|
||||||
super.destroy();
|
|
||||||
Iterator<Thread> i = this.shootThreads.iterator();
|
|
||||||
while(i.hasNext()) {
|
|
||||||
Thread thread = i.next();
|
|
||||||
try {
|
|
||||||
thread.join();
|
|
||||||
i.remove();
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
// TODO Auto-generated catch block
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -7,10 +7,6 @@ import java.net.InetSocketAddress;
|
||||||
import java.net.ServerSocket;
|
import java.net.ServerSocket;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
|
|
||||||
/**
|
|
||||||
* Provides an Interface to communicate using a socket asynchronously
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public class AsyncSocket {
|
public class AsyncSocket {
|
||||||
private Socket socket;
|
private Socket socket;
|
||||||
private Thread checkerThread;
|
private Thread checkerThread;
|
||||||
|
@ -23,12 +19,6 @@ public class AsyncSocket {
|
||||||
private BufferedReader in;
|
private BufferedReader in;
|
||||||
private BufferedWriter out;
|
private BufferedWriter out;
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a socket server and turns the first incoming connection into an AsyncSocket
|
|
||||||
* @param port the port to listen on for a connection
|
|
||||||
* @param handler the handler that will be called when a message is received
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public AsyncSocket(int port, AsyncSocketListener handler) {
|
public AsyncSocket(int port, AsyncSocketListener handler) {
|
||||||
this.setHandler(handler);
|
this.setHandler(handler);
|
||||||
|
|
||||||
|
@ -55,12 +45,6 @@ public class AsyncSocket {
|
||||||
this.connectorThread.start();
|
this.connectorThread.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Connects to the address provided and turns the resulting socket into an AsyncSocket
|
|
||||||
* @param address the socket address to connect to
|
|
||||||
* @param handler the handler that will be called when a message is received
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public AsyncSocket(InetSocketAddress address, AsyncSocketListener handler) {
|
public AsyncSocket(InetSocketAddress address, AsyncSocketListener handler) {
|
||||||
this.setHandler(handler);
|
this.setHandler(handler);
|
||||||
|
|
||||||
|
@ -84,22 +68,11 @@ public class AsyncSocket {
|
||||||
this.connectorThread.start();
|
this.connectorThread.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param socket the socket to be wrapped in an AsyncSocket
|
|
||||||
* @param handler the handler that will be called when a message is received
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public AsyncSocket(Socket socket, AsyncSocketListener handler) throws IOException {
|
public AsyncSocket(Socket socket, AsyncSocketListener handler) throws IOException {
|
||||||
this.setHandler(handler);
|
this.setHandler(handler);
|
||||||
this.initSocket(socket);
|
this.initSocket(socket);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* creates input and ouput writer / readers as well as a checker thread to repeatedly check
|
|
||||||
* for incoming messages
|
|
||||||
* @param socket the socket to be wrapped
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
private void initSocket(Socket socket) throws IOException {
|
private void initSocket(Socket socket) throws IOException {
|
||||||
System.out.println("Initialising sockets");
|
System.out.println("Initialising sockets");
|
||||||
this.socket = socket;
|
this.socket = socket;
|
||||||
|
@ -149,39 +122,17 @@ public class AsyncSocket {
|
||||||
this.flushBuffer();
|
this.flushBuffer();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* sets the message handler for the async socket
|
|
||||||
* @param handler the `AsyncSocketListener` to be set as the new handler
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public void setHandler(AsyncSocketListener handler) {
|
public void setHandler(AsyncSocketListener handler) {
|
||||||
this.handler = handler;
|
this.handler = handler;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* sends a message through the socket
|
|
||||||
* @param socketPackage the socket package to be sent
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public synchronized void send(SocketPackage socketPackage) {
|
public synchronized void send(SocketPackage socketPackage) {
|
||||||
this.sendLine(socketPackage.toString());
|
this.sendLine(socketPackage.toString());
|
||||||
}
|
}
|
||||||
/**
|
|
||||||
* sends a message through the socket
|
|
||||||
* @param packageName the name of the package to be sent
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public synchronized void send(String packageName) {
|
public synchronized void send(String packageName) {
|
||||||
this.send(packageName, "");
|
this.send(packageName, "");
|
||||||
}
|
}
|
||||||
/**
|
|
||||||
* sends a message through the socket
|
|
||||||
* @param packageName the name of the package to be sent
|
|
||||||
* @param packageContent the content of the package to be sent.
|
|
||||||
* `packageName` and `packageContent` are joined with a space " "
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public synchronized void send(String packageName, String packageContent) {
|
public synchronized void send(String packageName, String packageContent) {
|
||||||
if (packageContent.length() > 0) {
|
if (packageContent.length() > 0) {
|
||||||
packageContent = " " + packageContent;
|
packageContent = " " + packageContent;
|
||||||
|
@ -189,19 +140,11 @@ public class AsyncSocket {
|
||||||
this.sendLine(packageName + packageContent);
|
this.sendLine(packageName + packageContent);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* sends a string of text into the socket, concatenated with CRLF
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public synchronized void sendLine(String message) {
|
public synchronized void sendLine(String message) {
|
||||||
sendBuffer = sendBuffer + message + "\r\n";
|
sendBuffer = sendBuffer + message + "\r\n";
|
||||||
this.flushBuffer();
|
this.flushBuffer();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* flushes the buffers to send all pending messages
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
private synchronized void flushBuffer() {
|
private synchronized void flushBuffer() {
|
||||||
if (!this.sendBuffer.isEmpty() && this.out != null) {
|
if (!this.sendBuffer.isEmpty() && this.out != null) {
|
||||||
try {
|
try {
|
||||||
|
@ -216,16 +159,12 @@ public class AsyncSocket {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* closes the socket connection and removes the checker thread
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public void close() {
|
public void close() {
|
||||||
this.shouldStop = true;
|
this.shouldStop = true;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
this.socket.close();
|
this.socket.close();
|
||||||
if (this.checkerThread != null) this.checkerThread.join();
|
this.checkerThread.join();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
|
|
|
@ -1,6 +1,3 @@
|
||||||
/**
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public interface AsyncSocketListener {
|
public interface AsyncSocketListener {
|
||||||
public void receive(String message);
|
public void receive(String message);
|
||||||
}
|
}
|
|
@ -1,38 +1,14 @@
|
||||||
/**
|
|
||||||
* Diese Klasse ist das Board von eimem spieler und enthält alle logischen operationen.
|
|
||||||
* Sprich ist das Backend Board.
|
|
||||||
*
|
|
||||||
* @author Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class Board {
|
public class Board {
|
||||||
|
|
||||||
/**
|
|
||||||
* Alle bisher empfangenen HitResponsen
|
|
||||||
*/
|
|
||||||
private List<HitResponse> hits;
|
private List<HitResponse> hits;
|
||||||
|
|
||||||
/**
|
|
||||||
* Alle Schiffe des Semesters
|
|
||||||
*/
|
|
||||||
private List<Ship> ships;
|
private List<Ship> ships;
|
||||||
|
|
||||||
/**
|
|
||||||
* Die größe des Spielfeldes
|
|
||||||
*/
|
|
||||||
private final int size;
|
private final int size;
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Erstellt ein neues Board.
|
|
||||||
* setzt die übergebene Spielfeldgröße.
|
|
||||||
* Erstellt die Liste aller Schiffe des Semesters
|
|
||||||
* @param size Die größe des Spielfeldes
|
|
||||||
* @author Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
public Board(int size) {
|
public Board(int size) {
|
||||||
this.size = size;
|
this.size = size;
|
||||||
this.ships = new ArrayList<>();
|
this.ships = new ArrayList<>();
|
||||||
|
@ -40,12 +16,6 @@ public class Board {
|
||||||
this.createShip(size - 13);
|
this.createShip(size - 13);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Nimmt einen punkt entgegen und Gibt einen Hitrespons zurück.
|
|
||||||
* @param point auf den geschossen wurde
|
|
||||||
* @return
|
|
||||||
* @author Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
public synchronized HitResponse hit (Point point){
|
public synchronized HitResponse hit (Point point){
|
||||||
HitResponse response = new HitResponse(HitResponseType.MISS,point);
|
HitResponse response = new HitResponse(HitResponseType.MISS,point);
|
||||||
for (int i = 0; i < this.ships.size(); i++) {
|
for (int i = 0; i < this.ships.size(); i++) {
|
||||||
|
@ -72,12 +42,6 @@ public class Board {
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* finds adjacened hit responses and sets their type to SUNK if they are currently HIT
|
|
||||||
* this makes it so that all the points of the ship are marked as SUNK, not just the final hit
|
|
||||||
* @param p the Point from which to propate the SUNK type
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
private void propagateSunk(Point p) {
|
private void propagateSunk(Point p) {
|
||||||
HitResponse hit = this.getHitResponseOnPoint(p);
|
HitResponse hit = this.getHitResponseOnPoint(p);
|
||||||
|
|
||||||
|
@ -91,11 +55,6 @@ public class Board {
|
||||||
propagateSunk(new Point(p.getX(), p.getY() - 1));
|
propagateSunk(new Point(p.getX(), p.getY() - 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* creates all the ships on a board given a certain semester
|
|
||||||
* @param semester the semester to be played in
|
|
||||||
* @author Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
private void createShip(int semester){
|
private void createShip(int semester){
|
||||||
List<ShipData> shipData = Ship.semeterList.get(semester -1);
|
List<ShipData> shipData = Ship.semeterList.get(semester -1);
|
||||||
for (int i = 0; i < shipData.size(); i++) {
|
for (int i = 0; i < shipData.size(); i++) {
|
||||||
|
@ -103,22 +62,10 @@ public class Board {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @return a list of all the Ships on the board
|
|
||||||
*/
|
|
||||||
public List<Ship> getShips() {
|
public List<Ship> getShips() {
|
||||||
return ships;
|
return ships;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* adds a HitResponse to the list of Hits on the board
|
|
||||||
* If a hit response already exists on the same position, the hit response will not be added
|
|
||||||
* If the hit response is of type `HitResponseType.SUNK` it will propagate this hit response type
|
|
||||||
* to all adjacened hit responses with type HIT using `propagateSunk`.
|
|
||||||
* @param hitResponse the HitResponse to be added
|
|
||||||
* @return true when the hit response was added, otherwise false
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public synchronized boolean addHits(HitResponse hitResponse) {
|
public synchronized boolean addHits(HitResponse hitResponse) {
|
||||||
if (this.getHitResponseOnPoint(hitResponse.getPoint()) == null){
|
if (this.getHitResponseOnPoint(hitResponse.getPoint()) == null){
|
||||||
this.hits.add(hitResponse);
|
this.hits.add(hitResponse);
|
||||||
|
@ -134,11 +81,6 @@ public class Board {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param point the position to get the hit response from
|
|
||||||
* @return the hit response at the position `point`
|
|
||||||
* @author Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
public synchronized HitResponse getHitResponseOnPoint(Point point) {
|
public synchronized HitResponse getHitResponseOnPoint(Point point) {
|
||||||
for (int i = 0; i < this.hits.size(); i++){
|
for (int i = 0; i < this.hits.size(); i++){
|
||||||
if (this.hits.get(i).getPoint().equals(point)){
|
if (this.hits.get(i).getPoint().equals(point)){
|
||||||
|
@ -148,9 +90,6 @@ public class Board {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @return the size of the board
|
|
||||||
*/
|
|
||||||
public int getSize() {
|
public int getSize() {
|
||||||
return this.size;
|
return this.size;
|
||||||
}
|
}
|
||||||
|
|
|
@ -221,8 +221,5 @@ public class GameBoard extends JPanel {
|
||||||
public Player getP1() {
|
public Player getP1() {
|
||||||
return p1;
|
return p1;
|
||||||
}
|
}
|
||||||
public Player getP2() {
|
|
||||||
return p2;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,63 +5,28 @@ import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
|
||||||
* The central Backend Component
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public class GameController {
|
public class GameController {
|
||||||
|
|
||||||
private static MainFrame mainFrame;
|
private static MainFrame mainFrame;
|
||||||
|
|
||||||
/**
|
|
||||||
* @return the current MainFrame
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public static MainFrame getMainFrame() {
|
public static MainFrame getMainFrame() {
|
||||||
return GameController.mainFrame;
|
return GameController.mainFrame;
|
||||||
}
|
}
|
||||||
/**
|
|
||||||
* @param mainFrame the current MainFrame
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public static void setMainFrame(MainFrame mainFrame) {
|
public static void setMainFrame(MainFrame mainFrame) {
|
||||||
GameController.mainFrame = mainFrame;
|
GameController.mainFrame = mainFrame;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* converts a semester to be played in into its respective board size
|
|
||||||
* @param semester the semester to be played in
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public static int semesterToBoardSize(int semester) {
|
public static int semesterToBoardSize(int semester) {
|
||||||
return semester + 13;
|
return semester + 13;
|
||||||
}
|
}
|
||||||
/**
|
|
||||||
* converts a board size into its respective semester
|
|
||||||
* @param size the board size to get the semester of
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public static int boardSizeToSemester(int size) {
|
public static int boardSizeToSemester(int size) {
|
||||||
return size - 13;
|
return size - 13;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* the list of supported versions of the online protocol
|
|
||||||
* https://github.com/lgc-4/ProgProjekt-Netzwerkstandard
|
|
||||||
*/
|
|
||||||
public static HashMap<String, Class<? extends OnlinePlayer>> supportedVersions = new HashMap<>(Map.of(
|
public static HashMap<String, Class<? extends OnlinePlayer>> supportedVersions = new HashMap<>(Map.of(
|
||||||
"1.1.0", OnlinePlayer_1_1_0.class
|
"1.1.0", OnlinePlayer_1_1_0.class
|
||||||
));
|
));
|
||||||
|
|
||||||
/**
|
|
||||||
* Starts a game with one local player and one online player.
|
|
||||||
* @param localPlayerClass the class of the local player to be instantiated. must extend `LocalPlayer`
|
|
||||||
* @param localPlayerName the name of the local player. Only necessarry when localPlayerClass is `HumanPlayer`
|
|
||||||
* @param address the Address of the opposing player. If the hostname of the address is set to be 0.0.0.0, it
|
|
||||||
* is interpreted that this instance should instead listen on the defined port for incoming connections.
|
|
||||||
* @param size the board size that it is intended to be played in
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public static void startOnlineGame(Class<? extends LocalPlayer> localPlayerClass, String localPlayerName, InetSocketAddress address, int size) throws IOException {
|
public static void startOnlineGame(Class<? extends LocalPlayer> localPlayerClass, String localPlayerName, InetSocketAddress address, int size) throws IOException {
|
||||||
AsyncSocket clientSocket;
|
AsyncSocket clientSocket;
|
||||||
|
|
||||||
|
@ -125,7 +90,6 @@ public class GameController {
|
||||||
/**
|
/**
|
||||||
* finds the largest common version in two lists of version strings
|
* finds the largest common version in two lists of version strings
|
||||||
* @return null if no common versions are found
|
* @return null if no common versions are found
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
*/
|
||||||
public static String findMostRecentVersion(List<String> versions1, List<String> versions2) {
|
public static String findMostRecentVersion(List<String> versions1, List<String> versions2) {
|
||||||
if (versions1 == null || versions2 == null) return null;
|
if (versions1 == null || versions2 == null) return null;
|
||||||
|
@ -150,13 +114,10 @@ public class GameController {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* compares two version strings
|
* compares two version strings
|
||||||
* @param version1 the first version to be compared
|
|
||||||
* @param version2 the second version to be compared
|
|
||||||
* @return
|
* @return
|
||||||
* 0 if versions are equal
|
* 0 if versions are equal
|
||||||
* 1 if version1 is more recent than version2
|
* 1 if version1 is more recent than version2
|
||||||
* -1 otherwise
|
* -1 otherwise
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
*/
|
||||||
public static int compareVersions(String version1, String version2) {
|
public static int compareVersions(String version1, String version2) {
|
||||||
if (!checkVersionString(version1) || !checkVersionString(version2)) {
|
if (!checkVersionString(version1) || !checkVersionString(version2)) {
|
||||||
|
@ -182,21 +143,11 @@ public class GameController {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* checks if a provided string matches the format of a version number
|
* checks if a provided string matches the format of a version number
|
||||||
* @param versionString the version String to be checked
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
*/
|
||||||
public static boolean checkVersionString(String versionString) {
|
public static boolean checkVersionString(String versionString) {
|
||||||
return versionString != null && versionString.matches("\\d+\\.\\d+\\.\\d+");
|
return versionString != null && versionString.matches("\\d+\\.\\d+\\.\\d+");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Starts a game with two local players
|
|
||||||
* @param localPlayerClass the class of the local player to be instantiated. must extend `LocalPlayer`
|
|
||||||
* @param localPlayerName the name of the local player. Only necessarry when localPlayerClass is `HumanPlayer`
|
|
||||||
* @param enemyClass the class of the enemy player. Must extend `AiPlayer`. For Humans VS Human games use an online game instead.
|
|
||||||
* @param size the board size that it is intended to be played in
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public static void startLocalGame(Class<? extends LocalPlayer> localPlayerClass, String localPlayerName, Class<? extends AiPlayer> enemyClass, int size) {
|
public static void startLocalGame(Class<? extends LocalPlayer> localPlayerClass, String localPlayerName, Class<? extends AiPlayer> enemyClass, int size) {
|
||||||
|
|
||||||
LocalPlayer localPlayer;
|
LocalPlayer localPlayer;
|
||||||
|
@ -224,13 +175,6 @@ public class GameController {
|
||||||
startGameWithInstancedPlayers(localPlayer, aiPlayer, size);
|
startGameWithInstancedPlayers(localPlayer, aiPlayer, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* switches to the ingame screen
|
|
||||||
* @param p1 the first player. This player is always shown on the left side of the screen and is expected to be the Human player, if there is one.
|
|
||||||
* @param p2 the second player
|
|
||||||
* @param boardSize the board size to be played in
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public static void startGameWithInstancedPlayers(LocalPlayer p1, Player p2, int boardSize) {
|
public static void startGameWithInstancedPlayers(LocalPlayer p1, Player p2, int boardSize) {
|
||||||
mainFrame.showPanelSLG("GameBoard", boardSizeToSemester(boardSize), p1, p2);
|
mainFrame.showPanelSLG("GameBoard", boardSizeToSemester(boardSize), p1, p2);
|
||||||
|
|
||||||
|
|
|
@ -1,33 +1,14 @@
|
||||||
|
|
||||||
|
|
||||||
public class HitResponse {
|
public class HitResponse {
|
||||||
/**
|
|
||||||
* Speichert den typ der HitResponse.
|
|
||||||
*/
|
|
||||||
private HitResponseType type;
|
private HitResponseType type;
|
||||||
/**
|
|
||||||
* Speicher den Punkt wofür die HitResponse gilt.
|
|
||||||
*/
|
|
||||||
private Point point;
|
private Point point;
|
||||||
|
|
||||||
/**
|
|
||||||
* Erstellt eine neue HitResponse und setzt den Punkt und typ.
|
|
||||||
* @param type der HitResponse.
|
|
||||||
* @param point für den die HitResponse gilt.
|
|
||||||
* @author Peer Ole Wachtel.
|
|
||||||
*/
|
|
||||||
public HitResponse(HitResponseType type, Point point) {
|
public HitResponse(HitResponseType type, Point point) {
|
||||||
this.type = type;
|
this.type = type;
|
||||||
this.point = point;
|
this.point = point;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
*Erstellt eine neue HitResponse und setzt den Punkt und typ.
|
|
||||||
* @param typeIndex der HitResponse.
|
|
||||||
* @param point für den die HitResponse gilt.
|
|
||||||
* @throws IllegalArgumentException wenn der übergebene int nicht auf ein typ referenziert werden kann.
|
|
||||||
* @author Peer Ole Wachtel.
|
|
||||||
*/
|
|
||||||
public HitResponse (int typeIndex, Point point) {
|
public HitResponse (int typeIndex, Point point) {
|
||||||
if (typeIndex >= 0 && typeIndex < HitResponseType.values().length) {
|
if (typeIndex >= 0 && typeIndex < HitResponseType.values().length) {
|
||||||
this.type = HitResponseType.values()[typeIndex];
|
this.type = HitResponseType.values()[typeIndex];
|
||||||
|
@ -45,30 +26,15 @@ public class HitResponse {
|
||||||
return this.point;
|
return this.point;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Setter für den type
|
|
||||||
* @param type auf den die HitResponse gesetzt werden soll.
|
|
||||||
* @author Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
public void setType(HitResponseType type) {
|
public void setType(HitResponseType type) {
|
||||||
this.type = type;
|
this.type = type;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Gibt den passenden string nach Netzwerkstandard für eine HitResponse zurück.
|
|
||||||
* @return den passenden string nach Netzwerkstandard für eine HitResponse.
|
|
||||||
* @author Peer Ole Wachtel.
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return this.getPoint().toString() + " " + this.type.ordinal();
|
return this.getPoint().toString() + " " + this.type.ordinal();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Getter für den typ der HitResponse.
|
|
||||||
* @return den typ der HitRespnse.
|
|
||||||
* @author Peer Ole Wachtel.
|
|
||||||
*/
|
|
||||||
public HitResponseType getType() {
|
public HitResponseType getType() {
|
||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,3 @@
|
||||||
/**
|
|
||||||
* Stellt die verschiedenen möglichkeiten einer HitResponse als typ dar.
|
|
||||||
* @author Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
public enum HitResponseType {
|
public enum HitResponseType {
|
||||||
MISS, HIT, SUNK, VICTORY
|
MISS, HIT, SUNK, VICTORY
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,15 +1,4 @@
|
||||||
|
|
||||||
public class HumanPlayer extends LocalPlayer {
|
public class HumanPlayer extends LocalPlayer {
|
||||||
/**
|
|
||||||
* shoots a shot onto the provided point on the enemy board
|
|
||||||
* if it is not the players turn, this method does nothing.
|
|
||||||
* @param point the location to be shot
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void shoot(Point point) {
|
|
||||||
if (!this.myTurn) return;
|
|
||||||
this.myTurn = false;
|
|
||||||
enemy.receiveShoot(point);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,26 +2,14 @@ import java.util.Random;
|
||||||
|
|
||||||
public class LocalPlayer extends Player {
|
public class LocalPlayer extends Player {
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* erstellt einen LocalPlayer und setzt myCoin random
|
|
||||||
* @author Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
public LocalPlayer(){
|
public LocalPlayer(){
|
||||||
super();
|
super();
|
||||||
Random random = new Random();
|
Random random = new Random();
|
||||||
this.myCoin = random.nextBoolean();
|
this.myCoin = random.nextBoolean();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* receives a shot onto a point from the enemy
|
|
||||||
* @param point the location to be shot
|
|
||||||
* @author Luca Conte, Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void receiveShoot(Point point) {
|
public synchronized void receiveShoot(Point point) {
|
||||||
if (!this.enemy.myTurn) return;
|
|
||||||
|
|
||||||
HitResponse hitResponse = board.getHitResponseOnPoint(point);
|
HitResponse hitResponse = board.getHitResponseOnPoint(point);
|
||||||
if (!(hitResponse == null)){
|
if (!(hitResponse == null)){
|
||||||
enemy.receiveHit(hitResponse);
|
enemy.receiveHit(hitResponse);
|
||||||
|
@ -39,11 +27,6 @@ public class LocalPlayer extends Player {
|
||||||
GameController.getMainFrame().refreshGameBoard();
|
GameController.getMainFrame().refreshGameBoard();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* receives a hit response from the enemy as a response to a receiveShoot call
|
|
||||||
* @param hitResponse the hitresponse
|
|
||||||
* @author Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void receiveHit(HitResponse hitResponse) {
|
public synchronized void receiveHit(HitResponse hitResponse) {
|
||||||
enemy.board.addHits(hitResponse);
|
enemy.board.addHits(hitResponse);
|
||||||
|
@ -55,13 +38,6 @@ public class LocalPlayer extends Player {
|
||||||
GameController.getMainFrame().refreshGameBoard();
|
GameController.getMainFrame().refreshGameBoard();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* receives the enemies coin toss result
|
|
||||||
* this method does nothing if the player has already received the enemies coin
|
|
||||||
* it will also call `determineCoinToss`
|
|
||||||
* @param coin the coin of the enemy player
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void receiveCoin(boolean coin) {
|
public synchronized void receiveCoin(boolean coin) {
|
||||||
if (!this.hasReceivedCoin) {
|
if (!this.hasReceivedCoin) {
|
||||||
|
@ -70,19 +46,12 @@ public class LocalPlayer extends Player {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* sends shot to enemy player.
|
|
||||||
* should ONLY be called on HumanPlayer
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public void shoot(Point point){
|
public void shoot(Point point){
|
||||||
return;
|
this.myTurn = false;
|
||||||
|
enemy.receiveShoot(point);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* marks the player as ready, if all ships have been placed
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void ready() {
|
public synchronized void ready() {
|
||||||
for (Ship ship : this.board.getShips()) {
|
for (Ship ship : this.board.getShips()) {
|
||||||
|
|
|
@ -151,8 +151,6 @@ public class MainFrame extends JFrame {
|
||||||
if(player != gameBoard.getP1()){
|
if(player != gameBoard.getP1()){
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.gameBoard.getP1().destroy();
|
|
||||||
this.gameBoard.getP2().destroy();
|
|
||||||
LooseScreen looseScreen = new LooseScreen(this);
|
LooseScreen looseScreen = new LooseScreen(this);
|
||||||
mainPanel.add(looseScreen,panelName);
|
mainPanel.add(looseScreen,panelName);
|
||||||
mainPanel.revalidate();
|
mainPanel.revalidate();
|
||||||
|
|
|
@ -1,15 +1,9 @@
|
||||||
public abstract class OnlinePlayer extends Player implements AsyncSocketListener {
|
public abstract class OnlinePlayer extends Player implements AsyncSocketListener{
|
||||||
protected AsyncSocket socket;
|
protected AsyncSocket socket;
|
||||||
protected int wantedBoardSize;
|
protected int wantedBoardSize;
|
||||||
|
|
||||||
protected boolean hasReceivedCoinPackage;
|
protected boolean hasReceivedCoinPackage;
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructor
|
|
||||||
* @param size the size of the board the enemy player wants to play with
|
|
||||||
* the actual board size will be determined once the semester of the online partner is known
|
|
||||||
* @param socket an AsyncSocket to communicate with the enemy through
|
|
||||||
*/
|
|
||||||
public OnlinePlayer(Integer size, AsyncSocket socket) {
|
public OnlinePlayer(Integer size, AsyncSocket socket) {
|
||||||
this.socket = socket;
|
this.socket = socket;
|
||||||
this.wantedBoardSize = size;
|
this.wantedBoardSize = size;
|
||||||
|
@ -31,14 +25,4 @@ public abstract class OnlinePlayer extends Player implements AsyncSocketListener
|
||||||
@Override
|
@Override
|
||||||
public abstract void receiveCoin(boolean coin);
|
public abstract void receiveCoin(boolean coin);
|
||||||
|
|
||||||
/**
|
|
||||||
* closes the socket and does player cleanup work
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void destroy() {
|
|
||||||
super.destroy();
|
|
||||||
this.socket.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,12 +6,6 @@ public class OnlinePlayer_1_1_0 extends OnlinePlayer {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* receives a message from the AsyncSocket
|
|
||||||
* implemented according to version 1.1.0 of https://github.com/lgc-4/ProgProjekt-Netzwerkstandard
|
|
||||||
* @param message the message from the socket
|
|
||||||
* @author Peer Ole Wachtel, Luca Conte
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public void receive(String message) {
|
public void receive(String message) {
|
||||||
SocketPackage p = new SocketPackage(message);
|
SocketPackage p = new SocketPackage(message);
|
||||||
|
@ -40,8 +34,8 @@ public class OnlinePlayer_1_1_0 extends OnlinePlayer {
|
||||||
case "COIN":
|
case "COIN":
|
||||||
if(!this.hasReceivedCoinPackage && (p.getData().equals("1") || p.getData().equals("0"))){
|
if(!this.hasReceivedCoinPackage && (p.getData().equals("1") || p.getData().equals("0"))){
|
||||||
this.myCoin = p.getData().equals("1");
|
this.myCoin = p.getData().equals("1");
|
||||||
this.hasReceivedCoinPackage = true;
|
|
||||||
this.ready();
|
this.ready();
|
||||||
|
this.hasReceivedCoinPackage = true;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
@ -73,44 +67,22 @@ public class OnlinePlayer_1_1_0 extends OnlinePlayer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* sends introduction package IAM to online partner.
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void sendIAM() {
|
public synchronized void sendIAM() {
|
||||||
if (this.enemy == null) throw new RuntimeException("enemy has not yet been defined");
|
if (this.enemy == null) throw new RuntimeException("enemy has not yet been defined");
|
||||||
socket.send(new SocketPackage("IAM", GameController.boardSizeToSemester(this.wantedBoardSize) + " " + this.enemy.name));
|
socket.send(new SocketPackage("IAM", GameController.boardSizeToSemester(this.wantedBoardSize) + " " + this.enemy.name));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* receives a shot from the enemy and sends it to the online partner
|
|
||||||
* if it is not the enemies turn, this method does nothing.
|
|
||||||
* @param point the point to be shot
|
|
||||||
* @author Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void receiveShoot(Point point){
|
public synchronized void receiveShoot(Point point){
|
||||||
if (!this.enemy.myTurn) return;
|
|
||||||
super.socket.send(new SocketPackage("SHOOT",point.toString()));
|
super.socket.send(new SocketPackage("SHOOT",point.toString()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* receives a hitresponse from the enemy player and sends it to the online partner
|
|
||||||
* @param hitResponse the hitresponse to be sent
|
|
||||||
* @author Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void receiveHit(HitResponse hitResponse) {
|
public synchronized void receiveHit(HitResponse hitResponse) {
|
||||||
super.socket.send(new SocketPackage("HIT", hitResponse.toString()));
|
super.socket.send(new SocketPackage("HIT", hitResponse.toString()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* receives the coin toss result from the enemy player and sends it to the online partner
|
|
||||||
* if this player has already received the enemies coin, this method does nothing.
|
|
||||||
* @param coin the result of the coin toss
|
|
||||||
* @author Peer Ole Wachtel, Luca Conte
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void receiveCoin(boolean coin) {
|
public synchronized void receiveCoin(boolean coin) {
|
||||||
if (!this.hasReceivedCoin) {
|
if (!this.hasReceivedCoin) {
|
||||||
|
@ -122,7 +94,6 @@ public class OnlinePlayer_1_1_0 extends OnlinePlayer {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void shoot(Point point) {
|
public synchronized void shoot(Point point) {
|
||||||
// SHOULD NEVER BE CALLED ON ONLINE PLAYER. ONLY ON HUMAN PLAYER
|
super.socket.send(new SocketPackage("SHOOT", point.toString()));
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -20,70 +20,35 @@ public abstract class Player {
|
||||||
this.gameRunning = false;
|
this.gameRunning = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* initialises this players board
|
|
||||||
* @param size the size of the board to be created
|
|
||||||
* @author Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
public void createBoard(int size) {
|
public void createBoard(int size) {
|
||||||
this.board = new Board(size);
|
this.board = new Board(size);
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract void receiveShoot(Point point);
|
public abstract void receiveShoot(Point point);
|
||||||
|
|
||||||
public abstract
|
public abstract void receiveHit(HitResponse hitResponse);
|
||||||
void receiveHit(HitResponse hitResponse);
|
|
||||||
|
|
||||||
public abstract void shoot(Point point);
|
public abstract void shoot(Point point);
|
||||||
|
|
||||||
/**
|
|
||||||
* only relevant for AI Players.
|
|
||||||
* starts the first turn
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public void beginTurn() {
|
public void beginTurn() {
|
||||||
System.out.println("issa my turn-a");
|
System.out.println("issa my turn-a");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* sets the enemy Player
|
|
||||||
* @author Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
public void setEnemy(Player enemy) {
|
public void setEnemy(Player enemy) {
|
||||||
this.enemy = enemy;
|
this.enemy = enemy;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* sets the name of this player
|
|
||||||
* @param name the name of this player
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public void setName(String name) {
|
public void setName(String name) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
/**
|
|
||||||
* returns the name of this player
|
|
||||||
* @return the name of this player
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return this.name;
|
return this.name;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* returns the board of this player
|
|
||||||
* @return the board of this player
|
|
||||||
* @author Lucas Bronson
|
|
||||||
*/
|
|
||||||
public Board getBoard() {
|
public Board getBoard() {
|
||||||
return this.board;
|
return this.board;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* marks the player as ready by sending their coin to the enemy player
|
|
||||||
* calls determineCoinToss if the enemy coin has already been received
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public void ready() {
|
public void ready() {
|
||||||
this.enemy.receiveCoin(this.myCoin);
|
this.enemy.receiveCoin(this.myCoin);
|
||||||
this.sentCoin = true;
|
this.sentCoin = true;
|
||||||
|
@ -92,11 +57,6 @@ public abstract class Player {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* determines the result of the coin toss
|
|
||||||
* this method does nothing if either player is not ready yet or has not yet sent their coin
|
|
||||||
* @author Luca Conte, Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
protected void determineCoinToss() {
|
protected void determineCoinToss() {
|
||||||
if (!this.sentCoin || this.myCoin == null || !this.hasReceivedCoin || this.enemy.myCoin == null) return;
|
if (!this.sentCoin || this.myCoin == null || !this.hasReceivedCoin || this.enemy.myCoin == null) return;
|
||||||
boolean result = this.enemy.myCoin ^ this.myCoin; // XOR
|
boolean result = this.enemy.myCoin ^ this.myCoin; // XOR
|
||||||
|
@ -108,43 +68,13 @@ public abstract class Player {
|
||||||
GameController.getMainFrame().refreshGameBoard();
|
GameController.getMainFrame().refreshGameBoard();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* receives the coin toss from the enemy player
|
|
||||||
* @param coin the coin of the enemy player
|
|
||||||
* @author Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
public abstract void receiveCoin(boolean coin);
|
public abstract void receiveCoin(boolean coin);
|
||||||
|
|
||||||
/**
|
|
||||||
* returns whether the game this player is in has started, meaning both players are ready and have sent their coins
|
|
||||||
* @return the game's running state
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public boolean isGameRunning() {
|
public boolean isGameRunning() {
|
||||||
return this.gameRunning;
|
return this.gameRunning;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* returns whether this player is ready and has sent their coin to the enemy player
|
|
||||||
* @return the player's ready state
|
|
||||||
*/
|
|
||||||
public boolean isReady() {
|
public boolean isReady() {
|
||||||
return this.sentCoin;
|
return this.sentCoin;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* removes connections to the enemy and its board as well as setting myTurn and gameRunning to false
|
|
||||||
* this stops the AI Players from making more moves and allows the garbage collector to remove the boards
|
|
||||||
* and players
|
|
||||||
*
|
|
||||||
* This method should be called at the end of a game
|
|
||||||
*
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public void destroy() {
|
|
||||||
this.myTurn = false;
|
|
||||||
this.gameRunning = false;
|
|
||||||
this.board = null;
|
|
||||||
this.enemy = null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,27 +2,10 @@ public class Point {
|
||||||
private int x;
|
private int x;
|
||||||
private int y;
|
private int y;
|
||||||
|
|
||||||
/**
|
|
||||||
* initialises a point using X and Y coordinates starting at 0
|
|
||||||
* @param x the x coordinate of the point starting at 0
|
|
||||||
* @param y the y coordinate of the point starting at 0
|
|
||||||
* @author Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
public Point (int x, int y) {
|
public Point (int x, int y) {
|
||||||
this.setX(x);
|
this.setX(x);
|
||||||
this.setY(y);
|
this.setY(y);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* initialises a Point using a coordinate provided in the format of a letter followed by a number
|
|
||||||
* this coordinate is checked using `isValidSyntax`
|
|
||||||
* If the coordinate is not in a valid syntax, an `IllegalArgumentException` will be thrown, stating as such
|
|
||||||
* The number part of the coordinate starts at 1 instead of 0 so for example, the
|
|
||||||
* string A1 will result in the X and Y coordinates of (0, 0)
|
|
||||||
* @param str the coordinate in alphanumeric format
|
|
||||||
* @throws IllegalArgumentException if the coordinate is invalid according to `isValidSyntax`
|
|
||||||
* @author Peer Ole Wachtel, Luca Conte
|
|
||||||
*/
|
|
||||||
public Point (String str) {
|
public Point (String str) {
|
||||||
if (Point.isValidSyntax(str)) {
|
if (Point.isValidSyntax(str)) {
|
||||||
this.setX(str.charAt(0));
|
this.setX(str.charAt(0));
|
||||||
|
@ -32,78 +15,34 @@ public class Point {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* returns this point as a string in its alphanumeric format
|
|
||||||
* @return this point as a string in its alphanumeric format
|
|
||||||
* @author Luca Conte, Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return (char) ('A' + this.x) + String.valueOf(this.y + 1);
|
return (char) ('A' + this.x) + String.valueOf(this.y + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* returns the X coordinate of the point starting at 0
|
|
||||||
* @return the X coordinate of the point starting at 0
|
|
||||||
* @author Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
public int getX() {
|
public int getX() {
|
||||||
return x;
|
return x;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* returns the Y coordinate of the point starting at 0
|
|
||||||
* @return the Y coordinate of the point starting at 0
|
|
||||||
* @author Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
public int getY() {
|
public int getY() {
|
||||||
return y;
|
return y;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* sets the X coordinate of the point starting at 0
|
|
||||||
* @param x the X coordinate of the point starting at 0
|
|
||||||
* @author Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
public void setX(int x) {
|
public void setX(int x) {
|
||||||
this.x = x;
|
this.x = x;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* sets the Y coordinate of the point starting at 0
|
|
||||||
* @param y the Y coordinate of the point starting at 0
|
|
||||||
* @author Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
public void setY(int y) {
|
public void setY(int y) {
|
||||||
this.y = y;
|
this.y = y;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* sets the X coordinate of the from its character value in alphanumeric form
|
|
||||||
* @param c the character to be transformed into
|
|
||||||
* @author Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
public void setX(char c) {
|
public void setX(char c) {
|
||||||
this.x = c - 'A';
|
this.x = c - 'A';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* checks whether a string is a valid alphanumeric point coordinate
|
|
||||||
* @param str the string to be tested
|
|
||||||
* @return whether the string is valid according to the regular expression `^[A-Z]\\d+$`
|
|
||||||
* @author Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
public static boolean isValidSyntax(String str) {
|
public static boolean isValidSyntax(String str) {
|
||||||
return str.matches("^[A-Z]\\d+$");
|
return str.matches("^[A-Z]\\d+$");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* returns whether two points are equal
|
|
||||||
* two points with equivalent coordinates are considered equal
|
|
||||||
* @param o the other object/Point to compare this one to
|
|
||||||
* @return whether the objects are equal
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if (this == o) return true;
|
if (this == o) return true;
|
||||||
|
@ -113,13 +52,6 @@ public class Point {
|
||||||
return p.getX() == this.getX() && p.getY() == this.getY();
|
return p.getX() == this.getX() && p.getY() == this.getY();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* determines whether two points are neighbours
|
|
||||||
* points are considered neighbours if their positions are equal or within a difference of 1 on both X and Y axis
|
|
||||||
* @param other the point to check for neighbourship
|
|
||||||
* @return whether the points are neighbours
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public boolean neighbours(Point other) {
|
public boolean neighbours(Point other) {
|
||||||
if (other == null) return false;
|
if (other == null) return false;
|
||||||
return (int)Math.abs(this.getX() - other.getX()) <= 1 && (int)Math.abs(this.getY() - other.getY()) <= 1;
|
return (int)Math.abs(this.getX() - other.getX()) <= 1 && (int)Math.abs(this.getY() - other.getY()) <= 1;
|
||||||
|
|
177
src/Ship.java
177
src/Ship.java
|
@ -6,51 +6,43 @@ 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 =
|
static List<List<ShipData>> semeterList = Arrays.asList(Arrays.asList(
|
||||||
Arrays.asList(
|
new ShipData(2, "PRG 1"),
|
||||||
Arrays.asList(
|
new ShipData(2, "GDI"),
|
||||||
new ShipData(2, "PRG 1"),
|
new ShipData(2, "MAT 1"),
|
||||||
new ShipData(2, "GDI"),
|
new ShipData(2, "THI"),
|
||||||
new ShipData(2, "MAT 1"),
|
new ShipData(4, "STP"),
|
||||||
new ShipData(2, "THI"),
|
new ShipData(6, "ENG")),
|
||||||
new ShipData(4, "STP"),
|
Arrays.asList(
|
||||||
new ShipData(6, "ENG")
|
new ShipData(2, "PRG 2"),
|
||||||
),
|
new ShipData(2, "DBS 1"),
|
||||||
Arrays.asList(
|
new ShipData(2, "MAT 2"),
|
||||||
new ShipData(2, "PRG 2"),
|
new ShipData(2, "STA"),
|
||||||
new ShipData(2, "DBS 1"),
|
new ShipData(2, "AUD")),
|
||||||
new ShipData(2, "MAT 2"),
|
Arrays.asList(
|
||||||
new ShipData(2, "STA"),
|
new ShipData(2, "PRG 3"),
|
||||||
new ShipData(2, "AUD")
|
new ShipData(2, "DBS 2"),
|
||||||
),
|
new ShipData(2, "MAT 3"),
|
||||||
Arrays.asList(
|
new ShipData(2, "BSN 1"),
|
||||||
new ShipData(2, "PRG 3"),
|
new ShipData(4, "PRP"),
|
||||||
new ShipData(2, "DBS 2"),
|
new ShipData(6, "BWL")),
|
||||||
new ShipData(2, "MAT 3"),
|
Arrays.asList(
|
||||||
new ShipData(2, "BSN 1"),
|
new ShipData(2, "WEB"),
|
||||||
new ShipData(4, "PRP"),
|
new ShipData(2, "SE 1"),
|
||||||
new ShipData(6, "BWL")
|
new ShipData(2, "CG 1"),
|
||||||
),
|
new ShipData(2, "BSN 2"),
|
||||||
Arrays.asList(
|
new ShipData(4, "SEM"),
|
||||||
new ShipData(2, "WEB"),
|
new ShipData(6, "E BWL")),
|
||||||
new ShipData(2, "SE 1"),
|
Arrays.asList(
|
||||||
new ShipData(2, "CG 1"),
|
new ShipData(2, "WPF 1"),
|
||||||
new ShipData(2, "BSN 2"),
|
new ShipData(2, "SE 2"),
|
||||||
new ShipData(4, "SEM"),
|
new ShipData(2, "CG 2"),
|
||||||
new ShipData(6, "E BWL")
|
new ShipData(2, "PXP 1"),
|
||||||
),
|
new ShipData(6, "EF 1")),
|
||||||
Arrays.asList(
|
Arrays.asList(
|
||||||
new ShipData(2, "WPF 1"),
|
new ShipData(2, "WPF 2"),
|
||||||
new ShipData(2, "SE 2"),
|
new ShipData(1, "PXP 2"),
|
||||||
new ShipData(2, "CG 2"),
|
new ShipData(8, "BAA"))
|
||||||
new ShipData(2, "PXP 1"),
|
|
||||||
new ShipData(6, "EF 1")
|
|
||||||
),
|
|
||||||
Arrays.asList(
|
|
||||||
new ShipData(2, "WPF 2"),
|
|
||||||
new ShipData(1, "PXP 2"),
|
|
||||||
new ShipData(8, "BAA")
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
|
|
||||||
private int size;
|
private int size;
|
||||||
|
@ -60,12 +52,6 @@ public class Ship {
|
||||||
private int hitsOnMe;
|
private int hitsOnMe;
|
||||||
private boolean sunk;
|
private boolean sunk;
|
||||||
|
|
||||||
/**
|
|
||||||
* initialises a Ship with a given size and name
|
|
||||||
* @param size the size of the ship
|
|
||||||
* @param name the name of the ship
|
|
||||||
* @author Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
public Ship (int size, String name) {
|
public Ship (int size, String name) {
|
||||||
this.size = size;
|
this.size = size;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
|
@ -75,23 +61,10 @@ public class Ship {
|
||||||
this.sunk = false;
|
this.sunk = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* resets the position of this ship
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public void resetPosition() {
|
public void resetPosition() {
|
||||||
this.position = null;
|
this.position = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* sets the position of this ship, provided it is valid
|
|
||||||
* @param pos the position to move the ship to
|
|
||||||
* @param horizontal whether the ship is horizontal or not
|
|
||||||
* @param shipsList the list of other ships on this board. It will be checked whether the ship is touching any of them
|
|
||||||
* @param boardSize the size of the board the ship is to be placed on
|
|
||||||
* @return true if the position was set successfully. false if the ship is out of the bounds of the board or touches a different ship
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public boolean setPosition(Point pos, boolean horizontal, List<Ship> shipsList, int boardSize) {
|
public boolean setPosition(Point pos, boolean horizontal, List<Ship> shipsList, int boardSize) {
|
||||||
if (!this.checkValidPlacement(pos, horizontal, shipsList, boardSize)) return false;
|
if (!this.checkValidPlacement(pos, horizontal, shipsList, boardSize)) return false;
|
||||||
|
|
||||||
|
@ -101,15 +74,6 @@ public class Ship {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* checks whether a position is valid for ship placement
|
|
||||||
* @param pos the position to check the ship placement at
|
|
||||||
* @param horizontal whether the ship is to be placed horizontally or not
|
|
||||||
* @param shipsList the list of other ships on this board. It will be checked whether the ship is touching any of them
|
|
||||||
* @param boardSize the size of the board the ship is to be placed on
|
|
||||||
* @return true if the position is valid. false if the ship is out of the bounds of the board or touches a different ship
|
|
||||||
* @author Florian Hantzschel, Peer Ole Wachtel, Luca Conte
|
|
||||||
*/
|
|
||||||
public boolean checkValidPlacement(Point pos, boolean horizontal, List<Ship> shipsList, int boardSize) {
|
public boolean checkValidPlacement(Point pos, boolean horizontal, List<Ship> shipsList, int boardSize) {
|
||||||
// ueberpruefe boundaries
|
// ueberpruefe boundaries
|
||||||
if (pos.getX() < 0 || pos.getY() < 0 || pos.getX() >= boardSize || pos.getY() >= boardSize) {
|
if (pos.getX() < 0 || pos.getY() < 0 || pos.getX() >= boardSize || pos.getY() >= boardSize) {
|
||||||
|
@ -158,11 +122,7 @@ public class Ship {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the Points on the ship if it were to be placed at position `pos` in orientation defined by `horizontal`
|
* Returns the Points on the ship if it were to be placed at positino `pos` in orientation defined by `horizontal`
|
||||||
* @param pos the position where the ship should be placed
|
|
||||||
* @param horizontal whether the ship should be placed horizontally
|
|
||||||
* @return a list of points the ship would occupy, were it placed at position `pos` in orientation `horizontal`
|
|
||||||
* @author Florian Hantzschel, Luca Conte
|
|
||||||
*/
|
*/
|
||||||
public List<Point> getVirtualOccupiedPoints(Point pos, boolean horizontal) {
|
public List<Point> getVirtualOccupiedPoints(Point pos, boolean horizontal) {
|
||||||
List<Point> points = new ArrayList<>();
|
List<Point> points = new ArrayList<>();
|
||||||
|
@ -177,30 +137,23 @@ public class Ship {
|
||||||
return points;
|
return points;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the Points the ship occupies
|
|
||||||
* @return a list of points the ship occupies
|
|
||||||
* @author Florian Hantzschel, Luca Conte
|
|
||||||
*/
|
|
||||||
public List<Point> getOccupiedPoints() {
|
public List<Point> getOccupiedPoints() {
|
||||||
return this.getVirtualOccupiedPoints(this.position, this.horizontal);
|
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.getX() + i : this.position.getX();
|
||||||
|
int y = this.horizontal ? this.position.getY() : this.position.getY() + i;
|
||||||
|
points.add(new Point(x, y));
|
||||||
|
}
|
||||||
|
return points;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* returns the position of this ship
|
|
||||||
* @return the position of this ship
|
|
||||||
* @author Peer Ole Wachte
|
|
||||||
*/
|
|
||||||
public Point getPosition() {
|
public Point getPosition() {
|
||||||
return position;
|
return position;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* checks whether the ship occupies a certain point
|
|
||||||
* @param pos the point to be checkd
|
|
||||||
* @return whether the point provided is one of the points occupied by the ship
|
|
||||||
* @author Peer Ole Wachtel, Lucas Bronson
|
|
||||||
*/
|
|
||||||
public boolean isShipOnPos(Point pos){
|
public boolean isShipOnPos(Point pos){
|
||||||
if(this.position == null){
|
if(this.position == null){
|
||||||
return false;
|
return false;
|
||||||
|
@ -212,13 +165,6 @@ public class Ship {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* "shoots" this ship.
|
|
||||||
* @return a hit response, depending on whether the ship was hit or not. If the amount of times
|
|
||||||
* the ship was hit is greater or equal to the size of the ship, the ship is considered sunk.
|
|
||||||
* @param pos the point where the ship is shot
|
|
||||||
* @author Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
public HitResponseType shootOnShip(Point pos) {
|
public HitResponseType shootOnShip(Point pos) {
|
||||||
if (this.isShipOnPos(pos)) {
|
if (this.isShipOnPos(pos)) {
|
||||||
hitsOnMe++;
|
hitsOnMe++;
|
||||||
|
@ -233,47 +179,22 @@ public class Ship {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* returns whether the ship has been sunk or not
|
|
||||||
* @return whether the ship has been sunk or not
|
|
||||||
* @author Peer Ole Wachtel
|
|
||||||
*/
|
|
||||||
public boolean isSunk() {
|
public boolean isSunk() {
|
||||||
return sunk;
|
return sunk;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* sets the orientation of the ship
|
|
||||||
* @param horizontal whether the ship is to be placed in a horizontal orientation
|
|
||||||
* @author Lucas Bronson
|
|
||||||
*/
|
|
||||||
public void setHorizontal(boolean horizontal) {
|
public void setHorizontal(boolean horizontal) {
|
||||||
this.horizontal = horizontal;
|
this.horizontal = horizontal;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* returns the size of the ship
|
|
||||||
* @return the size of the ship
|
|
||||||
* @author Lucas Bronson
|
|
||||||
*/
|
|
||||||
public int getSize() {
|
public int getSize() {
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* returns the name of the ship
|
|
||||||
* @return the name of the ship
|
|
||||||
* @author Lucas Bronson
|
|
||||||
*/
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
//potentiell falsch neu
|
||||||
* returns whether the ship has been placed or not
|
|
||||||
* @return whether the ship has been placed or not
|
|
||||||
* @author Lucas Bronson
|
|
||||||
*/
|
|
||||||
public boolean isPlaced(){
|
public boolean isPlaced(){
|
||||||
return this.position != null;
|
return this.position != null;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,37 +1,17 @@
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
/**
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public class SocketPackage {
|
public class SocketPackage {
|
||||||
|
|
||||||
private String name = "";
|
private String name = "";
|
||||||
private String data = "";
|
private String data = "";
|
||||||
|
|
||||||
/**
|
|
||||||
* initialises a socket package by prividing a package name and data
|
|
||||||
* @param name the name of the package
|
|
||||||
* @param data the data of the package
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public SocketPackage(String name, String data) {
|
public SocketPackage(String name, String data) {
|
||||||
this.setName(name);
|
this.setName(name);
|
||||||
this.setData(data);
|
this.setData(data);
|
||||||
}
|
}
|
||||||
/**
|
|
||||||
* initialises an empty socket package
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public SocketPackage() {
|
public SocketPackage() {
|
||||||
this("","");
|
this("","");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* initialises a socket package from a message
|
|
||||||
* the message is parsed according to https://github.com/lgc-4/ProgProjekt-Netzwerkstandard
|
|
||||||
* @param message the message to be parsed
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public SocketPackage(String message) {
|
public SocketPackage(String message) {
|
||||||
if (message.length() <= 0) {
|
if (message.length() <= 0) {
|
||||||
throw new IllegalArgumentException("Socket message cannot be empty.");
|
throw new IllegalArgumentException("Socket message cannot be empty.");
|
||||||
|
@ -45,50 +25,24 @@ public class SocketPackage {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* sets the package name
|
|
||||||
* the name is always stored in upper case
|
|
||||||
* @param name the new name of the package
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public void setName(String name) {
|
public void setName(String name) {
|
||||||
if (name == null) name = "";
|
if (name == null) name = "";
|
||||||
this.name = name.toUpperCase();
|
this.name = name.toUpperCase();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* sets the package data
|
|
||||||
* @param name the new data of the package
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public void setData(String data) {
|
public void setData(String data) {
|
||||||
if (data == null) data = "";
|
if (data == null) data = "";
|
||||||
this.data = data;
|
this.data = data;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* returns the name of the package
|
|
||||||
* @return the name of the package
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return this.name;
|
return this.name;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* returns the data of the package
|
|
||||||
* @return the data of the package
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public String getData() {
|
public String getData() {
|
||||||
return this.data;
|
return this.data;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* parses the package into a string according to https://github.com/lgc-4/ProgProjekt-Netzwerkstandard
|
|
||||||
* the package name and data are joined using a space " " `0x20`
|
|
||||||
* @return the package in string format
|
|
||||||
*/
|
|
||||||
public String toString() {
|
public String toString() {
|
||||||
if (this.data == null || this.data.length() == 0) {
|
if (this.data == null || this.data.length() == 0) {
|
||||||
return this.name;
|
return this.name;
|
||||||
|
@ -97,11 +51,6 @@ public class SocketPackage {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* returns the data string as a list, split at every space " " `0x20`
|
|
||||||
* @return the data string as a list, split at every space " " `0x20`
|
|
||||||
* @author Luca Conte
|
|
||||||
*/
|
|
||||||
public List<String> splitData() {
|
public List<String> splitData() {
|
||||||
return Arrays.asList(this.data.split(" "));
|
return Arrays.asList(this.data.split(" "));
|
||||||
}
|
}
|
||||||
|
|
|
@ -67,9 +67,9 @@ public class SoundHandler {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* fügt einen neuen Sound zum SoundHanlder hinzu
|
* TODO funktion beschreiben (potentiell nicht benötigte Funktion?)
|
||||||
* @param soundName der intern zu verwendende Name des Sounds
|
* @param soundName
|
||||||
* @param path der Dateipfad zur Sound Datei
|
* @param path
|
||||||
* @author Ole Wachtel
|
* @author Ole Wachtel
|
||||||
*/
|
*/
|
||||||
static void add(String soundName, String path){
|
static void add(String soundName, String path){
|
||||||
|
@ -77,8 +77,8 @@ public class SoundHandler {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* schaltet den Ton an oder aus
|
* TODO funktion beschreiben (potentiell nicht benötigte Funktion?)
|
||||||
* @param sound ob der sound an ist
|
* @param sound
|
||||||
* @author Ole Wachtel
|
* @author Ole Wachtel
|
||||||
*/
|
*/
|
||||||
static void setSoundOn(boolean sound){
|
static void setSoundOn(boolean sound){
|
||||||
|
|
|
@ -1,13 +1,6 @@
|
||||||
/**
|
|
||||||
* Diese Klasse implementiert den Einfachsten Ki Spieler.
|
|
||||||
* @author Florian und Florian
|
|
||||||
* */
|
|
||||||
public class SpecificAiPlayerEasy extends AiPlayer{
|
public class SpecificAiPlayerEasy extends AiPlayer{
|
||||||
|
|
||||||
/**
|
|
||||||
* Bein einfachen Ki Spieler wird nur der AiPlayer initialisiert und der Name gesetzt,
|
|
||||||
* da in der Eltern-Klasse AiPlayer eine default implementierung für alle Methoden existieren.
|
|
||||||
*/
|
|
||||||
public SpecificAiPlayerEasy() {
|
public SpecificAiPlayerEasy() {
|
||||||
super();
|
super();
|
||||||
this.setName("AI Player Easy");
|
this.setName("AI Player Easy");
|
||||||
|
|
|
@ -1,7 +1,3 @@
|
||||||
/**
|
|
||||||
* Diese Klasse implementiert den Harten Ki Spieler.
|
|
||||||
* @author Florian und Florian
|
|
||||||
* */
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
|
@ -14,17 +10,6 @@ public class SpecificAiPlayerHard extends AiPlayer{
|
||||||
private int nextChessRow;
|
private int nextChessRow;
|
||||||
private int nextChessCol;
|
private int nextChessCol;
|
||||||
|
|
||||||
// Enum für die Orientierung
|
|
||||||
private enum Orientierung {
|
|
||||||
UNBEKANNT,
|
|
||||||
HORIZONTAL,
|
|
||||||
VERTIKAL
|
|
||||||
}
|
|
||||||
private Orientierung orientierung;
|
|
||||||
private Point firstHit; // Speichert den ersten Treffer zur Bestimmung der Orientierung
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public SpecificAiPlayerHard() {
|
public SpecificAiPlayerHard() {
|
||||||
super();
|
super();
|
||||||
this.setName("AI Player Hard");
|
this.setName("AI Player Hard");
|
||||||
|
@ -35,8 +20,6 @@ public class SpecificAiPlayerHard extends AiPlayer{
|
||||||
this.random = new Random();
|
this.random = new Random();
|
||||||
this.nextChessRow = 0;
|
this.nextChessRow = 0;
|
||||||
this.nextChessCol = 0;
|
this.nextChessCol = 0;
|
||||||
this.orientierung = Orientierung.UNBEKANNT;
|
|
||||||
this.firstHit = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Checks if a position has already been shot at
|
// Checks if a position has already been shot at
|
||||||
|
@ -50,16 +33,16 @@ public class SpecificAiPlayerHard extends AiPlayer{
|
||||||
this.gridSize = super.board.getSize();
|
this.gridSize = super.board.getSize();
|
||||||
this.shotsFired = new boolean[gridSize][gridSize];
|
this.shotsFired = new boolean[gridSize][gridSize];
|
||||||
}
|
}
|
||||||
// Wenn wir noch Treffer in der Queue haben, diese priorisieren
|
// If there are hits to process, prioritize those
|
||||||
while (!hitQueue.isEmpty()) {
|
while (!hitQueue.isEmpty()) {
|
||||||
Point target = hitQueue.remove(0);
|
Point target = hitQueue.remove(0);
|
||||||
|
|
||||||
if (!alreadyShot(target)) {
|
if (!alreadyShot(target)) {
|
||||||
shotsFired[target.getX()][target.getY()] = true;
|
|
||||||
return target;
|
return target;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ansonsten weiterhin "Schachbrettmuster"
|
// Otherwise, use chess pattern targeting
|
||||||
int row = nextChessRow;
|
int row = nextChessRow;
|
||||||
int col = nextChessCol;
|
int col = nextChessCol;
|
||||||
while (alreadyShot(new Point(row, col))) {
|
while (alreadyShot(new Point(row, col))) {
|
||||||
|
@ -76,109 +59,12 @@ public class SpecificAiPlayerHard extends AiPlayer{
|
||||||
@Override
|
@Override
|
||||||
public synchronized void receiveHit(HitResponse hitResponse) {
|
public synchronized void receiveHit(HitResponse hitResponse) {
|
||||||
super.receiveHit(hitResponse);
|
super.receiveHit(hitResponse);
|
||||||
|
// If it's a hit or sunk, add adjacent cells to the hitsQueue
|
||||||
// Wenn es ein Treffer ist, Adjacent-Punkte hinzufügen
|
|
||||||
if (hitResponse.getHitResponse() == HitResponseType.HIT) {
|
if (hitResponse.getHitResponse() == HitResponseType.HIT) {
|
||||||
Point hitPoint = hitResponse.getPoint();
|
addAdjacentPoints(hitResponse.getPoint());
|
||||||
|
|
||||||
// Wenn wir noch keinen ersten Treffer haben, speicher ihn
|
|
||||||
if (firstHit == null) {
|
|
||||||
firstHit = hitPoint;
|
|
||||||
// Orientierung noch unbekannt: alle möglichen Richtungen hinzufügen
|
|
||||||
addAdjacentPoints(hitPoint);
|
|
||||||
} else {
|
|
||||||
// Wenn Orientierung noch nicht bestimmt, jetzt prüfen
|
|
||||||
if (this.orientierung == Orientierung.UNBEKANNT) {
|
|
||||||
// Prüfen, ob der zweite Treffer horizontal oder vertikal liegt
|
|
||||||
if (firstHit.getY() == hitPoint.getY()) {
|
|
||||||
this.orientierung = Orientierung.VERTIKAL;
|
|
||||||
} else if (firstHit.getX() == hitPoint.getX()) {
|
|
||||||
this.orientierung = Orientierung.HORIZONTAL;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Sobald die Orientierung erkannt wurde, entferne alle „unpassenden“ Punkte
|
|
||||||
if (this.orientierung != Orientierung.UNBEKANNT) {
|
|
||||||
cleanUpHitQueue();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Für den aktuellen Treffer nur in passender Orientierung erweitern
|
|
||||||
addPointsByOrientation(hitPoint);
|
|
||||||
}
|
|
||||||
} else if (hitResponse.getHitResponse() == HitResponseType.SUNK) {
|
|
||||||
this.orientierung = Orientierung.UNBEKANNT;
|
|
||||||
firstHit = null;
|
|
||||||
hitQueue.clear();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Entfernt aus der hitQueue alle Punkte, die nicht der erkannten Orientierung entsprechen.
|
|
||||||
*/
|
|
||||||
private void cleanUpHitQueue() {
|
|
||||||
if (firstHit == null || this.orientierung == Orientierung.UNBEKANNT) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
ArrayList<Point> toRemove = new ArrayList<>();
|
|
||||||
for (Point p : hitQueue) {
|
|
||||||
if (this.orientierung == Orientierung.HORIZONTAL) {
|
|
||||||
// HORIZONTAL => gleiche Zeile wie firstHit
|
|
||||||
if (p.getX() != firstHit.getX()) {
|
|
||||||
toRemove.add(p);
|
|
||||||
}
|
|
||||||
} else if (this.orientierung == Orientierung.VERTIKAL) {
|
|
||||||
// VERTICAL => gleiche Spalte wie firstHit
|
|
||||||
if (p.getY() != firstHit.getY()) {
|
|
||||||
toRemove.add(p);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
hitQueue.removeAll(toRemove);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Fügt benachbarte Felder in der **erkannten Orientierung** hinzu.
|
|
||||||
* - Ist die Orientierung HORIZONTAL, so werden nur links/rechts hinzugefügt.
|
|
||||||
* - Ist sie VERTICAL, so werden nur oben/unten hinzugefügt.
|
|
||||||
*/
|
|
||||||
private void addPointsByOrientation(Point point) {
|
|
||||||
if (this.orientierung == Orientierung.UNBEKANNT) {
|
|
||||||
// Fallback: füge alle benachbarten Punkte hinzu
|
|
||||||
addAdjacentPoints(point);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
int x = point.getX();
|
|
||||||
int y = point.getY();
|
|
||||||
if (this.orientierung == Orientierung.HORIZONTAL) {
|
|
||||||
// Gleiche Zeile => links und rechts vom Point
|
|
||||||
Point left = new Point(x, y - 1);
|
|
||||||
Point right = new Point(x, y + 1);
|
|
||||||
|
|
||||||
if (isValidPoint(left) && !alreadyShot(left) && !hitQueue.contains(left)) {
|
|
||||||
hitQueue.add(left);
|
|
||||||
}
|
|
||||||
if (isValidPoint(right) && !alreadyShot(right) && !hitQueue.contains(right)) {
|
|
||||||
hitQueue.add(right);
|
|
||||||
}
|
|
||||||
} else if (this.orientierung == Orientierung.VERTIKAL) {
|
|
||||||
// Gleiche Spalte => oben und unten
|
|
||||||
Point up = new Point(x - 1, y);
|
|
||||||
Point down = new Point(x + 1, y);
|
|
||||||
|
|
||||||
if (isValidPoint(up) && !alreadyShot(up) && !hitQueue.contains(up)) {
|
|
||||||
hitQueue.add(up);
|
|
||||||
}
|
|
||||||
if (isValidPoint(down) && !alreadyShot(down) && !hitQueue.contains(down)) {
|
|
||||||
hitQueue.add(down);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Diese Methode erweitert die hitsQueue um die umliegenden Punkte die Schiffe seien könnten.
|
|
||||||
* @param point
|
|
||||||
*/
|
|
||||||
private void addAdjacentPoints(Point point) {
|
private void addAdjacentPoints(Point point) {
|
||||||
int x = point.getX();
|
int x = point.getX();
|
||||||
int y = point.getY();
|
int y = point.getY();
|
||||||
|
@ -197,18 +83,12 @@ public class SpecificAiPlayerHard extends AiPlayer{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/**
|
|
||||||
* Die Methode gibt zurück, ob eine Position auf dem Board ist. (Boolean)
|
|
||||||
* @param point Punkt der geprüft werden soll
|
|
||||||
* @return Ist auf dem Board oder nicht.
|
|
||||||
*/
|
|
||||||
private boolean isValidPoint(Point point) {
|
private boolean isValidPoint(Point point) {
|
||||||
return point.getX() >= 0 && point.getX() < gridSize &&
|
return point.getX() >= 0 && point.getX() < gridSize &&
|
||||||
point.getY() >= 0 && point.getY() < gridSize;
|
point.getY() >= 0 && point.getY() < gridSize;
|
||||||
}
|
}
|
||||||
/**
|
|
||||||
* Ki Methode um zu schießen.
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public void aiShoot() {
|
public void aiShoot() {
|
||||||
this.enemy.receiveShoot(getNextMove());
|
this.enemy.receiveShoot(getNextMove());
|
||||||
|
|
|
@ -1,14 +1,8 @@
|
||||||
/**
|
|
||||||
* Diese Klasse implementiert den Medium Ki Spieler.
|
|
||||||
* @author Florian und Florian
|
|
||||||
* */
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class SpecificAiPlayerMedium extends AiPlayer{
|
public class SpecificAiPlayerMedium extends AiPlayer{
|
||||||
/**
|
|
||||||
* Liste an Punkten die beschossen werden sollen. (Mögliche weitere Segmente vom schiff)
|
|
||||||
*/
|
|
||||||
private List<Point> hitsQueue = new ArrayList<>();
|
private List<Point> hitsQueue = new ArrayList<>();
|
||||||
|
|
||||||
public SpecificAiPlayerMedium() {
|
public SpecificAiPlayerMedium() {
|
||||||
|
@ -16,9 +10,6 @@ public class SpecificAiPlayerMedium extends AiPlayer{
|
||||||
this.setName("AI Player Medium");
|
this.setName("AI Player Medium");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Ki Methode um zu schießen.
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public void aiShoot() {
|
public void aiShoot() {
|
||||||
Point nextShot = ComputeNextShot();
|
Point nextShot = ComputeNextShot();
|
||||||
|
@ -35,10 +26,6 @@ public class SpecificAiPlayerMedium extends AiPlayer{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Die Methode bestimmt welche Position als nächstes beschossen werden soll.
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public Point ComputeNextShot() {
|
public Point ComputeNextShot() {
|
||||||
Point nextShot;
|
Point nextShot;
|
||||||
|
|
||||||
|
@ -58,10 +45,6 @@ public class SpecificAiPlayerMedium extends AiPlayer{
|
||||||
return nextShot;
|
return nextShot;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Diese Methode erweitert die hitsQueue um die umliegenden Punkte die Schiffe seien könnten.
|
|
||||||
* @param point
|
|
||||||
*/
|
|
||||||
private void addAdjacentPoints(Point point) {
|
private void addAdjacentPoints(Point point) {
|
||||||
int x = point.getX();
|
int x = point.getX();
|
||||||
int y = point.getY();
|
int y = point.getY();
|
||||||
|
@ -81,22 +64,12 @@ public class SpecificAiPlayerMedium extends AiPlayer{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Diese Methode gibt zurück, ob eine Position schon beschossen wurde. (Boolean)
|
|
||||||
* @param p Punkt der geprüft werden soll
|
|
||||||
* @return wurde schon beschossen oder nicht.
|
|
||||||
*/
|
|
||||||
private boolean alreadyShot(Point p) {
|
private boolean alreadyShot(Point p) {
|
||||||
|
|
||||||
return this.enemy.board.getHitResponseOnPoint(p) != null;
|
return this.enemy.board.getHitResponseOnPoint(p) != null;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Die Methode gibt zurück, ob eine Position auf dem Board ist. (Boolean)
|
|
||||||
* @param point Punkt der geprüft werden soll
|
|
||||||
* @return Ist auf dem Board oder nicht.
|
|
||||||
*/
|
|
||||||
private boolean isValidPoint(Point point) {
|
private boolean isValidPoint(Point point) {
|
||||||
return point.getX() >= 0 && point.getX() < board.getSize() &&
|
return point.getX() >= 0 && point.getX() < board.getSize() &&
|
||||||
point.getY() >= 0 && point.getY() < board.getSize();
|
point.getY() >= 0 && point.getY() < board.getSize();
|
||||||
|
|
Loading…
Reference in New Issue