implement ready for players and board refresh method

This commit is contained in:
Luca Conte 2024-12-15 14:21:17 +01:00
parent 970550308b
commit f565708461
7 changed files with 80 additions and 29 deletions

View File

@ -88,7 +88,6 @@ public class BoardDisplay extends JPanel {
Point o = new Point(x, y);
currentShip.setHorizontal(vertical);
handleFieldClick(o); // Linksklick -> Schiff platzieren
vertical = false; // TODO nur nach auswahl von neuem Modul wieder auf false setzten
}
}
});
@ -183,4 +182,8 @@ public class BoardDisplay extends JPanel {
}
public void refresh() {
paintFields();
}
}

View File

@ -13,6 +13,14 @@ import java.util.List;
* Der Benutzer kann hier seine Schiffe platzieren, das Spiel starten etc.
*/
public class GameBoard extends JPanel {
private BoardDisplay ownBoardPanel;
private BoardDisplay opponentBoardPanel;
private Player p1;
private Player p2;
// Funktionshilfen
//int semesterCounter = 1; //TODO: ersetzen durch param von vorpanel
@ -46,7 +54,9 @@ public class GameBoard extends JPanel {
* @param p2 Zweites Spielerobjekt
*/
GameBoard(MainFrame frame, int semesterCounter,Player p1, Player p2) {
buildPanel(frame, semesterCounter,p1,p2);
this.p1 = p1;
this.p2 = p2;
buildPanel(frame, semesterCounter);
List<Ship> shipsP1 =p1.getBoard().getShips();
List<Ship> shipsP2 =p2.getBoard().getShips();
backButton.addActionListener(e -> frame.showPanel("MainMenu"));
@ -93,18 +103,16 @@ public class GameBoard extends JPanel {
* TODO Funktion beschreiben etc.
* @param frame
* @param semesterCounter
* @param p1
* @param p2
*/
public void buildPanel(MainFrame frame, int semesterCounter,Player p1,Player p2) {
public void buildPanel(MainFrame frame, int semesterCounter) {
// Hauptlayout - BorderLayout für die Anordnung der Komponenten
setLayout(new BorderLayout());
// Spielfelder erstellen (eigenes und gegnerisches)
// Spielfelder werden in BoardDisplay erstellt
int gridSize = GameController.semesterToBoardSize(semesterCounter); // Größe des Spielfelds
BoardDisplay ownBoardPanel = new BoardDisplay(gridSize,p1);
BoardDisplay opponentBoardPanel = new BoardDisplay(gridSize, p2);
this.ownBoardPanel = new BoardDisplay(gridSize,p1);
this.opponentBoardPanel = new BoardDisplay(gridSize, p2);
// Panel für das Kontext-Text-Feld
JPanel headerPanel = new JPanel();
@ -137,6 +145,7 @@ public class GameBoard extends JPanel {
rightButtonsPanel.add(shipButton);
rightButtonGroup.add(shipButton);
opponentBoardPanel.addShipButton(shipButton);
shipButton.setEnabled(false);
}
JToggleButton readyButton = new JToggleButton("Bereit");
@ -157,8 +166,8 @@ public class GameBoard extends JPanel {
readyButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
kontextText.setText(kT2);
p1.ready();
}
});
// Panel für die Buttons des rechten Spielers (ganz rechts)
@ -200,4 +209,14 @@ public class GameBoard extends JPanel {
add(namesAndBoardsPanel, BorderLayout.CENTER);
timer.start();
}
public void refresh() {
if (this.p1.myTurn) {
this.kontextText.setText(kT5);
} else {
this.kontextText.setText(kT6);
}
this.ownBoardPanel.refresh();
this.opponentBoardPanel.refresh();
}
}

View File

@ -38,21 +38,24 @@ public class LocalPlayer extends Player {
@Override
public synchronized void receiveCoin(boolean coin) {
if (!this.haseReceivedCoin) {
boolean result = coin ^ this.myCoin; // XOR
this.myTurn = result == this.isServer;
this.haseReceivedCoin = true;
if (!this.hasReceivedCoin) {
this.hasReceivedCoin = true;
this.determineCoinToss();
}
}
public void sendCoin() {
enemy.receiveCoin(this.myCoin);
}
@Override
public void shoot(Point point){
this.myTurn = false;
enemy.receiveShoot(point);
}
@Override
public synchronized void ready() {
for (Ship ship : this.board.getShips()) {
if (!ship.isPlaced()) return;
}
super.ready();
}
}

View File

@ -21,6 +21,7 @@ public class MainFrame extends JFrame {
// Von startLocalGameLoadingScreen an GameBoard
int semesterCounter;
// ---------- //
private GameBoard gameBoard;
/**
* Konstruktor von MainFrame.
@ -95,7 +96,7 @@ public class MainFrame extends JFrame {
public void showPanelSLG(String panelName,int semesterCounter, Player p1, Player p2) {
this.semesterCounter = semesterCounter;
GameBoard gameBoard = new GameBoard(this, semesterCounter, p1, p2);
this.gameBoard = new GameBoard(this, semesterCounter, p1, p2);
mainPanel.add(gameBoard, panelName);
mainPanel.revalidate();
mainPanel.repaint();
@ -116,4 +117,8 @@ public class MainFrame extends JFrame {
mainPanel.repaint();
cardLayout.show(mainPanel, panelName); // Show the panel
}
public void refreshGameBoard() {
this.gameBoard.refresh();
}
}

View File

@ -7,6 +7,7 @@ public abstract class OnlinePlayer extends Player implements AsyncSocketListener
public OnlinePlayer(Integer size, AsyncSocket socket) {
this.socket = socket;
this.wantedBoardSize = size;
this.myCoin = null;
socket.setHandler(this);
//TODO Auto-generated constructor stub
}

View File

@ -33,8 +33,8 @@ public class OnlinePlayer_1_1_0 extends OnlinePlayer {
case "COIN":
if(!this.hasReceivedCoinPackage && (p.getData().equals("1") || p.getData().equals("0"))){
this.myCoin = p.getData().equals("1");
enemy.receiveCoin(this.myCoin);
this.myCoin = p.getData().equals("1");
this.ready();
this.hasReceivedCoinPackage = true;
}
break;
@ -85,9 +85,10 @@ public class OnlinePlayer_1_1_0 extends OnlinePlayer {
@Override
public synchronized void receiveCoin(boolean coin) {
if (!this.haseReceivedCoin) {
if (!this.hasReceivedCoin) {
super.socket.send(new SocketPackage("COIN", String.valueOf(coin ? 1 : 0)));
this.haseReceivedCoin = true;
this.hasReceivedCoin = true;
this.determineCoinToss();
}
}

View File

@ -2,19 +2,20 @@ public abstract class Player {
protected boolean myTurn;
protected boolean isServer;
protected boolean waitingForResponse;
protected Player enemy;
protected Player enemy;
protected String name;
protected Board board;
protected boolean myCoin;
protected Boolean myCoin;
protected boolean sendCoin;
protected boolean sentCoin;
protected boolean haseReceivedCoin;
protected boolean hasReceivedCoin;
public Player() {
this.setName("Player");
this.haseReceivedCoin = false;
this.sendCoin = false;
this.hasReceivedCoin = false;
this.sentCoin = false;
this.myTurn = false;
}
public void createBoard(int size) {
@ -27,8 +28,8 @@ public abstract class Player {
public abstract void shoot(Point point);
public void beginTrun() {
public void beginTurn() {
System.out.println("issa my turn-a");
}
public void setEnemy(Player enemy) {
@ -46,5 +47,23 @@ public abstract class Player {
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();
}
GameController.getMainFrame().refreshGameBoard();
}
public abstract void receiveCoin(boolean coin);
}