diff --git a/src/GameBoard.java b/src/GameBoard.java index 4b6aa7a..f9f915d 100644 --- a/src/GameBoard.java +++ b/src/GameBoard.java @@ -56,12 +56,9 @@ public class GameBoard extends JPanel { List shipsP1 =p1.getBoard().getShips(); List 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"); - } + giveUpButton.addActionListener((e) -> { + frame.showPanel("MainMenu"); + p1.withdraw(); }); } diff --git a/src/LocalPlayer.java b/src/LocalPlayer.java index aac063c..ffd87c5 100644 --- a/src/LocalPlayer.java +++ b/src/LocalPlayer.java @@ -33,7 +33,7 @@ public class LocalPlayer extends Player { switch (hitResponse.getType()) { case HIT, SUNK -> this.myTurn = false; case MISS -> this.myTurn = true; - case VICTORY -> GameController.getMainFrame().showPanelLose("LoseScreen", this); + case VICTORY -> this.lose(); } GameController.getMainFrame().refreshGameBoard(); } @@ -49,7 +49,7 @@ public class LocalPlayer extends Player { switch (hitResponse.getType()) { case HIT, SUNK -> this.myTurn = true; case MISS -> this.myTurn = false; - case VICTORY -> GameController.getMainFrame().showPanelWin("", this); + case VICTORY -> this.win(); } GameController.getMainFrame().refreshGameBoard(); } diff --git a/src/OnlinePlayer_1_1_0.java b/src/OnlinePlayer_1_1_0.java index 69cfaf7..2ba932a 100644 --- a/src/OnlinePlayer_1_1_0.java +++ b/src/OnlinePlayer_1_1_0.java @@ -84,10 +84,7 @@ public class OnlinePlayer_1_1_0 extends OnlinePlayer { break; case "WITHDRAW": - //Found funny cheese to do this without GUI Access - HitResponse tmp_hit = new HitResponse(0, new Point(0,0)); - tmp_hit.setType(HitResponseType.VICTORY); - this.receiveHit(tmp_hit); + this.withdraw(); break; default: @@ -160,4 +157,10 @@ public class OnlinePlayer_1_1_0 extends OnlinePlayer { // SHOULD NEVER BE CALLED ON ONLINE PLAYER. ONLY ON HUMAN PLAYER return; } + + @Override + public void receiveWithdraw() { + this.socket.send(new SocketPackage("WITHDRAW")); + super.receiveWithdraw(); + } } diff --git a/src/Player.java b/src/Player.java index 4adf76c..8dfc776 100644 --- a/src/Player.java +++ b/src/Player.java @@ -137,6 +137,7 @@ public abstract class Player { * and players * * This method should be called at the end of a game + * This method should be called at the end of a game * * @author Luca Conte */ @@ -146,4 +147,20 @@ public abstract class Player { this.board = null; this.enemy = null; } + + public void win() { + GameController.getMainFrame().showPanelWin("", this); + } + public void lose() { + GameController.getMainFrame().showPanelLose("", this); + } + + public void withdraw() { + this.enemy.receiveWithdraw(); + this.lose(); + } + + public void receiveWithdraw(){ + this.win(); + } }