Compare commits
83 Commits
f20896566c
...
bfb25dfe2c
Author | SHA1 | Date |
---|---|---|
|
bfb25dfe2c | |
|
3370975e57 | |
|
f4cf28f4bf | |
|
7ef04711c3 | |
|
fdd2e6d2f1 | |
|
31e4d91baf | |
|
3c03e63899 | |
|
f7d2e14196 | |
|
8e4a516e2c | |
|
e27b852c14 | |
|
15c23c1f2c | |
|
0b2ee88316 | |
|
42fc341608 | |
|
10e5a8ef5e | |
|
1f04760f6a | |
|
db2b0532bc | |
|
8c953a1f2b | |
|
626ae041e5 | |
|
f5a0f8c9fe | |
|
f1adf06035 | |
|
e0748ceff9 | |
|
0935c3bcb7 | |
|
78f6dd01f9 | |
|
ec35646dab | |
|
d8804d5369 | |
|
6857df94e0 | |
|
d7637e1e51 | |
|
7b80e3dfd3 | |
|
37a984772c | |
|
c0f22cec6e | |
|
d9a93952d2 | |
|
ad61fcecb7 | |
|
b4f1de82bd | |
|
60e44f2e1a | |
|
7605646b80 | |
|
5be3f4daf3 | |
|
0391b3265a | |
|
864ca20e18 | |
|
9e07350f7d | |
|
b5f1151a4f | |
|
f565708461 | |
|
970550308b | |
|
d1dbcfe603 | |
|
256d769ccf | |
|
80b5d8e701 | |
|
d3f5421808 | |
|
587d88f764 | |
|
decef526da | |
|
bf78db9404 | |
|
a7e7d75c62 | |
|
88ec23c5e5 | |
|
bd3976cc0c | |
|
fd5a97cd6e | |
|
dfbe4b3c53 | |
|
5ba5919775 | |
|
00df38dd7a | |
|
f397803b64 | |
|
fd82e64814 | |
|
84865e5468 | |
|
adac40156a | |
|
6f9f5c5063 | |
|
bb61a68f24 | |
|
663991e418 | |
|
8a9e7fb4b2 | |
|
60e6ed1277 | |
|
5cf8befdc1 | |
|
ffa04a50cb | |
|
21563dd469 | |
|
7f5e031574 | |
|
44f2b0f0f1 | |
|
3b6ffeb82d | |
|
9ab97b5f0c | |
|
f151a89ea4 | |
|
c55a62663e | |
|
b958ffd6b9 | |
|
6a57a6e1ff | |
|
9ff0958fbc | |
|
6118d480d9 | |
|
7a9b70c271 | |
|
0ef34e18eb | |
|
15ff3034d3 | |
|
7ee0f9c097 | |
|
48bc601a0d |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
After Width: | Height: | Size: 854 B |
Binary file not shown.
After Width: | Height: | Size: 875 B |
Binary file not shown.
After Width: | Height: | Size: 889 B |
|
@ -1,8 +1,17 @@
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
public abstract class AiPlayer extends LocalPlayer {
|
|
||||||
|
|
||||||
public AiPlayer() {}
|
public abstract class AiPlayer extends LocalPlayer implements Runnable {
|
||||||
|
|
||||||
|
List<Thread> shootThreads;
|
||||||
|
|
||||||
|
public AiPlayer() {
|
||||||
|
this.setName("AI Player");
|
||||||
|
this.shootThreads = new ArrayList<>();
|
||||||
|
}
|
||||||
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
|
||||||
|
@ -10,15 +19,78 @@ public abstract class AiPlayer extends LocalPlayer {
|
||||||
return new Point(posx,posy);
|
return new Point(posx,posy);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AiSetShips() {
|
@Override
|
||||||
|
public void createBoard(int size) {
|
||||||
|
super.createBoard(size);
|
||||||
|
this.aiSetShips();
|
||||||
|
this.ready();
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
while(!super.board.getShips().get(i).setPosition(RandomPoint(), super.board.getShips(), super.board.getSize())) {}
|
//TODO: set horizontal
|
||||||
|
while(!super.board.getShips().get(i).setPosition(RandomPoint(), true, super.board.getShips(), super.board.getSize())) {}
|
||||||
} // 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AiShoot() {
|
public void aiShoot() {
|
||||||
super.board.hit(RandomPoint());
|
this.enemy.receiveShoot(RandomPoint());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public synchronized void receiveHit(HitResponse hitResponse) {
|
||||||
|
super.receiveHit(hitResponse);
|
||||||
|
if (this.myTurn) {
|
||||||
|
Thread t = new Thread(this);
|
||||||
|
t.start();
|
||||||
|
this.shootThreads.add(t);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public synchronized void receiveShoot(Point point) {
|
||||||
|
super.receiveShoot(point);
|
||||||
|
if (this.myTurn) {
|
||||||
|
Thread t = new Thread(this);
|
||||||
|
t.start();
|
||||||
|
this.shootThreads.add(t);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void beginTurn() {
|
||||||
|
Thread t = new Thread(this);
|
||||||
|
t.start();
|
||||||
|
this.shootThreads.add(t);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() {
|
||||||
|
Iterator<Thread> i = this.shootThreads.iterator();
|
||||||
|
while(i.hasNext()) {
|
||||||
|
Thread thread = i.next();
|
||||||
|
if (!thread.isAlive()) {
|
||||||
|
try {
|
||||||
|
thread.join();
|
||||||
|
i.remove();
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
Thread.sleep(300);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
this.aiShoot();
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -1,27 +1,86 @@
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
|
import java.io.BufferedWriter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.io.OutputStreamWriter;
|
import java.io.OutputStreamWriter;
|
||||||
import java.io.PrintWriter;
|
import java.net.InetSocketAddress;
|
||||||
|
import java.net.ServerSocket;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
|
|
||||||
public class AsyncSocket {
|
public class AsyncSocket {
|
||||||
private Socket socket;
|
private Socket socket;
|
||||||
private Thread checkerThread;
|
private Thread checkerThread;
|
||||||
|
private Thread connectorThread;
|
||||||
private AsyncSocketListener handler;
|
private AsyncSocketListener handler;
|
||||||
private boolean shouldStop = false;
|
private boolean shouldStop = false;
|
||||||
|
|
||||||
|
private String sendBuffer = "";
|
||||||
|
|
||||||
private BufferedReader in;
|
private BufferedReader in;
|
||||||
private PrintWriter out;
|
private BufferedWriter out;
|
||||||
|
|
||||||
|
public AsyncSocket(int port, AsyncSocketListener handler) {
|
||||||
|
this.setHandler(handler);
|
||||||
|
|
||||||
|
// start server in new thread
|
||||||
|
this.connectorThread = new Thread(() -> {
|
||||||
|
try {
|
||||||
|
ServerSocket serverSocket = new ServerSocket(port);
|
||||||
|
|
||||||
|
System.out.println("Waiting for client connection on port " + port);
|
||||||
|
|
||||||
|
Socket socket = serverSocket.accept();
|
||||||
|
|
||||||
|
System.out.println("Socket connected.");
|
||||||
|
|
||||||
|
serverSocket.close();
|
||||||
|
|
||||||
|
this.initSocket(socket);
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
// TODO: proper error handling
|
||||||
|
}
|
||||||
|
});
|
||||||
|
this.connectorThread.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
public AsyncSocket(InetSocketAddress address, AsyncSocketListener handler) {
|
||||||
|
this.setHandler(handler);
|
||||||
|
|
||||||
|
// start client in new thread
|
||||||
|
this.connectorThread = new Thread(() -> {
|
||||||
|
System.out.println("Connecting to " + address.toString());
|
||||||
|
|
||||||
|
Socket socket = new Socket();
|
||||||
|
try {
|
||||||
|
socket.connect(address, 10);
|
||||||
|
System.out.println("Socket connected.");
|
||||||
|
|
||||||
|
this.initSocket(socket);
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
throw new RuntimeException("Connection timed out");
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
this.connectorThread.start();
|
||||||
|
}
|
||||||
|
|
||||||
public AsyncSocket(Socket socket, AsyncSocketListener handler) throws IOException {
|
public AsyncSocket(Socket socket, AsyncSocketListener handler) throws IOException {
|
||||||
|
this.setHandler(handler);
|
||||||
|
this.initSocket(socket);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initSocket(Socket socket) throws IOException {
|
||||||
|
System.out.println("Initialising sockets");
|
||||||
this.socket = socket;
|
this.socket = socket;
|
||||||
|
|
||||||
this.in = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
|
this.in = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
|
||||||
this.out = new PrintWriter(new OutputStreamWriter(this.socket.getOutputStream()));
|
this.out = new BufferedWriter(new OutputStreamWriter(this.socket.getOutputStream()));
|
||||||
|
|
||||||
this.handler = handler;
|
|
||||||
|
|
||||||
|
this.shouldStop = false;
|
||||||
this.checkerThread = new Thread(() -> {
|
this.checkerThread = new Thread(() -> {
|
||||||
while (!this.shouldStop) {
|
while (!this.shouldStop) {
|
||||||
try {
|
try {
|
||||||
|
@ -29,21 +88,38 @@ public class AsyncSocket {
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!this.connectorThread.isAlive()) {
|
||||||
|
try {
|
||||||
|
this.connectorThread.join();
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (!this.in.ready()) continue;
|
if (!this.in.ready()) {
|
||||||
if (this.handler == null) continue;
|
continue;
|
||||||
|
}
|
||||||
|
if (this.handler == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
String message = this.in.readLine();
|
String message = this.in.readLine();
|
||||||
if (message.length() <= 0) continue;
|
if (message.length() <= 0) continue;
|
||||||
|
|
||||||
|
|
||||||
message = message.strip();
|
message = message.strip();
|
||||||
|
System.out.println("RECEIVED - " + message);
|
||||||
this.handler.receive(message);
|
this.handler.receive(message);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
System.out.println("starting checker thread");
|
||||||
this.checkerThread.start();
|
this.checkerThread.start();
|
||||||
|
this.flushBuffer();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setHandler(AsyncSocketListener handler) {
|
public void setHandler(AsyncSocketListener handler) {
|
||||||
|
@ -51,21 +127,36 @@ public class AsyncSocket {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void send(SocketPackage socketPackage) {
|
public synchronized void send(SocketPackage socketPackage) {
|
||||||
this.sendLine(socketPackage.toString());
|
this.sendLine(socketPackage.toString());
|
||||||
}
|
}
|
||||||
public void send(String packageName) {
|
public synchronized void send(String packageName) {
|
||||||
this.send(packageName, "");
|
this.send(packageName, "");
|
||||||
}
|
}
|
||||||
public 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;
|
||||||
}
|
}
|
||||||
this.sendLine(packageName + packageContent);
|
this.sendLine(packageName + packageContent);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void sendLine(String message) {
|
public synchronized void sendLine(String message) {
|
||||||
this.out.print(message + "\r\n");
|
sendBuffer = sendBuffer + message + "\r\n";
|
||||||
|
this.flushBuffer();
|
||||||
|
}
|
||||||
|
|
||||||
|
private synchronized void flushBuffer() {
|
||||||
|
if (!this.sendBuffer.isEmpty() && this.out != null) {
|
||||||
|
try {
|
||||||
|
this.out.write(sendBuffer);
|
||||||
|
System.out.println("SENT - " + sendBuffer);
|
||||||
|
sendBuffer = "";
|
||||||
|
this.out.flush();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
// TODO: handle writing error
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void close() {
|
public void close() {
|
||||||
|
@ -73,8 +164,11 @@ public class AsyncSocket {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
this.socket.close();
|
this.socket.close();
|
||||||
|
this.checkerThread.join();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,4 +1,5 @@
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class Board {
|
public class Board {
|
||||||
|
@ -10,6 +11,8 @@ public class Board {
|
||||||
|
|
||||||
public Board(int size) {
|
public Board(int size) {
|
||||||
this.size = size;
|
this.size = size;
|
||||||
|
this.ships = new ArrayList<>();
|
||||||
|
this.hits = new ArrayList<>();
|
||||||
this.createShip(size - 13);
|
this.createShip(size - 13);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,7 +21,7 @@ public class Board {
|
||||||
for (int i = 0; i < this.ships.size(); i++) {
|
for (int i = 0; i < this.ships.size(); i++) {
|
||||||
HitResponseType type = this.ships.get(i).shootOnShip(point);
|
HitResponseType type = this.ships.get(i).shootOnShip(point);
|
||||||
if ( type == HitResponseType.SUNK) {
|
if ( type == HitResponseType.SUNK) {
|
||||||
for (int ii = 0; i < this.ships.size(); ii++) {
|
for (int ii = 0; ii < this.ships.size(); ii++) {
|
||||||
if (!this.ships.get(ii).isSunk()) {
|
if (!this.ships.get(ii).isSunk()) {
|
||||||
response.setType(type);
|
response.setType(type);
|
||||||
this.addHits(response);
|
this.addHits(response);
|
||||||
|
@ -39,6 +42,19 @@ public class Board {
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void propagateSunk(Point p) {
|
||||||
|
HitResponse hit = this.getHitResponseOnPoint(p);
|
||||||
|
|
||||||
|
if (hit == null || hit.getType() != HitResponseType.HIT) return;
|
||||||
|
|
||||||
|
hit.setType(HitResponseType.SUNK);
|
||||||
|
|
||||||
|
propagateSunk(new Point(p.getX() + 1, p.getY()));
|
||||||
|
propagateSunk(new Point(p.getX() - 1, p.getY()));
|
||||||
|
propagateSunk(new Point(p.getX(), p.getY() + 1));
|
||||||
|
propagateSunk(new Point(p.getX(), p.getY() - 1));
|
||||||
|
}
|
||||||
|
|
||||||
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++) {
|
||||||
|
@ -51,14 +67,21 @@ public class Board {
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized boolean addHits(HitResponse hitResponse) {
|
public synchronized boolean addHits(HitResponse hitResponse) {
|
||||||
if (this.getHitResponsOnPoint(hitResponse.getPoint()) == null){
|
if (this.getHitResponseOnPoint(hitResponse.getPoint()) == null){
|
||||||
this.hits.add(hitResponse);
|
this.hits.add(hitResponse);
|
||||||
|
|
||||||
|
//Propagate sunk for display purposes
|
||||||
|
if (hitResponse.getType() == HitResponseType.SUNK) {
|
||||||
|
hitResponse.setType(HitResponseType.HIT);
|
||||||
|
propagateSunk(hitResponse.getPoint());
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized HitResponse getHitResponsOnPoint(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)){
|
||||||
return this.hits.get(i);
|
return this.hits.get(i);
|
||||||
|
|
|
@ -0,0 +1,253 @@
|
||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.event.MouseAdapter;
|
||||||
|
import java.awt.event.MouseEvent;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dient dem Aufbau der Matrix (Spielfeld) und füllt diese mit Funktion
|
||||||
|
* @author Lucas Bronson, Luca Conte, Joshua Kuklok
|
||||||
|
*/
|
||||||
|
public class BoardDisplay extends JPanel {
|
||||||
|
private JButton[][] fields;
|
||||||
|
private int gridSize;
|
||||||
|
private Ship currentShip;
|
||||||
|
private Player player;
|
||||||
|
private boolean horizontal = false;
|
||||||
|
private List<ShipButton> shipButtonList;
|
||||||
|
private boolean enemyBoard;
|
||||||
|
private Point mousePosition;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fügt Buttons zu Liste hinzu und aktualisiert Feld durch Aufruf von paintFields
|
||||||
|
* @param button
|
||||||
|
* @author Joshua Kuklok
|
||||||
|
*/
|
||||||
|
public void addShipButton(ShipButton button) {
|
||||||
|
shipButtonList.add(button);
|
||||||
|
paintFields();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gibt currentShip zurück
|
||||||
|
* @return currentShip
|
||||||
|
* @author Lucas Bronson, Luca Conte, Joshua Kuklok
|
||||||
|
*/
|
||||||
|
public Ship getCurrentShip() {
|
||||||
|
return currentShip;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Konstruktor der startLocalGame.
|
||||||
|
* @param gridSize Die Größe des Spielfelds
|
||||||
|
* @param player Der Spieler
|
||||||
|
* @author Lucas Bronson
|
||||||
|
*/
|
||||||
|
public BoardDisplay(int gridSize, Player player, boolean enemyBoard) {
|
||||||
|
super(new GridLayout(gridSize + 1, gridSize + 1)); // +1 wegen extra Zeile/Spalte
|
||||||
|
this.fields = new JButton[gridSize][gridSize];
|
||||||
|
this.shipButtonList = new ArrayList<>();
|
||||||
|
this.player = player;
|
||||||
|
this.gridSize = gridSize;
|
||||||
|
this.enemyBoard = enemyBoard;
|
||||||
|
|
||||||
|
// Erstellung vom Spielfeld
|
||||||
|
for (int i = 0; i <= gridSize; i++) {
|
||||||
|
for (int j = 0; j <= gridSize; j++) {
|
||||||
|
final int x = j - 1; // Temporäre Variable
|
||||||
|
final int y = i - 1; // Temporäre Variable
|
||||||
|
if (i == 0 && j == 0) {
|
||||||
|
add(new JLabel(" "));
|
||||||
|
} else if (i == 0) {
|
||||||
|
JLabel colLabel = new JLabel(String.valueOf(j));
|
||||||
|
colLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
||||||
|
colLabel.setFont(new Font("Roboto", Font.BOLD, 14));
|
||||||
|
add(colLabel);
|
||||||
|
} else if (j == 0) {
|
||||||
|
JLabel rowLabel = new JLabel(String.valueOf((char) ('A' + i - 1)));
|
||||||
|
rowLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
||||||
|
rowLabel.setFont(new Font("Arial", Font.BOLD, 14));
|
||||||
|
add(rowLabel);
|
||||||
|
} else {
|
||||||
|
// Spielfeld (interaktive Zellen)
|
||||||
|
JButton field = new JButton("");
|
||||||
|
field.setBackground(Color.BLUE);
|
||||||
|
field.setOpaque(true);
|
||||||
|
field.setBorderPainted(true);
|
||||||
|
fields[x][y] = field;
|
||||||
|
add(field);
|
||||||
|
|
||||||
|
// Um Mausinteraktionen zu ermöglichen (Rechts/- Linksklick, Hover)
|
||||||
|
field.addMouseListener(new MouseAdapter() {
|
||||||
|
|
||||||
|
// Um beim "Hovern" Position zu setzten und weiterzugeben.
|
||||||
|
@Override
|
||||||
|
public void mouseEntered(MouseEvent e) {
|
||||||
|
mousePosition = new Point(x, y);
|
||||||
|
paintFields();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Um nach "wegbewegen" der Maus wieder zu entfärben
|
||||||
|
@Override
|
||||||
|
public void mouseExited(MouseEvent e) {
|
||||||
|
paintFields();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Um Schiffe zu rotieren/platzieren
|
||||||
|
@Override
|
||||||
|
public void mouseClicked(MouseEvent e) {
|
||||||
|
if (SwingUtilities.isRightMouseButton(e)) {
|
||||||
|
togglePlacementDirection();
|
||||||
|
} else if (SwingUtilities.isLeftMouseButton(e)) {
|
||||||
|
Point o = new Point(x, y);
|
||||||
|
handleFieldClick(o);
|
||||||
|
SoundHandler.playSound("plop");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Aktuelles Schiff auswählen
|
||||||
|
* @param ship Schiff zum Auswählen
|
||||||
|
* @author Lucas Bronson, Joshua Kuklok
|
||||||
|
*/
|
||||||
|
public void selectCurrentShip(Ship ship) {
|
||||||
|
this.currentShip = ship;
|
||||||
|
paintFields();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Zurücksetzen von aktuellem Schiff und allen Schiffen des Spielers.
|
||||||
|
* Danach blau färben des Spielfeldes
|
||||||
|
* @author Lucas Bronson, Joshua Kuklok
|
||||||
|
*/
|
||||||
|
public void resetAllShips() {
|
||||||
|
this.currentShip = null;
|
||||||
|
for (Ship ship : player.getBoard().getShips()) {
|
||||||
|
ship.resetPosition();
|
||||||
|
}
|
||||||
|
paintFields();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wechselt die Platzierungsrichtung zwischen horizontal und vertikal.
|
||||||
|
* @author Lucas Bronson, Joshua Kuklok
|
||||||
|
*/
|
||||||
|
private void togglePlacementDirection() {
|
||||||
|
horizontal = !horizontal;
|
||||||
|
String direction = horizontal ? "horizontal" : "vertikal";
|
||||||
|
System.out.println("Platzierungsrichtung geändert zu: " + direction);
|
||||||
|
paintFields();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handhabt das Platzieren eines Schiffs auf dem Spielfeld.
|
||||||
|
* @param o Die Koordinaten des Klicks.
|
||||||
|
* @author Lucas Bronson, Luca Conte
|
||||||
|
*/
|
||||||
|
private void handleFieldClick(Point o) {
|
||||||
|
System.out.println("CLICK " + o);
|
||||||
|
if (!this.enemyBoard && !this.player.isGameRunning() && !this.player.isReady()) {
|
||||||
|
this.currentShip.setPosition(o, horizontal, player.getBoard().getShips(),this.gridSize);
|
||||||
|
}
|
||||||
|
if (this.enemyBoard && this.player.isGameRunning() && this.player.enemy.myTurn) {
|
||||||
|
this.player.enemy.shoot(o);
|
||||||
|
}
|
||||||
|
paintFields();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Färbt das Spielfeld blau, überprüft, ob das aktuelle Schiff null ist.
|
||||||
|
* Färbt eine Preview in das Spielfeld ein, ob Schiff setzen möglich ist
|
||||||
|
* Schiffe die gesetzt wurden, werden grau gefärbt
|
||||||
|
* Aufrufen von refreshButtonState um zu zeigen, ob Button drückbar ist
|
||||||
|
* @author Lucas Bronson, Luca Conte, Joshua Kuklok
|
||||||
|
*/
|
||||||
|
public void paintFields() {
|
||||||
|
List<Point> test=new ArrayList<>();
|
||||||
|
if(currentShip != null) {
|
||||||
|
test = currentShip.getVirtualOccupiedPoints(mousePosition, horizontal);
|
||||||
|
}
|
||||||
|
for(int i = 0; i < gridSize; i++) {
|
||||||
|
for(int j = 0; j < gridSize; j++) {
|
||||||
|
if(fields[i][j] == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (this.enemyBoard) {
|
||||||
|
// enemy board only accessible if game is running AND it's "enemy's" so local player's turn
|
||||||
|
if (!this.player.isGameRunning() || !this.player.enemy.myTurn) {
|
||||||
|
fields[i][j].setEnabled(false);
|
||||||
|
} else {
|
||||||
|
fields[i][j].setEnabled(true);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (this.player.isGameRunning() || this.player.isReady()) {
|
||||||
|
fields[i][j].setEnabled(false);
|
||||||
|
} else {
|
||||||
|
fields[i][j].setEnabled(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (this.enemyBoard) {
|
||||||
|
fields[i][j].setBackground(Color.WHITE);
|
||||||
|
} else {
|
||||||
|
fields[i][j].setBackground(Color.BLUE);
|
||||||
|
}
|
||||||
|
if (!this.player.isReady()) {
|
||||||
|
for(Point p : test) {
|
||||||
|
if(i==p.getX() && j==p.getY()) {
|
||||||
|
if (currentShip.checkValidPlacement(mousePosition,horizontal,player.getBoard().getShips(),gridSize)) {
|
||||||
|
fields[i][j].setBackground(Color.GREEN);
|
||||||
|
} else {
|
||||||
|
fields[i][j].setBackground(Color.RED);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for(Ship ship: player.getBoard().getShips()) {
|
||||||
|
if(ship.isShipOnPos(new Point(i,j))) {
|
||||||
|
fields[i][j].setBackground(Color.LIGHT_GRAY);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
HitResponse hit = this.player.getBoard().getHitResponseOnPoint(new Point(i, j));
|
||||||
|
if (hit != null) {
|
||||||
|
switch (hit.getType()) {
|
||||||
|
case HIT:
|
||||||
|
fields[i][j].setBackground(Color.ORANGE);
|
||||||
|
//SoundHandler.playSound("hit");
|
||||||
|
break;
|
||||||
|
case SUNK:
|
||||||
|
//SoundHandler.playSound("destroyed");
|
||||||
|
case VICTORY:
|
||||||
|
fields[i][j].setBackground(Color.RED);
|
||||||
|
break;
|
||||||
|
case MISS:
|
||||||
|
if (this.enemyBoard) {
|
||||||
|
//SoundHandler.playSound("miss");
|
||||||
|
fields[i][j].setBackground(Color.BLUE);
|
||||||
|
} else {
|
||||||
|
fields[i][j].setBackground(Color.CYAN);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for( ShipButton shipButton: shipButtonList) {
|
||||||
|
shipButton.refreshButtonState();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ruft paintFields auf, um Felder zu aktualisieren
|
||||||
|
* @author Luca Conte
|
||||||
|
*/
|
||||||
|
public void refresh() {
|
||||||
|
paintFields();
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,113 +2,224 @@ import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionEvent;
|
||||||
import java.awt.event.ActionListener;
|
import java.awt.event.ActionListener;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Das GameBoard dient als Panel, in dem das tatsächliche Spiel stattfindet.
|
||||||
|
* Der Benutzer kann hier seine Schiffe platzieren, das Spiel starten etc.
|
||||||
|
* @author Lucas Bronson, Luca Conte, Joshua Kuklok
|
||||||
|
*/
|
||||||
public class GameBoard extends JPanel {
|
public class GameBoard extends JPanel {
|
||||||
// Funktionshilfen
|
|
||||||
//int semesterCounter = 1; //TODO: ersetzen durch param von vorpanel
|
private BoardDisplay ownBoardPanel;
|
||||||
|
private BoardDisplay opponentBoardPanel;
|
||||||
|
|
||||||
|
private Player p1;
|
||||||
|
private Player p2;
|
||||||
|
|
||||||
// Grafiken
|
// Grafiken
|
||||||
ImageIcon backButtonIcon = new ImageIcon("graphics/backButton.png");
|
ImageIcon backButtonIcon = new ImageIcon("graphics/backButton.png");
|
||||||
ImageIcon gameBoardEmtpy = new ImageIcon("graphics/gameboardempty.png");
|
ImageIcon gameBoardEmtpy = new ImageIcon("graphics/gameboardempty.png");
|
||||||
ImageIcon gameBoardX = new ImageIcon("graphics/gameboardx.png");
|
ImageIcon gameBoardX = new ImageIcon("graphics/gameboardx.png");
|
||||||
|
|
||||||
|
// kontextText Text-Strings
|
||||||
|
String kT1 = "Bitte Schiffe setzten";
|
||||||
|
String kT2 = "Warte auf Gegner";
|
||||||
|
String kT3 = "Du fängst an";
|
||||||
|
String kT4 = "Dein Gegner fängt an";
|
||||||
|
String kT5 = "Du bist am Zug";
|
||||||
|
String kT6 = "Dein Gegner ist am Zug";
|
||||||
|
String kT7 = "Du hast das Spiel gewonnen";
|
||||||
|
String kT8 = "Du hast das Spiel verloren";
|
||||||
|
String kT9 = "Bitte erst alle Schiffe setzten";
|
||||||
|
|
||||||
// Labels
|
// Labels
|
||||||
JLabel frameTitle = new JLabel("GameBoard");
|
JLabel frameTitle = new JLabel("GameBoard");
|
||||||
JLabel kontextText = new JLabel("Beispielhafter Kontext-Text");
|
JLabel kontextText = new JLabel(kT1);
|
||||||
//kontextText.setFont(new Font("Roboto", Font.BOLD, 24)); //TODO setFont fixen
|
|
||||||
|
|
||||||
JButton backButton = new JButton(backButtonIcon);
|
// Buttons
|
||||||
// Eigene ModulButtons
|
JButton giveUpButton = new JButton("Aufgeben");
|
||||||
JButton leftPlayerModul1 = new JButton("Modul 1"); //TODO: Dynamische Namen durch abgleich mit Semester
|
|
||||||
JButton leftPlayerModul2 = new JButton("Modul 2");
|
|
||||||
JButton leftPlayerModul3 = new JButton("Modul 3");
|
|
||||||
JButton leftPlayerModul4 = new JButton("Modul 4");
|
|
||||||
JButton leftPlayerModul5 = new JButton("Modul 5");
|
|
||||||
JButton leftPlayerModul6 = new JButton("Modul 6");
|
|
||||||
JButton leftPlayerModul7 = new JButton("Reset");
|
|
||||||
// Gegnerische ModulButtons
|
|
||||||
JButton rightPlayerModul1 = new JButton("Modul 1");
|
|
||||||
JButton rightPlayerModul2 = new JButton("Modul 2");
|
|
||||||
JButton rightPlayerModul3 = new JButton("Modul 3");
|
|
||||||
JButton rightPlayerModul4 = new JButton("Modul 4");
|
|
||||||
JButton rightPlayerModul5 = new JButton("Modul 5");
|
|
||||||
JButton rightPlayerModul6 = new JButton("Modul 6");
|
|
||||||
JButton rightPlayerModul7 = new JButton("Bereit");
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Konstruktor von GameBoard.
|
||||||
|
* @param frame Der Mainframe der Anwendung über den alle Panels angezeigt werden.
|
||||||
|
* @param semesterCounter Ausgewähltes Semester
|
||||||
|
* @param p1 Erstes Spielerobjekt
|
||||||
|
* @param p2 Zweites Spielerobjekt
|
||||||
|
* @author Lucas Bronson, Luca Conte, Joshua Kuklok
|
||||||
|
*/
|
||||||
|
GameBoard(MainFrame frame, int semesterCounter,Player p1, Player p2) {
|
||||||
|
this.p1 = p1;
|
||||||
|
this.p2 = p2;
|
||||||
|
buildPanel(frame, semesterCounter);
|
||||||
|
List<Ship> shipsP1 =p1.getBoard().getShips();
|
||||||
|
List<Ship> shipsP2 =p2.getBoard().getShips();
|
||||||
|
|
||||||
|
giveUpButton.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
// TODO Hier könnte Ihr Backend Code stehen
|
||||||
|
frame.showPanel("MainMenu");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Timer für pulsierenden SchwarzGrau-Effekt
|
||||||
|
Timer timer = new Timer(10, new ActionListener() {
|
||||||
|
// Start-Grauwert (0 = Schwarz, 255 = Weiß)
|
||||||
|
private int value = 50;
|
||||||
|
private boolean increasing = false;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
kontextText.setForeground(new Color(value, value, value));
|
||||||
|
|
||||||
|
if (increasing) {
|
||||||
|
value++;
|
||||||
|
if (value >= 90) {
|
||||||
|
increasing = false;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
value--;
|
||||||
|
if (value <= 0) {
|
||||||
|
increasing = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Baut das grundlegende Spielboard
|
||||||
|
* @param frame Der Mainframe der Anwendung über den alle Panels angezeigt werden.
|
||||||
|
* @param semesterCounter Ausgewähltes Semester
|
||||||
|
* @author Lucas Bronson, Luca Conte, Joshua Kuklok
|
||||||
|
*/
|
||||||
public void buildPanel(MainFrame frame, int semesterCounter) {
|
public void buildPanel(MainFrame frame, int semesterCounter) {
|
||||||
// Hauptlayout - BorderLayout für die Anordnung der Komponenten
|
// Hauptlayout - BorderLayout für die Anordnung der Komponenten
|
||||||
setLayout(new BorderLayout());
|
setLayout(new BorderLayout());
|
||||||
|
|
||||||
|
// Spielfelder erstellen (eigenes und gegnerisches)
|
||||||
|
// Spielfelder werden in BoardDisplay erstellt
|
||||||
|
int gridSize = GameController.semesterToBoardSize(semesterCounter); // Größe des Spielfelds
|
||||||
|
this.ownBoardPanel = new BoardDisplay(gridSize,p1, false);
|
||||||
|
this.opponentBoardPanel = new BoardDisplay(gridSize, p2, true);
|
||||||
|
|
||||||
// Panel für das Kontext-Text-Feld
|
// Panel für das Kontext-Text-Feld
|
||||||
JPanel headerPanel = new JPanel();
|
JPanel headerPanel = new JPanel();
|
||||||
headerPanel.setLayout(new BorderLayout());
|
headerPanel.setLayout(new BorderLayout());
|
||||||
headerPanel.add(kontextText, BorderLayout.WEST);
|
headerPanel.add(kontextText, BorderLayout.WEST);
|
||||||
headerPanel.add(backButton, BorderLayout.EAST);
|
kontextText.setFont(new Font("Roboto", Font.BOLD, 30)); //TODO setFont fixen
|
||||||
|
headerPanel.add(giveUpButton, BorderLayout.EAST);
|
||||||
|
|
||||||
// Panel für die Buttons des linken Spielers (ganz links)
|
|
||||||
JPanel leftButtonsPanel = new JPanel();
|
JPanel leftButtonsPanel = new JPanel();
|
||||||
leftButtonsPanel.setLayout(new GridLayout(7, 1)); // 6 Buttons untereinander
|
leftButtonsPanel.setLayout(new GridLayout(7, 1)); // 6 Buttons untereinander
|
||||||
leftButtonsPanel.add(leftPlayerModul1);
|
|
||||||
leftButtonsPanel.add(leftPlayerModul2);
|
|
||||||
leftButtonsPanel.add(leftPlayerModul3);
|
|
||||||
leftButtonsPanel.add(leftPlayerModul4);
|
|
||||||
leftButtonsPanel.add(leftPlayerModul5);
|
|
||||||
leftButtonsPanel.add(leftPlayerModul6);
|
|
||||||
leftButtonsPanel.add(leftPlayerModul7);
|
|
||||||
|
|
||||||
// Panel für die Buttons des rechten Spielers (ganz rechts)
|
|
||||||
JPanel rightButtonsPanel = new JPanel();
|
JPanel rightButtonsPanel = new JPanel();
|
||||||
rightButtonsPanel.setLayout(new GridLayout(7, 1)); // 6 Buttons untereinander
|
rightButtonsPanel.setLayout(new GridLayout(7, 1));
|
||||||
rightButtonsPanel.add(rightPlayerModul1);
|
|
||||||
rightButtonsPanel.add(rightPlayerModul2);
|
|
||||||
rightButtonsPanel.add(rightPlayerModul3);
|
|
||||||
rightButtonsPanel.add(rightPlayerModul4);
|
|
||||||
rightButtonsPanel.add(rightPlayerModul5);
|
|
||||||
rightButtonsPanel.add(rightPlayerModul6);
|
|
||||||
rightButtonsPanel.add(rightPlayerModul7);
|
|
||||||
|
|
||||||
// Spielfelder erstellen (eigenes und gegnerisches)
|
//Buttons in eine Gruppe packen damit diese beim drücken eines anderen Buttons wieder entwählt werden
|
||||||
int gridSize = 13 + semesterCounter; // Größe des Spielfelds
|
ButtonGroup leftButtonGroup= new ButtonGroup();
|
||||||
JPanel ownBoardPanel = new JPanel(new GridLayout(gridSize, gridSize));
|
ButtonGroup rightButtonGroup= new ButtonGroup();
|
||||||
JPanel opponentBoardPanel = new JPanel(new GridLayout(gridSize, gridSize));
|
|
||||||
|
|
||||||
// Buttons für das eigene Spielfeld hinzufügen
|
// Panel für die Buttons des linken Spielers (ganz links)
|
||||||
for (int i = 0; i < gridSize; i++) {
|
for(Ship ship : p1.getBoard().getShips()) {
|
||||||
for (int j = 0; j < gridSize; j++) {
|
ShipButton shipButton= new ShipButton(ship,ownBoardPanel);
|
||||||
ownBoardPanel.add(new JButton(gameBoardEmtpy));
|
leftButtonsPanel.add(shipButton);
|
||||||
}
|
leftButtonGroup.add(shipButton);
|
||||||
|
ownBoardPanel.addShipButton(shipButton);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Buttons für das gegnerische Spielfeld hinzufügen
|
for(Ship ship : p2.getBoard().getShips()) {
|
||||||
for (int i = 0; i < gridSize; i++) {
|
ShipButton shipButton= new ShipButton(ship,opponentBoardPanel);
|
||||||
for (int j = 0; j < gridSize; j++) {
|
rightButtonsPanel.add(shipButton);
|
||||||
opponentBoardPanel.add(new JButton(gameBoardEmtpy));
|
rightButtonGroup.add(shipButton);
|
||||||
}
|
opponentBoardPanel.addShipButton(shipButton);
|
||||||
|
shipButton.setEnabled(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
JToggleButton readyButton = new JToggleButton("Bereit");
|
||||||
|
readyButton.setBackground(Color.GREEN);
|
||||||
|
rightButtonsPanel.add(readyButton);
|
||||||
|
|
||||||
|
JToggleButton resetButton = new JToggleButton("Reset");
|
||||||
|
resetButton.setBackground(Color.RED);
|
||||||
|
leftButtonsPanel.add(resetButton);
|
||||||
|
|
||||||
|
resetButton.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
ownBoardPanel.resetAllShips();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
// TODO buttons erst disablen wenn alle Schiffe platziert sind
|
||||||
|
// Um Bereit-Meldung and Backend zu geben, kontextText zu setzten und ready/reset Button zu deaktivieren
|
||||||
|
readyButton.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
kontextText.setText(kT2);
|
||||||
|
p1.ready();
|
||||||
|
if(true) {
|
||||||
|
readyButton.setEnabled(false);
|
||||||
|
resetButton.setEnabled(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// Panel für beide Spielfelder (nebeneinander in der Mitte)
|
// Panel für beide Spielfelder (nebeneinander in der Mitte)
|
||||||
JPanel centerPanel = new JPanel();
|
JPanel centerPanel = new JPanel();
|
||||||
centerPanel.setLayout(new GridLayout(1, 2, 20, 0)); // 2 Spielfelder nebeneinander, mit Abstand von 20 Pixeln
|
centerPanel.setLayout(new GridLayout(1, 2, 20, 0)); // 2 Spielfelder nebeneinander, mit Abstand von 20 Pixeln
|
||||||
centerPanel.add(ownBoardPanel);
|
centerPanel.add(ownBoardPanel);
|
||||||
centerPanel.add(opponentBoardPanel);
|
centerPanel.add(opponentBoardPanel);
|
||||||
|
|
||||||
|
// Spieler-Namen über den Spielfeldern hinzufügen
|
||||||
|
JPanel playerNamesPanel = new JPanel();
|
||||||
|
playerNamesPanel.setLayout(new GridLayout(1, 2)); // Zwei Labels nebeneinander
|
||||||
|
JLabel player1NameLabel = new JLabel(p1.getName(), SwingConstants.CENTER);
|
||||||
|
JLabel player2NameLabel = new JLabel(p2.getName(), SwingConstants.CENTER);
|
||||||
|
System.out.println("Name in Gameboard: " + player1NameLabel.getText());
|
||||||
|
|
||||||
|
// Schrift und Formatierung der Labels
|
||||||
|
player1NameLabel.setFont(new Font("Roboto", Font.BOLD, 18));
|
||||||
|
player2NameLabel.setFont(new Font("Roboto", Font.BOLD, 18));
|
||||||
|
|
||||||
|
// Spieler-Labels zum Panel hinzufügen
|
||||||
|
playerNamesPanel.add(player1NameLabel);
|
||||||
|
playerNamesPanel.add(player2NameLabel);
|
||||||
|
|
||||||
|
// Spieler-Namen-Panel oberhalb der Spielfelder hinzufügen
|
||||||
|
JPanel namesAndBoardsPanel = new JPanel(new BorderLayout());
|
||||||
|
namesAndBoardsPanel.add(playerNamesPanel, BorderLayout.NORTH);
|
||||||
|
namesAndBoardsPanel.add(centerPanel, BorderLayout.CENTER);
|
||||||
|
|
||||||
// Panels dem Hauptlayout hinzufügen
|
// Panels dem Hauptlayout hinzufügen
|
||||||
add(leftButtonsPanel, BorderLayout.WEST);
|
add(leftButtonsPanel, BorderLayout.WEST);
|
||||||
add(rightButtonsPanel, BorderLayout.EAST);
|
add(rightButtonsPanel, BorderLayout.EAST);
|
||||||
add(headerPanel, BorderLayout.NORTH);
|
add(headerPanel, BorderLayout.NORTH);
|
||||||
add(centerPanel, BorderLayout.CENTER);
|
add(namesAndBoardsPanel, BorderLayout.CENTER);
|
||||||
|
timer.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
GameBoard(MainFrame frame,int semesterCounter) {
|
/**
|
||||||
buildPanel(frame, semesterCounter);
|
* Aktualisiert Zustand(kontextText) je nach Zug
|
||||||
/*
|
* @author Luca Conte
|
||||||
rightPlayerRightButton.addActionListener(new ActionListener() {
|
*/
|
||||||
@Override
|
public void refresh() {
|
||||||
public void actionPerformed(ActionEvent e) {
|
if (this.p1.myTurn) {
|
||||||
toggleRightPlayerIcon();
|
this.kontextText.setText(kT5);
|
||||||
updateTextFields();
|
} else {
|
||||||
}
|
this.kontextText.setText(kT6);
|
||||||
}); */
|
}
|
||||||
backButton.addActionListener(e -> frame.showPanel("MainMenu"));
|
this.ownBoardPanel.refresh();
|
||||||
|
this.opponentBoardPanel.refresh();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Getter für Player1
|
||||||
|
* @return Player 1
|
||||||
|
* @author Peer Ole Wachtel
|
||||||
|
*/
|
||||||
|
public Player getP1() {
|
||||||
|
return p1;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,5 @@
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.net.ServerSocket;
|
|
||||||
import java.net.Socket;
|
|
||||||
import java.net.SocketTimeoutException;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -19,12 +16,12 @@ public class GameController {
|
||||||
GameController.mainFrame = mainFrame;
|
GameController.mainFrame = mainFrame;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Connection timeout for client sockets in milliseconds
|
|
||||||
public static final int CONNECTION_TIMEOUT = 10 * 1000;
|
|
||||||
|
|
||||||
public static int semesterToBoardSize(int semester) {
|
public static int semesterToBoardSize(int semester) {
|
||||||
return semester + 13;
|
return semester + 13;
|
||||||
}
|
}
|
||||||
|
public static int boardSizeToSemester(int size) {
|
||||||
|
return size - 13;
|
||||||
|
}
|
||||||
|
|
||||||
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
|
||||||
|
@ -33,34 +30,15 @@ public class GameController {
|
||||||
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;
|
||||||
|
|
||||||
boolean localPlayerIsServer = address.getHostName() == null;
|
boolean localPlayerIsServer = address.getHostName() == null || address.getHostName().isEmpty() || address.getHostName().equals("0.0.0.0");
|
||||||
|
|
||||||
if (localPlayerIsServer) {
|
if (localPlayerIsServer) {
|
||||||
// SERVER MODE
|
// SERVER MODE
|
||||||
|
clientSocket = new AsyncSocket(address.getPort(), null);
|
||||||
|
|
||||||
ServerSocket serverSocket = new ServerSocket(address.getPort());
|
|
||||||
|
|
||||||
System.out.println("Waiting for client connection...");
|
|
||||||
|
|
||||||
clientSocket = new AsyncSocket(serverSocket.accept(), null);
|
|
||||||
|
|
||||||
serverSocket.close();
|
|
||||||
} else {
|
} else {
|
||||||
// CLIENT MODE
|
// CLIENT MODE
|
||||||
|
clientSocket = new AsyncSocket(address, null);
|
||||||
Socket socket = new Socket();
|
|
||||||
|
|
||||||
try {
|
|
||||||
socket.connect(address, CONNECTION_TIMEOUT);
|
|
||||||
|
|
||||||
clientSocket = new AsyncSocket(socket, null);
|
|
||||||
} catch (SocketTimeoutException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
throw new RuntimeException("Connection timed out");
|
|
||||||
} finally {
|
|
||||||
socket.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
clientSocket.send("VERSION", "Gruppe03 " + String.join(" ", supportedVersions.keySet()));
|
clientSocket.send("VERSION", "Gruppe03 " + String.join(" ", supportedVersions.keySet()));
|
||||||
|
@ -84,17 +62,24 @@ public class GameController {
|
||||||
OnlinePlayer onlinePlayer;
|
OnlinePlayer onlinePlayer;
|
||||||
try {
|
try {
|
||||||
localPlayer = localPlayerClass.getDeclaredConstructor().newInstance();
|
localPlayer = localPlayerClass.getDeclaredConstructor().newInstance();
|
||||||
onlinePlayer = onlinePlayerClass.getDeclaredConstructor().newInstance((Integer)size, clientSocket);
|
onlinePlayer = onlinePlayerClass.getDeclaredConstructor(Integer.class, AsyncSocket.class).newInstance((Integer)size, clientSocket);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
throw new RuntimeException("Unable to instantiate players");
|
throw new RuntimeException("Unable to instantiate players");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
localPlayer.isServer = localPlayerIsServer;
|
localPlayer.isServer = localPlayerIsServer;
|
||||||
onlinePlayer.isServer = !localPlayerIsServer;
|
onlinePlayer.isServer = !localPlayerIsServer;
|
||||||
|
|
||||||
startGameWithInstancedPlayers(localPlayer, onlinePlayer);
|
localPlayer.setName(localPlayerName);
|
||||||
|
|
||||||
|
localPlayer.setEnemy(onlinePlayer);
|
||||||
|
onlinePlayer.setEnemy(localPlayer);
|
||||||
|
|
||||||
|
onlinePlayer.sendIAM();
|
||||||
|
|
||||||
|
// Start game only after IAM Package was exchanged
|
||||||
} else {
|
} else {
|
||||||
throw new RuntimeException("Unexpected Package received before game initialisation");
|
throw new RuntimeException("Unexpected Package received before game initialisation");
|
||||||
}
|
}
|
||||||
|
@ -175,15 +160,23 @@ public class GameController {
|
||||||
throw new RuntimeException("Unable to instantiate players");
|
throw new RuntimeException("Unable to instantiate players");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
localPlayer.isServer = true;
|
||||||
|
aiPlayer.isServer = false;
|
||||||
|
|
||||||
|
|
||||||
|
localPlayer.setEnemy(aiPlayer);
|
||||||
|
aiPlayer.setEnemy(localPlayer);
|
||||||
|
|
||||||
localPlayer.createBoard(size);
|
localPlayer.createBoard(size);
|
||||||
aiPlayer.createBoard(size);
|
aiPlayer.createBoard(size);
|
||||||
|
|
||||||
startGameWithInstancedPlayers(localPlayer, aiPlayer);
|
localPlayer.setName(localPlayerName);
|
||||||
|
|
||||||
|
startGameWithInstancedPlayers(localPlayer, aiPlayer, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void startGameWithInstancedPlayers(LocalPlayer p1, Player p2) {
|
public static void startGameWithInstancedPlayers(LocalPlayer p1, Player p2, int boardSize) {
|
||||||
p1.setEnemy(p2);
|
mainFrame.showPanelSLG("GameBoard", boardSizeToSemester(boardSize), p1, p2);
|
||||||
p2.setEnemy(p1);
|
|
||||||
|
|
||||||
// TODO: frontend configuration
|
// TODO: frontend configuration
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,21 +1,26 @@
|
||||||
|
/**
|
||||||
|
* Hauptklasse über die der MainFrame gestartet wird
|
||||||
|
* @author Lucas Bronson, Ole Wachtel, Joshua Kuklok
|
||||||
|
*/
|
||||||
public class HalloSchiffeVersenken {
|
public class HalloSchiffeVersenken {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Erstellt und setzt den Mainframe
|
||||||
|
* @param args Argumente an Main durch Konsole etc.
|
||||||
|
* @throws InterruptedException
|
||||||
|
*/
|
||||||
public static void main(String[] args) throws InterruptedException {
|
public static void main(String[] args) throws InterruptedException {
|
||||||
MainFrame mf = new MainFrame();
|
MainFrame mf = new MainFrame();
|
||||||
mf.setVisible(true);
|
mf.setVisible(true);
|
||||||
|
|
||||||
System.out.println("HelloSchiffeVersenekn");
|
|
||||||
|
|
||||||
|
|
||||||
System.out.println("sound");
|
|
||||||
SoundHandler.playSound("hit");
|
|
||||||
|
|
||||||
Thread.sleep(10000);
|
|
||||||
|
|
||||||
SoundHandler.setSoundOn(false);
|
|
||||||
System.out.println("sound off");
|
|
||||||
SoundHandler.playSound("hit");
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
*TODO
|
||||||
|
* Alle Textfonts anpassen
|
||||||
|
* SOUND interrupt?
|
||||||
|
* BACKEND aufruf bei aufgeben
|
||||||
|
* check bevor ready ob alle schiffe platziert?
|
||||||
|
* testcode rausnehmen
|
||||||
|
* FidgetButton als surprise (button während spiels, der zufällig farbe wechseln kann und sounds abspielt)
|
||||||
|
*/
|
|
@ -1,27 +1,58 @@
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Das JoinGame Panel dient zum setzten des Ports/IP-Adresse.
|
||||||
|
* Anschließend kann das Verbinden Panel gezeigt werden.
|
||||||
|
* @author Lucas Bronson
|
||||||
|
*/
|
||||||
public class JoinGame extends JPanel {
|
public class JoinGame extends JPanel {
|
||||||
|
|
||||||
|
// Grafiken
|
||||||
ImageIcon backButtonIcon = new ImageIcon("graphics/backButton.png");
|
ImageIcon backButtonIcon = new ImageIcon("graphics/backButton.png");
|
||||||
|
|
||||||
|
// Labels
|
||||||
JLabel spielBeitretenLabel;
|
JLabel spielBeitretenLabel;
|
||||||
JLabel ipLabel = new JLabel("IP-Adresse");
|
JLabel ipLabel = new JLabel("IP-Adresse");
|
||||||
JLabel portLabel = new JLabel("Port");
|
JLabel portLabel = new JLabel("Port");
|
||||||
|
|
||||||
|
// Textfelder
|
||||||
JTextField ipTextField = new JTextField(20);
|
JTextField ipTextField = new JTextField(20);
|
||||||
JTextField portTextField = new JTextField(20);
|
JTextField portTextField = new JTextField(20);
|
||||||
|
|
||||||
|
// Buttons
|
||||||
JButton losButton = new JButton("Los!");
|
JButton losButton = new JButton("Los!");
|
||||||
JButton backButton = new JButton(backButtonIcon);
|
JButton backButton = new JButton(backButtonIcon);
|
||||||
|
|
||||||
|
// Font
|
||||||
Font robotoFont = new Font("Roboto", Font.BOLD, 45);
|
Font robotoFont = new Font("Roboto", Font.BOLD, 45);
|
||||||
|
|
||||||
public JoinGame(MainFrame frame,int g) {
|
/**
|
||||||
|
* Erstellt mittels Funktionsaufrufen das Panel.
|
||||||
|
* @param frame Der Mainframe der Anwendung über den alle Panels angezeigt werden.
|
||||||
|
* @param g int-Anzeige, ob es sich um "spiel erstellen" oder "spiel beitreten" handelt.
|
||||||
|
* @param playerType int-Anzeige, ob es sich um einen HumanPlayer,AIEasy... handelt.
|
||||||
|
* @param playerName Name des Spielers
|
||||||
|
* @author Lucas Bronson, Joshua Kuklok
|
||||||
|
*/
|
||||||
|
public JoinGame(MainFrame frame,int g,int playerType,String playerName) {
|
||||||
setLayout(null);
|
setLayout(null);
|
||||||
buildPanel(frame,g);
|
buildPanel(frame,g,playerType,playerName);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void buildPanel(MainFrame frame,int g) {
|
/**
|
||||||
|
* Erstellt das Panel.
|
||||||
|
* @param frame Der Mainframe der Anwendung über den alle Panels angezeigt werden.
|
||||||
|
* @param g int-Anzeige, ob es sich um "spiel erstellen" oder "spiel beitreten" handelt.
|
||||||
|
* @param playerType int-Anzeige, ob es sich um einen HumanPlayer,AIEasy... handelt.
|
||||||
|
* @param playerName Name des Spielers
|
||||||
|
* @author Lucas Bronson, Joshua Kuklok
|
||||||
|
*/
|
||||||
|
private void buildPanel(MainFrame frame,int g,int playerType,String playerName) {
|
||||||
if(g==1){
|
if(g==1){
|
||||||
spielBeitretenLabel= new JLabel("Spiel beitreten");
|
spielBeitretenLabel= new JLabel("Spiel beitreten");
|
||||||
}else{
|
}else{
|
||||||
|
@ -32,12 +63,14 @@ public class JoinGame extends JPanel {
|
||||||
losButton.setBounds(320, 225, 100, 50);
|
losButton.setBounds(320, 225, 100, 50);
|
||||||
backButton.setBounds(1380, 20, 80, 80);
|
backButton.setBounds(1380, 20, 80, 80);
|
||||||
|
|
||||||
ipLabel.setBounds(50, 125, 200, 30);
|
|
||||||
portLabel.setBounds(50, 200, 200, 30);
|
portLabel.setBounds(50, 200, 200, 30);
|
||||||
|
|
||||||
ipTextField.setBounds(50, 150, 250, 50);
|
if(g==1) { // Wenn man Spiel erstellen will, werden IP-Felder nicht angezeigt.
|
||||||
portTextField.setBounds(50, 225, 250, 50);
|
ipLabel.setBounds(50, 125, 200, 30);
|
||||||
|
ipTextField.setBounds(50, 150, 250, 50);
|
||||||
|
}
|
||||||
|
|
||||||
|
portTextField.setBounds(50, 225, 250, 50);
|
||||||
|
|
||||||
spielBeitretenLabel.setFont(robotoFont.deriveFont(50f));
|
spielBeitretenLabel.setFont(robotoFont.deriveFont(50f));
|
||||||
|
|
||||||
|
@ -49,7 +82,45 @@ public class JoinGame extends JPanel {
|
||||||
add(portTextField);
|
add(portTextField);
|
||||||
add(backButton);
|
add(backButton);
|
||||||
|
|
||||||
backButton.addActionListener(e -> frame.showPanel("MultiplayerGame"));
|
// ActionListener für Buttons.
|
||||||
losButton.addActionListener(e -> frame.showPanel("Verbinden"));
|
// Um in das MultiplayerGame zurückzuwechseln.
|
||||||
|
backButton.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
frame.showPanel("MultiplayerGame");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Um das Verbinden Panel anzuzeigen und Daten an Backend weiterzureichen.
|
||||||
|
losButton.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
|
||||||
|
String ipAddress = ipTextField.getText();
|
||||||
|
if (ipAddress.isEmpty()) {
|
||||||
|
ipAddress = "0.0.0.0";
|
||||||
|
}
|
||||||
|
String portText = portTextField.getText();
|
||||||
|
|
||||||
|
int port = Integer.parseInt(portText);
|
||||||
|
InetSocketAddress address = new InetSocketAddress(ipAddress, port);
|
||||||
|
|
||||||
|
frame.showPanel("Verbinden");
|
||||||
|
|
||||||
|
try {
|
||||||
|
if(playerType == 0) {
|
||||||
|
GameController.startOnlineGame(HumanPlayer.class, playerName, address,GameController.semesterToBoardSize(2));
|
||||||
|
} else if(playerType == 1) {
|
||||||
|
GameController.startOnlineGame(SpecificAiPlayerEasy.class, playerName, address,GameController.semesterToBoardSize(2));
|
||||||
|
} else if (playerType == 2) {
|
||||||
|
GameController.startOnlineGame(SpecificAiPlayerMedium.class, playerName, address,GameController.semesterToBoardSize(2));
|
||||||
|
} else if (playerType == 3) {
|
||||||
|
GameController.startOnlineGame(SpecificAiPlayerHard.class, playerName, address,GameController.semesterToBoardSize(2));
|
||||||
|
}
|
||||||
|
} catch (IOException ex) {
|
||||||
|
throw new RuntimeException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -10,7 +10,7 @@ public class LocalPlayer extends Player {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void receiveShoot(Point point) {
|
public synchronized void receiveShoot(Point point) {
|
||||||
HitResponse hitResponse = board.getHitResponsOnPoint(point);
|
HitResponse hitResponse = board.getHitResponseOnPoint(point);
|
||||||
if (!(hitResponse == null)){
|
if (!(hitResponse == null)){
|
||||||
enemy.receiveHit(hitResponse);
|
enemy.receiveHit(hitResponse);
|
||||||
|
|
||||||
|
@ -22,8 +22,9 @@ public class LocalPlayer extends Player {
|
||||||
switch (hitResponse.getType()) {
|
switch (hitResponse.getType()) {
|
||||||
case HIT, SUNK -> this.myTurn = false;
|
case HIT, SUNK -> this.myTurn = false;
|
||||||
case MISS -> this.myTurn = true;
|
case MISS -> this.myTurn = true;
|
||||||
case VICTORY -> System.out.println("Game Over"); //TODO Was halt bei victory passiert ist hier wurder verloheren
|
case VICTORY -> GameController.getMainFrame().showPanelLoose("", this); //TODO Was halt bei victory passiert ist hier wurder verloheren
|
||||||
}
|
}
|
||||||
|
GameController.getMainFrame().refreshGameBoard();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -32,27 +33,31 @@ public class LocalPlayer extends Player {
|
||||||
switch (hitResponse.getType()) {
|
switch (hitResponse.getType()) {
|
||||||
case HIT, SUNK -> this.myTurn = true;
|
case HIT, SUNK -> this.myTurn = true;
|
||||||
case MISS -> this.myTurn = false;
|
case MISS -> this.myTurn = false;
|
||||||
case VICTORY -> System.out.println("Win"); // TODO was halt beim victory passier ist hier wurde gewonnen
|
case VICTORY -> GameController.getMainFrame().showPanelWin("", this); // TODO was halt beim victory passier ist hier wurde gewonnen
|
||||||
}
|
}
|
||||||
|
GameController.getMainFrame().refreshGameBoard();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void receiveCoin(boolean coin) {
|
public synchronized void receiveCoin(boolean coin) {
|
||||||
if (!this.haseReceivedCoin) {
|
if (!this.hasReceivedCoin) {
|
||||||
boolean result = coin ^ this.myCoin; // XOR
|
this.hasReceivedCoin = true;
|
||||||
this.myTurn = result == this.isServer;
|
this.determineCoinToss();
|
||||||
this.haseReceivedCoin = true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void sendCoin() {
|
|
||||||
enemy.receiveCoin(this.myCoin);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void shoot(Point point){
|
public void shoot(Point point){
|
||||||
this.myTurn = false;
|
this.myTurn = false;
|
||||||
enemy.receiveShoot(point);
|
enemy.receiveShoot(point);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public synchronized void ready() {
|
||||||
|
for (Ship ship : this.board.getShips()) {
|
||||||
|
if (!ship.isPlaced()) return;
|
||||||
|
}
|
||||||
|
super.ready();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -0,0 +1,34 @@
|
||||||
|
import java.awt.*;
|
||||||
|
import javax.swing.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Klasse für Erstellung von looseScreen Objekten
|
||||||
|
* Dient zur Anzeige das ein Spiel verloren wurde
|
||||||
|
*/
|
||||||
|
public class LooseScreen extends JPanel {
|
||||||
|
JLabel looseLabel = new JLabel("Du hast Verloren");
|
||||||
|
JButton okButton = new JButton("Zurück zum Hauptmenü");
|
||||||
|
Font robotoFont = new Font("Roboto", Font.BOLD, 45);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Konstruktor der LooseScreen Klasse
|
||||||
|
* @param frame Der Mainframe der Anwendung über den alle Panels angezeigt werden.
|
||||||
|
* @author Lucas Bronson
|
||||||
|
*/
|
||||||
|
public LooseScreen(MainFrame frame) {
|
||||||
|
setLayout(new BorderLayout());
|
||||||
|
buildPanel(frame);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Panel bauen/Objekte hinzufügen
|
||||||
|
* @param frame Der Mainframe der Anwendung über den alle Panels angezeigt werden.
|
||||||
|
* @author Lucas Bronson
|
||||||
|
*/
|
||||||
|
public void buildPanel(MainFrame frame) {
|
||||||
|
add(looseLabel);
|
||||||
|
okButton.setBounds(650,525,200,50);
|
||||||
|
looseLabel.setBounds(500,450,500,50);
|
||||||
|
looseLabel.setFont(robotoFont);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,11 @@
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Der MainFrame dient als Hub und Übergreifendes Fenster auf dem alle weiteren Panel angezeigt werden.
|
||||||
|
* Dadurch werden keine weiteren Fenster geöffnet.
|
||||||
|
* @author Lucas Bronson, Luca Conte, Joshua Kuklok
|
||||||
|
*/
|
||||||
public class MainFrame extends JFrame {
|
public class MainFrame extends JFrame {
|
||||||
|
|
||||||
private CardLayout cardLayout;
|
private CardLayout cardLayout;
|
||||||
|
@ -11,43 +16,45 @@ public class MainFrame extends JFrame {
|
||||||
// Von startMultiplayerGame an JoinGame
|
// Von startMultiplayerGame an JoinGame
|
||||||
int localMult;
|
int localMult;
|
||||||
|
|
||||||
// Von startLocalGame an GameBoard
|
// Von startLocalGame an startLocalGameLoadingScreen
|
||||||
|
// Von startLocalGameLoadingScreen an GameBoard
|
||||||
int semesterCounter;
|
int semesterCounter;
|
||||||
// ---------- //
|
// ---------- //
|
||||||
|
|
||||||
|
private GameBoard gameBoard;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Konstruktor von MainFrame.
|
||||||
|
* Ermöglicht es Panel anzuzeigen.
|
||||||
|
* @author Lucas Bronson, Luca Conte, Joshua Kuklok
|
||||||
|
*/
|
||||||
public MainFrame() {
|
public MainFrame() {
|
||||||
|
|
||||||
|
GameController.setMainFrame(this);
|
||||||
|
|
||||||
setTitle("Studium Versenken");
|
setTitle("Studium Versenken");
|
||||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
setSize(1500, 1000);
|
setSize(1500, 1000);
|
||||||
setLocationRelativeTo(null);
|
setLocationRelativeTo(null);
|
||||||
|
|
||||||
//JLabel backgroundLabel = new JLabel(new ImageIcon("graphics/mainmenubackground.png"));
|
|
||||||
// backgroundLabel.setBounds(0, 0, 1500, 1000);
|
|
||||||
// getContentPane().add(backgroundLabel);
|
|
||||||
|
|
||||||
// backgroundLabel.setOpaque(true);
|
|
||||||
|
|
||||||
// CardLayout und Hauptpanel erstellen
|
// CardLayout und Hauptpanel erstellen
|
||||||
cardLayout = new CardLayout();
|
cardLayout = new CardLayout();
|
||||||
mainPanel = new JPanel(cardLayout);
|
mainPanel = new JPanel(cardLayout);
|
||||||
|
|
||||||
// Verschiedene Panels erstellen und hinzufügen
|
// Panels erstellen
|
||||||
MainMenuView mainMenuView = new MainMenuView(this);
|
MainMenuView mainMenuView = new MainMenuView(this);
|
||||||
startLocalGame localGame = new startLocalGame(this);
|
startLocalGame localGame = new startLocalGame(this);
|
||||||
startMultiplayerGame multiplayerGame = new startMultiplayerGame(this);
|
startMultiplayerGame multiplayerGame = new startMultiplayerGame(this);
|
||||||
coinToss coinToss = new coinToss(this);
|
coinToss coinToss = new coinToss(this);
|
||||||
Verbinden verbinden = new Verbinden(this);
|
Verbinden verbinden = new Verbinden(this);
|
||||||
//JoinGame joinGame = new JoinGame(this,localMult);
|
|
||||||
//GameBoard gameBoard = new GameBoard(this, localMult);
|
|
||||||
|
|
||||||
|
// Panels hinzufügen
|
||||||
mainPanel.add(mainMenuView, "MainMenu");
|
mainPanel.add(mainMenuView, "MainMenu");
|
||||||
mainPanel.add(localGame, "LocalGame");
|
mainPanel.add(localGame, "LocalGame");
|
||||||
mainPanel.add(multiplayerGame, "MultiplayerGame");
|
mainPanel.add(multiplayerGame, "MultiplayerGame");
|
||||||
mainPanel.add(coinToss, "coinToss");
|
mainPanel.add(coinToss, "coinToss");
|
||||||
mainPanel.add(verbinden, "Verbinden");
|
mainPanel.add(verbinden, "Verbinden");
|
||||||
//mainPanel.add(joinGame, "JoinGame");
|
// mainPanel.add(winLooseScreen, "WinLooseScreen");
|
||||||
//mainPanel.add(gameBoard, "GameBoard");
|
|
||||||
|
|
||||||
// Hauptpanel in JFrame hinzufügen
|
// Hauptpanel in JFrame hinzufügen
|
||||||
add(mainPanel);
|
add(mainPanel);
|
||||||
|
@ -56,50 +63,109 @@ public class MainFrame extends JFrame {
|
||||||
cardLayout.show(mainPanel, "MainMenu");
|
cardLayout.show(mainPanel, "MainMenu");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Methode, um die Ansicht zu wechseln
|
/**
|
||||||
|
* Methode, um die Ansicht zu wechseln
|
||||||
|
* @param panelName Name des anzuzeigenden Panels
|
||||||
|
* @author Joshua Kuklok
|
||||||
|
*/
|
||||||
public void showPanel(String panelName) {
|
public void showPanel(String panelName) {
|
||||||
cardLayout.show(mainPanel, panelName);
|
cardLayout.show(mainPanel, panelName);
|
||||||
}
|
}
|
||||||
// --- ShowPanel der startMultiplayerGame Klasse
|
|
||||||
public void showPanelSMG(String panelName, int num) {
|
/**
|
||||||
|
* Spezifische ShowPanel-Funktion der startMultiplayerGame Klasse
|
||||||
|
* @param panelName Name des anzuzeigenden Panels
|
||||||
|
* @param num Hilfsvariable um abzugleichen, ob "Spiel erstellen" oder "Spiel beitreten" ausgewählt wurde
|
||||||
|
* @param playerType Spielertyp(HumanPlayer, AIEasy etc.)
|
||||||
|
* @param playerName Name des Spielers
|
||||||
|
* @author Lucas Bronson, Joshua Kuklok
|
||||||
|
*/
|
||||||
|
public void showPanelSMG(String panelName, int num, int playerType,String playerName) {
|
||||||
this.localMult = num;
|
this.localMult = num;
|
||||||
|
|
||||||
//if (!isPanelPresent(panelName)) { //TODO potentiell raus
|
JoinGame joinGame = new JoinGame(this, localMult, playerType, playerName);
|
||||||
JoinGame joinGame = new JoinGame(this, localMult);
|
mainPanel.add(joinGame, panelName);
|
||||||
mainPanel.add(joinGame, panelName);
|
mainPanel.revalidate();
|
||||||
mainPanel.revalidate(); // Refresh
|
mainPanel.repaint();
|
||||||
mainPanel.repaint();
|
cardLayout.show(mainPanel, panelName);
|
||||||
//}
|
|
||||||
|
|
||||||
cardLayout.show(mainPanel, panelName); // Show the panel
|
|
||||||
}
|
}
|
||||||
// --- ShowPanel der startLocalGame Klasse
|
|
||||||
public void showPanelSLG(String panelName,int semesterCounter) {
|
/**
|
||||||
|
* Spezifische ShowPanel der startLocalGameLoadingScreen Klasse (DURCH BACKEND AUFGERUFEN)
|
||||||
|
* @param panelName Name des anzuzeigenden Panels
|
||||||
|
* @param semesterCounter Ausgewähltes Semester
|
||||||
|
* @param p1 Erstes Spielerobjekt
|
||||||
|
* @param p2 Zweites Spielerobjekt
|
||||||
|
* @author Lucas Bronson, Luca Conte, Joshua Kuklok
|
||||||
|
*/
|
||||||
|
public void showPanelSLG(String panelName,int semesterCounter, Player p1, Player p2) {
|
||||||
this.semesterCounter = semesterCounter;
|
this.semesterCounter = semesterCounter;
|
||||||
|
|
||||||
//if (!isPanelPresent(panelName)) { //TODO potentiell raus
|
this.gameBoard = new GameBoard(this, semesterCounter, p1, p2);
|
||||||
GameBoard gameBoard = new GameBoard(this, semesterCounter);
|
mainPanel.add(gameBoard, panelName);
|
||||||
mainPanel.add(gameBoard, panelName);
|
mainPanel.revalidate();
|
||||||
mainPanel.revalidate(); // Refresh
|
mainPanel.repaint();
|
||||||
mainPanel.repaint();
|
cardLayout.show(mainPanel, panelName); // Panel anzeigen
|
||||||
//}
|
|
||||||
|
|
||||||
cardLayout.show(mainPanel, panelName); // Show the panel
|
|
||||||
}
|
}
|
||||||
/* TODO ist dies unnötig?
|
|
||||||
private boolean isPanelPresent(String panelName) {
|
|
||||||
for (Component component : mainPanel.getComponents()) {
|
|
||||||
if (panelName.equals(mainPanel.getClientProperty("name"))) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
} */
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
/**
|
||||||
SwingUtilities.invokeLater(() -> {
|
* Spezifische ShowPanel der startLocalGame Klasse
|
||||||
MainFrame frame = new MainFrame();
|
* @param panelName Name des anzuzeigenden Panels
|
||||||
frame.setVisible(true);
|
* @param semesterCounter Ausgewähltes Semester
|
||||||
});
|
* @author Joshua Kuklok
|
||||||
|
*/
|
||||||
|
public void showPanelSLGLS(String panelName,int semesterCounter) {
|
||||||
|
this.semesterCounter = semesterCounter;
|
||||||
|
|
||||||
|
startLocalGameLoadingScreen LocalGameLoadingScreen = new startLocalGameLoadingScreen(this, semesterCounter);
|
||||||
|
mainPanel.add(LocalGameLoadingScreen, panelName);
|
||||||
|
mainPanel.revalidate();
|
||||||
|
mainPanel.repaint();
|
||||||
|
cardLayout.show(mainPanel, panelName); // Panel anzeigen
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Spezifische ShowPanel für WinScreen Klasse
|
||||||
|
* @param panelName Name des anzuzeigenden Panels
|
||||||
|
* @param player Player von dem die funktion aufgerufen worden ist
|
||||||
|
* @author Lucas Bronson, Peer Ole Wachtel
|
||||||
|
*/
|
||||||
|
public void showPanelWin(String panelName, Player player){
|
||||||
|
if(player != gameBoard.getP1()){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
WinScreen winScreen = new WinScreen(this);
|
||||||
|
mainPanel.add(winScreen, panelName);
|
||||||
|
mainPanel.revalidate();
|
||||||
|
mainPanel.repaint();
|
||||||
|
cardLayout.show(mainPanel, panelName);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Spezifische ShowPanel für LooseScreen Klasse
|
||||||
|
* @param panelName Name des anzuzeigenden Panels
|
||||||
|
* @param player Player von dem die funktion aufgerufen worden ist
|
||||||
|
* @author Lucas Bronson, Peer Ole Wachtel
|
||||||
|
*/
|
||||||
|
public void showPanelLoose(String panelName, Player player){
|
||||||
|
if(player != gameBoard.getP1()){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
LooseScreen looseScreen = new LooseScreen(this);
|
||||||
|
mainPanel.add(looseScreen,panelName);
|
||||||
|
mainPanel.revalidate();
|
||||||
|
mainPanel.repaint();
|
||||||
|
cardLayout.show(mainPanel, panelName);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Aktualisiert das Spielfeld (kontextText)
|
||||||
|
* @author Luca Conte
|
||||||
|
*/
|
||||||
|
public void refreshGameBoard() {
|
||||||
|
if(this.gameBoard == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.gameBoard.refresh();
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,35 +0,0 @@
|
||||||
import java.awt.event.ActionEvent;
|
|
||||||
import java.awt.event.ActionListener;
|
|
||||||
import javax.swing.*;
|
|
||||||
|
|
||||||
public class MainMenuController implements ActionListener {
|
|
||||||
private MainMenuView view;
|
|
||||||
private MainMenuModel model;
|
|
||||||
|
|
||||||
public MainMenuController(MainMenuModel model, MainMenuView view) {
|
|
||||||
this.view = view;
|
|
||||||
this.model = model;
|
|
||||||
//this.view.getLocalButton().addActionListener(this);
|
|
||||||
//this.view.getMultiButton().addActionListener(this);
|
|
||||||
// this.view.getSoundButton().addActionListener(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
//if (e.getSource() == view.getMultiButton()) {
|
|
||||||
// model.setGameMode("Multiplayer");
|
|
||||||
// MultiMenuModel multiModel = new MultiMenuModel();
|
|
||||||
// MultiMenuView multiView = new MultiMenuView();
|
|
||||||
// MultiMenuController multiMenuController = new MultiMenuController(multiModel, multiView);
|
|
||||||
// view.addPanel(multiView.getMultiPanel(), "MultiMenu");
|
|
||||||
// view.showPanel("MultiMenu");
|
|
||||||
// }else if (e.getSource() == view.getSoundButton()) {
|
|
||||||
// view.toggleMute();
|
|
||||||
// }else if(e.getSource() == view.getLocalButton()) {
|
|
||||||
model.setGameMode("LocalGame");
|
|
||||||
//startLocalGame localGame = new startLocalGame();
|
|
||||||
// view.addPanel(localGame.getLocalPanel(), "LocalMenu");
|
|
||||||
// view.showPanel("LocalMenu");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//}
|
|
|
@ -1,11 +0,0 @@
|
||||||
public class MainMenuModel {
|
|
||||||
private String gameMode;
|
|
||||||
|
|
||||||
public void setGameMode(String mode) {
|
|
||||||
this.gameMode = mode;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getGameMode() {
|
|
||||||
return gameMode;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,9 +1,13 @@
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionEvent;
|
||||||
import java.awt.event.ActionListener;
|
import java.awt.event.ActionListener;
|
||||||
import javax.swing.JFrame;
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Klasse zum Erstellen von MainMenu Objekten
|
||||||
|
* Dient als Hauptmenü für die Anwendung
|
||||||
|
* @author Lucas Bronson
|
||||||
|
*/
|
||||||
public class MainMenuView extends JPanel {
|
public class MainMenuView extends JPanel {
|
||||||
|
|
||||||
private JLabel titelLabel = new JLabel("Studium versenken");
|
private JLabel titelLabel = new JLabel("Studium versenken");
|
||||||
|
@ -15,11 +19,21 @@ public class MainMenuView extends JPanel {
|
||||||
private ImageIcon soundIcon = new ImageIcon("graphics/sound button.png");
|
private ImageIcon soundIcon = new ImageIcon("graphics/sound button.png");
|
||||||
private ImageIcon muteIcon = new ImageIcon("graphics/sound button muted.png");
|
private ImageIcon muteIcon = new ImageIcon("graphics/sound button muted.png");
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Konstruktor der MainMenuView Klasse
|
||||||
|
* @param frame Der Mainframe der Anwendung über den alle Panels angezeigt werden.
|
||||||
|
* @author Lucas Bronson
|
||||||
|
*/
|
||||||
public MainMenuView(MainFrame frame) {
|
public MainMenuView(MainFrame frame) {
|
||||||
setLayout(null);
|
setLayout(null);
|
||||||
buildPanel(frame);
|
buildPanel(frame);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Methode zum Füllen des Panels, Objekte werden dem frame hinzugefügt
|
||||||
|
* @param frame Der Mainframe der Anwendung über den alle Panels angezeigt werden.
|
||||||
|
* @author Lucas Bronson
|
||||||
|
*/
|
||||||
private void buildPanel(MainFrame frame) {
|
private void buildPanel(MainFrame frame) {
|
||||||
lokalButton.setBounds(200, 200, 500, 500);
|
lokalButton.setBounds(200, 200, 500, 500);
|
||||||
multiButton.setBounds(800, 200, 500, 500);
|
multiButton.setBounds(800, 200, 500, 500);
|
||||||
|
@ -36,7 +50,7 @@ public class MainMenuView extends JPanel {
|
||||||
add(multiButton);
|
add(multiButton);
|
||||||
add(soundButton);
|
add(soundButton);
|
||||||
|
|
||||||
// Event Listener für Buttons
|
// ActionListener um vom Hauptmenü zum LocalGame Menü zu wechseln
|
||||||
lokalButton.addActionListener(new ActionListener() {
|
lokalButton.addActionListener(new ActionListener() {
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
@ -45,6 +59,7 @@ public class MainMenuView extends JPanel {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// ActionListener um vom Hauptmenü zum MultiPlayerGame Menü zu wechseln
|
||||||
multiButton.addActionListener(new ActionListener() {
|
multiButton.addActionListener(new ActionListener() {
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
@ -53,6 +68,7 @@ public class MainMenuView extends JPanel {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
//Aufruf von toggleMute, falls der soundButton geklickt wird
|
||||||
soundButton.addActionListener(new ActionListener() {
|
soundButton.addActionListener(new ActionListener() {
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
@ -62,11 +78,18 @@ public class MainMenuView extends JPanel {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Setzt Sound auf Stumm/Laut und ändert Grafik, sodass
|
||||||
|
* der aktuelle Stand der Grafik entspricht
|
||||||
|
* @author Lucas Bronson, Joshua Kuklok
|
||||||
|
*/
|
||||||
private void toggleMute() {
|
private void toggleMute() {
|
||||||
if (soundButton.getIcon() == soundIcon) {
|
if (soundButton.getIcon() == soundIcon) {
|
||||||
soundButton.setIcon(muteIcon);
|
soundButton.setIcon(muteIcon);
|
||||||
|
SoundHandler.setSoundOn(false);
|
||||||
} else {
|
} else {
|
||||||
soundButton.setIcon(soundIcon);
|
soundButton.setIcon(soundIcon);
|
||||||
|
SoundHandler.setSoundOn(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,22 +0,0 @@
|
||||||
import java.awt.event.ActionEvent;
|
|
||||||
import java.awt.event.ActionListener;
|
|
||||||
import javax.swing.*;
|
|
||||||
|
|
||||||
public class MultiMenuController implements ActionListener {
|
|
||||||
private MultiMenuView view;
|
|
||||||
private MultiMenuModel model;
|
|
||||||
|
|
||||||
public MultiMenuController(MultiMenuModel model, MultiMenuView view) {
|
|
||||||
this.view = view;
|
|
||||||
this.model = model;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
if (e.getSource() == view.getPlayerLeftButton()){
|
|
||||||
view.togglePlayerIcon();
|
|
||||||
}else if(e.getSource() == view.getPlayerRightButton()){
|
|
||||||
view.togglePlayerIcon();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,11 +0,0 @@
|
||||||
public class MultiMenuModel {
|
|
||||||
private String gameMode;
|
|
||||||
|
|
||||||
public void setGameMode(String mode) {
|
|
||||||
this.gameMode = mode;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getGameMode() {
|
|
||||||
return gameMode;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,83 +0,0 @@
|
||||||
import java.awt.*;
|
|
||||||
import javax.swing.JFrame;
|
|
||||||
import javax.swing.*;
|
|
||||||
|
|
||||||
public class MultiMenuView {
|
|
||||||
int semesterCounter = 1;
|
|
||||||
private JPanel multiPanel = new JPanel(null);
|
|
||||||
|
|
||||||
private ImageIcon humanPlayerIcon = new ImageIcon("graphics/humanPlayer.png");
|
|
||||||
private ImageIcon aiPlayerIcon = new ImageIcon("graphics/aiPlayer.png");
|
|
||||||
|
|
||||||
JButton PlayerLeftButton = new JButton("<-");
|
|
||||||
JButton PlayerRightButton = new JButton("->");
|
|
||||||
JButton semesterUpButton = new JButton("^");
|
|
||||||
JButton semesterDownButton = new JButton("v");
|
|
||||||
|
|
||||||
JLabel semesterLabel = new JLabel("Semester");
|
|
||||||
private JLabel titelLabel = new JLabel("Multiplayer");
|
|
||||||
private JLabel PlayerIcon = new JLabel(humanPlayerIcon);
|
|
||||||
private JLabel PlayerName = new JLabel("Name");
|
|
||||||
private JTextField PlayerTextField = new JTextField(20);
|
|
||||||
JLabel semesterCounterLabel = new JLabel(String.valueOf(semesterCounter));
|
|
||||||
|
|
||||||
private Font robotoFont = new Font("Roboto", Font.BOLD, 45);
|
|
||||||
public MultiMenuView() {
|
|
||||||
buildPanel();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public void buildPanel(){
|
|
||||||
titelLabel.setBounds(20,20,700,100);
|
|
||||||
PlayerIcon.setBounds(75, 400, 200, 128);
|
|
||||||
PlayerName.setBounds(50, 625, 200, 30);
|
|
||||||
PlayerTextField.setBounds(50, 650, 250, 50);
|
|
||||||
semesterCounterLabel.setBounds(725, 475, 50, 50); // zwischen den Up/Down-Buttons
|
|
||||||
PlayerLeftButton.setBounds(50, 450, 50, 50);
|
|
||||||
PlayerRightButton.setBounds(250, 450, 50, 50);
|
|
||||||
semesterLabel.setBounds(500, 400, 200, 30);
|
|
||||||
semesterUpButton.setBounds(700, 400, 50, 50);
|
|
||||||
semesterDownButton.setBounds(700, 550, 50, 50);
|
|
||||||
|
|
||||||
|
|
||||||
multiPanel.setLayout(null);
|
|
||||||
titelLabel.setFont(robotoFont.deriveFont(50f));
|
|
||||||
semesterLabel.setFont(robotoFont.deriveFont(20f));
|
|
||||||
|
|
||||||
multiPanel.add(PlayerName);
|
|
||||||
multiPanel.add(titelLabel);
|
|
||||||
multiPanel.add(PlayerIcon);
|
|
||||||
multiPanel.add(PlayerTextField);
|
|
||||||
multiPanel.add(semesterCounterLabel);
|
|
||||||
multiPanel.add(semesterUpButton);
|
|
||||||
multiPanel.add(semesterDownButton);
|
|
||||||
multiPanel.add(PlayerLeftButton);
|
|
||||||
multiPanel.add(PlayerRightButton);
|
|
||||||
multiPanel.add(semesterLabel);
|
|
||||||
}
|
|
||||||
|
|
||||||
public JPanel getMultiPanel() {
|
|
||||||
return multiPanel;
|
|
||||||
}
|
|
||||||
|
|
||||||
//public void showMultiPanel(String panelName) {
|
|
||||||
// CardLayout cl = (CardLayout)mainPanel.getLayout();
|
|
||||||
// cl.show(multiPanel, panelName);
|
|
||||||
//}
|
|
||||||
|
|
||||||
public void togglePlayerIcon() {
|
|
||||||
if (PlayerIcon.getIcon() == humanPlayerIcon) {
|
|
||||||
PlayerIcon.setIcon(aiPlayerIcon);
|
|
||||||
} else {
|
|
||||||
PlayerIcon.setIcon(humanPlayerIcon);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public JButton getPlayerLeftButton() {
|
|
||||||
return PlayerLeftButton;
|
|
||||||
}
|
|
||||||
|
|
||||||
public JButton getPlayerRightButton() {
|
|
||||||
return PlayerRightButton;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -4,13 +4,16 @@ public abstract class OnlinePlayer extends Player implements AsyncSocketListener
|
||||||
|
|
||||||
protected boolean hasReceivedCoinPackage;
|
protected boolean hasReceivedCoinPackage;
|
||||||
|
|
||||||
public OnlinePlayer(int size, AsyncSocket socket) {
|
public OnlinePlayer(Integer size, AsyncSocket socket) {
|
||||||
this.socket = socket;
|
this.socket = socket;
|
||||||
this.wantedBoardSize = size;
|
this.wantedBoardSize = size;
|
||||||
|
this.myCoin = null;
|
||||||
socket.setHandler(this);
|
socket.setHandler(this);
|
||||||
//TODO Auto-generated constructor stub
|
//TODO Auto-generated constructor stub
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public abstract void sendIAM();
|
||||||
|
|
||||||
public abstract void receive(String message);
|
public abstract void receive(String message);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -1,12 +1,11 @@
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class OnlinePlayer_1_1_0 extends OnlinePlayer {
|
public class OnlinePlayer_1_1_0 extends OnlinePlayer {
|
||||||
public OnlinePlayer_1_1_0(int size, AsyncSocket socket) {
|
public OnlinePlayer_1_1_0(Integer size, AsyncSocket socket) {
|
||||||
super(size, socket);
|
super(size, socket);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void receive(String message) {
|
public void receive(String message) {
|
||||||
SocketPackage p = new SocketPackage(message);
|
SocketPackage p = new SocketPackage(message);
|
||||||
|
@ -26,14 +25,16 @@ public class OnlinePlayer_1_1_0 extends OnlinePlayer {
|
||||||
this.createBoard(usedBoardSize);
|
this.createBoard(usedBoardSize);
|
||||||
this.enemy.createBoard(usedBoardSize);
|
this.enemy.createBoard(usedBoardSize);
|
||||||
|
|
||||||
|
GameController.startGameWithInstancedPlayers((LocalPlayer)this.enemy, (Player)this, usedBoardSize);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// TODO: IAMU
|
// TODO: IAMU
|
||||||
|
|
||||||
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");
|
||||||
enemy.receiveCoin(this.myCoin);
|
this.ready();
|
||||||
this.hasReceivedCoinPackage = true;
|
this.hasReceivedCoinPackage = true;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
@ -66,6 +67,12 @@ public class OnlinePlayer_1_1_0 extends OnlinePlayer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public synchronized void sendIAM() {
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void receiveShoot(Point point){
|
public synchronized void receiveShoot(Point point){
|
||||||
super.socket.send(new SocketPackage("SHOOT",point.toString()));
|
super.socket.send(new SocketPackage("SHOOT",point.toString()));
|
||||||
|
@ -78,9 +85,10 @@ public class OnlinePlayer_1_1_0 extends OnlinePlayer {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void receiveCoin(boolean coin) {
|
public synchronized void receiveCoin(boolean coin) {
|
||||||
if (!this.haseReceivedCoin) {
|
if (!this.hasReceivedCoin) {
|
||||||
super.socket.send(new SocketPackage("COIN", String.valueOf(coin ? 1 : 0)));
|
super.socket.send(new SocketPackage("COIN", String.valueOf(coin ? 1 : 0)));
|
||||||
this.haseReceivedCoin = true;
|
this.hasReceivedCoin = true;
|
||||||
|
this.determineCoinToss();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
import java.util.Random;
|
|
||||||
|
|
||||||
public abstract class Player {
|
public abstract class Player {
|
||||||
protected boolean myTurn;
|
protected boolean myTurn;
|
||||||
protected boolean isServer;
|
protected boolean isServer;
|
||||||
|
@ -7,15 +5,19 @@ public abstract class Player {
|
||||||
protected Player enemy;
|
protected Player enemy;
|
||||||
protected String name;
|
protected String name;
|
||||||
protected Board board;
|
protected Board board;
|
||||||
protected boolean myCoin;
|
protected Boolean myCoin;
|
||||||
|
protected boolean gameRunning;
|
||||||
|
|
||||||
protected boolean sendCoin;
|
protected boolean sentCoin;
|
||||||
|
|
||||||
protected boolean haseReceivedCoin;
|
protected boolean hasReceivedCoin;
|
||||||
|
|
||||||
public Player() {
|
public Player() {
|
||||||
this.haseReceivedCoin = false;
|
this.setName("Player");
|
||||||
this.sendCoin = false;
|
this.hasReceivedCoin = false;
|
||||||
|
this.sentCoin = false;
|
||||||
|
this.myTurn = false;
|
||||||
|
this.gameRunning = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void createBoard(int size) {
|
public void createBoard(int size) {
|
||||||
|
@ -28,8 +30,8 @@ public abstract class Player {
|
||||||
|
|
||||||
public abstract void shoot(Point point);
|
public abstract void shoot(Point point);
|
||||||
|
|
||||||
public void beginTrun() {
|
public void beginTurn() {
|
||||||
|
System.out.println("issa my turn-a");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setEnemy(Player enemy) {
|
public void setEnemy(Player enemy) {
|
||||||
|
@ -43,6 +45,36 @@ public abstract class Player {
|
||||||
return this.name;
|
return this.name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Board getBoard() {
|
||||||
|
return this.board;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ready() {
|
||||||
|
this.enemy.receiveCoin(this.myCoin);
|
||||||
|
this.sentCoin = true;
|
||||||
|
if (hasReceivedCoin) {
|
||||||
|
this.determineCoinToss();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
protected void determineCoinToss() {
|
||||||
|
if (!this.sentCoin || this.myCoin == null || !this.hasReceivedCoin || this.enemy.myCoin == null) return;
|
||||||
|
boolean result = this.enemy.myCoin ^ this.myCoin; // XOR
|
||||||
|
this.myTurn = result == this.isServer;
|
||||||
|
if (this.myTurn) {
|
||||||
|
this.beginTurn();
|
||||||
|
}
|
||||||
|
this.gameRunning = true;
|
||||||
|
GameController.getMainFrame().refreshGameBoard();
|
||||||
|
}
|
||||||
|
|
||||||
public abstract void receiveCoin(boolean coin);
|
public abstract void receiveCoin(boolean coin);
|
||||||
|
|
||||||
|
public boolean isGameRunning() {
|
||||||
|
return this.gameRunning;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isReady() {
|
||||||
|
return this.sentCoin;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,7 @@ public class Point {
|
||||||
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));
|
||||||
this.setY(Integer.parseInt(str.substring(1)));
|
this.setY(Integer.parseInt(str.substring(1)) - 1);
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("String ist keine gültige Koordinate");
|
throw new IllegalArgumentException("String ist keine gültige Koordinate");
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,7 @@ public class Point {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return (char) ('A' + this.x) + String.valueOf(this.y);
|
return (char) ('A' + this.x) + String.valueOf(this.y + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getX() {
|
public int getX() {
|
||||||
|
@ -42,4 +42,18 @@ public class Point {
|
||||||
public static boolean isValidSyntax(String str) {
|
public static boolean isValidSyntax(String str) {
|
||||||
return str.matches("^[A-Z]\\d+$");
|
return str.matches("^[A-Z]\\d+$");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) return true;
|
||||||
|
if (o == null || this.getClass() != o.getClass()) return false;
|
||||||
|
|
||||||
|
Point p = (Point)o;
|
||||||
|
return p.getX() == this.getX() && p.getY() == this.getY();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean neighbours(Point other) {
|
||||||
|
if (other == null) return false;
|
||||||
|
return (int)Math.abs(this.getX() - other.getX()) <= 1 && (int)Math.abs(this.getY() - other.getY()) <= 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -61,8 +61,21 @@ public class Ship {
|
||||||
this.sunk = false;
|
this.sunk = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean setPosition(Point pos, List<Ship> shipsList, int boardSize) {
|
public void resetPosition() {
|
||||||
// ueberpruefe boundaries
|
this.position = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean setPosition(Point pos, boolean horizontal, List<Ship> shipsList, int boardSize) {
|
||||||
|
if (!this.checkValidPlacement(pos, horizontal, shipsList, boardSize)) return false;
|
||||||
|
|
||||||
|
// kein ueberlappen also setze das Schiff
|
||||||
|
this.position = pos;
|
||||||
|
this.horizontal = horizontal;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean checkValidPlacement(Point pos, boolean horizontal, List<Ship> shipsList, int boardSize) {
|
||||||
|
// 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) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -71,7 +84,7 @@ public class Ship {
|
||||||
int endX = pos.getX();
|
int endX = pos.getX();
|
||||||
int endY = pos.getY();
|
int endY = pos.getY();
|
||||||
|
|
||||||
if (this.horizontal) { // rechts links
|
if (horizontal) { // rechts links
|
||||||
endX = pos.getX() + this.size - 1;
|
endX = pos.getX() + this.size - 1;
|
||||||
if (endX >= boardSize) {
|
if (endX >= boardSize) {
|
||||||
return false;
|
return false;
|
||||||
|
@ -84,12 +97,7 @@ 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 = this.getVirtualOccupiedPoints(pos, horizontal);
|
||||||
for (int i = 0; i < this.size; i++) {
|
|
||||||
int x = this.horizontal ? pos.getX() + i : pos.getX(); //falls horizontal dann pos.x + i ansonsten pos.x
|
|
||||||
int y = this.horizontal ? pos.getY() : pos.getY() + i;
|
|
||||||
shipPoints.add(new Point(x, y));
|
|
||||||
}
|
|
||||||
|
|
||||||
// ueberlappen mit anderen Schiffen pruefen
|
// ueberlappen mit anderen Schiffen pruefen
|
||||||
for (Ship otherShip : shipsList) {
|
for (Ship otherShip : shipsList) {
|
||||||
|
@ -105,18 +113,30 @@ public class Ship {
|
||||||
List<Point> otherShipPoints = otherShip.getOccupiedPoints();
|
List<Point> otherShipPoints = otherShip.getOccupiedPoints();
|
||||||
// ueberlappen checken
|
// ueberlappen checken
|
||||||
for (Point p : shipPoints) {
|
for (Point p : shipPoints) {
|
||||||
if (otherShipPoints.contains(p)) {
|
for (Point otherPoint : otherShipPoints) {
|
||||||
// ueberlappen entdeckt
|
if (otherPoint.neighbours(p)) return false;
|
||||||
return false;
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// kein ueberlappen also setze das Schiff
|
|
||||||
this.position = pos;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the Points on the ship if it were to be placed at positino `pos` in orientation defined by `horizontal`
|
||||||
|
*/
|
||||||
|
public List<Point> getVirtualOccupiedPoints(Point pos, boolean horizontal) {
|
||||||
|
List<Point> points = new ArrayList<>();
|
||||||
|
if (pos == null) {
|
||||||
|
return points;
|
||||||
|
}
|
||||||
|
for (int i = 0; i < this.size; i++) {
|
||||||
|
int x = horizontal ? pos.getX() + i : pos.getX();
|
||||||
|
int y = horizontal ? pos.getY() : pos.getY() + i;
|
||||||
|
points.add(new Point(x, y));
|
||||||
|
}
|
||||||
|
return points;
|
||||||
|
}
|
||||||
|
|
||||||
public List<Point> getOccupiedPoints() {
|
public List<Point> getOccupiedPoints() {
|
||||||
List<Point> points = new ArrayList<>();
|
List<Point> points = new ArrayList<>();
|
||||||
if (this.position == null) {
|
if (this.position == null) {
|
||||||
|
@ -135,6 +155,9 @@ public class Ship {
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isShipOnPos(Point pos){
|
public boolean isShipOnPos(Point pos){
|
||||||
|
if(this.position == null){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
if ((this.horizontal && pos.getY() == this.position.getY() && pos.getX() >= this.position.getX() && pos.getX() < this.position.getX() + size) ||
|
if ((this.horizontal && pos.getY() == this.position.getY() && pos.getX() >= this.position.getX() && pos.getX() < this.position.getX() + size) ||
|
||||||
(!(this.horizontal) && pos.getX() == this.position.getX() && pos.getY() >= this.position.getY() && pos.getY() < this.position.getY() + size)) {
|
(!(this.horizontal) && pos.getX() == this.position.getX() && pos.getY() >= this.position.getY() && pos.getY() < this.position.getY() + size)) {
|
||||||
return true;
|
return true;
|
||||||
|
@ -159,4 +182,20 @@ public class Ship {
|
||||||
public boolean isSunk() {
|
public boolean isSunk() {
|
||||||
return sunk;
|
return sunk;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setHorizontal(boolean horizontal) {
|
||||||
|
this.horizontal = horizontal;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getSize() {
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
//potentiell falsch neu
|
||||||
|
public boolean isPlaced(){
|
||||||
|
return this.position != null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,44 @@
|
||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Das ShipButton Panel dient dem Erstellen eines lokalen Spiels.
|
||||||
|
* Hier kann der Benutzer Spieler inklusive Namen und das Semester, in dem sich der Benutzer befindet, einstellen.
|
||||||
|
* @author Lucas Bronson, Luca Conte, Joshua Kuklok
|
||||||
|
*/
|
||||||
|
public class ShipButton extends JButton {
|
||||||
|
private Ship ship;
|
||||||
|
private BoardDisplay boardDisplay;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TODO fertig beschreiben
|
||||||
|
* @param ship
|
||||||
|
* @param boardDisplay
|
||||||
|
* @author Lucas Bronson, Luca Conte, Joshua Kuklok
|
||||||
|
*/
|
||||||
|
public ShipButton(Ship ship, BoardDisplay boardDisplay) {
|
||||||
|
super(ship.getName());
|
||||||
|
this.ship = ship;
|
||||||
|
this.boardDisplay = boardDisplay;
|
||||||
|
this.addActionListener((e) -> {
|
||||||
|
boardDisplay.selectCurrentShip(this.ship);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Setzt Farbe der Modulbuttons.
|
||||||
|
* Verschiedene Farben für:
|
||||||
|
* Modul ausgewählt, platziert nicht platziert.
|
||||||
|
* @author Joshua Kuklok
|
||||||
|
*/
|
||||||
|
public void refreshButtonState() {
|
||||||
|
if (ship.isPlaced()) {
|
||||||
|
setBackground(Color.LIGHT_GRAY);
|
||||||
|
} else {
|
||||||
|
setBackground(Color.WHITE);
|
||||||
|
}
|
||||||
|
if (boardDisplay.getCurrentShip() == ship) {
|
||||||
|
setBackground(Color.CYAN);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,23 +1,41 @@
|
||||||
import javazoom.jl.decoder.JavaLayerException;
|
import javazoom.jl.decoder.JavaLayerException;
|
||||||
import javazoom.jl.player.Player;
|
import javazoom.jl.player.Player;
|
||||||
|
|
||||||
|
import java.awt.List;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.Iterator;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Der SoundHandler dient zum Anlegen und Abspielen von Sounds
|
||||||
|
* @author Lucas Bronson, Luca Conte, Ole Wachtel, Joshua Kuklok
|
||||||
|
*/
|
||||||
public class SoundHandler {
|
public class SoundHandler {
|
||||||
|
|
||||||
private static boolean soundOn = true;
|
private static boolean soundOn = true;
|
||||||
|
|
||||||
|
private static ArrayList<Thread> runningThreads = new ArrayList<Thread>();
|
||||||
|
|
||||||
// Wenn fehler beim erstellen von .jar mit sound hier gucken
|
// Wenn fehler beim erstellen von .jar mit sound hier gucken
|
||||||
private static HashMap<String, String> sounds = new HashMap<String, String>(Map.of(
|
private static HashMap<String, String> sounds = new HashMap<String, String>(Map.of(
|
||||||
"hit", "./Sound/water-drip.mp3"
|
"miss", "./Sound/water-drip.mp3",
|
||||||
|
"hit", "./Sound/hit.mp3",
|
||||||
|
"destroyed", "./Sound/hit.mp3",
|
||||||
|
"plop", "./Sound/plop.mp3",
|
||||||
|
"loose", "./Sound/loosescreenWAH.mp3"
|
||||||
));
|
));
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Erstellt neuen Thread, um ausgewählten Sound abzuspielen
|
||||||
|
* @param soundName Name der Audiodatei, welche abgespielt werden soll
|
||||||
|
* @author Ole Wachtel, Luca Conte
|
||||||
|
*/
|
||||||
public static void playSound(String soundName) {
|
public static void playSound(String soundName) {
|
||||||
if (soundOn) {
|
if (soundOn) {
|
||||||
new Thread(new Runnable() {
|
Thread thread = new Thread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
|
@ -27,16 +45,42 @@ public class SoundHandler {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}).start();
|
});
|
||||||
|
thread.start();
|
||||||
|
runningThreads.add(thread);
|
||||||
}
|
}
|
||||||
|
Iterator<Thread> i = runningThreads.iterator();
|
||||||
|
while (i.hasNext()) {
|
||||||
|
Thread oldThread = i.next();
|
||||||
|
if (!oldThread.isAlive()) {
|
||||||
|
|
||||||
|
try {
|
||||||
|
oldThread.join();
|
||||||
|
i.remove();
|
||||||
|
System.out.println("cleared thread");
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TODO funktion beschreiben (potentiell nicht benötigte Funktion?)
|
||||||
|
* @param soundName
|
||||||
|
* @param path
|
||||||
|
* @author Ole Wachtel
|
||||||
|
*/
|
||||||
static void add(String soundName, String path){
|
static void add(String soundName, String path){
|
||||||
sounds.put(soundName, path);
|
sounds.put(soundName, path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TODO funktion beschreiben (potentiell nicht benötigte Funktion?)
|
||||||
|
* @param sound
|
||||||
|
* @author Ole Wachtel
|
||||||
|
*/
|
||||||
static void setSoundOn(boolean sound){
|
static void setSoundOn(boolean sound){
|
||||||
soundOn= sound;
|
soundOn= sound;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,9 @@
|
||||||
|
|
||||||
public class SpecificAiPlayerEasy extends AiPlayer{
|
public class SpecificAiPlayerEasy extends AiPlayer{
|
||||||
|
|
||||||
|
public SpecificAiPlayerEasy() {
|
||||||
|
super();
|
||||||
|
this.setName("AI Player Easy");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,125 @@
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
public class SpecificAiPlayerHard extends AiPlayer{
|
public class SpecificAiPlayerHard extends AiPlayer{
|
||||||
|
|
||||||
|
private int gridSize;
|
||||||
|
private boolean[][] shotsFired;
|
||||||
|
private final ArrayList<Point> hitQueue;
|
||||||
|
private final Random random;
|
||||||
|
private int nextChessRow;
|
||||||
|
private int nextChessCol;
|
||||||
|
|
||||||
|
public SpecificAiPlayerHard() {
|
||||||
|
super();
|
||||||
|
this.setName("AI Player Hard");
|
||||||
|
/*this.gridSize = super.board.getSize();
|
||||||
|
this.shotsFired = new boolean[gridSize][gridSize];*/
|
||||||
|
this.gridSize = 0;
|
||||||
|
this.hitQueue = new ArrayList<>();
|
||||||
|
this.random = new Random();
|
||||||
|
this.nextChessRow = 0;
|
||||||
|
this.nextChessCol = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Checks if a position has already been shot at
|
||||||
|
public boolean alreadyShot(Point p) {
|
||||||
|
return shotsFired[p.getX()][p.getY()];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generates the next move for the AI
|
||||||
|
public Point getNextMove() {
|
||||||
|
if(gridSize == 0) {
|
||||||
|
this.gridSize = super.board.getSize();
|
||||||
|
this.shotsFired = new boolean[gridSize][gridSize];
|
||||||
|
}
|
||||||
|
// If there are hits to process, prioritize those
|
||||||
|
while (!hitQueue.isEmpty()) {
|
||||||
|
Point target = hitQueue.remove(0);
|
||||||
|
|
||||||
|
if (!alreadyShot(target)) {
|
||||||
|
return target;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Otherwise, use chess pattern targeting
|
||||||
|
int row = nextChessRow;
|
||||||
|
int col = nextChessCol;
|
||||||
|
while (alreadyShot(new Point(row, col))) {
|
||||||
|
advanceChessPattern();
|
||||||
|
row = nextChessRow;
|
||||||
|
col = nextChessCol;
|
||||||
|
}
|
||||||
|
|
||||||
|
shotsFired[row][col] = true;
|
||||||
|
advanceChessPattern();
|
||||||
|
return new Point(row, col);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public synchronized void receiveHit(HitResponse hitResponse) {
|
||||||
|
super.receiveHit(hitResponse);
|
||||||
|
// If it's a hit or sunk, add adjacent cells to the hitsQueue
|
||||||
|
if (hitResponse.getHitResponse() == HitResponseType.HIT) {
|
||||||
|
addAdjacentPoints(hitResponse.getPoint());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addAdjacentPoints(Point point) {
|
||||||
|
int x = point.getX();
|
||||||
|
int y = point.getY();
|
||||||
|
|
||||||
|
// Possible adjacent positions (up, down, left, right)
|
||||||
|
Point[] adjacentPoints = {
|
||||||
|
new Point(x, y - 1),
|
||||||
|
new Point(x, y + 1),
|
||||||
|
new Point(x - 1, y),
|
||||||
|
new Point(x + 1, y)
|
||||||
|
};
|
||||||
|
|
||||||
|
for (Point p : adjacentPoints) {
|
||||||
|
if (isValidPoint(p) && !alreadyShot(p) && !hitQueue.contains(p)) {
|
||||||
|
hitQueue.add(p);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isValidPoint(Point point) {
|
||||||
|
return point.getX() >= 0 && point.getX() < gridSize &&
|
||||||
|
point.getY() >= 0 && point.getY() < gridSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void aiShoot() {
|
||||||
|
this.enemy.receiveShoot(getNextMove());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Advances the chess pattern to the next cell
|
||||||
|
private void advanceChessPattern() {
|
||||||
|
nextChessCol += 2;
|
||||||
|
if (nextChessCol >= gridSize) {
|
||||||
|
nextChessRow += 1;
|
||||||
|
nextChessCol = (nextChessRow % 2 == 0) ? 0 : 1; // Alternate starting points for chess pattern
|
||||||
|
}
|
||||||
|
if (nextChessRow >= gridSize) {
|
||||||
|
nextChessRow = 0;
|
||||||
|
nextChessCol = 0; // Reset if pattern wraps around
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Adds adjacent cells to the hit queue
|
||||||
|
/* public void processHit(int row, int col) {
|
||||||
|
if (row > 0 && !alreadyShot(row - 1, col)) {
|
||||||
|
hitQueue.add(new int[]{row - 1, col});
|
||||||
|
}
|
||||||
|
if (row < gridSize - 1 && !alreadyShot(row + 1, col)) {
|
||||||
|
hitQueue.add(new int[]{row + 1, col});
|
||||||
|
}
|
||||||
|
if (col > 0 && !alreadyShot(row, col - 1)) {
|
||||||
|
hitQueue.add(new int[]{row, col - 1});
|
||||||
|
}
|
||||||
|
if (col < gridSize - 1 && !alreadyShot(row, col + 1)) {
|
||||||
|
hitQueue.add(new int[]{row, col + 1});
|
||||||
|
}
|
||||||
|
}*/
|
||||||
}
|
}
|
|
@ -5,16 +5,24 @@ public class SpecificAiPlayerMedium extends AiPlayer{
|
||||||
|
|
||||||
private List<Point> hitsQueue = new ArrayList<>();
|
private List<Point> hitsQueue = new ArrayList<>();
|
||||||
|
|
||||||
|
public SpecificAiPlayerMedium() {
|
||||||
|
super();
|
||||||
|
this.setName("AI Player Medium");
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void AiShoot() {
|
public void aiShoot() {
|
||||||
Point nextShot = ComputeNextShot();
|
Point nextShot = ComputeNextShot();
|
||||||
// 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);
|
@Override
|
||||||
|
public synchronized void receiveHit(HitResponse hitResponse) {
|
||||||
|
super.receiveHit(hitResponse);
|
||||||
// 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(hitResponse.getPoint());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,8 +65,9 @@ public class SpecificAiPlayerMedium extends AiPlayer{
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean alreadyShot(Point p) {
|
private boolean alreadyShot(Point p) {
|
||||||
// TODO Auto-generated method stub
|
|
||||||
throw new UnsupportedOperationException("Unimplemented method 'alreadyShot'");
|
return this.enemy.board.getHitResponseOnPoint(p) != null;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isValidPoint(Point point) {
|
private boolean isValidPoint(Point point) {
|
||||||
|
|
|
@ -1,6 +1,10 @@
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Das Verbinden Panel dient als "Überblende", während im Backend das Spiel erstellt/ eine Verbindung hergestellt wird.
|
||||||
|
* @author Lucas Bronson
|
||||||
|
*/
|
||||||
public class Verbinden extends JPanel{
|
public class Verbinden extends JPanel{
|
||||||
|
|
||||||
ImageIcon backButtonIcon = new ImageIcon("graphics/backButton.png");
|
ImageIcon backButtonIcon = new ImageIcon("graphics/backButton.png");
|
||||||
|
@ -9,12 +13,22 @@ public class Verbinden extends JPanel{
|
||||||
|
|
||||||
Font robotoFont = new Font("Roboto", Font.BOLD, 45);
|
Font robotoFont = new Font("Roboto", Font.BOLD, 45);
|
||||||
|
|
||||||
public Verbinden(MainFrame frame) {
|
/**
|
||||||
|
* Konstruktor der Verbinden Klasse.
|
||||||
|
* @param frame Der Mainframe der Anwendung über den alle Panels angezeigt werden.
|
||||||
|
* @author Lucas Bronson
|
||||||
|
*/
|
||||||
|
public Verbinden(MainFrame frame) {
|
||||||
setLayout(null);
|
setLayout(null);
|
||||||
buildPanel(frame);
|
buildPanel(frame);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void buildPanel(MainFrame frame) {
|
/**
|
||||||
|
* Baut Panel auf.
|
||||||
|
* @param frame Der Mainframe der Anwendung über den alle Panels angezeigt werden.
|
||||||
|
* @author Lucas Bronson
|
||||||
|
*/
|
||||||
|
private void buildPanel(MainFrame frame) {
|
||||||
setLayout(new BorderLayout());
|
setLayout(new BorderLayout());
|
||||||
verbindenLabel.setFont(robotoFont.deriveFont(50f));
|
verbindenLabel.setFont(robotoFont.deriveFont(50f));
|
||||||
add(verbindenLabel, BorderLayout.CENTER);
|
add(verbindenLabel, BorderLayout.CENTER);
|
||||||
|
|
|
@ -0,0 +1,64 @@
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
|
import javax.swing.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Klasse für Erstellung von winScreen Objekten
|
||||||
|
* Dient zur Anzeige des Sieges nachdem ein Spiel
|
||||||
|
* @author Lucas Bronson
|
||||||
|
*/
|
||||||
|
public class WinScreen extends JPanel {
|
||||||
|
JLabel winLabel = new JLabel("Du hast Gewonnen!");
|
||||||
|
Font robotoFont = new Font("Roboto", Font.BOLD, 45);
|
||||||
|
JButton okButton = new JButton("Zurück zum Hauptmenü");
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Konstruktor der WinScreen Klasse
|
||||||
|
* @param frame Der Mainframe der Anwendung über den alle Panels angezeigt werden.
|
||||||
|
* @author Lucas Bronson
|
||||||
|
*/
|
||||||
|
public WinScreen(MainFrame frame) {
|
||||||
|
setLayout(null);
|
||||||
|
buildPanel(frame);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Panel bauen/Objekte hinzufuegen
|
||||||
|
* @param frame Der Mainframe der Anwendung über den alle Panels angezeigt werden.
|
||||||
|
* @author Lucas Bronson
|
||||||
|
*/
|
||||||
|
public void buildPanel(MainFrame frame) {
|
||||||
|
Timer timer = new Timer(5, new ActionListener() {
|
||||||
|
private float hue = 0; // Farbton-Wert für HSB-Farbmodell
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
// Farbe basierend auf dem Farbton-Wert berechnen
|
||||||
|
Color pulsierendeFarbe = Color.getHSBColor(hue, 0.8f, 0.8f); // Sättigung und Helligkeit fix
|
||||||
|
winLabel.setForeground(pulsierendeFarbe);
|
||||||
|
|
||||||
|
// Farbton leicht verändern (Zyklus zwischen 0 und 1)
|
||||||
|
hue += 0.01f;
|
||||||
|
if (hue > 1) {
|
||||||
|
hue = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
okButton.setBounds(650,525,200,50);
|
||||||
|
winLabel.setBounds(500,450,500,50);
|
||||||
|
timer.start(); // Timer starten
|
||||||
|
winLabel.setFont(robotoFont);
|
||||||
|
winLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
||||||
|
//Zurückkehren zum Hauptmenü, wenn okButton gedrückt wird
|
||||||
|
okButton.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
frame.showPanel("MainMenu");
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
add(winLabel);
|
||||||
|
add(okButton);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
// TODO Klasse löschen da nicht gebraucht
|
||||||
public class coinToss extends JPanel {
|
public class coinToss extends JPanel {
|
||||||
private int reihenfolge = 1; // 1 = Spieler 1 fängt an, 0 = Spieler 2 fängt an
|
private int reihenfolge = 1; // 1 = Spieler 1 fängt an, 0 = Spieler 2 fängt an
|
||||||
private Timer timer;
|
private Timer timer;
|
||||||
|
|
|
@ -1,27 +1,41 @@
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionEvent;
|
||||||
import java.awt.event.ActionListener;
|
import java.awt.event.ActionListener;
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Das startLocalGame Panel dient dem Erstellen eines lokalen Spiels.
|
||||||
|
* Hier kann der Benutzer Spieler inklusive Namen und das Semester, in dem sich der Benutzer befindet, einstellen.
|
||||||
|
* @author Lucas Bronson, Joshua Kuklok
|
||||||
|
*/
|
||||||
public class startLocalGame extends JPanel {
|
public class startLocalGame extends JPanel {
|
||||||
|
// Player
|
||||||
|
// TODO: entfernen (generell auch test button)
|
||||||
|
Player p1;
|
||||||
|
Player p2;
|
||||||
|
|
||||||
// Funktionshilfen
|
// Funktionshilfen
|
||||||
int semesterCounter = 1; // Semester Counter Label
|
int semesterCounter = 1; // Semester Counter Label
|
||||||
String leftPlayerNickname = "Spieler 1";
|
String leftPlayerNickname = "Spieler 1";
|
||||||
String rightPlayerNickname = "Spieler 2";
|
String rightPlayerNickname = "Einfach";
|
||||||
|
|
||||||
// Grafiken
|
// Grafiken
|
||||||
ImageIcon backButtonIcon = new ImageIcon("graphics/backButton.png");
|
ImageIcon backButtonIcon = new ImageIcon("graphics/backButton.png");
|
||||||
ImageIcon humanPlayerIcon = new ImageIcon("graphics/humanPlayer.png");
|
ImageIcon humanPlayerIcon = new ImageIcon("graphics/humanPlayer.png");
|
||||||
ImageIcon aiPlayerIcon = new ImageIcon("graphics/aiPlayer.png");
|
ImageIcon aiPlayerEasyIcon = new ImageIcon("graphics/botPlayerEasy.png");
|
||||||
|
ImageIcon aiPlayerNormalIcon = new ImageIcon("graphics/botPlayerNormal.png");
|
||||||
|
ImageIcon aiPlayerHardIcon = new ImageIcon("graphics/botPlayerHard.png");
|
||||||
|
|
||||||
// Labels und Buttons
|
// Labels
|
||||||
JLabel frameTitle = new JLabel("Lokales Spiel");
|
JLabel frameTitle = new JLabel("Lokales Spiel");
|
||||||
JLabel semesterLabel = new JLabel("Semester");
|
JLabel semesterLabel = new JLabel("Semester");
|
||||||
JLabel leftPlayerName = new JLabel("Name");
|
JLabel leftPlayerName = new JLabel("Name");
|
||||||
JLabel rightPlayerName = new JLabel("KI-Level");
|
JLabel rightPlayerName = new JLabel("KI-Level");
|
||||||
JLabel leftPlayerIcon = new JLabel(humanPlayerIcon);
|
JLabel leftPlayerIcon = new JLabel(humanPlayerIcon);
|
||||||
JLabel rightPlayerIcon = new JLabel(aiPlayerIcon);
|
JLabel rightPlayerIcon = new JLabel(aiPlayerEasyIcon);
|
||||||
JLabel semesterCounterLabel = new JLabel(String.valueOf(semesterCounter));
|
JLabel semesterCounterLabel = new JLabel(String.valueOf(semesterCounter));
|
||||||
|
|
||||||
|
// Buttons
|
||||||
JButton backButton = new JButton(backButtonIcon);
|
JButton backButton = new JButton(backButtonIcon);
|
||||||
JButton leftPlayerLeftButton = new JButton("<-");
|
JButton leftPlayerLeftButton = new JButton("<-");
|
||||||
JButton leftPlayerRightButton = new JButton("->");
|
JButton leftPlayerRightButton = new JButton("->");
|
||||||
|
@ -30,13 +44,23 @@ public class startLocalGame extends JPanel {
|
||||||
JButton rightPlayerLeftButton = new JButton("<-");
|
JButton rightPlayerLeftButton = new JButton("<-");
|
||||||
JButton rightPlayerRightButton = new JButton("->");
|
JButton rightPlayerRightButton = new JButton("->");
|
||||||
JButton startButton = new JButton("Start!");
|
JButton startButton = new JButton("Start!");
|
||||||
|
JButton testButton = new JButton("Test");
|
||||||
|
|
||||||
|
// Textfelder
|
||||||
JTextField leftPlayerTextField = new JTextField(20);
|
JTextField leftPlayerTextField = new JTextField(20);
|
||||||
JTextField rightPlayerTextField = new JTextField(20);
|
JTextField rightPlayerTextField = new JTextField(20);
|
||||||
|
|
||||||
// Methode zur Erstellung des Panels
|
/**
|
||||||
|
* Konstruktor der startLocalGame.
|
||||||
|
* Fügt Buttons, Textfelder und Label hinzu.
|
||||||
|
* Fügt ebenfalls ActionListeners hinzu, damit Buttons etc. ihre gewünschte Funktion haben
|
||||||
|
*
|
||||||
|
* @param frame Der Mainframe der Anwendung über den alle Panels angezeigt werden.
|
||||||
|
* @author Lucas Bronson, Joshua Kuklok
|
||||||
|
*/
|
||||||
startLocalGame(MainFrame frame) {
|
startLocalGame(MainFrame frame) {
|
||||||
setLayout(null); // Stelle das Layout des Panels ein
|
// Layout des Panels
|
||||||
|
setLayout(null);
|
||||||
|
|
||||||
// Setze Komponentenpositionen
|
// Setze Komponentenpositionen
|
||||||
frameTitle.setBounds(20, 20, 200, 30);
|
frameTitle.setBounds(20, 20, 200, 30);
|
||||||
|
@ -61,6 +85,9 @@ public class startLocalGame extends JPanel {
|
||||||
semesterCounterLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
semesterCounterLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
||||||
add(semesterCounterLabel);
|
add(semesterCounterLabel);
|
||||||
|
|
||||||
|
testButton.setBounds(500,800,50,50);
|
||||||
|
add(testButton);
|
||||||
|
|
||||||
backButton.setBounds(1380, 20, 80, 80);
|
backButton.setBounds(1380, 20, 80, 80);
|
||||||
add(backButton);
|
add(backButton);
|
||||||
|
|
||||||
|
@ -93,7 +120,8 @@ public class startLocalGame extends JPanel {
|
||||||
rightPlayerTextField.setText(rightPlayerNickname);
|
rightPlayerTextField.setText(rightPlayerNickname);
|
||||||
add(rightPlayerTextField);
|
add(rightPlayerTextField);
|
||||||
|
|
||||||
// ActionListener für Buttons
|
// ActionListener für Buttons.
|
||||||
|
// Um das Semester zu erhöhen.
|
||||||
semesterUpButton.addActionListener(e -> {
|
semesterUpButton.addActionListener(e -> {
|
||||||
if (semesterCounter < 6) {
|
if (semesterCounter < 6) {
|
||||||
semesterCounter++;
|
semesterCounter++;
|
||||||
|
@ -101,6 +129,7 @@ public class startLocalGame extends JPanel {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Um das Semester zu senken.
|
||||||
semesterDownButton.addActionListener(e -> {
|
semesterDownButton.addActionListener(e -> {
|
||||||
if (semesterCounter > 1) {
|
if (semesterCounter > 1) {
|
||||||
semesterCounter--;
|
semesterCounter--;
|
||||||
|
@ -108,73 +137,218 @@ public class startLocalGame extends JPanel {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Um linken Player nach links zu "rotieren".
|
||||||
leftPlayerLeftButton.addActionListener(new ActionListener() {
|
leftPlayerLeftButton.addActionListener(new ActionListener() {
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
toggleLeftPlayerIcon();
|
toggleLeftPlayerIconLeft();
|
||||||
updateTextFields();
|
updateTextFields();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Um linken Player nach rechts zu "rotieren".
|
||||||
leftPlayerRightButton.addActionListener(new ActionListener() {
|
leftPlayerRightButton.addActionListener(new ActionListener() {
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
toggleLeftPlayerIcon();
|
toggleLeftPlayerIconRight();
|
||||||
updateTextFields();
|
updateTextFields();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Um rechten Player nach links zu "rotieren".
|
||||||
rightPlayerLeftButton.addActionListener(new ActionListener() {
|
rightPlayerLeftButton.addActionListener(new ActionListener() {
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
toggleRightPlayerIcon();
|
toggleRightPlayerIconLeft();
|
||||||
updateTextFields();
|
updateTextFields();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Um den rechten Player nach rechts zu "rotieren".
|
||||||
rightPlayerRightButton.addActionListener(new ActionListener() {
|
rightPlayerRightButton.addActionListener(new ActionListener() {
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
toggleRightPlayerIcon();
|
toggleRightPlayerIconRight();
|
||||||
updateTextFields();
|
updateTextFields();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
backButton.addActionListener(e -> frame.showPanel("MainMenu"));
|
// Um Namen des linken Spielers zu speichern.
|
||||||
|
leftPlayerTextField.addActionListener(e -> {
|
||||||
|
leftPlayerNickname = leftPlayerTextField.getText();
|
||||||
|
System.out.println("Linker Spielername geändert zu: " + leftPlayerNickname); // Debugging-Ausgabe
|
||||||
|
});
|
||||||
|
|
||||||
startButton.addActionListener(e -> frame.showPanelSLG("GameBoard", semesterCounter)); // TODO ECHTE FUNKTION EINFÜGEN
|
// Um Namen des linken Spielers zu speichern, auch wenn nicht Enter gedrückt wird.
|
||||||
|
leftPlayerTextField.addFocusListener(new java.awt.event.FocusAdapter() {
|
||||||
|
@Override
|
||||||
|
public void focusLost(java.awt.event.FocusEvent evt) {
|
||||||
|
leftPlayerNickname = leftPlayerTextField.getText();
|
||||||
|
System.out.println("Linker Spielername geändert zu: " + leftPlayerNickname); // Debugging-Ausgabe
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Um zum MainMenu zu wechseln.
|
||||||
|
backButton.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
frame.showPanel("MainMenu");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Um zum Gameboard zu wechseln.
|
||||||
|
testButton.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
//frame.showPanelWin("WinPanel");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Um zum startLocalGameLoadingScreen zu wechseln und Daten an Backend weiterzureichen.
|
||||||
|
startButton.addActionListener(new ActionListener() {
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
HashMap<ImageIcon, Class<? extends LocalPlayer>> playerClassMap = new HashMap<>();
|
||||||
|
playerClassMap.put(humanPlayerIcon, HumanPlayer.class);
|
||||||
|
playerClassMap.put(aiPlayerEasyIcon, SpecificAiPlayerEasy.class);
|
||||||
|
playerClassMap.put(aiPlayerNormalIcon, SpecificAiPlayerMedium.class);
|
||||||
|
playerClassMap.put(aiPlayerHardIcon, SpecificAiPlayerHard.class);
|
||||||
|
|
||||||
|
|
||||||
|
frame.showPanelSLGLS("startLocalGameLoadingScreen", semesterCounter); //TODO
|
||||||
|
|
||||||
|
Class<? extends LocalPlayer> leftPlayerClass = playerClassMap.get(leftPlayerIcon.getIcon());
|
||||||
|
Class<? extends AiPlayer> rightPlayerClass = (Class<? extends AiPlayer>) playerClassMap.get(rightPlayerIcon.getIcon());
|
||||||
|
GameController.startLocalGame(
|
||||||
|
leftPlayerClass, leftPlayerNickname,
|
||||||
|
rightPlayerClass,
|
||||||
|
GameController.semesterToBoardSize(semesterCounter)
|
||||||
|
);
|
||||||
|
|
||||||
|
// if (leftPlayerIcon.getIcon() == humanPlayerIcon) {// TODO Wird name wirklich weitergegeben?
|
||||||
|
// if (rightPlayerIcon.getIcon() == aiPlayerEasyIcon) {
|
||||||
|
// GameController.startLocalGame(HumanPlayer.class, leftPlayerNickname, SpecificAiPlayerEasy.class, GameController.semesterToBoardSize(semesterCounter));
|
||||||
|
// } else if (rightPlayerIcon.getIcon() == aiPlayerNormalIcon) {
|
||||||
|
// GameController.startLocalGame(HumanPlayer.class, leftPlayerNickname, SpecificAiPlayerMedium.class, GameController.semesterToBoardSize(semesterCounter));
|
||||||
|
// } else if (rightPlayerIcon.getIcon() == aiPlayerHardIcon) {
|
||||||
|
// GameController.startLocalGame(HumanPlayer.class, leftPlayerNickname, SpecificAiPlayerHard.class, GameController.semesterToBoardSize(semesterCounter));
|
||||||
|
// }
|
||||||
|
// } else if (leftPlayerIcon.getIcon() == aiPlayerEasyIcon) {
|
||||||
|
// if (rightPlayerIcon.getIcon() == aiPlayerEasyIcon) {
|
||||||
|
// GameController.startLocalGame(SpecificAiPlayerEasy.class, leftPlayerNickname, SpecificAiPlayerEasy.class, GameController.semesterToBoardSize(semesterCounter));
|
||||||
|
// } else if (rightPlayerIcon.getIcon() == aiPlayerNormalIcon) {
|
||||||
|
// GameController.startLocalGame(SpecificAiPlayerEasy.class, leftPlayerNickname, SpecificAiPlayerMedium.class, GameController.semesterToBoardSize(semesterCounter));
|
||||||
|
// } else if (rightPlayerIcon.getIcon() == aiPlayerHardIcon) {
|
||||||
|
// GameController.startLocalGame(SpecificAiPlayerEasy.class, leftPlayerNickname, SpecificAiPlayerHard.class, GameController.semesterToBoardSize(semesterCounter));
|
||||||
|
// }
|
||||||
|
// } else if (rightPlayerIcon.getIcon() == aiPlayerNormalIcon) {
|
||||||
|
// if (rightPlayerIcon.getIcon() == aiPlayerEasyIcon) {
|
||||||
|
// GameController.startLocalGame(SpecificAiPlayerMedium.class, leftPlayerNickname, SpecificAiPlayerEasy.class, GameController.semesterToBoardSize(semesterCounter));
|
||||||
|
// } else if (rightPlayerIcon.getIcon() == aiPlayerNormalIcon) {
|
||||||
|
// GameController.startLocalGame(SpecificAiPlayerMedium.class, leftPlayerNickname, SpecificAiPlayerMedium.class, GameController.semesterToBoardSize(semesterCounter));
|
||||||
|
// } else if (rightPlayerIcon.getIcon() == aiPlayerHardIcon) {
|
||||||
|
// GameController.startLocalGame(SpecificAiPlayerMedium.class, leftPlayerNickname, SpecificAiPlayerHard.class, GameController.semesterToBoardSize(semesterCounter));
|
||||||
|
// }
|
||||||
|
// } else if (rightPlayerIcon.getIcon() == aiPlayerHardIcon) {
|
||||||
|
// if (rightPlayerIcon.getIcon() == aiPlayerEasyIcon) {
|
||||||
|
// GameController.startLocalGame(SpecificAiPlayerHard.class, leftPlayerNickname, SpecificAiPlayerEasy.class, GameController.semesterToBoardSize(semesterCounter));
|
||||||
|
// } else if (rightPlayerIcon.getIcon() == aiPlayerNormalIcon) {
|
||||||
|
// GameController.startLocalGame(SpecificAiPlayerHard.class, leftPlayerNickname, SpecificAiPlayerMedium.class, GameController.semesterToBoardSize(semesterCounter));
|
||||||
|
// } else if (rightPlayerIcon.getIcon() == aiPlayerHardIcon) {
|
||||||
|
// GameController.startLocalGame(SpecificAiPlayerHard.class, leftPlayerNickname, SpecificAiPlayerHard.class, GameController.semesterToBoardSize(semesterCounter));
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
// Hilfsfunktionen
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Setzt das jeweils "nächste" Icon, wenn der leftPlayerLeftButton gedrückt wird.
|
||||||
|
* @author Joshua Kuklok
|
||||||
|
*/
|
||||||
|
private void toggleLeftPlayerIconLeft() {
|
||||||
|
if (leftPlayerIcon.getIcon() == humanPlayerIcon) {
|
||||||
|
leftPlayerIcon.setIcon(aiPlayerHardIcon);
|
||||||
|
} else if (leftPlayerIcon.getIcon() == aiPlayerEasyIcon){
|
||||||
|
leftPlayerIcon.setIcon(humanPlayerIcon);
|
||||||
|
} else if (leftPlayerIcon.getIcon() == aiPlayerNormalIcon) {
|
||||||
|
leftPlayerIcon.setIcon(aiPlayerEasyIcon);
|
||||||
|
} else if (leftPlayerIcon.getIcon() == aiPlayerHardIcon) {
|
||||||
|
leftPlayerIcon.setIcon(aiPlayerNormalIcon);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void toggleLeftPlayerIcon() {
|
/**
|
||||||
|
* Setzt das jeweils "nächste" Icon, wenn der leftPlayerRightButton gedrückt wird.
|
||||||
|
* @author Joshua Kuklok
|
||||||
|
*/
|
||||||
|
private void toggleLeftPlayerIconRight() {
|
||||||
if (leftPlayerIcon.getIcon() == humanPlayerIcon) {
|
if (leftPlayerIcon.getIcon() == humanPlayerIcon) {
|
||||||
leftPlayerIcon.setIcon(aiPlayerIcon);
|
leftPlayerIcon.setIcon(aiPlayerEasyIcon);
|
||||||
} else {
|
} else if (leftPlayerIcon.getIcon() == aiPlayerEasyIcon){
|
||||||
|
leftPlayerIcon.setIcon(aiPlayerNormalIcon);
|
||||||
|
} else if (leftPlayerIcon.getIcon() == aiPlayerNormalIcon) {
|
||||||
|
leftPlayerIcon.setIcon(aiPlayerHardIcon);
|
||||||
|
} else if (leftPlayerIcon.getIcon() == aiPlayerHardIcon) {
|
||||||
leftPlayerIcon.setIcon(humanPlayerIcon);
|
leftPlayerIcon.setIcon(humanPlayerIcon);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void toggleRightPlayerIcon() {
|
/**
|
||||||
if (rightPlayerIcon.getIcon() == humanPlayerIcon) {
|
* Setzt das jeweils "nächste" Icon, wenn der RightPlayerLeftButton gedrückt wird.
|
||||||
rightPlayerIcon.setIcon(aiPlayerIcon);
|
* @author Joshua Kuklok
|
||||||
} else {
|
*/
|
||||||
rightPlayerIcon.setIcon(humanPlayerIcon);
|
private void toggleRightPlayerIconLeft() {
|
||||||
|
if (rightPlayerIcon.getIcon() == aiPlayerEasyIcon) {
|
||||||
|
rightPlayerIcon.setIcon(aiPlayerHardIcon);
|
||||||
|
} else if (rightPlayerIcon.getIcon() == aiPlayerNormalIcon){
|
||||||
|
rightPlayerIcon.setIcon(aiPlayerEasyIcon);
|
||||||
|
} else if (rightPlayerIcon.getIcon() == aiPlayerHardIcon) {
|
||||||
|
rightPlayerIcon.setIcon(aiPlayerNormalIcon);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Methode zum Aktualisieren der Textfelder basierend auf den ausgewählten Icons
|
/**
|
||||||
|
* Setzt das jeweils "nächste" Icon, wenn der RightPlayerRightButton gedrückt wird.
|
||||||
|
* @author Joshua Kuklok
|
||||||
|
*/
|
||||||
|
private void toggleRightPlayerIconRight() {
|
||||||
|
if (rightPlayerIcon.getIcon() == aiPlayerEasyIcon) {
|
||||||
|
rightPlayerIcon.setIcon(aiPlayerNormalIcon);
|
||||||
|
} else if (rightPlayerIcon.getIcon() == aiPlayerNormalIcon){
|
||||||
|
rightPlayerIcon.setIcon(aiPlayerHardIcon);
|
||||||
|
} else if (rightPlayerIcon.getIcon() == aiPlayerHardIcon) {
|
||||||
|
rightPlayerIcon.setIcon(aiPlayerEasyIcon);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Aktualisiert die Textfelder basierend auf den Icons
|
||||||
|
* @author Joshua Kuklok
|
||||||
|
*/
|
||||||
private void updateTextFields() {
|
private void updateTextFields() {
|
||||||
// Linker Spieler
|
// Für Linken Spieler
|
||||||
if (leftPlayerIcon.getIcon() == humanPlayerIcon) {
|
if (leftPlayerIcon.getIcon() == humanPlayerIcon) {
|
||||||
leftPlayerTextField.setText("Spieler 1");
|
leftPlayerTextField.setText(leftPlayerNickname);
|
||||||
} else {
|
} else if (leftPlayerIcon.getIcon() == aiPlayerEasyIcon){
|
||||||
leftPlayerTextField.setText("Leicht");
|
leftPlayerTextField.setText("Einfach");
|
||||||
|
} else if (leftPlayerIcon.getIcon() == aiPlayerNormalIcon) {
|
||||||
|
leftPlayerTextField.setText("Mittel");
|
||||||
|
} else if (leftPlayerIcon.getIcon() == aiPlayerHardIcon) {
|
||||||
|
leftPlayerTextField.setText("Schwer");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Rechter Spieler
|
// Für Rechten Spieler
|
||||||
if (rightPlayerIcon.getIcon() == humanPlayerIcon) {
|
if (rightPlayerIcon.getIcon() == aiPlayerEasyIcon){
|
||||||
rightPlayerTextField.setText("Spieler 2");
|
rightPlayerTextField.setText("Einfach");
|
||||||
} else {
|
} else if (rightPlayerIcon.getIcon() == aiPlayerNormalIcon) {
|
||||||
rightPlayerTextField.setText("Leicht");
|
rightPlayerTextField.setText("Mittel");
|
||||||
|
} else if (rightPlayerIcon.getIcon() == aiPlayerHardIcon) {
|
||||||
|
rightPlayerTextField.setText("Schwer");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -0,0 +1,32 @@
|
||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Das startLocalGameLoadingScreen Panel dient als "Überblende", während im Backend das Spiel erstellt wird.
|
||||||
|
* Hier wird lediglich Text angezeigt
|
||||||
|
* @author Joshua Kuklok
|
||||||
|
*/
|
||||||
|
public class startLocalGameLoadingScreen extends JPanel{
|
||||||
|
/**
|
||||||
|
* Konstruktor der startLocalGameLoadingScreen.
|
||||||
|
* @param frame Der Mainframe der Anwendung über den alle Panels angezeigt werden.
|
||||||
|
* @param semesterCounter Ein Zähler, der das gewählte Semester speichert (hier unbenutzt)
|
||||||
|
* @author Joshua Kuklok
|
||||||
|
*/
|
||||||
|
startLocalGameLoadingScreen(MainFrame frame, int semesterCounter) {
|
||||||
|
|
||||||
|
// Layout des Panels
|
||||||
|
setLayout(new BorderLayout());
|
||||||
|
|
||||||
|
// Label mit dem Text erstellen
|
||||||
|
JLabel loadingLabel = new JLabel("Spiel wird gestartet, bitte warten...");
|
||||||
|
loadingLabel.setHorizontalAlignment(SwingConstants.CENTER); // Horizontal zentrieren
|
||||||
|
loadingLabel.setVerticalAlignment(SwingConstants.CENTER); // Vertikal zentrieren
|
||||||
|
|
||||||
|
// Schriftgröße anpassen
|
||||||
|
loadingLabel.setFont(new Font("Roboto", Font.BOLD, 45));
|
||||||
|
|
||||||
|
// Label zum Panel hinzufügen
|
||||||
|
add(loadingLabel, BorderLayout.CENTER);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,13 +1,24 @@
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Das startMultiplayerGame Panel dient dem Erstellen eines Online Spiels.
|
||||||
|
* Hier kann der Benutzer Spieler inklusive Namen und das Semester, in dem sich der Benutzer befindet, einstellen.
|
||||||
|
* @author Joshua Kuklok
|
||||||
|
*/
|
||||||
public class startMultiplayerGame extends JPanel {
|
public class startMultiplayerGame extends JPanel {
|
||||||
|
|
||||||
// Funktionshilfen
|
// Funktionshilfen
|
||||||
int semesterCounter = 1; // Semester Counter Label
|
int semesterCounter = 1;
|
||||||
String PlayerNickname = "Spieler 1";
|
String PlayerNickname = "Spieler 1";
|
||||||
|
|
||||||
// Grafiken
|
// Grafiken
|
||||||
ImageIcon backButtonIcon = new ImageIcon("graphics/backButton.png");
|
ImageIcon backButtonIcon = new ImageIcon("graphics/backButton.png");
|
||||||
ImageIcon humanPlayerIcon = new ImageIcon("graphics/humanPlayer.png");
|
ImageIcon humanPlayerIcon = new ImageIcon("graphics/humanPlayer.png");
|
||||||
ImageIcon aiPlayerIcon = new ImageIcon("graphics/aiPlayer.png");
|
ImageIcon aiPlayerEasyIcon = new ImageIcon("graphics/botPlayerEasy.png");
|
||||||
|
ImageIcon aiPlayerNormalIcon = new ImageIcon("graphics/botPlayerNormal.png");
|
||||||
|
ImageIcon aiPlayerHardIcon = new ImageIcon("graphics/botPlayerHard.png");
|
||||||
|
|
||||||
// Labels
|
// Labels
|
||||||
JLabel frameTitle = new JLabel("Multiplayer Spiel");
|
JLabel frameTitle = new JLabel("Multiplayer Spiel");
|
||||||
|
@ -28,11 +39,19 @@ public class startMultiplayerGame extends JPanel {
|
||||||
// Textfelder
|
// Textfelder
|
||||||
JTextField PlayerTextField = new JTextField(20);
|
JTextField PlayerTextField = new JTextField(20);
|
||||||
|
|
||||||
// Konstruktor
|
/**
|
||||||
|
* Konstruktor der startLocalGame.
|
||||||
|
* Fügt Buttons, Textfelder und Label hinzu.
|
||||||
|
* Fügt ebenfalls ActionListeners hinzu, damit Buttons etc. ihre gewünschte Funktion haben
|
||||||
|
* @param frame Der Mainframe der Anwendung über den alle Panels angezeigt werden.
|
||||||
|
* @author Joshua Kuklok
|
||||||
|
*/
|
||||||
startMultiplayerGame(MainFrame frame) {
|
startMultiplayerGame(MainFrame frame) {
|
||||||
setLayout(null); // Setze das Layout für das Panel auf null
|
|
||||||
|
|
||||||
// Setze Komponentenpositionen und füge sie dem Panel hinzu
|
// Layout des Panels
|
||||||
|
setLayout(null);
|
||||||
|
|
||||||
|
// Setze Komponentenpositionen
|
||||||
frameTitle.setBounds(20, 20, 200, 30);
|
frameTitle.setBounds(20, 20, 200, 30);
|
||||||
add(frameTitle);
|
add(frameTitle);
|
||||||
|
|
||||||
|
@ -49,7 +68,6 @@ public class startMultiplayerGame extends JPanel {
|
||||||
semesterCounterLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
semesterCounterLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
||||||
add(semesterCounterLabel);
|
add(semesterCounterLabel);
|
||||||
|
|
||||||
// Füge Buttons hinzu und setze ihre Positionen
|
|
||||||
backButton.setBounds(1380, 20, 80, 80);
|
backButton.setBounds(1380, 20, 80, 80);
|
||||||
add(backButton);
|
add(backButton);
|
||||||
|
|
||||||
|
@ -71,13 +89,12 @@ public class startMultiplayerGame extends JPanel {
|
||||||
createGameButton.setBounds(1100, 550, 200, 50);
|
createGameButton.setBounds(1100, 550, 200, 50);
|
||||||
add(createGameButton);
|
add(createGameButton);
|
||||||
|
|
||||||
// Füge das Textfeld hinzu
|
|
||||||
PlayerTextField.setBounds(50, 650, 250, 50);
|
PlayerTextField.setBounds(50, 650, 250, 50);
|
||||||
PlayerTextField.setText(PlayerNickname);
|
PlayerTextField.setText(PlayerNickname);
|
||||||
add(PlayerTextField);
|
add(PlayerTextField);
|
||||||
|
|
||||||
// ActionListener für Buttons
|
// ActionListener für Buttons
|
||||||
// SEMESTERBUTTONS
|
// Um das Semester zu erhöhen.
|
||||||
semesterUpButton.addActionListener(e -> {
|
semesterUpButton.addActionListener(e -> {
|
||||||
if (semesterCounter < 6) {
|
if (semesterCounter < 6) {
|
||||||
semesterCounter++;
|
semesterCounter++;
|
||||||
|
@ -85,6 +102,7 @@ public class startMultiplayerGame extends JPanel {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Um das Semester zu senken.
|
||||||
semesterDownButton.addActionListener(e -> {
|
semesterDownButton.addActionListener(e -> {
|
||||||
if (semesterCounter > 1) {
|
if (semesterCounter > 1) {
|
||||||
semesterCounter--;
|
semesterCounter--;
|
||||||
|
@ -92,38 +110,122 @@ public class startMultiplayerGame extends JPanel {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// PLAYERTOGGLEBUTTONS
|
// Um Player nach links zu "rotieren".
|
||||||
PlayerLeftButton.addActionListener(e -> {
|
PlayerLeftButton.addActionListener(e -> {
|
||||||
toggleLeftPlayerIcon();
|
togglePlayerIconLeft();
|
||||||
updateTextFields();
|
updateTextFields();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Um Player nach rechts zu "rotieren".
|
||||||
PlayerRightButton.addActionListener(e -> {
|
PlayerRightButton.addActionListener(e -> {
|
||||||
toggleLeftPlayerIcon();
|
togglePlayerIconRight();
|
||||||
updateTextFields();
|
updateTextFields();
|
||||||
});
|
});
|
||||||
|
|
||||||
// ActionListener für den "Back" Button, um zum vorherigen Panel zurückzukehren
|
// Um Namen des linken Spielers zu speichern.
|
||||||
|
PlayerTextField.addActionListener(e -> {
|
||||||
|
PlayerNickname = PlayerTextField.getText();
|
||||||
|
System.out.println("Linker Spielername geändert zu: " + PlayerNickname); // Debugging-Ausgabe
|
||||||
|
});
|
||||||
|
|
||||||
backButton.addActionListener(e -> frame.showPanel("MainMenu"));
|
// Um Namen des linken Spielers zu speichern, auch wenn nicht Enter gedrückt wird.
|
||||||
joinGameButton.addActionListener(e -> frame.showPanelSMG("JoinGame",1));
|
PlayerTextField.addFocusListener(new java.awt.event.FocusAdapter() {
|
||||||
createGameButton.addActionListener(e -> frame.showPanelSMG("JoinGame",0));
|
@Override
|
||||||
|
public void focusLost(java.awt.event.FocusEvent evt) {
|
||||||
|
PlayerNickname = PlayerTextField.getText();
|
||||||
|
System.out.println("Linker Spielername geändert zu: " + PlayerNickname); // Debugging-Ausgabe
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Um zum MainMenu zu wechseln.
|
||||||
|
backButton.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
frame.showPanel("MainMenu");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Um zu JoinGame mit richtigen Parametern zu wechseln.
|
||||||
|
joinGameButton.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
if (PlayerIcon.getIcon() == humanPlayerIcon) {
|
||||||
|
frame.showPanelSMG("JoinGame",1,0, PlayerNickname);
|
||||||
|
} else if ( PlayerIcon.getIcon() == aiPlayerEasyIcon) {
|
||||||
|
frame.showPanelSMG("JoinGame",1,1, PlayerNickname);
|
||||||
|
} else if ( PlayerIcon.getIcon() == aiPlayerNormalIcon) {
|
||||||
|
frame.showPanelSMG("JoinGame",1,2, PlayerNickname);
|
||||||
|
} else if ( PlayerIcon.getIcon() == aiPlayerHardIcon) {
|
||||||
|
frame.showPanelSMG("JoinGame",1,3, PlayerNickname);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Um zu JoinGame mit richtigen Parametern zu wechseln.
|
||||||
|
createGameButton.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
//Parameter -> panelName, Spiel erstellen oder beitreten (int), Spielertyp(int 0-3), Spielername
|
||||||
|
if (PlayerIcon.getIcon() == humanPlayerIcon) {
|
||||||
|
frame.showPanelSMG("JoinGame",0,0, PlayerNickname);
|
||||||
|
} else if ( PlayerIcon.getIcon() == aiPlayerEasyIcon) {
|
||||||
|
frame.showPanelSMG("JoinGame",0,1, PlayerNickname);
|
||||||
|
} else if ( PlayerIcon.getIcon() == aiPlayerNormalIcon) {
|
||||||
|
frame.showPanelSMG("JoinGame",0,2, PlayerNickname);
|
||||||
|
} else if ( PlayerIcon.getIcon() == aiPlayerHardIcon) {
|
||||||
|
frame.showPanelSMG("JoinGame",0,3, PlayerNickname);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// TOGGLE METHODEN
|
// Hilfsfunktionen
|
||||||
private void toggleLeftPlayerIcon() {
|
|
||||||
|
/**
|
||||||
|
* Setzt das jeweils "nächste" Icon, wenn der PlayerLeftButton gedrückt wird.
|
||||||
|
* @author Joshua Kuklok
|
||||||
|
*/
|
||||||
|
private void togglePlayerIconLeft() {
|
||||||
if (PlayerIcon.getIcon() == humanPlayerIcon) {
|
if (PlayerIcon.getIcon() == humanPlayerIcon) {
|
||||||
PlayerIcon.setIcon(aiPlayerIcon);
|
PlayerIcon.setIcon(aiPlayerHardIcon);
|
||||||
} else {
|
} else if (PlayerIcon.getIcon() == aiPlayerEasyIcon){
|
||||||
|
PlayerIcon.setIcon(humanPlayerIcon);
|
||||||
|
} else if (PlayerIcon.getIcon() == aiPlayerNormalIcon) {
|
||||||
|
PlayerIcon.setIcon(aiPlayerEasyIcon);
|
||||||
|
} else if (PlayerIcon.getIcon() == aiPlayerHardIcon) {
|
||||||
|
PlayerIcon.setIcon(aiPlayerNormalIcon);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Setzt das jeweils "nächste" Icon, wenn der PlayerRightButton gedrückt wird.
|
||||||
|
* @author Joshua Kuklok
|
||||||
|
*/
|
||||||
|
private void togglePlayerIconRight() {
|
||||||
|
if (PlayerIcon.getIcon() == humanPlayerIcon) {
|
||||||
|
PlayerIcon.setIcon(aiPlayerEasyIcon);
|
||||||
|
} else if (PlayerIcon.getIcon() == aiPlayerEasyIcon){
|
||||||
|
PlayerIcon.setIcon(aiPlayerNormalIcon);
|
||||||
|
} else if (PlayerIcon.getIcon() == aiPlayerNormalIcon) {
|
||||||
|
PlayerIcon.setIcon(aiPlayerHardIcon);
|
||||||
|
} else if (PlayerIcon.getIcon() == aiPlayerHardIcon) {
|
||||||
PlayerIcon.setIcon(humanPlayerIcon);
|
PlayerIcon.setIcon(humanPlayerIcon);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Aktualisiert die Textfelder basierend auf den Icons
|
||||||
|
* @author Joshua Kuklok
|
||||||
|
*/
|
||||||
private void updateTextFields() {
|
private void updateTextFields() {
|
||||||
if (PlayerIcon.getIcon() == humanPlayerIcon) {
|
if (PlayerIcon.getIcon() == humanPlayerIcon) {
|
||||||
PlayerTextField.setText(PlayerNickname);
|
PlayerTextField.setText(PlayerNickname);
|
||||||
} else {
|
} else if (PlayerIcon.getIcon() == aiPlayerEasyIcon){
|
||||||
PlayerTextField.setText("Leicht");
|
PlayerTextField.setText("Einfach");
|
||||||
|
} else if (PlayerIcon.getIcon() == aiPlayerNormalIcon) {
|
||||||
|
PlayerTextField.setText("Mittel");
|
||||||
|
} else if (PlayerIcon.getIcon() == aiPlayerHardIcon) {
|
||||||
|
PlayerTextField.setText("Schwer");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue